Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.old/src/world_entities/npcs/gate.cc @ 10662

Last change on this file since 10662 was 9406, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

File size: 5.0 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: Silvan Nellen
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
25
26#include "gate.h"
27#include "class_list.h"
28
29#include "effects/explosion.h"
30
31
32
33
34
35
36CREATE_FACTORY(Gate, CL_GATE);
37
38
39#include "script_class.h"
40CREATE_SCRIPTABLE_CLASS(Gate, CL_GATE,
41                            addMethod("hide", ExecutorLua0<WorldEntity>(&WorldEntity::hide))
42                            ->addMethod("unhide", ExecutorLua0<WorldEntity>(&WorldEntity::unhide))
43                            ->addMethod("destroy", ExecutorLua0<Gate>(&Gate::destroy))   
44                            ->addMethod("setAbsCoor", ExecutorLua3<PNode,float,float,float>(&PNode::setAbsCoor))
45                            ->addMethod("getAbsCoorX", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorX))
46                            ->addMethod("getAbsCoorY", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorY))
47                            ->addMethod("getAbsCoorZ", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorZ))
48                       );
49
50
51//! list of all different animations a std md2model supports
52sAnim Gate::animationList[3] =
53{
54 // begin, end, fps, interruptable
55  {   0,  7,  30,  0 },    //!< OPEN
56  {   8, 15,  30,  0 },    //!< CLOSE
57  {  16, 18,  30,  0 }     //!< DIE
58};
59
60
61
62Gate::Gate(const TiXmlElement* root)
63{
64
65  this->setClassID(CL_GATE, "Gate");
66  this->scale = 1.0f;
67  this->actionRadius = 1.0;
68  this->destroyed = false;
69
70  if( root != NULL)
71    this->loadParams(root);
72
73  this->toList(OM_COMMON);
74  this->bLocked = false;
75  this->bOpen = false;
76
77  this->loadMD2Texture("maps/wheel.jpg");
78  this->loadModel("models/creatures/hypergate.md2", this->scale);
79
80  this->setAnimation(GATE_CLOSE, MD2_ANIM_ONCE);
81}
82
83
84Gate::~Gate ()
85{}
86
87
88
89/**
90 * loads the Settings of a MD2Creature from an XML-element.
91 * @param root the XML-element to load the MD2Creature's properties from
92 */
93void Gate::loadParams(const TiXmlElement* root)
94{
95  WorldEntity::loadParams(root);
96
97  LoadParam(root, "action-radius", this, Gate, setActionRadius)
98      .describe("sets the action radius of the door")
99      .defaultValues(3.0);
100  LoadParam(root, "scale", this, Gate, setScale)
101      .describe("sets the scale of the door")
102      .defaultValues(1.0);
103}
104
105
106/**
107 * sets the animatin of this entity
108 */
109void  Gate::setAnimation(int animNum, int playbackMode)
110{
111  if( likely(this->getModel(0) != NULL))
112    ((InteractiveModel*)this->getModel(0))->setAnimation(animationList[animNum].firstFrame,
113                                                         animationList[animNum].lastFrame,
114                                                         animationList[animNum].fps,
115                                                         animationList[animNum].bStoppable,
116                                                         playbackMode);
117}
118
119
120/**
121 * ticks the door
122 * @param time: time since last tick
123 */
124void Gate::tick (float time)
125{
126  if( likely(this->getModel(0) != NULL))
127    ((InteractiveModel*)this->getModel(0))->tick(time);
128
129
130  if( !this->bOpen)
131  {
132    if( this->checkOpen())
133    {
134      this->open();
135    }
136  }
137  else
138  {
139    if( !this->checkOpen())
140    {
141      this->close();
142    }
143  }
144
145
146}
147
148
149/**
150 * open the door
151 */
152void Gate::open()
153{
154  if( this->bLocked || this->destroyed)
155    return;
156
157  this->setAnimation(GATE_OPEN, MD2_ANIM_ONCE);
158  this->bOpen = true;
159}
160
161
162/**
163 * close the door
164 */
165void Gate::close()
166{
167 
168  if( this->destroyed)
169    return;
170 
171  this->setAnimation(GATE_CLOSE, MD2_ANIM_ONCE);
172  this->bOpen = false;
173}
174
175
176void Gate::destroy()
177{
178  if( this->destroyed)
179    return;
180 
181  this->setAnimation(GATE_DIE, MD2_ANIM_ONCE);
182
183  Explosion::explode(this, Vector(this->getScaling()/160,this->getScaling()/160,this->getScaling()/160));
184 
185 
186  this->destroyed = true;
187}
188
189
190/**
191 * checks if the door is open
192 */
193bool Gate::checkOpen()
194{
195
196  std::list<BaseObject*>::const_iterator it;
197  const std::list<BaseObject*>* list = ClassList::getList(CL_PLAYABLE);
198  WorldEntity* entity;
199  float distance;
200
201  if( list == NULL)
202    return false;
203
204  // for all players
205  for( it = list->begin(); it != list->end(); it++)
206  {
207    entity = dynamic_cast<WorldEntity*>(*it);
208
209    distance = fabs((this->getAbsCoor() - entity->getAbsCoor()).len());
210    if( distance < this->actionRadius)
211      return true;
212  }
213
214
215  list = ClassList::getList(CL_GENERIC_NPC);
216  if( list == NULL)
217    return false;
218  for( it = list->begin(); it != list->end(); it++)
219  {
220    entity = dynamic_cast<WorldEntity*>(*it);
221
222    distance = fabs((this->getAbsCoor() - entity->getAbsCoor()).len());
223    if( distance < this->actionRadius)
224      return true;
225  }
226
227  return false;
228}
229
230
231
Note: See TracBrowser for help on using the repository browser.