Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

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

File size: 6.3 KB
RevLine 
[6080]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"
[6424]18
[7193]19#include "util/loading/load_param.h"
20#include "util/loading/factory.h"
[6424]21
[8068]22#include "world_entity.h"
23
[9008]24#include "class_list.h"
25
[6093]26#include "compiler.h"
[6080]27
[8802]28#include "state.h"
29#include "game_rules.h"
[6424]30
[9008]31#include "shared_network_data.h"
[8068]32
[9406]33
34/// TODO REMOVE converter.h
35#include "converter.h"
36
[9008]37CREATE_FACTORY( SpawningPoint, CL_SPAWNING_POINT );
38
[6080]39/**
[7357]40 *  constructor
[6080]41 */
[9008]42SpawningPoint::SpawningPoint( const TiXmlElement * root )
[6084]43{
[9008]44  this->setAbsCoor( 0, 0, 0 );
[6088]45
46  this->init();
[9406]47
[9008]48  if (root != NULL)
49    this->loadParams(root);
[6084]50}
[6080]51
[6088]52void SpawningPoint::init()
53{
[7357]54  this->setClassID(CL_SPAWNING_POINT, "SpawningPoint");
[9008]55  PRINTF(0)("Created SpawningPoint\n");
[9406]56
[8802]57  this->teamId = -1;
[9008]58  this->localTimer = 0.0f;
[9406]59
[9008]60  this->toList( OM_DEAD_TICK );
[9406]61
[9008]62  MessageManager::getInstance()->registerMessageHandler( MSGID_RESPAWN, respawnMessageHandler, NULL );
[9406]63
[9235]64  this->setSynchronized( true );
[6081]65}
66
67
68/**
[6080]69 *  deconstructor
70 */
71SpawningPoint::~SpawningPoint ()
72{}
73
74
[6081]75/**
[6086]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{
[6087]81  /* let the world entity its stuff first */
[6512]82  WorldEntity::loadParams(root);
[6086]83
[8802]84  /* load teamId */
85  LoadParam(root, "teamId", this, SpawningPoint, setTeamId)
86      .describe("sets teamId");
[6086]87}
88
89
[8068]90
[6086]91/**
[8068]92 * pushes a world entity to the spawning queue
93 *  @param entity WorldEntity to be added
94 */
[9008]95void SpawningPoint::pushEntity(Playable* entity, float delay)
[8068]96{
[8802]97  QueueEntry qe;
98  qe.entity = entity;
99  qe.respawnTime = this->localTimer + delay;
[9406]100
[8802]101  queue.push_back( qe );
[8068]102}
103
104
105/**
[6081]106 *  spawn the entity
107 */
[9008]108void SpawningPoint::spawn(Playable* entity)
[6083]109{
[9235]110  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
[9406]111
[9235]112  bool found = false;
[9406]113
[9235]114  if ( !list )
115    return;
[9406]116
[9235]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  }
[9406]125
[9235]126  if ( !found )
127    return;
128
[9406]129  PRINTF(0)("Spawningpoint spawns Entity (%s)\n", entity->getClassCName());
[6424]130
[8068]131
[8802]132  entity->setAbsCoor( this->getAbsCoor() );
133  entity->setAbsDir( this->getAbsDir() );
[9406]134
[8802]135  //TODO set camera (not smooth)
[9406]136
[9008]137  if ( State::getGameRules() )
138  {
139    (State::getGameRules())->registerSpawn( entity );
140  }
[9406]141
[9008]142  entity->respawn();
[6083]143}
[6080]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)
[6084]153{
[7357]154  this->localTimer += dt;
[8802]155  std::list<QueueEntry>::iterator it = this->queue.begin();
156  for( ; it != this->queue.end(); )
[6084]157  {
[9008]158    //PRINTF(0)("%f <= %f\n", it->respawnTime, this->localTimer);
[8802]159    if( it->respawnTime <= this->localTimer)
[8068]160    {
161      //spawn the player
[8802]162      this->spawn(it->entity);
[9406]163
[9235]164      const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
[9406]165
[9235]166      bool found = false;
[9406]167
[9235]168      if ( !list )
169        return;
[9406]170
[9235]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      }
[9406]179
[9494]180      if ( found && SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/)
[9008]181        this->sendRespawnMessage( it->entity->getUniqueID() );
182
[8802]183      std::list<QueueEntry>::iterator delit = it;
184      it++;
[9406]185
[8802]186      queue.erase( delit );
[9406]187
[8802]188      continue;
[8068]189    }
[9406]190
[8802]191    it++;
[6084]192  }
[8068]193
[6084]194}
[6080]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 */
[9494]203void SpawningPoint::draw() const
[9008]204{
205}
206
207void SpawningPoint::sendRespawnMessage( int uniqueId )
208{
[9656]209  byte buf[2*INTSIZE];
[9406]210
[9008]211  assert( Converter::intToByteArray( this->getUniqueID(), buf, INTSIZE ) == INTSIZE );
212  assert( Converter::intToByteArray( uniqueId, buf + INTSIZE, INTSIZE ) == INTSIZE );
[9406]213
[9656]214  MessageManager::getInstance()->sendMessage( MSGID_RESPAWN, buf, 2*INTSIZE, RT_ALL_BUT_ME, NET_UNASSIGNED, MP_HIGHBANDWIDTH );
[9008]215}
216
[9656]217/**
218 * message handler for respawn message
219 */
220bool SpawningPoint::respawnMessageHandler( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  )
[9008]221{
[9494]222  if ( SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/)
[9008]223  {
224    PRINTF(2)("server received spawn message!\n");
225    return true;
226  }
[9406]227
[9008]228  int spUniqueId;
229  int uniqueId;
[9406]230
[9008]231  if ( dataLength != 2*INTSIZE )
232  {
233    PRINTF(2)("spawn message has wrong size: %d\n", dataLength );
234    return true;
235  }
[9406]236
[9008]237  assert( Converter::byteArrayToInt( data, &spUniqueId ) == INTSIZE );
238  assert( Converter::byteArrayToInt( data+INTSIZE, &uniqueId ) == INTSIZE );
[9406]239
[9235]240  PRINTF(0)("SPAWNMESSAGE %d\n", uniqueId);
[9406]241
[9008]242  SpawningPoint * sp = NULL;
243  Playable      * playable = NULL;
[9406]244
[9008]245  const std::list<BaseObject*> * list = ClassList::getList( CL_SPAWNING_POINT );
[9406]246
[9008]247  if ( list )
248  {
249    for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
250    {
[9235]251      PRINTF(0)("%d:%d\n", dynamic_cast<SpawningPoint*>(*it)->getUniqueID(), spUniqueId);
252      if ( dynamic_cast<SpawningPoint*>(*it)->getUniqueID() == spUniqueId )
[9008]253      {
254        sp = dynamic_cast<SpawningPoint*>(*it);
255        break;
256      }
257    }
258  }
[9406]259
[9008]260  if ( !sp )
261  {
[9235]262    PRINTF(0)("could not find spawning point\n");
[9008]263    return false;
264  }
[9406]265
[9008]266  list = ClassList::getList( CL_PLAYABLE );
[9406]267
[9008]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  }
[9406]279
[9008]280  if ( !playable )
281  {
[9235]282    PRINTF(0)("could not find playable\n");
[9008]283    return false;
284  }
[9406]285
[9008]286  sp->spawn( playable );
[9406]287
[9008]288  return true;
289}
290
Note: See TracBrowser for help on using the repository browser.