Exemple #1
0
 public static void OnPositionChanged(PositionChangedCommand positionChangedCommand, ILocationControl locationControl, IClientContext clientContext)
 {
     var character = clientContext.Characters.Get(positionChangedCommand.CharacterId);
     if (character == null)
         return;
     character.Data.Position = positionChangedCommand.Position;
     locationControl.Update(character);
 }
        public static void Execute(this KeyPressedCommand command, IPEndPoint endPoint, IServerContext serverContext)
        {
            var character = GetCharacter(endPoint, command.Token, serverContext);
            if (character == null)
                return;
            var position = character.Data.Position;

            const float step = 2.2f;
            if (command.Keys.Contains(Key.Left))
                position.X -= step;
            if (command.Keys.Contains(Key.Right))
                position.X += step;
            if (command.Keys.Contains(Key.Up))
                position.Y -= step;
            if (command.Keys.Contains(Key.Down))
                position.Y += step;

            // Оповещаем клиентов о перемещении перса
            var positionChangedCommand = new PositionChangedCommand { CharacterId = character.Id, Position = position, Token = command.Token };
            SendToAll(() => positionChangedCommand, serverContext);
        }
Exemple #3
0
 public static CommandBase Parse(byte[] data)
 {
     using (var stream = new MemoryStream(data))
     using (var reader = new BinaryReader(stream))
     {
         CommandBase command;
         switch ((CommandType)reader.ReadUInt16())
         {
             case CommandType.JoinCharacter:
                 command = new JoinCharacterCommand();
                 break;
             case CommandType.CharacterJoined:
                 command = new CharacterJoinedCommand();
                 break;
             case CommandType.CharacterDetected:
                 command = new CharacterDetectedCommand();
                 break;
             case CommandType.KeyPressed:
                 command = new KeyPressedCommand();
                 break;
             case CommandType.PositionChanged:
                 command = new PositionChangedCommand();
                 break;
             default:
                 throw new NotImplementedException();
         }
         command.Token = reader.ReadUInt16();
         command.Deserialize(reader);
         return command;
     }
 }