Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/player_stats.cc @ 8082

Last change on this file since 8082 was 8068, checked in by patrick, 18 years ago

trunk: merged the network branche back to trunk

File size: 3.6 KB
RevLine 
[7974]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Christoph Renner
13   co-programmer: ...
14*/
15
16#include "player_stats.h"
17
18#include "class_list.h"
19#include "src/lib/util/loading/factory.h"
20
[7984]21#include "player.h"
22#include "state.h"
[7974]23
[7984]24
[7974]25CREATE_FACTORY(PlayerStats, CL_PLAYER_STATS);
26
27/**
28 * constructor
29 */
30PlayerStats::PlayerStats( int userId )
31{
32  init();
33 
34  this->userId = userId;
35}
36
37/**
38 * constructor
39 */
40PlayerStats::PlayerStats( const TiXmlElement* root )
41{
42  init();
43}
44
45void PlayerStats::init( )
46{
47  this->setClassID( CL_PLAYER_STATS, "PlayerStats" );
48
49  this->userId = 0;
[8067]50  this->teamId = TEAM_NOTEAM;
51  this->preferedTeamId = TEAM_NOTEAM;
[7974]52  this->score = 0;
53  this->playableClassId = 0;
54  this->modelFileName = "";
55 
56  userId_handle = registerVarId( new SynchronizeableInt( &userId, &userId, "userId" ) );
[8067]57  teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId" ) );
58  preferedTeamId_handle = registerVarId( new SynchronizeableInt( &preferedTeamId, &preferedTeamId, "preferedUserId" ) );
[7974]59  score_handle = registerVarId( new SynchronizeableInt( &score, &score, "score" ) );
60  playableClassId_handle = registerVarId( new SynchronizeableInt( &playableClassId, &playableClassId, "playableClassId") );
61  playableUniqueId_handle = registerVarId( new SynchronizeableInt( &playableUniqueId, &playableUniqueId, "playableUniqueId" ) );
62  modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) );
[8014]63 
64  PRINTF(0)("PlayerStats created\n");
[7974]65}
66
67
68/**
69 * standard deconstructor
70 */
71PlayerStats::~PlayerStats()
72{
73}
74
75
76 /**
77 * override this function to be notified on change
78 * of your registred variables.
79 * @param id id's which have changed
80  */
81void PlayerStats::varChangeHandler( std::list< int > & id )
82{
83  if ( std::find( id.begin(), id.end(), playableUniqueId_handle ) != id.end() )
84  {
85    this->setPlayableUniqueId( this->playableUniqueId );
[7984]86   
[8014]87    PRINTF(0)("uniqueID changed %d %d\n", userId, getHostID());
88   
[7984]89    if ( userId == getHostID() )
90      State::getPlayer()->setPlayable( getPlayable() );
[7974]91  }
92}
93
94/**
95 * get stats for user userId
96 * @param userId user's id
97 * @return stats assigned to user
98 */
99PlayerStats * PlayerStats::getStats( int userId )
100{
101  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
102 
103  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
104  {
105    if ( dynamic_cast<PlayerStats*>(*it)->getUserId() == userId )
106    {
107      return dynamic_cast<PlayerStats*>(*it);
108    }
109  }
110 
111  //TODO maybe we should create an object here
112 
113  return NULL;
114}
115
116/**
117 * set playable class id and set playable
118 */
119void PlayerStats::setPlayableUniqueId( int uniqueId )
120{
121  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
122 
123  this->playable = NULL;
124  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
125  {
126    if ( dynamic_cast<Playable*>(*it)->getUniqueID() == uniqueId )
127    {
128      this->playable = dynamic_cast<Playable*>(*it);
129      break;
130    }
131  }
132 
133  this->playableUniqueId = uniqueId;
134}
135
136/**
137 * get playable associated to player
138 * @return playable associated to player
139 */
140Playable * PlayerStats::getPlayable()
141{
142  if ( playable )
143    return playable;
144 
145  assert( playableUniqueId > 0 );
146 
147  setPlayableUniqueId( playableUniqueId );
148 
149  assert( playable );
150 
151  return playable;
152}
153
154
Note: See TracBrowser for help on using the repository browser.