Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/npcs/generic_npc.cc @ 8854

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

trunk: generic npc model wolking function

File size: 4.7 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
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
34using namespace std;
35
36
37
38CREATE_FACTORY(GenericNPC, CL_GENERIC_NPC);
39
40
41
42/**
43 * constructor
44 */
45GenericNPC::GenericNPC(const TiXmlElement* root)
46  : NPC(root)
47{
48  this->init();
49
50  if (root != NULL)
51    this->loadParams(root);
52}
53
54
55/**
56 * deconstructor
57 */
58GenericNPC::~GenericNPC ()
59{
60  if( this->currentAnim != NULL)
61    delete this->currentAnim;
62}
63
64
65/**
66 * initializing the npc enity
67 */
68void GenericNPC::init()
69{
70  this->setClassID(CL_GENERIC_NPC, "GenericNPC");
71  this->toList(OM_GROUP_00);
72
73  if (this->soundBuffer != NULL)
74    ResourceManager::getInstance()->unload(this->soundBuffer);
75  this->soundBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/rain.wav", WAV);
76
77  // collision reaction registration
78//   this->subscribeReaction(CREngine::CR_PHYSICS_GROUND_WALK, CL_BSP_ENTITY);
79}
80
81
82/**
83 * loads the Settings of a MD2Creature from an XML-element.
84 * @param root the XML-element to load the MD2Creature's properties from
85 */
86void GenericNPC::loadParams(const TiXmlElement* root)
87{
88  NPC::loadParams(root);
89
90}
91
92
93/**
94 * sets the animation of this npc
95 * @param anumationIndex: the animation index
96 * @param anumPlaybackMode: the playback mode
97 */
98void GenericNPC::setAnimation(int animationIndex, int animPlaybackMode)
99{
100  if( likely(this->getModel(0) != NULL))
101    ((InteractiveModel*)this->getModel(0))->setAnimation(animationIndex, animPlaybackMode);
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::playAnimation(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 * play a sound
120 * @param filename: name of the file
121 */
122void GenericNPC::playSound(std::string filename)
123{
124
125}
126
127
128/**
129 * walt to
130 * @param coordinate: coordinate to go to
131 */
132float GenericNPC::walkTo(float x, float y, float z, float qu, float qx, float qy, float qz)
133{
134  Vector destCoor = Vector(x, y, z);
135  Quaternion destDir = Quaternion(Vector(qx, qy, qz), qu);
136
137  // check if this is the current goal
138  if( this->destCoor != destCoor && this->destDir != destDir)
139  {
140    this->destCoor = Vector(x, y, 0.0f);
141    this->destDir = Quaternion(Vector(qx, qy, qz), qu);
142
143    float time = 5.0f;
144
145    if( this->currentAnim != NULL)
146      delete this->currentAnim;
147
148    this->currentAnim = new Animation3D(this);
149    this->currentAnim->addKeyFrame(this->getAbsCoor(), this->getAbsDir(), 0.0f);
150    this->currentAnim->addKeyFrame(this->destCoor, this->destDir, time);
151  }
152
153  // calculate the distance
154  Vector distance = this->getAbsCoor() - this->destCoor;
155  return distance.len();
156}
157
158
159
160/**
161 * walk to a specific place with direction
162 *
163 * @param x: x coordinate to go to
164 * @param y: y coordinate to go to
165 * @param qu: angle to rotate
166 * @param qx: x coordinate of rotation vector
167 * @param qy: y coordinate of rotation vector
168 * @param qz: z coordinate of rotation vector
169 *
170 */
171float GenericNPC::walkTo(float x, float y, float qu, float qx, float qy, float qz)
172{
173  return this->walkTo(x, y, 0.0f, qu, qx, qy, qz);
174}
175
176
177/**
178 * walk to a specific place with direction
179 *
180 * @param coor: vector place
181 * @param dir: direction
182 *
183 */
184float GenericNPC::walkTo(const Vector& coor, const Quaternion& dir)
185{
186  return this->walkTo(coor.x, coor.y, coor.z, dir.w, dir.v.x, dir.v.y, dir.v.z);
187}
188
189
190/**
191 * tick this world entity
192 * @param time: time in seconds expirded since the last tick
193 */
194void GenericNPC::tick (float time)
195{
196  if( likely(this->getModel(0) != NULL))
197    ((InteractiveModel*)this->getModel(0))->tick(time);
198}
199
200
201
202void GenericNPC::destroy()
203{
204  int randi = (int)(5.0f * (float)rand()/(float)RAND_MAX);
205
206  if( randi == 1)
207    this->setAnimation(DEATH_FALLBACK, MD2_ANIM_ONCE);
208  else if( randi == 2)
209    this->setAnimation(DEATH_FALLFORWARD, MD2_ANIM_ONCE);
210  else if( randi == 3)
211    this->setAnimation(DEATH_FALLBACKSLOW, MD2_ANIM_ONCE);
212  else if( randi == 4)
213    this->setAnimation(CROUCH_DEATH, MD2_ANIM_ONCE);
214  else
215    this->setAnimation(DEATH_FALLBACK, MD2_ANIM_ONCE);
216}
217
Note: See TracBrowser for help on using the repository browser.