[10651] | 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; |
---|
[10656] | 41 | this->dPitch = 0.0; |
---|
[10651] | 42 | |
---|
[10662] | 43 | this->maxSpeed = 10; |
---|
| 44 | this->acceleration = 3; |
---|
| 45 | this->speed = 0; |
---|
| 46 | |
---|
[10651] | 47 | if ( root ) |
---|
| 48 | this->loadParams( root ); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | ActionboxEnemy::~ActionboxEnemy() |
---|
| 52 | { |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | void ActionboxEnemy::loadParams(const TiXmlElement* root) |
---|
| 56 | { |
---|
| 57 | WorldEntity::loadParams( root ); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | void ActionboxEnemy::tick( float dt ) |
---|
| 61 | { |
---|
[10656] | 62 | myDir = this->getAbsDir(); |
---|
| 63 | myCoor = this->getAbsCoor(); |
---|
| 64 | |
---|
[10651] | 65 | this->pitch += this->dPitch*dt; |
---|
[10656] | 66 | while ( pitch > 2*PI ) |
---|
| 67 | pitch -= 2*PI; |
---|
| 68 | while ( pitch < 0 ) |
---|
| 69 | pitch += 2*PI; |
---|
[10651] | 70 | |
---|
[10656] | 71 | myDir *= qPitch.inverse(); |
---|
| 72 | |
---|
| 73 | qPitch = Quaternion( pitch, Vector( 1, 0, 0 ) ); |
---|
| 74 | |
---|
[10651] | 75 | moveTowardsBox( NULL, dt ); |
---|
[10656] | 76 | |
---|
[10651] | 77 | if ( isActive && State::getActionBox() ) |
---|
| 78 | { |
---|
| 79 | ActionBox* box = State::getActionBox(); |
---|
| 80 | if ( box->isPointInBox( this->getAbsCoor() ) ) |
---|
| 81 | { |
---|
| 82 | attackPlayer( box, dt ); |
---|
| 83 | } |
---|
| 84 | else |
---|
| 85 | { |
---|
| 86 | moveTowardsBox( box, dt ); |
---|
| 87 | } |
---|
| 88 | } |
---|
[10656] | 89 | |
---|
| 90 | myDir *= qPitch; |
---|
| 91 | |
---|
| 92 | this->setAbsDir( myDir ); |
---|
| 93 | this->setAbsCoor( myCoor ); |
---|
[10651] | 94 | } |
---|
| 95 | |
---|
| 96 | void ActionboxEnemy::attackPlayer( ActionBox * box, float dt ) |
---|
| 97 | { |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void ActionboxEnemy::moveTowardsBox( ActionBox * box, float dt ) |
---|
| 101 | { |
---|
[10662] | 102 | //Vector targetPos = State::getPlayer()->getPlayable()->getAbsCoor(); |
---|
| 103 | static float time = 0; |
---|
| 104 | time += dt; |
---|
| 105 | Vector targetPos = State::getPlayer()->getPlayable()->getAbsCoor() + State::getPlayer()->getPlayable()->getAbsDir().apply(Vector(1, 0, 0))*50*(1.5 + sin(time/5)); |
---|
[10656] | 106 | Vector targetDir = targetPos - myCoor; |
---|
[10651] | 107 | |
---|
[10656] | 108 | Quaternion cur = myDir; |
---|
[10657] | 109 | Quaternion rx( dt, Vector( 0, 0, 1 ) ); |
---|
[10651] | 110 | |
---|
[10655] | 111 | Quaternion tmp1 = cur * rx; |
---|
| 112 | Quaternion tmp2 = cur * rx.inverse(); |
---|
[10651] | 113 | |
---|
[10656] | 114 | Quaternion dec; |
---|
[10655] | 115 | if ( tmp1.apply( Vector(1, 0, 0) ).dot(targetDir) > tmp2.apply( Vector(1, 0, 0)).dot(targetDir) ) |
---|
[10656] | 116 | dec = tmp1; |
---|
[10655] | 117 | else |
---|
[10656] | 118 | dec = tmp2; |
---|
[10651] | 119 | |
---|
[10656] | 120 | float dp = dec.apply( Vector(1, 0, 0) ).dot(Vector(0, 1, 0)); |
---|
| 121 | if ( dp > -0.9 && dp < 0.9 ) |
---|
| 122 | { |
---|
| 123 | cur = dec; |
---|
| 124 | } |
---|
| 125 | |
---|
[10657] | 126 | Quaternion ry( dt, cur.inverse().apply( Vector( 0, 1, 0 ) ) ); |
---|
[10651] | 127 | |
---|
[10655] | 128 | tmp1 = cur * ry; |
---|
| 129 | tmp2 = cur * ry.inverse(); |
---|
[10651] | 130 | |
---|
[10655] | 131 | if ( tmp1.apply( Vector(1, 0, 0) ).dot(targetDir) > tmp2.apply( Vector(1, 0, 0)).dot(targetDir) ) |
---|
| 132 | cur = tmp1; |
---|
| 133 | else |
---|
| 134 | cur = tmp2; |
---|
| 135 | |
---|
[10662] | 136 | myDir = cur; |
---|
| 137 | |
---|
| 138 | Vector fw = cur.apply( Vector(1, 0, 0) ); |
---|
| 139 | |
---|
| 140 | this->speed += this->acceleration*dt; |
---|
| 141 | if ( this->speed > this->maxSpeed ) |
---|
| 142 | this->speed = this->maxSpeed; |
---|
| 143 | this->myCoor += fw*speed*dt; |
---|
[10651] | 144 | } |
---|
| 145 | |
---|
| 146 | void ActionboxEnemy::draw( ) const |
---|
| 147 | { |
---|
[10656] | 148 | #if 0 |
---|
[10651] | 149 | Vector fw = this->getAbsDir().apply( Vector( 1, 0, 0 ) ); |
---|
| 150 | fw.normalize(); |
---|
| 151 | fw = fw * 100; |
---|
| 152 | |
---|
| 153 | Vector mp = this->getAbsCoor(); |
---|
| 154 | Vector op = mp + fw; |
---|
| 155 | |
---|
| 156 | Vector targetPos = State::getPlayer()->getPlayable()->getAbsCoor(); |
---|
| 157 | Vector dv = targetPos - this->getAbsCoor(); |
---|
| 158 | dv.normalize(); |
---|
| 159 | dv *= 100; |
---|
| 160 | dv += mp; |
---|
| 161 | |
---|
[10655] | 162 | Vector spUp = this->getAbsDir().inverse().apply( this->getAbsDir().apply( Vector( 0, 1, 0 ) ) ); |
---|
| 163 | spUp.normalize(); |
---|
| 164 | spUp *= 100; |
---|
| 165 | spUp += mp; |
---|
| 166 | |
---|
| 167 | Vector up = fw.cross( dv ); |
---|
[10651] | 168 | up += mp; |
---|
| 169 | |
---|
| 170 | //PRINTF(0)("DEBUG\n"); |
---|
| 171 | //mp.debug(); |
---|
| 172 | //op.debug(); |
---|
| 173 | |
---|
| 174 | glMatrixMode(GL_MODELVIEW); |
---|
| 175 | glPushMatrix(); |
---|
| 176 | |
---|
| 177 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 178 | |
---|
| 179 | glDisable(GL_LIGHTING); |
---|
| 180 | glDisable(GL_TEXTURE_2D); |
---|
| 181 | glDisable(GL_BLEND); |
---|
| 182 | glLineWidth(2.0); |
---|
| 183 | glColor3f(1.0, 0.0, 0.0 ); |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | glBegin(GL_LINE_STRIP); |
---|
| 187 | glVertex3f(mp.x, mp.y, mp.z); |
---|
| 188 | glVertex3f(op.x, op.y, op.z); |
---|
| 189 | glEnd(); |
---|
| 190 | |
---|
[10655] | 191 | glColor3f(0.0, 1.0, 0.0 ); |
---|
[10651] | 192 | glBegin(GL_LINE_STRIP); |
---|
| 193 | glVertex3f(mp.x, mp.y, mp.z); |
---|
| 194 | glVertex3f(dv.x, dv.y, dv.z); |
---|
| 195 | glEnd(); |
---|
| 196 | |
---|
[10655] | 197 | glColor3f(0.0, 0.0, 1.0 ); |
---|
[10651] | 198 | glBegin(GL_LINE_STRIP); |
---|
| 199 | glVertex3f(mp.x, mp.y, mp.z); |
---|
| 200 | glVertex3f(up.x, up.y, up.z); |
---|
| 201 | glEnd(); |
---|
[10655] | 202 | |
---|
| 203 | glColor3f(1.0, 1.0, 1.0 ); |
---|
| 204 | glBegin(GL_LINE_STRIP); |
---|
| 205 | glVertex3f(mp.x, mp.y, mp.z); |
---|
| 206 | glVertex3f(spUp.x, spUp.y, spUp.z); |
---|
| 207 | glEnd(); |
---|
[10651] | 208 | |
---|
| 209 | glPopMatrix(); |
---|
[10657] | 210 | #endif |
---|
[10651] | 211 | WorldEntity::draw(); |
---|
| 212 | } |
---|