1 | |
---|
2 | /* |
---|
3 | orxonox - the future of 3D-vertical-scrollers |
---|
4 | |
---|
5 | Copyright (C) 2004 orx |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation; either version 2, or (at your option) |
---|
10 | any later version. |
---|
11 | |
---|
12 | ### File Specific: |
---|
13 | main-programmer: Patrick Boenzli |
---|
14 | co-programmer: |
---|
15 | */ |
---|
16 | |
---|
17 | #include "spawning_point.h" |
---|
18 | |
---|
19 | #include "util/loading/load_param.h" |
---|
20 | #include "util/loading/factory.h" |
---|
21 | |
---|
22 | #include "world_entity.h" |
---|
23 | |
---|
24 | #include "class_list.h" |
---|
25 | |
---|
26 | #include "compiler.h" |
---|
27 | |
---|
28 | #include "state.h" |
---|
29 | #include "game_rules.h" |
---|
30 | |
---|
31 | #include "shared_network_data.h" |
---|
32 | |
---|
33 | CREATE_FACTORY( SpawningPoint, CL_SPAWNING_POINT ); |
---|
34 | |
---|
35 | /** |
---|
36 | * constructor |
---|
37 | */ |
---|
38 | SpawningPoint::SpawningPoint( const TiXmlElement * root ) |
---|
39 | { |
---|
40 | this->setAbsCoor( 0, 0, 0 ); |
---|
41 | |
---|
42 | this->init(); |
---|
43 | |
---|
44 | if (root != NULL) |
---|
45 | this->loadParams(root); |
---|
46 | } |
---|
47 | |
---|
48 | void SpawningPoint::init() |
---|
49 | { |
---|
50 | this->setClassID(CL_SPAWNING_POINT, "SpawningPoint"); |
---|
51 | PRINTF(0)("Created SpawningPoint\n"); |
---|
52 | |
---|
53 | this->teamId = -1; |
---|
54 | this->localTimer = 0.0f; |
---|
55 | |
---|
56 | this->toList( OM_DEAD_TICK ); |
---|
57 | |
---|
58 | MessageManager::getInstance()->registerMessageHandler( MSGID_RESPAWN, respawnMessageHandler, NULL ); |
---|
59 | |
---|
60 | this->setSynchronized( true ); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | /** |
---|
65 | * deconstructor |
---|
66 | */ |
---|
67 | SpawningPoint::~SpawningPoint () |
---|
68 | {} |
---|
69 | |
---|
70 | |
---|
71 | /** |
---|
72 | * loads the WorldEntity Specific Parameters. |
---|
73 | * @param root: the XML-Element to load the Data From |
---|
74 | */ |
---|
75 | void SpawningPoint::loadParams(const TiXmlElement* root) |
---|
76 | { |
---|
77 | /* let the world entity its stuff first */ |
---|
78 | WorldEntity::loadParams(root); |
---|
79 | |
---|
80 | /* load teamId */ |
---|
81 | LoadParam(root, "teamId", this, SpawningPoint, setTeamId) |
---|
82 | .describe("sets teamId"); |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | /** |
---|
88 | * pushes a world entity to the spawning queue |
---|
89 | * @param entity WorldEntity to be added |
---|
90 | */ |
---|
91 | void SpawningPoint::pushEntity(Playable* entity, float delay) |
---|
92 | { |
---|
93 | QueueEntry qe; |
---|
94 | qe.entity = entity; |
---|
95 | qe.respawnTime = this->localTimer + delay; |
---|
96 | |
---|
97 | queue.push_back( qe ); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | /** |
---|
102 | * spawn the entity |
---|
103 | */ |
---|
104 | void SpawningPoint::spawn(Playable* entity) |
---|
105 | { |
---|
106 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE ); |
---|
107 | |
---|
108 | bool found = false; |
---|
109 | |
---|
110 | if ( !list ) |
---|
111 | return; |
---|
112 | |
---|
113 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
114 | { |
---|
115 | if ( *it == entity ) |
---|
116 | { |
---|
117 | found = true; |
---|
118 | break; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | if ( !found ) |
---|
123 | return; |
---|
124 | |
---|
125 | PRINTF(0)("Spawningpoint spawns Entity (%s)\n", entity->getClassName()); |
---|
126 | |
---|
127 | |
---|
128 | entity->setAbsCoor( this->getAbsCoor() ); |
---|
129 | entity->setAbsDir( this->getAbsDir() ); |
---|
130 | |
---|
131 | //TODO set camera (not smooth) |
---|
132 | |
---|
133 | if ( State::getGameRules() ) |
---|
134 | { |
---|
135 | (State::getGameRules())->registerSpawn( entity ); |
---|
136 | } |
---|
137 | |
---|
138 | entity->respawn(); |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | /** |
---|
143 | * this method is called every frame |
---|
144 | * @param time: the time in seconds that has passed since the last tick |
---|
145 | * |
---|
146 | * Handle all stuff that should update with time inside this method (movement, animation, etc.) |
---|
147 | */ |
---|
148 | void SpawningPoint::tick(float dt) |
---|
149 | { |
---|
150 | this->localTimer += dt; |
---|
151 | std::list<QueueEntry>::iterator it = this->queue.begin(); |
---|
152 | for( ; it != this->queue.end(); ) |
---|
153 | { |
---|
154 | //PRINTF(0)("%f <= %f\n", it->respawnTime, this->localTimer); |
---|
155 | if( it->respawnTime <= this->localTimer) |
---|
156 | { |
---|
157 | //spawn the player |
---|
158 | this->spawn(it->entity); |
---|
159 | |
---|
160 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE ); |
---|
161 | |
---|
162 | bool found = false; |
---|
163 | |
---|
164 | if ( !list ) |
---|
165 | return; |
---|
166 | |
---|
167 | for ( std::list<BaseObject*>::const_iterator it2 = list->begin(); it2 != list->end(); it2++ ) |
---|
168 | { |
---|
169 | if ( *it2 == it->entity ) |
---|
170 | { |
---|
171 | found = true; |
---|
172 | break; |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | if ( found && SharedNetworkData::getInstance()->isGameServer() ) |
---|
177 | this->sendRespawnMessage( it->entity->getUniqueID() ); |
---|
178 | |
---|
179 | std::list<QueueEntry>::iterator delit = it; |
---|
180 | it++; |
---|
181 | |
---|
182 | queue.erase( delit ); |
---|
183 | |
---|
184 | continue; |
---|
185 | } |
---|
186 | |
---|
187 | it++; |
---|
188 | } |
---|
189 | |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | /** |
---|
194 | * the entity is drawn onto the screen with this function |
---|
195 | * |
---|
196 | * This is a central function of an entity: call it to let the entity painted to the screen. |
---|
197 | * Just override this function with whatever you want to be drawn. |
---|
198 | */ |
---|
199 | void SpawningPoint::draw() |
---|
200 | { |
---|
201 | } |
---|
202 | |
---|
203 | void SpawningPoint::sendRespawnMessage( int uniqueId ) |
---|
204 | { |
---|
205 | byte * buf = new byte[2*INTSIZE]; |
---|
206 | |
---|
207 | assert( Converter::intToByteArray( this->getUniqueID(), buf, INTSIZE ) == INTSIZE ); |
---|
208 | assert( Converter::intToByteArray( uniqueId, buf + INTSIZE, INTSIZE ) == INTSIZE ); |
---|
209 | |
---|
210 | MessageManager::getInstance()->sendMessage( MSGID_RESPAWN, buf, 2*INTSIZE, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH ); |
---|
211 | } |
---|
212 | |
---|
213 | bool SpawningPoint::respawnMessageHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) |
---|
214 | { |
---|
215 | if ( SharedNetworkData::getInstance()->isGameServer() ) |
---|
216 | { |
---|
217 | PRINTF(2)("server received spawn message!\n"); |
---|
218 | return true; |
---|
219 | } |
---|
220 | |
---|
221 | int spUniqueId; |
---|
222 | int uniqueId; |
---|
223 | |
---|
224 | if ( dataLength != 2*INTSIZE ) |
---|
225 | { |
---|
226 | PRINTF(2)("spawn message has wrong size: %d\n", dataLength ); |
---|
227 | return true; |
---|
228 | } |
---|
229 | |
---|
230 | assert( Converter::byteArrayToInt( data, &spUniqueId ) == INTSIZE ); |
---|
231 | assert( Converter::byteArrayToInt( data+INTSIZE, &uniqueId ) == INTSIZE ); |
---|
232 | |
---|
233 | PRINTF(0)("SPAWNMESSAGE %d\n", uniqueId); |
---|
234 | |
---|
235 | SpawningPoint * sp = NULL; |
---|
236 | Playable * playable = NULL; |
---|
237 | |
---|
238 | const std::list<BaseObject*> * list = ClassList::getList( CL_SPAWNING_POINT ); |
---|
239 | |
---|
240 | if ( list ) |
---|
241 | { |
---|
242 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
243 | { |
---|
244 | PRINTF(0)("%d:%d\n", dynamic_cast<SpawningPoint*>(*it)->getUniqueID(), spUniqueId); |
---|
245 | if ( dynamic_cast<SpawningPoint*>(*it)->getUniqueID() == spUniqueId ) |
---|
246 | { |
---|
247 | sp = dynamic_cast<SpawningPoint*>(*it); |
---|
248 | break; |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | if ( !sp ) |
---|
254 | { |
---|
255 | PRINTF(0)("could not find spawning point\n"); |
---|
256 | return false; |
---|
257 | } |
---|
258 | |
---|
259 | list = ClassList::getList( CL_PLAYABLE ); |
---|
260 | |
---|
261 | if ( list ) |
---|
262 | { |
---|
263 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
264 | { |
---|
265 | if ( dynamic_cast<Playable*>(*it)->getUniqueID() == uniqueId ) |
---|
266 | { |
---|
267 | playable = dynamic_cast<Playable*>(*it); |
---|
268 | break; |
---|
269 | } |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | if ( !playable ) |
---|
274 | { |
---|
275 | PRINTF(0)("could not find playable\n"); |
---|
276 | return false; |
---|
277 | } |
---|
278 | |
---|
279 | sp->spawn( playable ); |
---|
280 | |
---|
281 | return true; |
---|
282 | } |
---|
283 | |
---|