1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific |
---|
14 | main-programmer: Patrick Boenzli |
---|
15 | co-programmer: |
---|
16 | */ |
---|
17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
18 | |
---|
19 | |
---|
20 | #include "util/loading/factory.h" |
---|
21 | #include "util/loading/load_param.h" |
---|
22 | |
---|
23 | #include "interactive_model.h" |
---|
24 | #include "md2/md2Model.h" |
---|
25 | |
---|
26 | #include "sound_buffer.h" |
---|
27 | |
---|
28 | #include "loading/resource_manager.h" |
---|
29 | |
---|
30 | #include "generic_npc.h" |
---|
31 | |
---|
32 | #include "animation/animation3d.h" |
---|
33 | |
---|
34 | using namespace std; |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | CREATE_FACTORY(GenericNPC, CL_GENERIC_NPC); |
---|
39 | |
---|
40 | #include "script_class.h" |
---|
41 | CREATE_SCRIPTABLE_CLASS(GenericNPC, CL_GENERIC_NPC, |
---|
42 | //addMethod("walkTo", ExecutorLua7ret<GenericNPC,float, float, float, float, float, float, float, float>(&GenericNPC::walkTo)) |
---|
43 | addMethod("walkTo", ExecutorLua3ret<GenericNPC,float,float,float,float>(&GenericNPC::walkTo)) |
---|
44 | ->addMethod("setTime", ExecutorLua1<GenericNPC,float>(&GenericNPC::setTime)) |
---|
45 | ); |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | /** |
---|
50 | * constructor |
---|
51 | */ |
---|
52 | GenericNPC::GenericNPC(const TiXmlElement* root) |
---|
53 | : NPC(root) |
---|
54 | { |
---|
55 | this->init(); |
---|
56 | |
---|
57 | if (root != NULL) |
---|
58 | this->loadParams(root); |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | GenericNPC::GenericNPC() |
---|
63 | : NPC(NULL) |
---|
64 | { |
---|
65 | |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * deconstructor |
---|
70 | */ |
---|
71 | GenericNPC::~GenericNPC () |
---|
72 | { |
---|
73 | if( this->currentAnim != NULL) |
---|
74 | delete this->currentAnim; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | /** |
---|
79 | * initializing the npc enity |
---|
80 | */ |
---|
81 | void GenericNPC::init() |
---|
82 | { |
---|
83 | this->setClassID(CL_GENERIC_NPC, "GenericNPC"); |
---|
84 | this->toList(OM_GROUP_00); |
---|
85 | |
---|
86 | if (this->soundBuffer != NULL) |
---|
87 | ResourceManager::getInstance()->unload(this->soundBuffer); |
---|
88 | this->soundBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/rain.wav", WAV); |
---|
89 | |
---|
90 | time = 30.0f; |
---|
91 | // collision reaction registration |
---|
92 | // this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | /** |
---|
97 | * loads the Settings of a MD2Creature from an XML-element. |
---|
98 | * @param root the XML-element to load the MD2Creature's properties from |
---|
99 | */ |
---|
100 | void GenericNPC::loadParams(const TiXmlElement* root) |
---|
101 | { |
---|
102 | NPC::loadParams(root); |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | /** |
---|
108 | * sets the animation of this npc |
---|
109 | * @param anumationIndex: the animation index |
---|
110 | * @param anumPlaybackMode: the playback mode |
---|
111 | */ |
---|
112 | void GenericNPC::setAnimation(int animationIndex, int animPlaybackMode) |
---|
113 | { |
---|
114 | if( likely(this->getModel(0) != NULL)) |
---|
115 | ((InteractiveModel*)this->getModel(0))->setAnimation(animationIndex, animPlaybackMode); |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | /** |
---|
120 | * sets the animation of this npc |
---|
121 | * @param anumationIndex: the animation index |
---|
122 | * @param anumPlaybackMode: the playback mode |
---|
123 | */ |
---|
124 | void GenericNPC::playAnimation(int animationIndex, int animPlaybackMode) |
---|
125 | { |
---|
126 | if( likely(this->getModel(0) != NULL)) |
---|
127 | ((InteractiveModel*)this->getModel(0))->setAnimation(animationIndex, animPlaybackMode); |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | /** |
---|
133 | * play a sound |
---|
134 | * @param filename: name of the file |
---|
135 | */ |
---|
136 | void GenericNPC::playSound(std::string filename) |
---|
137 | { |
---|
138 | |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | /** |
---|
143 | * walt to |
---|
144 | * @param coordinate: coordinate to go to |
---|
145 | */ |
---|
146 | float GenericNPC::walkTo(float x, float y, float z, float qu, float qx, float qy, float qz) |
---|
147 | { |
---|
148 | Vector destCoor = Vector(x, y, z); |
---|
149 | Quaternion destDir = Quaternion(Vector(qx, qy, qz), qu); |
---|
150 | |
---|
151 | // check if this is the current goal |
---|
152 | if( this->destCoor != destCoor || this->destDir != destDir) |
---|
153 | { |
---|
154 | this->destCoor = destCoor; |
---|
155 | this->destDir = destDir; |
---|
156 | |
---|
157 | float time = 100.0f; |
---|
158 | |
---|
159 | if( this->currentAnim != NULL) |
---|
160 | delete this->currentAnim; |
---|
161 | |
---|
162 | this->currentAnim = new Animation3D(this); |
---|
163 | this->currentAnim->addKeyFrame(this->getAbsCoor(), this->getAbsDir(), 0.0f, ANIM_LINEAR, ANIM_LINEAR); |
---|
164 | this->currentAnim->addKeyFrame(this->getAbsCoor(), this->getAbsDir(),time, ANIM_LINEAR, ANIM_LINEAR); |
---|
165 | this->currentAnim->addKeyFrame(this->destCoor, this->destDir, time, ANIM_LINEAR, ANIM_LINEAR); |
---|
166 | // this->currentAnim->addKeyFrame(this->getAbsCoor(), this->getAbsDir(), time, ANIM_LINEAR, ANIM_LINEAR); |
---|
167 | // this->currentAnim->addKeyFrame(this->destCoor, this->destDir, time, ANIM_LINEAR, ANIM_LINEAR); |
---|
168 | |
---|
169 | this->currentAnim->setInfinity(ANIM_INF_CONSTANT); |
---|
170 | this->currentAnim->play(); |
---|
171 | |
---|
172 | this->setAnimation(RUN, MD2_ANIM_LOOP); |
---|
173 | } |
---|
174 | |
---|
175 | // calculate the distance |
---|
176 | Vector distance = this->getAbsCoor() - this->destCoor; |
---|
177 | return distance.len(); |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | /** |
---|
182 | * walk to a specific place with direction |
---|
183 | * |
---|
184 | * @param x: x coordinate to go to |
---|
185 | * @param y: y coordinate to go to |
---|
186 | * @param z: z coordinate to go to |
---|
187 | * |
---|
188 | * without turning itself |
---|
189 | */ |
---|
190 | float GenericNPC::walkTo(float x, float y, float z) |
---|
191 | { |
---|
192 | Quaternion q = this->getAbsDir(); |
---|
193 | |
---|
194 | //printf("%s moving to %f, %f, %f \n",this->getName(),x,y,z); |
---|
195 | |
---|
196 | return this->walkTo(x, y, z, q.w, q.v.x, q.v.y, q.v.z); |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * walk to a specific place with direction |
---|
201 | * |
---|
202 | * @param x: x coordinate to go to |
---|
203 | * @param y: y coordinate to go to |
---|
204 | * @param qu: angle to rotate |
---|
205 | * @param qx: x coordinate of rotation vector |
---|
206 | * @param qy: y coordinate of rotation vector |
---|
207 | * @param qz: z coordinate of rotation vector |
---|
208 | * |
---|
209 | */ |
---|
210 | float GenericNPC::walkTo(float x, float y, float qu, float qx, float qy, float qz) |
---|
211 | { |
---|
212 | return this->walkTo(x, y, 0.0f, qu, qx, qy, qz); |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | /** |
---|
217 | * walk to a specific place with direction |
---|
218 | * |
---|
219 | * @param coor: vector place |
---|
220 | * @param dir: direction |
---|
221 | * |
---|
222 | */ |
---|
223 | float GenericNPC::walkTo(const Vector& coor, const Quaternion& dir) |
---|
224 | { |
---|
225 | return this->walkTo(coor.x, coor.y, coor.z, dir.w, dir.v.x, dir.v.y, dir.v.z); |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | |
---|
230 | /** |
---|
231 | * run to a specific place with direction |
---|
232 | * |
---|
233 | * @param x: x coordinate to go to |
---|
234 | * @param y: y coordinate to go to |
---|
235 | * @param z: z coordinate to go to |
---|
236 | * @param qu: angle to rotate |
---|
237 | * @param qx: x coordinate of rotation vector |
---|
238 | * @param qy: y coordinate of rotation vector |
---|
239 | * @param qz: z coordinate of rotation vector |
---|
240 | * |
---|
241 | */ |
---|
242 | float GenericNPC::runTo(float x, float y, float z, float qu, float qx, float qy, float qz) |
---|
243 | { |
---|
244 | Vector destCoor = Vector(x, y, z); |
---|
245 | Quaternion destDir = Quaternion(Vector(qx, qy, qz), qu); |
---|
246 | |
---|
247 | // check if this is the current goal |
---|
248 | if( this->destCoor != destCoor || this->destDir != destDir) |
---|
249 | { |
---|
250 | this->destCoor = destCoor; |
---|
251 | this->destDir = destDir; |
---|
252 | |
---|
253 | float time = 5.0f; |
---|
254 | |
---|
255 | if( this->currentAnim != NULL) |
---|
256 | delete this->currentAnim; |
---|
257 | |
---|
258 | this->currentAnim = new Animation3D(this); |
---|
259 | this->currentAnim->addKeyFrame(this->getAbsCoor(), this->getAbsDir(), 0.1f, ANIM_LINEAR, ANIM_LINEAR); |
---|
260 | this->currentAnim->addKeyFrame(this->destCoor, this->destDir, time, ANIM_LINEAR, ANIM_LINEAR); |
---|
261 | this->currentAnim->addKeyFrame(this->getAbsCoor(), this->getAbsDir(), time, ANIM_LINEAR, ANIM_LINEAR); |
---|
262 | this->currentAnim->addKeyFrame(this->destCoor, this->destDir, time, ANIM_LINEAR, ANIM_LINEAR); |
---|
263 | |
---|
264 | this->currentAnim->setInfinity(ANIM_INF_CONSTANT); |
---|
265 | this->currentAnim->play(); |
---|
266 | |
---|
267 | this->setAnimation(RUN, MD2_ANIM_LOOP); |
---|
268 | } |
---|
269 | |
---|
270 | // calculate the distance |
---|
271 | Vector distance = this->getAbsCoor() - this->destCoor; |
---|
272 | return distance.len(); |
---|
273 | } |
---|
274 | |
---|
275 | |
---|
276 | /** |
---|
277 | * run to a specific place with direction |
---|
278 | * |
---|
279 | * @param x: x coordinate to go to |
---|
280 | * @param y: y coordinate to go to |
---|
281 | * @param qu: angle to rotate |
---|
282 | * @param qx: x coordinate of rotation vector |
---|
283 | * @param qy: y coordinate of rotation vector |
---|
284 | * @param qz: z coordinate of rotation vector |
---|
285 | * |
---|
286 | */ |
---|
287 | float GenericNPC::runTo(float x, float y, float qu, float qx, float qy, float qz) |
---|
288 | { |
---|
289 | this->runTo(x, y, 0.0f, qu, qx, qy, qz); |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | /** |
---|
294 | * run to a specific place with direction |
---|
295 | * |
---|
296 | * @param coor: vector place |
---|
297 | * @param dir: direction |
---|
298 | * |
---|
299 | */ |
---|
300 | float GenericNPC::runTo(const Vector& coordinate, const Quaternion& dir) |
---|
301 | { |
---|
302 | this->runTo(coordinate.x, coordinate.y, coordinate.z, dir.w, dir.v.x, dir.v.y, dir.v.z); |
---|
303 | } |
---|
304 | |
---|
305 | |
---|
306 | /** |
---|
307 | * crouch to a specific place with direction |
---|
308 | * |
---|
309 | * @param x: x coordinate to go to |
---|
310 | * @param y: y coordinate to go to |
---|
311 | * @param z: z coordinate to go to |
---|
312 | * @param qu: angle to rotate |
---|
313 | * @param qx: x coordinate of rotation vector |
---|
314 | * @param qy: y coordinate of rotation vector |
---|
315 | * @param qz: z coordinate of rotation vector |
---|
316 | * |
---|
317 | */ |
---|
318 | float GenericNPC::crouchTo(float x, float y, float z, float qu, float qx, float qy, float qz) |
---|
319 | { |
---|
320 | Vector destCoor = Vector(x, y, z); |
---|
321 | Quaternion destDir = Quaternion(Vector(qx, qy, qz), qu); |
---|
322 | |
---|
323 | // check if this is the current goal |
---|
324 | if( this->destCoor != destCoor || this->destDir != destDir) |
---|
325 | { |
---|
326 | this->destCoor = destCoor; |
---|
327 | this->destDir = destDir; |
---|
328 | |
---|
329 | |
---|
330 | if( this->currentAnim != NULL) |
---|
331 | delete this->currentAnim; |
---|
332 | |
---|
333 | this->currentAnim = new Animation3D(this); |
---|
334 | this->currentAnim->addKeyFrame(this->getAbsCoor(), this->getAbsDir(), time, ANIM_LINEAR, ANIM_LINEAR); |
---|
335 | this->currentAnim->addKeyFrame(this->destCoor, this->destDir, time, ANIM_LINEAR, ANIM_LINEAR); |
---|
336 | |
---|
337 | |
---|
338 | this->currentAnim->setInfinity(ANIM_INF_CONSTANT); |
---|
339 | this->currentAnim->play(); |
---|
340 | |
---|
341 | this->setAnimation(CROUCH_WALK, MD2_ANIM_LOOP); |
---|
342 | } |
---|
343 | |
---|
344 | // calculate the distance |
---|
345 | Vector distance = this->getAbsCoor() - this->destCoor; |
---|
346 | return distance.len(); |
---|
347 | } |
---|
348 | |
---|
349 | |
---|
350 | /** |
---|
351 | * couch to a specific place with direction |
---|
352 | * |
---|
353 | * @param x: x coordinate to go to |
---|
354 | * @param y: y coordinate to go to |
---|
355 | * @param qu: angle to rotate |
---|
356 | * @param qx: x coordinate of rotation vector |
---|
357 | * @param qy: y coordinate of rotation vector |
---|
358 | * @param qz: z coordinate of rotation vector |
---|
359 | * |
---|
360 | */ |
---|
361 | float GenericNPC::crouchTo(float x, float y, float qu, float qx, float qy, float qz) |
---|
362 | { |
---|
363 | this->crouchTo(x, y, 0.0f, qu, qx, qy, qz); |
---|
364 | } |
---|
365 | |
---|
366 | |
---|
367 | |
---|
368 | /** |
---|
369 | * crouch to a specific place with direction |
---|
370 | * |
---|
371 | * @param coor: vector place |
---|
372 | * @param dir: direction |
---|
373 | * |
---|
374 | */ |
---|
375 | float GenericNPC::crouchTo(const Vector& coordinate, const Quaternion& dir) |
---|
376 | { |
---|
377 | this->crouchTo(coordinate.x, coordinate.y, coordinate.z, dir.w, dir.v.x, dir.v.y, dir.v.z); |
---|
378 | } |
---|
379 | |
---|
380 | |
---|
381 | /** |
---|
382 | * stops the generic animation |
---|
383 | */ |
---|
384 | void GenericNPC::stop() |
---|
385 | { |
---|
386 | if( this->currentAnim != NULL) |
---|
387 | { |
---|
388 | this->currentAnim->stop(); |
---|
389 | delete this->currentAnim; |
---|
390 | this->currentAnim = NULL; |
---|
391 | |
---|
392 | this->setAnimation(STAND, MD2_ANIM_LOOP); |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|
396 | |
---|
397 | /** |
---|
398 | * lookat a world entity |
---|
399 | * @param worldEntity: the worldentity to look at |
---|
400 | */ |
---|
401 | float GenericNPC::lookAt(WorldEntity* worldEntity) |
---|
402 | {} |
---|
403 | |
---|
404 | |
---|
405 | /** |
---|
406 | * talk to a world entity and play a sound/music/voice |
---|
407 | * @param worldEntity: entity |
---|
408 | * @param dialogNr: sound nr to be played (from the xml load tags) |
---|
409 | */ |
---|
410 | float GenericNPC::talkTo(WorldEntity* worldEntity, int dialogNr) |
---|
411 | {} |
---|
412 | |
---|
413 | |
---|
414 | /** |
---|
415 | * world entity to shoot at if there is any weapon on the npc |
---|
416 | * @param entity: entity to shoot entity |
---|
417 | */ |
---|
418 | void GenericNPC::shootAt(WorldEntity* entity) |
---|
419 | {} |
---|
420 | |
---|
421 | |
---|
422 | |
---|
423 | |
---|
424 | |
---|
425 | |
---|
426 | |
---|
427 | |
---|
428 | |
---|
429 | |
---|
430 | /** |
---|
431 | * tick this world entity |
---|
432 | * @param time: time in seconds expirded since the last tick |
---|
433 | */ |
---|
434 | void GenericNPC::tick (float time) |
---|
435 | { |
---|
436 | if( likely(this->getModel(0) != NULL)) |
---|
437 | ((InteractiveModel*)this->getModel(0))->tick(time); |
---|
438 | |
---|
439 | // tick this animation |
---|
440 | if( this->currentAnim != NULL) |
---|
441 | this->currentAnim->tick(time); |
---|
442 | } |
---|
443 | |
---|
444 | |
---|
445 | |
---|
446 | void GenericNPC::destroy() |
---|
447 | { |
---|
448 | int randi = (int)(5.0f * (float)rand()/(float)RAND_MAX); |
---|
449 | |
---|
450 | if( randi == 1) |
---|
451 | this->setAnimation(DEATH_FALLBACK, MD2_ANIM_ONCE); |
---|
452 | else if( randi == 2) |
---|
453 | this->setAnimation(DEATH_FALLFORWARD, MD2_ANIM_ONCE); |
---|
454 | else if( randi == 3) |
---|
455 | this->setAnimation(DEATH_FALLBACKSLOW, MD2_ANIM_ONCE); |
---|
456 | else if( randi == 4) |
---|
457 | this->setAnimation(CROUCH_DEATH, MD2_ANIM_ONCE); |
---|
458 | else |
---|
459 | this->setAnimation(DEATH_FALLBACK, MD2_ANIM_ONCE); |
---|
460 | } |
---|
461 | |
---|