private void HandleCommand(GameCommand command) { TankController t = FindTankObject(command.Token); if (TrainingRoomMain.currentGameState == TrainingRoomMain.GameState.gameOver) { return; } if (TrainingRoomMain.currentGameState == TrainingRoomMain.GameState.notStarted) { if (command.Type != CommandType.PlayerCreate) { return; } } MovementParameter p = null; switch (command.Type) { case (CommandType.PlayerCreate): PlayerCreate create = command.Payload as PlayerCreate; var tank = CreatePlayer(create); break; case (CommandType.Despawn): if (t != null) { RemoveTank(t); } break; case (CommandType.PlayerCreateTest): PlayerCreateTest createtest = command.Payload as PlayerCreateTest; var tanktest = CreatePlayerTest(createtest); break; case (CommandType.TurnToHeading): p = command.Payload as MovementParameter; if (t != null) { t.TurnToHeading(p.Amount); } break; case (CommandType.TurnTurretToHeading): p = command.Payload as MovementParameter; if (t != null) { t.TurnTurretToHeading(p.Amount); } break; case (CommandType.MoveForwardDistance): p = command.Payload as MovementParameter; if (t != null) { t.MoveDistance(p.Amount); } break; case (CommandType.MoveBackDistance): p = command.Payload as MovementParameter; if (t != null) { t.MoveDistance(-p.Amount); } break; case (CommandType.ToggleForward): if (t != null) { t.ToggleForward(); } break; case (CommandType.ToggleReverse): if (t != null) { t.ToggleReverse(); } break; case (CommandType.ToggleRight): if (t != null) { t.ToggleRight(); } break; case (CommandType.ToggleLeft): if (t != null) { t.ToggleLeft(); } break; case (CommandType.ToggleTurretLeft): if (t != null) { t.ToggleTurretLeft(); } break; case (CommandType.ToggleTurretRight): if (t != null) { t.ToggleTurretRight(); } break; case (CommandType.FullStop): if (t != null) { t.FullStop(); } break; case (CommandType.StopTurret): if (t != null) { t.StopTurret(); } break; case (CommandType.StopMove): if (t != null) { t.StopMove(); } break; case (CommandType.StopTurn): if (t != null) { t.StopTurn(); } break; case (CommandType.Fire): if (t != null) { t.Fire(); } break; } }
private void HandleMessage(NetworkMessage newMessage) { try { //make the id of the tank the IP address of the client string clientId = GetTokenFromEndpoint(newMessage.sender); switch (newMessage.type) { case (NetworkMessageType.test): byte[] testReturn = new byte[2]; testReturn[0] = 0; testReturn[1] = 0; SendMessage(newMessage.sender, testReturn); break; case (NetworkMessageType.createTank): CreatePlayer messageData = JsonUtility.FromJson <CreatePlayer>((string)newMessage.data); sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.PlayerCreate, Payload = new PlayerCreate() { Name = messageData.Name, Token = clientId, Color = "" }, Token = clientId }); break; case (NetworkMessageType.despawnTank): sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.Despawn, Token = clientId, Payload = null }); break; case (NetworkMessageType.fire): sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.Fire, Token = clientId, Payload = null }); break; case (NetworkMessageType.stopTurret): sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.StopTurret, Token = clientId, Payload = null }); break; case (NetworkMessageType.stopMove): sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.StopMove, Token = clientId, Payload = null }); break; case (NetworkMessageType.stopTurn): sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.StopTurn, Token = clientId, Payload = null }); break; case (NetworkMessageType.stopAll): sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.FullStop, Token = clientId, Payload = null }); break; case (NetworkMessageType.toggleTurretLeft): sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.ToggleTurretLeft, Token = clientId, Payload = null }); break; case (NetworkMessageType.toggleTurretRight): sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.ToggleTurretRight, Token = clientId, Payload = null }); break; case (NetworkMessageType.toggleLeft): sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.ToggleLeft, Token = clientId, Payload = null }); break; case (NetworkMessageType.toggleRight): sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.ToggleRight, Token = clientId, Payload = null }); break; case (NetworkMessageType.toggleReverse): sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.ToggleReverse, Token = clientId, Payload = null }); break; case (NetworkMessageType.toggleForward): sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.ToggleForward, Token = clientId, Payload = null }); break; case (NetworkMessageType.turnToHeading): MovementParameter movement = JsonUtility.FromJson <MovementParameter>((string)newMessage.data); sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.TurnToHeading, Token = clientId, Payload = movement }); break; case (NetworkMessageType.turnTurretToHeading): MovementParameter movement2 = JsonUtility.FromJson <MovementParameter>((string)newMessage.data); sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.TurnTurretToHeading, Token = clientId, Payload = movement2 }); break; case (NetworkMessageType.moveForwardDistance): MovementParameter movement3 = JsonUtility.FromJson <MovementParameter>((string)newMessage.data); sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.MoveForwardDistance, Token = clientId, Payload = movement3 }); break; case (NetworkMessageType.moveBackwardsDistance): MovementParameter movement4 = JsonUtility.FromJson <MovementParameter>((string)newMessage.data); sim.enqueuedCommands.Enqueue(new GameCommand() { Type = CommandType.MoveBackDistance, Token = clientId, Payload = movement4 }); break; } } catch (Exception ex) { } }