Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed the fast animation bug. now npc have normal animation speed

File size: 7.5 KB
RevLine 
[8514]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
[8908]19#include "generic_npc.h"
[8514]20
[8908]21
[8514]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
[8516]28#include "sound_buffer.h"
29
30#include "loading/resource_manager.h"
31
[8514]32
33CREATE_FACTORY(GenericNPC, CL_GENERIC_NPC);
34
[8894]35#include "script_class.h"
36CREATE_SCRIPTABLE_CLASS(GenericNPC, CL_GENERIC_NPC,
[8913]37                        // Move
[8908]38                        addMethod("walkTo", ExecutorLua3<GenericNPC,float,float,float>(&GenericNPC::walkTo))
[8917]39                        ->addMethod("runTo", ExecutorLua3<GenericNPC,float,float,float>(&GenericNPC::runTo))
[8908]40                        ->addMethod("turnTo", ExecutorLua1<GenericNPC,float>(&GenericNPC::turnTo))
[8917]41                        ->addMethod("finalGoalReached", ExecutorLua0ret<GenericNPC,bool>(&GenericNPC::finalGoalReached))
[8913]42                        // Display
[8894]43                        ->addMethod("hide", ExecutorLua0<WorldEntity>(&WorldEntity::hide))
44                        ->addMethod("unhide", ExecutorLua0<WorldEntity>(&WorldEntity::unhide))
[8913]45                        // Coordinates
[8894]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))
[8908]51
[8894]52                       );
[8514]53
54
[8894]55
[8514]56/**
57 * constructor
58 */
59GenericNPC::GenericNPC(const TiXmlElement* root)
[8913]60    : NPC(root)
[8514]61{
62  this->init();
63
64  if (root != NULL)
65    this->loadParams(root);
66}
67
68
69/**
70 * deconstructor
71 */
72GenericNPC::~GenericNPC ()
[8913]73{}
[8514]74
75
76/**
77 * initializing the npc enity
78 */
79void GenericNPC::init()
80{
81  this->setClassID(CL_GENERIC_NPC, "GenericNPC");
82  this->toList(OM_GROUP_00);
[8516]83
84  if (this->soundBuffer != NULL)
85    ResourceManager::getInstance()->unload(this->soundBuffer);
86  this->soundBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/rain.wav", WAV);
[8705]87
[8894]88  time = 30.0f;
[8705]89  // collision reaction registration
[8913]90  //   this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY);
[8514]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{
[8705]100  NPC::loadParams(root);
[8514]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 * sets the animation of this npc
119 * @param anumationIndex: the animation index
120 * @param anumPlaybackMode: the playback mode
121 */
[8705]122void GenericNPC::playAnimation(int animationIndex, int animPlaybackMode)
[8514]123{
124  if( likely(this->getModel(0) != NULL))
125    ((InteractiveModel*)this->getModel(0))->setAnimation(animationIndex, animPlaybackMode);
126
127}
128
129
130/**
131 * play a sound
132 * @param filename: name of the file
133 */
[8705]134void GenericNPC::playSound(std::string filename)
[8913]135{}
[8705]136
[8514]137
138
[8894]139/**
[8908]140 * stops the generic animation
[8894]141 */
[8908]142void GenericNPC::stop()
[8913]143{}
144
145void GenericNPC::nextStep()
[8894]146{
[8913]147  if (!this->behaviourList.empty())
148    this->behaviourList.pop_front();
149  else
150    return;
[8802]151
[8913]152  if (!this->behaviourList.empty())
153  {
154    switch(this->behaviourList.front().type)
155    {
156      case Walk:
157        this->setAnimation(RUN, MD2_ANIM_LOOP);
158        break;
159      case Run:
160        this->setAnimation(RUN, MD2_ANIM_LOOP);
161        break;
162      case Crouch:
163        this->setAnimation(CROUCH_WALK, MD2_ANIM_LOOP);
164        break;
165      case LookAt:
166        this->setAnimation(STAND, MD2_ANIM_LOOP);
167
168        break;
169      case Shoot:
170        this->setAnimation(STAND, MD2_ANIM_LOOP);
171        break;
172
173      default:
174        this->setAnimation(STAND, MD2_ANIM_LOOP);
175        break;
176
177    }
178  }
179  else
180  {
181    this->setAnimation(STAND, MD2_ANIM_LOOP);
182  }
[8894]183}
184
[8802]185
[8908]186void GenericNPC::walkTo(const Vector& coordinate)
[8805]187{
[8936]188
[8909]189  GenericNPC::Anim anim;
190  anim.v = coordinate;
191  anim.type = Walk;
192
193  this->behaviourList.push_back(anim);
[8805]194}
195
[8908]196void GenericNPC::walkTo(float x, float y, float z)
[8909]197{
[8915]198  printf("Walking to %f, %f, %f \n",x,y,z);
[8909]199  this->walkTo(Vector(x,y,z));
[8894]200
[8909]201}
202
[8908]203/* running functions */
204void GenericNPC::runTo(const Vector& coordinate)
[8909]205{
206  GenericNPC::Anim anim;
207  anim.v = coordinate;
208  anim.type = Run;
209
210  this->behaviourList.push_back(anim);
211}
212
[8908]213void GenericNPC::runTo(float x, float y, float z)
[8909]214{
215  this->runTo(Vector(x,y,z));
216}
[8894]217
[8908]218/* couching functinos */
219void GenericNPC::crouchTo(const Vector& coordinate)
[8909]220{
221  GenericNPC::Anim anim;
222  anim.v = coordinate;
223  anim.type = Crouch;
224
225  this->behaviourList.push_back(anim);
226}
[8908]227void GenericNPC::crouchTo(float x, float y, float z)
[8909]228{
229  this->crouchTo(Vector(x,y,z));
230}
[8894]231
232
233
[8908]234void GenericNPC::turnTo(float degreeInY)
[8909]235{
236  GenericNPC::Anim anim;
237  anim.q = Quaternion(Vector(0,1,0), degreeInY);
238  anim.type = TurnTo;
[8894]239
[8909]240  this->behaviourList.push_back(anim);
241}
[8894]242
243
[8909]244
[8894]245/**
246 * lookat a world entity
247 * @param worldEntity: the worldentity to look at
248 */
[8908]249void GenericNPC::lookAt(WorldEntity* worldEntity)
[8909]250{
251  GenericNPC::Anim anim;
252  anim.entity = worldEntity;
253  anim.type = LookAt;
[8894]254
[8909]255  this->behaviourList.push_back(anim);
256}
[8894]257
258
259
[8909]260
[8894]261/**
262 * talk to a world entity and play a sound/music/voice
263 * @param worldEntity: entity
264 * @param dialogNr: sound nr to be played (from the xml load tags)
265 */
[8908]266void GenericNPC::talkTo(WorldEntity* worldEntity, int dialogNr)
[8894]267{}
268
269
270/**
271 * world entity to shoot at if there is any weapon on the npc
272 * @param entity: entity to shoot entity
273 */
274void GenericNPC::shootAt(WorldEntity* entity)
275{}
276
277
278
279
280
281
282
283
284
285
286/**
[8514]287 * tick this world entity
288 * @param time: time in seconds expirded since the last tick
289 */
[8914]290void GenericNPC::tick (float dt)
[8514]291{
292  if( likely(this->getModel(0) != NULL))
[8936]293    ((InteractiveModel*)this->getModel(0))->tick(dt);
[8894]294
[8909]295
296  if (!this->behaviourList.empty())
297  {
298    switch(this->behaviourList.front().type)
299    {
300      case Walk:
[8913]301        {
[8914]302          Vector dir = this->getAbsCoor() - this->behaviourList.front().v;
303          if (dir.len() < .5)
304            this->nextStep();
305          else
306          {
307            this->shiftCoor(dir.getNormalized() * dt);
308          }
[8913]309        }
[8909]310        break;
311      case Run:
312        break;
313      case Crouch:
314        break;
[8914]315      case TurnTo:
316        //Quaternion direction = this->
317        break;
[8909]318      case LookAt:
319        break;
320      case Shoot:
321        break;
322
[8913]323      default:
324        break;
325
[8909]326    }
327  }
328
[8514]329}
330
331
332
333void GenericNPC::destroy()
334{
335  int randi = (int)(5.0f * (float)rand()/(float)RAND_MAX);
336
337  if( randi == 1)
338    this->setAnimation(DEATH_FALLBACK, MD2_ANIM_ONCE);
339  else if( randi == 2)
340    this->setAnimation(DEATH_FALLFORWARD, MD2_ANIM_ONCE);
341  else if( randi == 3)
342    this->setAnimation(DEATH_FALLBACKSLOW, MD2_ANIM_ONCE);
343  else if( randi == 4)
344    this->setAnimation(CROUCH_DEATH, MD2_ANIM_ONCE);
345  else
346    this->setAnimation(DEATH_FALLBACK, MD2_ANIM_ONCE);
347}
348
Note: See TracBrowser for help on using the repository browser.