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 "door.h" |
---|
27 | #include "class_list.h" |
---|
28 | |
---|
29 | |
---|
30 | using namespace std; |
---|
31 | |
---|
32 | |
---|
33 | CREATE_FACTORY(Door, CL_DOOR); |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | //! list of all different animations a std md2model supports |
---|
38 | sAnim Door::animationList[2] = |
---|
39 | { |
---|
40 | // begin, end, fps, interruptable |
---|
41 | { 0, 15, 30, 0 }, //!< OPEN |
---|
42 | { 15, 29, 30, 0 } //!< CLOSE |
---|
43 | }; |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | Door::Door(const TiXmlElement* root) |
---|
48 | { |
---|
49 | |
---|
50 | this->setClassID(CL_DOOR, "Door"); |
---|
51 | this->scale = 1.0f; |
---|
52 | this->actionRadius = 1.0; |
---|
53 | |
---|
54 | if( root != NULL) |
---|
55 | this->loadParams(root); |
---|
56 | |
---|
57 | this->toList(OM_COMMON); |
---|
58 | this->bLocked = false; |
---|
59 | this->bOpen = false; |
---|
60 | |
---|
61 | this->loadMD2Texture("maps/doors.jpg"); |
---|
62 | this->loadModel("models/creatures/doors.md2", this->scale); |
---|
63 | |
---|
64 | this->setAnimation(DOOR_CLOSE, MD2_ANIM_ONCE); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | Door::~Door () |
---|
69 | {} |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | /** |
---|
74 | * loads the Settings of a MD2Creature from an XML-element. |
---|
75 | * @param root the XML-element to load the MD2Creature's properties from |
---|
76 | */ |
---|
77 | void Door::loadParams(const TiXmlElement* root) |
---|
78 | { |
---|
79 | WorldEntity::loadParams(root); |
---|
80 | |
---|
81 | LoadParam(root, "action-radius", this, Door, setActionRadius) |
---|
82 | .describe("sets the action radius of the door") |
---|
83 | .defaultValues(3.0); |
---|
84 | LoadParam(root, "scale", this, Door, setScale) |
---|
85 | .describe("sets the scale of the door") |
---|
86 | .defaultValues(1.0); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | /** |
---|
91 | * sets the animatin of this entity |
---|
92 | */ |
---|
93 | void Door::setAnimation(int animNum, int playbackMode) |
---|
94 | { |
---|
95 | if( likely(this->getModel(0) != NULL)) |
---|
96 | ((InteractiveModel*)this->getModel(0))->setAnimation(animationList[animNum].firstFrame, |
---|
97 | animationList[animNum].lastFrame, |
---|
98 | animationList[animNum].fps, |
---|
99 | animationList[animNum].bStoppable, |
---|
100 | playbackMode); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | /** |
---|
105 | * ticks the door |
---|
106 | * @param time: time since last tick |
---|
107 | */ |
---|
108 | void Door::tick (float time) |
---|
109 | { |
---|
110 | if( likely(this->getModel(0) != NULL)) |
---|
111 | ((InteractiveModel*)this->getModel(0))->tick(time); |
---|
112 | |
---|
113 | if( !this->bOpen) |
---|
114 | { |
---|
115 | if( this->checkOpen()) |
---|
116 | { |
---|
117 | this->open(); |
---|
118 | } |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | if( !this->checkOpen()) |
---|
123 | { |
---|
124 | this->close(); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | /** |
---|
132 | * open the door |
---|
133 | */ |
---|
134 | void Door::open() |
---|
135 | { |
---|
136 | if( this->bLocked) |
---|
137 | return; |
---|
138 | |
---|
139 | this->setAnimation(DOOR_OPEN, MD2_ANIM_ONCE); |
---|
140 | this->bOpen = true; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | /** |
---|
145 | * close the door |
---|
146 | */ |
---|
147 | void Door::close() |
---|
148 | { |
---|
149 | this->setAnimation(DOOR_CLOSE, MD2_ANIM_ONCE); |
---|
150 | this->bOpen = false; |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | |
---|
155 | /** |
---|
156 | * checks if the door is open |
---|
157 | */ |
---|
158 | bool Door::checkOpen() |
---|
159 | { |
---|
160 | |
---|
161 | std::list<BaseObject*>::const_iterator it; |
---|
162 | const std::list<BaseObject*>* list = ClassList::getList(CL_PLAYABLE); |
---|
163 | WorldEntity* entity; |
---|
164 | float distance; |
---|
165 | |
---|
166 | if( list == NULL) |
---|
167 | return false; |
---|
168 | |
---|
169 | // for all players |
---|
170 | for( it = list->begin(); it != list->end(); it++) |
---|
171 | { |
---|
172 | entity = dynamic_cast<WorldEntity*>(*it); |
---|
173 | |
---|
174 | distance = fabs((this->getAbsCoor() - entity->getAbsCoor()).len()); |
---|
175 | if( distance < this->actionRadius) |
---|
176 | return true; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | list = ClassList::getList(CL_GENERIC_NPC); |
---|
181 | if( list == NULL) |
---|
182 | return false; |
---|
183 | for( it = list->begin(); it != list->end(); it++) |
---|
184 | { |
---|
185 | entity = dynamic_cast<WorldEntity*>(*it); |
---|
186 | |
---|
187 | distance = fabs((this->getAbsCoor() - entity->getAbsCoor()).len()); |
---|
188 | if( distance < this->actionRadius) |
---|
189 | return true; |
---|
190 | } |
---|
191 | |
---|
192 | return false; |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | |
---|