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