Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8917 was 8917, checked in by snellen, 18 years ago

made genericNPC::finalGoalReached() scriptable

File size: 7.5 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/**
57 * constructor
58 */
59GenericNPC::GenericNPC(const TiXmlElement* root)
60    : NPC(root)
61{
62  this->init();
63
64  if (root != NULL)
65    this->loadParams(root);
66}
67
68
69/**
70 * deconstructor
71 */
72GenericNPC::~GenericNPC ()
73{}
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);
83
84  if (this->soundBuffer != NULL)
85    ResourceManager::getInstance()->unload(this->soundBuffer);
86  this->soundBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/rain.wav", WAV);
87
88  time = 30.0f;
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 * sets the animation of this npc
119 * @param anumationIndex: the animation index
120 * @param anumPlaybackMode: the playback mode
121 */
122void GenericNPC::playAnimation(int animationIndex, int animPlaybackMode)
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 */
134void GenericNPC::playSound(std::string filename)
135{}
136
137
138
139/**
140 * stops the generic animation
141 */
142void GenericNPC::stop()
143{}
144
145void GenericNPC::nextStep()
146{
147  if (!this->behaviourList.empty())
148    this->behaviourList.pop_front();
149  else
150    return;
151
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  }
183}
184
185
186void GenericNPC::walkTo(const Vector& coordinate)
187{
188 
189  GenericNPC::Anim anim;
190  anim.v = coordinate;
191  anim.type = Walk;
192
193  this->behaviourList.push_back(anim);
194}
195
196void GenericNPC::walkTo(float x, float y, float z)
197{
198  printf("Walking to %f, %f, %f \n",x,y,z);
199  this->walkTo(Vector(x,y,z));
200
201}
202
203/* running functions */
204void GenericNPC::runTo(const Vector& coordinate)
205{
206  GenericNPC::Anim anim;
207  anim.v = coordinate;
208  anim.type = Run;
209
210  this->behaviourList.push_back(anim);
211}
212
213void GenericNPC::runTo(float x, float y, float z)
214{
215  this->runTo(Vector(x,y,z));
216}
217
218/* couching functinos */
219void GenericNPC::crouchTo(const Vector& coordinate)
220{
221  GenericNPC::Anim anim;
222  anim.v = coordinate;
223  anim.type = Crouch;
224
225  this->behaviourList.push_back(anim);
226}
227void GenericNPC::crouchTo(float x, float y, float z)
228{
229  this->crouchTo(Vector(x,y,z));
230}
231
232
233
234void GenericNPC::turnTo(float degreeInY)
235{
236  GenericNPC::Anim anim;
237  anim.q = Quaternion(Vector(0,1,0), degreeInY);
238  anim.type = TurnTo;
239
240  this->behaviourList.push_back(anim);
241}
242
243
244
245/**
246 * lookat a world entity
247 * @param worldEntity: the worldentity to look at
248 */
249void GenericNPC::lookAt(WorldEntity* worldEntity)
250{
251  GenericNPC::Anim anim;
252  anim.entity = worldEntity;
253  anim.type = LookAt;
254
255  this->behaviourList.push_back(anim);
256}
257
258
259
260
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 */
266void GenericNPC::talkTo(WorldEntity* worldEntity, int dialogNr)
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/**
287 * tick this world entity
288 * @param time: time in seconds expirded since the last tick
289 */
290void GenericNPC::tick (float dt)
291{
292  if( likely(this->getModel(0) != NULL))
293    ((InteractiveModel*)this->getModel(0))->tick(time);
294
295
296  if (!this->behaviourList.empty())
297  {
298    switch(this->behaviourList.front().type)
299    {
300      case Walk:
301        {
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          }
309        }
310        break;
311      case Run:
312        break;
313      case Crouch:
314        break;
315      case TurnTo:
316        //Quaternion direction = this->
317        break;
318      case LookAt:
319        break;
320      case Shoot:
321        break;
322
323      default:
324        break;
325
326    }
327  }
328
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.