[游戏]服务器和客户端之间的同步策略[steeven]

  Client                            
  Player{ //Game.Me
1 void CommandA{
  cmd = new Command(methodInfo,args[]);
  server.channel.Send(cmd)
  }
  }
  Unit{
  event LocationChanged;
  ID; //该单元的在游戏中的唯一标志
  OwnerID; //该单元属于谁
  _Location;
  Location{
8 set{
  _Location = value;
  if (LocationChanged != null)
  LocationChanged(this,EventArgs.Empty);
 
 
  }
                                  Server 
  Channel{ //server端,每个channel属于某个用户
  int ID; //Player id
2 void OnCommand{
  server.CommandQueue.Add(ID,cmd);
  }
  }
  Server{
  void Main{
  while(true)
3 if (CommandQueue.count>0)
  processCommand();
  }
4 ProcessCommand(){
  Player p = FindPlayer(CommnadQueue.Peek().ID);
  cmd.method.invoke(p,p.Peek().args)//如果不用reflection,需要一个大的switch
  }
7 BroadcastCommand(players,cmd){
  foreach(p in players)
  p.channel.send(cmd);
  }
  }
  Player{ //注意这里的Player是服务器端
5 void CommandA{
  //check condition
  unit.Location = xxx; '//perform command
  //也许同步策略加到这里
  }
  Unit{
  event LocationChanged;
  ID; //该单元的在游戏中的唯一标志
  OwnerID; //该单元属于谁
  _Location;
  Location{
6 set{
  if (value != _Location){ //发生变化
  IList players = … //同步策略
  Server.Broadcast(players,new command(ID,methodInfo,value));
  }
  _Location = value;
  if (LocationChanged != null) //也可能通过这里注册Location同步策略分析器
  LocationChanged(this,EventArgs.Empty);
 
 
  }
  注释:                                                    
我更希望通过AOP把某些方法自动重定向或者同步到Server/Client端执行,编程上可以简化很多

你可能感兴趣的:([游戏]服务器和客户端之间的同步策略[steeven])