Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/npcs/gate.cc @ 9235

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

merged the presentation back

File size: 4.8 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
25
26#include "gate.h"
27#include "class_list.h"
28
29#include "effects/explosion.h"
30
31
32
33using namespace std;
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
69  if( root != NULL)
70    this->loadParams(root);
71
72  this->toList(OM_COMMON);
73  this->bLocked = false;
74  this->bOpen = false;
75
76  this->loadMD2Texture("maps/wheel.jpg");
77  this->loadModel("models/creatures/hypergate.md2", this->scale);
78
79  this->setAnimation(GATE_CLOSE, MD2_ANIM_ONCE);
80}
81
82
83Gate::~Gate ()
84{}
85
86
87
88/**
89 * loads the Settings of a MD2Creature from an XML-element.
90 * @param root the XML-element to load the MD2Creature's properties from
91 */
92void Gate::loadParams(const TiXmlElement* root)
93{
94  WorldEntity::loadParams(root);
95
96  LoadParam(root, "action-radius", this, Gate, setActionRadius)
97      .describe("sets the action radius of the door")
98      .defaultValues(3.0);
99  LoadParam(root, "scale", this, Gate, setScale)
100      .describe("sets the scale of the door")
101      .defaultValues(1.0);
102}
103
104
105/**
106 * sets the animatin of this entity
107 */
108void  Gate::setAnimation(int animNum, int playbackMode)
109{
110  if( likely(this->getModel(0) != NULL))
111    ((InteractiveModel*)this->getModel(0))->setAnimation(animationList[animNum].firstFrame,
112                                                         animationList[animNum].lastFrame,
113                                                         animationList[animNum].fps,
114                                                         animationList[animNum].bStoppable,
115                                                         playbackMode);
116}
117
118
119/**
120 * ticks the door
121 * @param time: time since last tick
122 */
123void Gate::tick (float time)
124{
125  if( likely(this->getModel(0) != NULL))
126    ((InteractiveModel*)this->getModel(0))->tick(time);
127
128
129  if( !this->bOpen)
130  {
131    if( this->checkOpen())
132    {
133      this->open();
134    }
135  }
136  else
137  {
138    if( !this->checkOpen())
139    {
140      this->close();
141    }
142  }
143
144
145}
146
147
148/**
149 * open the door
150 */
151void Gate::open()
152{
153  if( this->bLocked)
154    return;
155
156  this->setAnimation(GATE_OPEN, MD2_ANIM_ONCE);
157  this->bOpen = true;
158}
159
160
161/**
162 * close the door
163 */
164void Gate::close()
165{
166  this->setAnimation(GATE_CLOSE, MD2_ANIM_ONCE);
167  this->bOpen = false;
168}
169
170
171void Gate::destroy()
172{
173  this->setAnimation(GATE_DIE, MD2_ANIM_ONCE);
174
175  Explosion::explode(this, Vector(10,10,10));
176}
177
178
179/**
180 * checks if the door is open
181 */
182bool Gate::checkOpen()
183{
184
185  std::list<BaseObject*>::const_iterator it;
186  const std::list<BaseObject*>* list = ClassList::getList(CL_PLAYABLE);
187  WorldEntity* entity;
188  float distance;
189
190  if( list == NULL)
191    return false;
192
193  // for all players
194  for( it = list->begin(); it != list->end(); it++)
195  {
196    entity = dynamic_cast<WorldEntity*>(*it);
197
198    distance = fabs((this->getAbsCoor() - entity->getAbsCoor()).len());
199    if( distance < this->actionRadius)
200      return true;
201  }
202
203
204  list = ClassList::getList(CL_GENERIC_NPC);
205  if( list == NULL)
206    return false;
207  for( it = list->begin(); it != list->end(); it++)
208  {
209    entity = dynamic_cast<WorldEntity*>(*it);
210
211    distance = fabs((this->getAbsCoor() - entity->getAbsCoor()).len());
212    if( distance < this->actionRadius)
213      return true;
214  }
215
216  return false;
217}
218
219
220
Note: See TracBrowser for help on using the repository browser.