Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/spawning_point.cc @ 9705

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

adapted many more classes

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