| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Christoph Renner |
|---|
| 13 | co-programmer: |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
|---|
| 17 | |
|---|
| 18 | #include "loading/factory.h" |
|---|
| 19 | #include "debug.h" |
|---|
| 20 | #include "loading/load_param.h" |
|---|
| 21 | #include "util/loading/load_param_xml.h" |
|---|
| 22 | #include "state.h" |
|---|
| 23 | #include "player.h" |
|---|
| 24 | #include "playable.h" |
|---|
| 25 | |
|---|
| 26 | #include "actionbox_enemy.h" |
|---|
| 27 | |
|---|
| 28 | ObjectListDefinition(ActionboxEnemy); |
|---|
| 29 | CREATE_FACTORY(ActionboxEnemy); |
|---|
| 30 | |
|---|
| 31 | ActionboxEnemy::ActionboxEnemy(const TiXmlElement* root) |
|---|
| 32 | { |
|---|
| 33 | PRINTF(0)("ActionboxEnemy\n"); |
|---|
| 34 | |
|---|
| 35 | this->registerObject(this, ActionboxEnemy::_objectList); |
|---|
| 36 | this->toList(OM_GROUP_00); |
|---|
| 37 | |
|---|
| 38 | this->isActive = true; |
|---|
| 39 | |
|---|
| 40 | this->pitch = 0.0f; |
|---|
| 41 | this->dPitch = 0.0; |
|---|
| 42 | |
|---|
| 43 | if ( root ) |
|---|
| 44 | this->loadParams( root ); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | ActionboxEnemy::~ActionboxEnemy() |
|---|
| 48 | { |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | void ActionboxEnemy::loadParams(const TiXmlElement* root) |
|---|
| 52 | { |
|---|
| 53 | WorldEntity::loadParams( root ); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | void ActionboxEnemy::tick( float dt ) |
|---|
| 57 | { |
|---|
| 58 | myDir = this->getAbsDir(); |
|---|
| 59 | myCoor = this->getAbsCoor(); |
|---|
| 60 | |
|---|
| 61 | this->pitch += this->dPitch*dt; |
|---|
| 62 | while ( pitch > 2*PI ) |
|---|
| 63 | pitch -= 2*PI; |
|---|
| 64 | while ( pitch < 0 ) |
|---|
| 65 | pitch += 2*PI; |
|---|
| 66 | |
|---|
| 67 | myDir *= qPitch.inverse(); |
|---|
| 68 | |
|---|
| 69 | qPitch = Quaternion( pitch, Vector( 1, 0, 0 ) ); |
|---|
| 70 | |
|---|
| 71 | moveTowardsBox( NULL, dt ); |
|---|
| 72 | |
|---|
| 73 | if ( isActive && State::getActionBox() ) |
|---|
| 74 | { |
|---|
| 75 | ActionBox* box = State::getActionBox(); |
|---|
| 76 | if ( box->isPointInBox( this->getAbsCoor() ) ) |
|---|
| 77 | { |
|---|
| 78 | attackPlayer( box, dt ); |
|---|
| 79 | } |
|---|
| 80 | else |
|---|
| 81 | { |
|---|
| 82 | moveTowardsBox( box, dt ); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | myDir *= qPitch; |
|---|
| 87 | |
|---|
| 88 | this->setAbsDir( myDir ); |
|---|
| 89 | this->setAbsCoor( myCoor ); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void ActionboxEnemy::attackPlayer( ActionBox * box, float dt ) |
|---|
| 93 | { |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void ActionboxEnemy::moveTowardsBox( ActionBox * box, float dt ) |
|---|
| 97 | { |
|---|
| 98 | Vector targetPos = State::getPlayer()->getPlayable()->getAbsCoor(); |
|---|
| 99 | Vector targetDir = targetPos - myCoor; |
|---|
| 100 | |
|---|
| 101 | Quaternion cur = myDir; |
|---|
| 102 | Quaternion rx( 5*dt, Vector( 0, 0, 1 ) ); |
|---|
| 103 | |
|---|
| 104 | Quaternion tmp1 = cur * rx; |
|---|
| 105 | Quaternion tmp2 = cur * rx.inverse(); |
|---|
| 106 | |
|---|
| 107 | Quaternion dec; |
|---|
| 108 | if ( tmp1.apply( Vector(1, 0, 0) ).dot(targetDir) > tmp2.apply( Vector(1, 0, 0)).dot(targetDir) ) |
|---|
| 109 | dec = tmp1; |
|---|
| 110 | else |
|---|
| 111 | dec = tmp2; |
|---|
| 112 | |
|---|
| 113 | float dp = dec.apply( Vector(1, 0, 0) ).dot(Vector(0, 1, 0)); |
|---|
| 114 | if ( dp > -0.9 && dp < 0.9 ) |
|---|
| 115 | { |
|---|
| 116 | cur = dec; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | Quaternion ry( 5*dt, cur.inverse().apply( Vector( 0, 1, 0 ) ) ); |
|---|
| 120 | |
|---|
| 121 | tmp1 = cur * ry; |
|---|
| 122 | tmp2 = cur * ry.inverse(); |
|---|
| 123 | |
|---|
| 124 | if ( tmp1.apply( Vector(1, 0, 0) ).dot(targetDir) > tmp2.apply( Vector(1, 0, 0)).dot(targetDir) ) |
|---|
| 125 | cur = tmp1; |
|---|
| 126 | else |
|---|
| 127 | cur = tmp2; |
|---|
| 128 | |
|---|
| 129 | myDir = cur; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | void ActionboxEnemy::draw( ) const |
|---|
| 133 | { |
|---|
| 134 | #if 0 |
|---|
| 135 | Vector fw = this->getAbsDir().apply( Vector( 1, 0, 0 ) ); |
|---|
| 136 | fw.normalize(); |
|---|
| 137 | fw = fw * 100; |
|---|
| 138 | |
|---|
| 139 | Vector mp = this->getAbsCoor(); |
|---|
| 140 | Vector op = mp + fw; |
|---|
| 141 | |
|---|
| 142 | Vector targetPos = State::getPlayer()->getPlayable()->getAbsCoor(); |
|---|
| 143 | Vector dv = targetPos - this->getAbsCoor(); |
|---|
| 144 | dv.normalize(); |
|---|
| 145 | dv *= 100; |
|---|
| 146 | dv += mp; |
|---|
| 147 | |
|---|
| 148 | Vector spUp = this->getAbsDir().inverse().apply( this->getAbsDir().apply( Vector( 0, 1, 0 ) ) ); |
|---|
| 149 | spUp.normalize(); |
|---|
| 150 | spUp *= 100; |
|---|
| 151 | spUp += mp; |
|---|
| 152 | |
|---|
| 153 | Vector up = fw.cross( dv ); |
|---|
| 154 | up += mp; |
|---|
| 155 | |
|---|
| 156 | //PRINTF(0)("DEBUG\n"); |
|---|
| 157 | //mp.debug(); |
|---|
| 158 | //op.debug(); |
|---|
| 159 | |
|---|
| 160 | glMatrixMode(GL_MODELVIEW); |
|---|
| 161 | glPushMatrix(); |
|---|
| 162 | |
|---|
| 163 | glPushAttrib(GL_ENABLE_BIT); |
|---|
| 164 | |
|---|
| 165 | glDisable(GL_LIGHTING); |
|---|
| 166 | glDisable(GL_TEXTURE_2D); |
|---|
| 167 | glDisable(GL_BLEND); |
|---|
| 168 | glLineWidth(2.0); |
|---|
| 169 | glColor3f(1.0, 0.0, 0.0 ); |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | glBegin(GL_LINE_STRIP); |
|---|
| 173 | glVertex3f(mp.x, mp.y, mp.z); |
|---|
| 174 | glVertex3f(op.x, op.y, op.z); |
|---|
| 175 | glEnd(); |
|---|
| 176 | |
|---|
| 177 | glColor3f(0.0, 1.0, 0.0 ); |
|---|
| 178 | glBegin(GL_LINE_STRIP); |
|---|
| 179 | glVertex3f(mp.x, mp.y, mp.z); |
|---|
| 180 | glVertex3f(dv.x, dv.y, dv.z); |
|---|
| 181 | glEnd(); |
|---|
| 182 | |
|---|
| 183 | glColor3f(0.0, 0.0, 1.0 ); |
|---|
| 184 | glBegin(GL_LINE_STRIP); |
|---|
| 185 | glVertex3f(mp.x, mp.y, mp.z); |
|---|
| 186 | glVertex3f(up.x, up.y, up.z); |
|---|
| 187 | glEnd(); |
|---|
| 188 | |
|---|
| 189 | glColor3f(1.0, 1.0, 1.0 ); |
|---|
| 190 | glBegin(GL_LINE_STRIP); |
|---|
| 191 | glVertex3f(mp.x, mp.y, mp.z); |
|---|
| 192 | glVertex3f(spUp.x, spUp.y, spUp.z); |
|---|
| 193 | glEnd(); |
|---|
| 194 | |
|---|
| 195 | glPopMatrix(); |
|---|
| 196 | #enif |
|---|
| 197 | WorldEntity::draw(); |
|---|
| 198 | } |
|---|