Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/player_stats.cc @ 7984

Last change on this file since 7984 was 7984, checked in by rennerc, 18 years ago

new NetworkGameManager

File size: 3.4 KB
Line 
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
21#include "player.h"
22#include "state.h"
23
24
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;
50  this->teamId = 0;
51  this->score = 0;
52  this->playableClassId = 0;
53  this->modelFileName = "";
54 
55  userId_handle = registerVarId( new SynchronizeableInt( &userId, &userId, "userId" ) );
56  groupId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId" ) );
57  score_handle = registerVarId( new SynchronizeableInt( &score, &score, "score" ) );
58  playableClassId_handle = registerVarId( new SynchronizeableInt( &playableClassId, &playableClassId, "playableClassId") );
59  playableUniqueId_handle = registerVarId( new SynchronizeableInt( &playableUniqueId, &playableUniqueId, "playableUniqueId" ) );
60  modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) );
61}
62
63
64/**
65 * standard deconstructor
66 */
67PlayerStats::~PlayerStats()
68{
69}
70
71
72 /**
73 * override this function to be notified on change
74 * of your registred variables.
75 * @param id id's which have changed
76  */
77void PlayerStats::varChangeHandler( std::list< int > & id )
78{
79  if ( std::find( id.begin(), id.end(), playableUniqueId_handle ) != id.end() )
80  {
81    this->setPlayableUniqueId( this->playableUniqueId );
82   
83    if ( userId == getHostID() )
84      State::getPlayer()->setPlayable( getPlayable() );
85  }
86}
87
88/**
89 * get stats for user userId
90 * @param userId user's id
91 * @return stats assigned to user
92 */
93PlayerStats * PlayerStats::getStats( int userId )
94{
95  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
96 
97  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
98  {
99    if ( dynamic_cast<PlayerStats*>(*it)->getUserId() == userId )
100    {
101      return dynamic_cast<PlayerStats*>(*it);
102    }
103  }
104 
105  //TODO maybe we should create an object here
106 
107  return NULL;
108}
109
110/**
111 * set playable class id and set playable
112 */
113void PlayerStats::setPlayableUniqueId( int uniqueId )
114{
115  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
116 
117  this->playable = NULL;
118  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
119  {
120    if ( dynamic_cast<Playable*>(*it)->getUniqueID() == uniqueId )
121    {
122      this->playable = dynamic_cast<Playable*>(*it);
123      break;
124    }
125  }
126 
127  this->playableUniqueId = uniqueId;
128}
129
130/**
131 * get playable associated to player
132 * @return playable associated to player
133 */
134Playable * PlayerStats::getPlayable()
135{
136  if ( playable )
137    return playable;
138 
139  assert( playableUniqueId > 0 );
140 
141  setPlayableUniqueId( playableUniqueId );
142 
143  assert( playable );
144 
145  return playable;
146}
147
148
Note: See TracBrowser for help on using the repository browser.