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