[10663] | 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: Reto Luechinger |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
| 17 | #include "adm_turret.h" |
---|
[10664] | 18 | #include "weapons/weapon_manager.h" |
---|
| 19 | #include "weapons/weapon.h" |
---|
[10663] | 20 | #include "lib/util/loading/factory.h" |
---|
| 21 | #include "world_entities/projectiles/projectile.h" |
---|
[10664] | 22 | #include "loading/load_param.h" |
---|
| 23 | #include "debug.h" |
---|
[10677] | 24 | #include "loading/load_param_xml.h" |
---|
[10663] | 25 | |
---|
| 26 | ObjectListDefinition(AdmTurret); |
---|
| 27 | CREATE_FACTORY(AdmTurret); |
---|
| 28 | |
---|
[10664] | 29 | |
---|
[10663] | 30 | /** |
---|
| 31 | * Standard constructor |
---|
| 32 | */ |
---|
| 33 | AdmTurret::AdmTurret () |
---|
| 34 | { |
---|
[10664] | 35 | this->init(); |
---|
[10663] | 36 | } |
---|
| 37 | |
---|
| 38 | /** |
---|
[10666] | 39 | * destructs the turret, deletes allocated memory |
---|
[10663] | 40 | */ |
---|
| 41 | AdmTurret::~AdmTurret () |
---|
| 42 | { |
---|
| 43 | // will be deleted |
---|
| 44 | } |
---|
| 45 | |
---|
[10666] | 46 | /** |
---|
| 47 | * Constructor with XML Element |
---|
| 48 | */ |
---|
[10664] | 49 | AdmTurret::AdmTurret (const TiXmlElement* root) |
---|
[10663] | 50 | { |
---|
[10664] | 51 | this->init(); |
---|
| 52 | if (root != NULL) |
---|
| 53 | { |
---|
| 54 | this->loadParams(root); |
---|
| 55 | } |
---|
[10663] | 56 | } |
---|
[10677] | 57 | /** |
---|
| 58 | * XML Loader |
---|
| 59 | */ |
---|
| 60 | void AdmTurret::loadParams(const TiXmlElement* root) |
---|
| 61 | { |
---|
| 62 | if (root != NULL) |
---|
| 63 | { |
---|
| 64 | WorldEntity::loadParams(root); |
---|
[10664] | 65 | |
---|
[10677] | 66 | LoadParam(root, "target", this, AdmTurret, setTarget) |
---|
| 67 | .describe("The filename of the World Entity, that is to be shot at") |
---|
| 68 | .defaultValues(""); |
---|
| 69 | |
---|
| 70 | LoadParamXML(root, "Cannons", this, AdmTurret, addCannons) |
---|
| 71 | .describe("add cannons to ADM"); |
---|
| 72 | LoadParamXML(root, "Sensor", this, AdmTurret, addSensor) |
---|
| 73 | .describe("add sensor to ADM"); |
---|
| 74 | |
---|
| 75 | } |
---|
[10679] | 76 | |
---|
| 77 | //HACK this is really ugly because if you move the AdmTurret its cannons wont follow |
---|
| 78 | if ( this->cannons ) |
---|
| 79 | this->cannons->setParent( NullParent::getNullParent() ); |
---|
[10677] | 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Sets Target onto the World Entity with filename "target", given as String |
---|
| 84 | */ |
---|
| 85 | void AdmTurret::setTarget(const std::string& target) |
---|
| 86 | { |
---|
| 87 | WorldEntity* targetEntity = WorldEntity::objectList().getObject(target); |
---|
| 88 | if (targetEntity != NULL) |
---|
| 89 | { |
---|
| 90 | this->myTarget = targetEntity; |
---|
| 91 | } |
---|
| 92 | else |
---|
| 93 | { |
---|
| 94 | PRINTF(1)("ERROR ADMTURRET : Target %s does not exist\n", target.c_str()); |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | |
---|
[10663] | 98 | void AdmTurret::init() |
---|
| 99 | { |
---|
[10664] | 100 | this->registerObject(this, AdmTurret::_objectList); |
---|
[10679] | 101 | |
---|
| 102 | //this->removeNodeFlags( 7 ); |
---|
[10663] | 103 | } |
---|
| 104 | |
---|
[10677] | 105 | void AdmTurret::tick(float dt) |
---|
[10663] | 106 | { |
---|
[10677] | 107 | WorldEntity::tick(dt); |
---|
[10664] | 108 | |
---|
[10677] | 109 | //rotate sensor 2PI/sec |
---|
| 110 | this->sensor->setAbsDir( this->sensor->getAbsDir() * Quaternion( PI*2*dt, Vector( 0, 1, 0 ) ) ); |
---|
[10663] | 111 | |
---|
[10677] | 112 | |
---|
| 113 | Vector playerPos = this->myTarget->getAbsCoor(); |
---|
| 114 | this->moveTowards( playerPos - ( this->cannons->getAbsCoor() ), dt); |
---|
[10664] | 115 | } |
---|
[10663] | 116 | |
---|
[10664] | 117 | void AdmTurret::draw() const |
---|
| 118 | { |
---|
[10677] | 119 | WorldEntity::draw(); |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | glMatrixMode(GL_MODELVIEW); |
---|
| 123 | glPushMatrix(); |
---|
| 124 | |
---|
| 125 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 126 | |
---|
| 127 | glDisable(GL_LIGHTING); |
---|
| 128 | glDisable(GL_TEXTURE_2D); |
---|
| 129 | glDisable(GL_BLEND); |
---|
| 130 | glLineWidth(2.0); |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | Vector mp = this->cannons->getAbsCoor(); |
---|
| 134 | Vector op = this->cannons->getAbsDir().apply( Vector(-1, 0, 0) ); |
---|
| 135 | op *= 100; |
---|
| 136 | op += mp; |
---|
| 137 | |
---|
| 138 | glColor3f(1.0, 0.0, 0.0 ); |
---|
| 139 | glBegin(GL_LINE_STRIP); |
---|
| 140 | glVertex3f(mp.x, mp.y, mp.z); |
---|
| 141 | glVertex3f(op.x, op.y, op.z); |
---|
| 142 | glEnd(); |
---|
| 143 | |
---|
| 144 | op = this->myTarget->getAbsCoor() - ( this->cannons->getAbsCoor() ); |
---|
| 145 | op += mp; |
---|
| 146 | |
---|
| 147 | glColor3f(0.0, 1.0, 0.0 ); |
---|
| 148 | glBegin(GL_LINE_STRIP); |
---|
| 149 | glVertex3f(mp.x, mp.y, mp.z); |
---|
| 150 | glVertex3f(op.x, op.y, op.z); |
---|
| 151 | glEnd(); |
---|
| 152 | |
---|
| 153 | glPopAttrib(); |
---|
| 154 | glPopMatrix(); |
---|
| 155 | |
---|
[10664] | 156 | } |
---|
[10663] | 157 | |
---|
[10664] | 158 | void AdmTurret::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 159 | { |
---|
| 160 | } |
---|
[10663] | 161 | |
---|
[10664] | 162 | void AdmTurret::activate() |
---|
[10663] | 163 | { |
---|
| 164 | } |
---|
[10664] | 165 | |
---|
| 166 | void AdmTurret::deactivate() |
---|
| 167 | { |
---|
[10663] | 168 | } |
---|
| 169 | |
---|
| 170 | bool AdmTurret::isVisible(const WorldEntity myTarget) |
---|
[10664] | 171 | { |
---|
| 172 | return true; |
---|
[10663] | 173 | } |
---|
| 174 | |
---|
[10664] | 175 | float AdmTurret::aim(const WorldEntity Target) |
---|
| 176 | { |
---|
| 177 | return 0; |
---|
| 178 | } |
---|
[10663] | 179 | |
---|
| 180 | void AdmTurret::fire() |
---|
| 181 | { |
---|
[10664] | 182 | // Projectile* pj = this->getProjectile(); |
---|
| 183 | // if (pj == NULL) return; |
---|
[10663] | 184 | |
---|
[10664] | 185 | //pj->setOwner(this->getOwner()); |
---|
| 186 | //pj->setParent(PNode::getNullParent()); |
---|
| 187 | //pj->setVelocity(this->getAbsDir().apply(Vector( , , )) ); |
---|
| 188 | //pj->setAbsCoor(this->getEmissionPoint()); |
---|
| 189 | //pj->setAbsDir(this->getAbsDir()); |
---|
| 190 | //pj->activate(); |
---|
| 191 | } |
---|
[10677] | 192 | |
---|
| 193 | void AdmTurret::addCannons( const TiXmlElement * root ) |
---|
| 194 | { |
---|
| 195 | this->cannons = new WorldEntity(); |
---|
| 196 | this->cannons->setParent(this); |
---|
| 197 | this->cannons->loadParams( root ); |
---|
| 198 | |
---|
[10679] | 199 | //this->cannons->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
| 200 | //this->cannons->addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
[10677] | 201 | |
---|
| 202 | this->cannons->toList( getOMListNumber() ); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | void AdmTurret::addSensor( const TiXmlElement * root ) |
---|
| 206 | { |
---|
| 207 | this->sensor = new WorldEntity(); |
---|
| 208 | this->sensor->setParent(this); |
---|
| 209 | this->sensor->loadParams( root ); |
---|
| 210 | |
---|
[10679] | 211 | //this->sensor->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
| 212 | //this->sensor->addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
[10677] | 213 | this->sensor->toList( getOMListNumber() ); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | void AdmTurret::moveTowards( Vector targetDir, float dt ) |
---|
| 217 | { |
---|
[10679] | 218 | #if 0 |
---|
| 219 | static Quaternion mrot = this->getAbsDir(); |
---|
| 220 | Quaternion ry( dt, Vector(0, 1, 0) ); |
---|
| 221 | mrot *= ry; |
---|
| 222 | this->setAbsDir(mrot); |
---|
[10677] | 223 | |
---|
[10679] | 224 | static Quaternion rot = this->cannons->getAbsDir(); |
---|
| 225 | Quaternion rx( dt, Vector( 0, 0, 1 ) ); |
---|
| 226 | rot *= rx; |
---|
| 227 | this->cannons->setAbsDir(rot*mrot); |
---|
| 228 | return; |
---|
| 229 | #endif |
---|
[10677] | 230 | |
---|
| 231 | |
---|
[10679] | 232 | |
---|
| 233 | |
---|
[10677] | 234 | Quaternion cur = this->getAbsDir(); |
---|
| 235 | |
---|
[10679] | 236 | Quaternion ry( dt, cur.inverse().apply( Vector( 0, 1, 0 ) ) ); |
---|
[10677] | 237 | |
---|
| 238 | Quaternion tmp1 = cur * ry; |
---|
| 239 | Quaternion tmp2 = cur * ry.inverse(); |
---|
| 240 | |
---|
[10679] | 241 | bool usetmp1 = false; |
---|
| 242 | Quaternion r1; |
---|
[10677] | 243 | if ( tmp1.apply( Vector(1, 0, 0) ).dot(targetDir) < tmp2.apply( Vector(1, 0, 0)).dot(targetDir) ) |
---|
[10679] | 244 | { |
---|
| 245 | usetmp1 = true; |
---|
[10677] | 246 | cur = tmp1; |
---|
[10679] | 247 | r1 = ry; |
---|
| 248 | } |
---|
[10677] | 249 | else |
---|
[10679] | 250 | { |
---|
[10677] | 251 | cur = tmp2; |
---|
[10679] | 252 | r1 = ry.inverse(); |
---|
| 253 | } |
---|
[10677] | 254 | |
---|
| 255 | this->setAbsDir( cur ); |
---|
| 256 | |
---|
| 257 | |
---|
[10679] | 258 | Quaternion cur2 = this->cannons->getAbsDir(); |
---|
| 259 | Quaternion rx( dt, cur.inverse().apply( Vector( 0, 0, 1 ) ) ); |
---|
| 260 | Quaternion rr( dt, cur2.inverse().apply( Vector( 0, 1, 0 ) ) ); |
---|
| 261 | if ( !usetmp1 ) |
---|
| 262 | rr = rr.inverse(); |
---|
| 263 | |
---|
| 264 | |
---|
| 265 | tmp1 = cur2 * rx; |
---|
| 266 | tmp2 = cur2 * rx.inverse(); |
---|
[10677] | 267 | |
---|
| 268 | Quaternion dec; |
---|
[10679] | 269 | Quaternion r2; |
---|
| 270 | if ( tmp1.apply( Vector(-1, 0, 0) ).dot(targetDir) > tmp2.apply( Vector(-1, 0, 0) ).dot(targetDir) ) |
---|
| 271 | { |
---|
| 272 | r2 = rx; |
---|
[10677] | 273 | dec = tmp1; |
---|
[10679] | 274 | } |
---|
[10677] | 275 | else |
---|
[10679] | 276 | { |
---|
| 277 | r2 = rx.inverse(); |
---|
[10677] | 278 | dec = tmp2; |
---|
[10679] | 279 | } |
---|
[10677] | 280 | |
---|
| 281 | float dp = dec.apply( Vector(-1, 0, 0) ).dot(Vector(0, 1, 0)); |
---|
[10679] | 282 | if ( dp > -0.9 && dp < 0.9 ) |
---|
[10677] | 283 | { |
---|
[10679] | 284 | cur2 = dec; |
---|
[10677] | 285 | } |
---|
| 286 | |
---|
[10679] | 287 | this->cannons->setAbsDir( cur2 ); |
---|
[10677] | 288 | |
---|
| 289 | |
---|
[10679] | 290 | |
---|
[10677] | 291 | } |
---|