Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/single_player_map/src/world_entities/npcs/generic_npc.cc @ 9040

Last change on this file since 9040 was 9040, checked in by patrick, 18 years ago

npc walk at the ground with correct orientation

File size: 12.6 KB
Line 
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#include "generic_npc.h"
20
21
22#include "util/loading/factory.h"
23#include "util/loading/load_param.h"
24
25#include "interactive_model.h"
26#include "md2/md2Model.h"
27
28#include "sound_buffer.h"
29
30#include "loading/resource_manager.h"
31
32
33CREATE_FACTORY(GenericNPC, CL_GENERIC_NPC);
34
35#include "script_class.h"
36CREATE_SCRIPTABLE_CLASS(GenericNPC, CL_GENERIC_NPC,
37                        // Move
38                        addMethod("walkTo", ExecutorLua3<GenericNPC,float,float,float>(&GenericNPC::walkTo))
39                        ->addMethod("runTo", ExecutorLua3<GenericNPC,float,float,float>(&GenericNPC::runTo))
40                        ->addMethod("turnTo", ExecutorLua1<GenericNPC,float>(&GenericNPC::turnTo))
41                        ->addMethod("finalGoalReached", ExecutorLua0ret<GenericNPC,bool>(&GenericNPC::finalGoalReached))
42                        // Display
43                        ->addMethod("hide", ExecutorLua0<WorldEntity>(&WorldEntity::hide))
44                        ->addMethod("unhide", ExecutorLua0<WorldEntity>(&WorldEntity::unhide))
45                        // Coordinates
46                        ->addMethod("getAbsCoorX", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorX))
47                        ->addMethod("getAbsCoorY", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorY))
48                        ->addMethod("getAbsCoorZ", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorZ))
49                        ->addMethod("setAbsCoor", ExecutorLua3<PNode,float,float,float>(&PNode::setAbsCoor))
50                        ->addMethod("setAbsDir", ExecutorLua4<PNode,float,float,float,float>(&PNode::setAbsDir))
51                       );
52
53
54
55/**
56 * constructor
57 */
58GenericNPC::GenericNPC(const TiXmlElement* root)
59    : NPC(root)
60{
61  this->init();
62
63  if (root != NULL)
64    this->loadParams(root);
65}
66
67
68/**
69 * deconstructor
70 */
71GenericNPC::~GenericNPC ()
72{}
73
74
75/**
76 * initializing the npc enity
77 */
78void GenericNPC::init()
79{
80  this->setClassID(CL_GENERIC_NPC, "GenericNPC");
81  this->toList(OM_GROUP_00);
82
83  if (this->soundBuffer != NULL)
84    ResourceManager::getInstance()->unload(this->soundBuffer);
85  this->soundBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/rain.wav", WAV);
86
87  time = 30.0f;
88
89  // collision reaction registration
90  this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY);
91}
92
93
94/**
95 * loads the Settings of a MD2Creature from an XML-element.
96 * @param root the XML-element to load the MD2Creature's properties from
97 */
98void GenericNPC::loadParams(const TiXmlElement* root)
99{
100  NPC::loadParams(root);
101
102}
103
104
105/**
106 * sets the animation of this npc
107 * @param anumationIndex: the animation index
108 * @param anumPlaybackMode: the playback mode
109 */
110void GenericNPC::setAnimation(int animationIndex, int animPlaybackMode)
111{
112  if( likely(this->getModel(0) != NULL))
113    ((InteractiveModel*)this->getModel(0))->setAnimation(animationIndex, animPlaybackMode);
114}
115
116
117
118/**
119 * @returns the current animation number
120 */
121int GenericNPC::getAnimation()
122{
123  if( likely(this->getModel(0) != NULL))
124    return ((InteractiveModel*)this->getModel(0))->getAnimation();
125  else
126    return -1;
127}
128
129
130
131/**
132 * @returns true if animation is finished
133 */
134bool GenericNPC::isAnimationFinished()
135{
136  if( likely(this->getModel(0) != NULL))
137    return ((InteractiveModel*)this->getModel(0))->isAnimationFinished();
138  else
139    return false;
140}
141
142
143/**
144 * sets the animation speed of this entity
145 */
146void GenericNPC::setAnimationSpeed(float speed)
147{
148  if( likely(this->getModel(0) != NULL))
149    ((InteractiveModel*)this->getModel(0))->setAnimationSpeed(speed);
150}
151
152
153
154/**
155 * sets the animation of this npc
156 * @param anumationIndex: the animation index
157 * @param anumPlaybackMode: the playback mode
158 */
159void GenericNPC::playAnimation(int animationIndex, int animPlaybackMode)
160{
161  if( likely(this->getModel(0) != NULL))
162    ((InteractiveModel*)this->getModel(0))->setAnimation(animationIndex, animPlaybackMode);
163
164}
165
166
167/**
168 * play a sound
169 * @param filename: name of the file
170 */
171void GenericNPC::playSound(const std::string& filename)
172{}
173
174
175
176/**
177 * stops the generic animation
178 */
179void GenericNPC::stop()
180{
181
182}
183
184
185
186void GenericNPC::initNPC()
187{
188  if (!this->behaviourList.empty())
189  {
190    GenericNPC::Anim currentAnimation = this->behaviourList.front();
191
192    switch(this->behaviourList.front().type)
193    {
194      case Walk:
195      {
196        if( this->getAnimation() != RUN)
197          this->setAnimation(RUN, MD2_ANIM_LOOP);
198
199        Vector dir = (currentAnimation.v - this->getAbsCoor());
200        dir.y = 0.0f;
201        dir.getNormalized();
202        this->setAbsDir(Quaternion(dir, Vector(0.0, 1.0, 0.0)) * Quaternion(-M_PI_2, Vector(0.0, 1.0, 0.0)));
203
204        this->setAnimationSpeed(0.5f);
205      }
206        break;
207      case Run:
208      {
209        if( this->getAnimation() != RUN)
210          this->setAnimation(RUN, MD2_ANIM_LOOP);
211
212        Vector dir = (currentAnimation.v - this->getAbsCoor()).getNormalized();
213        this->setAbsDir(Quaternion(dir, Vector(0.0, 1.0, 0.0)) * Quaternion(-M_PI_2, Vector(0.0, 1.0, 0.0)));
214
215        this->setAnimationSpeed(1.0f);
216      }
217        break;
218      case Crouch:
219      {
220        if( this->getAnimation() != CROUCH_WALK)
221          this->setAnimation(CROUCH_WALK, MD2_ANIM_LOOP);
222
223        Vector dir = (currentAnimation.v - this->getAbsCoor()).getNormalized();
224        this->setAbsDir(Quaternion(dir, Vector(0.0, 1.0, 0.0)) * Quaternion(-M_PI_2, Vector(0.0, 1.0, 0.0)));
225
226        this->setAnimationSpeed(1.0f);
227      }
228        break;
229      case LookAt:
230        if( this->getAnimation() != STAND)
231          this->setAnimation(STAND, MD2_ANIM_LOOP);
232        break;
233      case Shoot:
234        if( this->getAnimation() != STAND)
235          this->setAnimation(STAND, MD2_ANIM_LOOP);
236        break;
237
238      default:
239        if( this->getAnimation() != STAND)
240          this->setAnimation(STAND, MD2_ANIM_LOOP);
241        break;
242
243    }
244  }
245}
246
247
248void GenericNPC::nextStep()
249{
250  if (!this->behaviourList.empty())
251    this->behaviourList.pop_front();
252  else
253    return;
254
255
256  if (!this->behaviourList.empty())
257  {
258    GenericNPC::Anim currentAnimation = this->behaviourList.front();
259
260    switch( currentAnimation.type)
261    {
262      case Walk:
263      {
264        if( this->getAnimation() != RUN)
265          this->setAnimation(RUN, MD2_ANIM_LOOP);
266
267
268        Vector dir = (currentAnimation.v - this->getAbsCoor());
269        dir.y = 0.0f;
270        dir.getNormalized();
271        this->setAbsDirSoft(Quaternion(dir, Vector(0.0, 1.0, 0.0)) * Quaternion(-M_PI_2, Vector(0.0, 1.0, 0.0)), 4.0);
272
273        this->setAnimationSpeed(0.5f);
274      }
275        break;
276      case Run:
277      {
278        if( this->getAnimation() != RUN)
279          this->setAnimation(RUN, MD2_ANIM_LOOP);
280
281        Vector dir = (currentAnimation.v - this->getAbsCoor()).getNormalized();
282        this->setAbsDirSoft(Quaternion(dir, Vector(0.0, 1.0, 0.0)) * Quaternion(-M_PI_2, Vector(0.0, 1.0, 0.0)), 4.0);
283
284        this->setAnimationSpeed(1.0f);
285      }
286        break;
287      case Crouch:
288      {
289        if( this->getAnimation() != CROUCH_WALK)
290          this->setAnimation(CROUCH_WALK, MD2_ANIM_LOOP);
291
292        Vector dir = (currentAnimation.v - this->getAbsCoor()).getNormalized();
293        this->setAbsDirSoft(Quaternion(dir, Vector(0.0, 1.0, 0.0)) * Quaternion(-M_PI_2, Vector(0.0, 1.0, 0.0)), 4.0);
294
295        this->setAnimationSpeed(1.0f);
296      }
297        break;
298      case LookAt:
299      {
300        if( this->getAnimation() != STAND)
301          this->setAnimation(STAND, MD2_ANIM_LOOP);
302      }
303        break;
304      case Shoot:
305        if( this->getAnimation() != STAND)
306        this->setAnimation(STAND, MD2_ANIM_LOOP);
307        break;
308
309      default:
310        if( this->getAnimation() != STAND)
311        this->setAnimation(STAND, MD2_ANIM_LOOP);
312        break;
313
314    }
315  }
316  else
317  {
318    this->setAnimation(STAND, MD2_ANIM_LOOP);
319  }
320}
321
322
323
324
325void GenericNPC::walkTo(const Vector& coordinate)
326{
327
328  GenericNPC::Anim anim;
329  anim.v = coordinate;
330  anim.type = Walk;
331  anim.speed = 30.0f;
332
333  if( this->behaviourList.empty())
334  {
335    this->behaviourList.push_back(anim);
336    this->initNPC();
337  }
338  else
339    this->behaviourList.push_back(anim);
340}
341
342void GenericNPC::walkTo(float x, float y, float z)
343{
344  //printf("Walking to %f, %f, %f \n",x,y,z);
345  this->walkTo(Vector(x,y,z));
346
347}
348
349/* running functions */
350void GenericNPC::runTo(const Vector& coordinate)
351{
352  GenericNPC::Anim anim;
353  anim.v = coordinate;
354  anim.type = Run;
355  anim.speed = 60.0f;
356
357  if( this->behaviourList.empty())
358  {
359    this->behaviourList.push_back(anim);
360    this->initNPC();
361  }
362  else
363    this->behaviourList.push_back(anim);
364}
365
366void GenericNPC::runTo(float x, float y, float z)
367{
368  this->runTo(Vector(x,y,z));
369}
370
371/* couching functinos */
372void GenericNPC::crouchTo(const Vector& coordinate)
373{
374  GenericNPC::Anim anim;
375  anim.v = coordinate;
376  anim.type = Crouch;
377
378  if( this->behaviourList.empty())
379  {
380    this->behaviourList.push_back(anim);
381    this->initNPC();
382  }
383  else
384    this->behaviourList.push_back(anim);
385}
386void GenericNPC::crouchTo(float x, float y, float z)
387{
388  this->crouchTo(Vector(x,y,z));
389}
390
391
392
393void GenericNPC::turnTo(float degreeInY)
394{
395  GenericNPC::Anim anim;
396  anim.q = Quaternion(Vector(0,1,0), degreeInY);
397  anim.type = TurnTo;
398
399  if( this->behaviourList.empty())
400  {
401    this->behaviourList.push_back(anim);
402    this->initNPC();
403  }
404  else
405    this->behaviourList.push_back(anim);
406}
407
408
409
410/**
411 * lookat a world entity
412 * @param worldEntity: the worldentity to look at
413 */
414void GenericNPC::lookAt(WorldEntity* worldEntity)
415{
416  GenericNPC::Anim anim;
417  anim.entity = worldEntity;
418  anim.type = LookAt;
419
420  if( this->behaviourList.empty())
421  {
422    this->behaviourList.push_back(anim);
423    this->initNPC();
424  }
425  else
426    this->behaviourList.push_back(anim);
427}
428
429
430
431
432/**
433 * talk to a world entity and play a sound/music/voice
434 * @param worldEntity: entity
435 * @param dialogNr: sound nr to be played (from the xml load tags)
436 */
437void GenericNPC::talkTo(WorldEntity* worldEntity, int dialogNr)
438{}
439
440
441/**
442 * world entity to shoot at if there is any weapon on the npc
443 * @param entity: entity to shoot entity
444 */
445void GenericNPC::shootAt(WorldEntity* entity)
446{}
447
448
449
450
451
452
453
454
455
456
457/**
458 * tick this world entity
459 * @param time: time in seconds expirded since the last tick
460 */
461void GenericNPC::tick (float dt)
462{
463  if( likely(this->getModel(0) != NULL))
464    ((InteractiveModel*)this->getModel(0))->tick(dt);
465
466
467  if (!this->behaviourList.empty())
468  {
469    GenericNPC::Anim currentAnimation = this->behaviourList.front();
470
471    switch( currentAnimation.type)
472    {
473      case Walk:
474        {
475          Vector dest = currentAnimation.v - this->getAbsCoor();
476          dest.y = 0.0f;
477          if (dest.len() < .5)
478            this->nextStep();
479          else
480          {
481            Vector move = dest.getNormalized() * currentAnimation.speed * dt;
482            this->shiftCoor(move);
483          }
484        }
485        break;
486
487      case Run:
488      {
489        Vector dest = currentAnimation.v - this->getAbsCoor();
490        if (dest.len() < .5)
491          this->nextStep();
492        else
493        {
494          this->shiftCoor(dest.getNormalized() * currentAnimation.speed * dt);
495        }
496      }
497      break;
498
499      case Crouch:
500      {
501        Vector dest = currentAnimation.v - this->getAbsCoor();
502        if (dest.len() < .5)
503          this->nextStep();
504        else
505        {
506          this->shiftCoor(dest.getNormalized() * currentAnimation.speed * dt);
507        }
508      }
509      break;
510
511      case TurnTo:
512        //Quaternion direction = this->
513        break;
514
515      case LookAt:
516        break;
517
518      case Shoot:
519        break;
520
521      default:
522        break;
523
524    }
525  }
526
527  // physical falling of the player
528  if( !this->isOnGround())
529  {
530    this->fallVelocity += 300.0f * dt;
531    //velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity;
532   // PRINTF(0)("%s is not on ground\n", this->getName());
533
534    this->shiftCoor(Vector(0, -this->fallVelocity * dt,0));
535
536  }
537  else
538  {
539    this->fallVelocity = 0.0f;
540  }
541
542}
543
544
545
546void GenericNPC::destroy()
547{
548  int randi = (int)(5.0f * (float)rand()/(float)RAND_MAX);
549
550  this->setAnimationSpeed(1.0f);
551
552  if( randi == 1)
553    this->setAnimation(DEATH_FALLBACK, MD2_ANIM_ONCE);
554  else if( randi == 2)
555    this->setAnimation(DEATH_FALLFORWARD, MD2_ANIM_ONCE);
556  else if( randi == 3)
557    this->setAnimation(DEATH_FALLBACKSLOW, MD2_ANIM_ONCE);
558  else if( randi == 4)
559    this->setAnimation(CROUCH_DEATH, MD2_ANIM_ONCE);
560  else
561    this->setAnimation(DEATH_FALLBACK, MD2_ANIM_ONCE);
562}
563
Note: See TracBrowser for help on using the repository browser.