My Project
GameNetCommon.h
1 #pragma once
2 #include "ValueTracker.h"
3 #include <map>
4 
6 #define CLIENT_NORMAL_UPDATE_RATE 3
7 
9 #define SERVER_NORMAL_UPDATE_RATE 3
10 
11 namespace ParaEngine
12 {
13  using namespace std;
17  class CNetTime
18  {
19  public:
20  CNetTime()
21  {
22  Reset();
23  }
24 
25  void Reset()
26  {
27  m_nTime = 0;
28  }
29 
30  inline int GetTime()
31  {
32  return m_nTime;
33  }
34 
38  void FrameMove()
39  {
40  ++m_nTime;
41  }
42  private:
43  int m_nTime;
44  };
45 
50  {
51  public:
52  CNormalUpdatePacket(void);
53  ~CNormalUpdatePacket(void);
54  };
55 
60  {
61  public:
62  CCharacterTracker(void){
63  m_nLastSendTime.resize(3, INVALID_TIME);
64  m_nLastReceiveTime.resize(3, INVALID_TIME);
65  m_vPos.SetSize(6);
66  m_arrayCustomModelData.SetSize(1);
67  };
68  ~CCharacterTracker(void){};
69 
70  public:
74  void Reset()
75  {
76  for(int i=0;i<(int)m_nLastSendTime.size();++i)
77  m_nLastSendTime[i] = INVALID_TIME;
78 
79  for(int i=0;i<(int)m_nLastReceiveTime.size();++i)
80  m_nLastReceiveTime[i] = INVALID_TIME;
81 
82  m_vPos.Reset();
83  m_fFacing.Reset();
84  m_fVerticalSpeed.Reset();
85  m_fSpeed.Reset();
86  m_nAnimID.Reset();
87  m_bWalking.Reset();
88  m_bRunning.Reset();
89  m_bIsGlobal.Reset();
90 
91  m_fSizeScale.Reset();
92  m_sBasemodel.Reset();
93  m_bIsCustomModel.Reset();
94  m_arrayCustomModelData.Reset();
95  m_nSkinIndex.Reset();
96  }
98  int GetLastSendTime(int nGroupIndex = 0)
99  {
100  if(nGroupIndex>=0 && nGroupIndex<(int)m_nLastSendTime.size())
101  return m_nLastSendTime[nGroupIndex];
102  else
103  return INVALID_TIME;
104  }
106  void SetLastSendTime(int nTime, int nGroupIndex = 0)
107  {
108  if(nGroupIndex>=0 && nGroupIndex<(int)m_nLastSendTime.size())
109  m_nLastSendTime[nGroupIndex] = nTime;
110  }
111 
113  int GetLastReceiveTime(int nGroupIndex = 0)
114  {
115  if(nGroupIndex>=0 && nGroupIndex<(int)m_nLastReceiveTime.size())
116  return m_nLastReceiveTime[nGroupIndex];
117  else
118  return INVALID_TIME;
119  }
120 
122  void SetLastReceiveTime(int nTime, int nGroupIndex = 0)
123  {
124  if(nGroupIndex>=0 && nGroupIndex<(int)m_nLastReceiveTime.size())
125  m_nLastReceiveTime[nGroupIndex] = nTime;
126  }
127 
131  bool IsContant()
132  {
133  return IsGroupConstant(1) && IsGroupConstant(2);
134  }
135 
139  bool IsGroupConstant(int nGroup)
140  {
141  switch(nGroup)
142  {
143  case 1:
144  return m_vPos.IsConstant() && m_fFacing.IsConstant() && m_fVerticalSpeed.IsConstant() && m_fSpeed.IsConstant() && m_nAnimID.IsConstant() &&
145  m_bWalking.IsConstant() && m_bRunning.IsConstant();
146  case 2:
147  return m_fSizeScale.IsConstant() && m_sBasemodel.IsConstant();
148  default:
149  return false;
150  }
151  }
152 
153  public:
155  // group 1: position, facing, speed and animation
156  CVector3Tracker m_vPos;
158  CFloatTracker m_fVerticalSpeed;
159  CFloatTracker m_fSpeed;
160  CIntTracker m_nAnimID;
161  CBooleanTracker m_bWalking;
162  CBooleanTracker m_bRunning;
163  CBooleanTracker m_bIsGlobal; // not in any group
164 
166  // group 2: appearance and attribute
167  CFloatTracker m_fSizeScale;
168  CStringTracker m_sBasemodel;
169  CBooleanTracker m_bIsCustomModel; // not in any group
170  CNetByteArrayTracker m_arrayCustomModelData; // not in any group
171  CIntTracker m_nSkinIndex; // not in any group
172  private:
174  vector<int> m_nLastSendTime;
175 
177  vector<int> m_nLastReceiveTime;
178  // invalid time
179  static const int INVALID_TIME = -1;
180  };
181 
182 
183 
189  {
190  public:
192  virtual ~ICharacterTrackerContainer();
193 
195  void ResetCharTrackers();
196 
202  CCharacterTracker* GetCharTracker(const char* sName=NULL);
203 
209  CCharacterTracker* CreateCharTracker(const char* sName=NULL);
210 
216  bool RemoveCharTracker(const char* sName);
224  int RemoveCharTracker(int nBeforePerceivedTime);
225 
226 
228  int GetLastTrackerGCTime() {return m_nLastTrackerGCTime;};
230  void SetLastTrackerGCTime(int nLag) {m_nLastTrackerGCTime = nLag;};
231 
233  int GetTimeLag() {return m_nTimeLag;};
235  void SetTimeLag(int nLag) {m_nTimeLag = nLag;};
236  private:
238  int m_nTimeLag;
240  int m_nLastTrackerGCTime;
241 
242  CCharacterTracker m_charTracker;
243  map<string, CCharacterTracker> m_perceptionTracker;
244  };
245 
246 }
247 
void FrameMove()
increase the time by 1 frame
Definition: GameNetCommon.h:38
CFloatTracker m_fFacing
character position tracker
Definition: GameNetCommon.h:157
It tracks a character on the network, such as its last update time, position track, etc.
Definition: GameNetCommon.h:59
Current network simulation time.
Definition: GameNetCommon.h:17
int GetLastReceiveTime(int nGroupIndex=0)
last time that the character info is received from the other side of the network. ...
Definition: GameNetCommon.h:113
different physics engine has different winding order.
Definition: EventBinding.h:32
int GetLastTrackerGCTime()
get last character tracker garbage collection time.
Definition: GameNetCommon.h:228
bool IsContant()
this function will return true if all field of the tracker are constant.
Definition: GameNetCommon.h:131
int GetTimeLag()
get the time since last network synchronization.
Definition: GameNetCommon.h:233
it is a class for holding various character trackers by their name.
Definition: GameNetCommon.h:188
OBSOLETED: It represents a normal update packet.
Definition: GameNetCommon.h:49
void SetLastSendTime(int nTime, int nGroupIndex=0)
last time that the character info is send to the other side of the network
Definition: GameNetCommon.h:106
bool IsGroupConstant(int nGroup)
this function will return true if all trackers in the given group are a constant. ...
Definition: GameNetCommon.h:139
void SetLastTrackerGCTime(int nLag)
set last character tracker garbage collection time.
Definition: GameNetCommon.h:230
void SetTimeLag(int nLag)
set the time since last network synchronization.
Definition: GameNetCommon.h:235
void Reset()
reset to tacker to empty
Definition: GameNetCommon.h:74
void SetLastReceiveTime(int nTime, int nGroupIndex=0)
last time that the character info is received from the other side of the network. ...
Definition: GameNetCommon.h:122
int GetLastSendTime(int nGroupIndex=0)
last time that the character info is send to the other side of the network
Definition: GameNetCommon.h:98