Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10435 was 10114, checked in by patrick, 18 years ago

merged network back to trunk

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