Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/spawning_point.cc @ 9717

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

orxonox/trunk: merged the proxy bache back with no conflicts

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