static void HandleAgentInputs(ref GameParametersStruct gameParameters, ref GameState gs, ActionsTypes chosenPlayer1Actions, ActionsTypes chosenPlayer2Actions) { for (var i = 0; i < gs.players.Length; i++) { float rotationVelocity = 0.0f; Vector2 velocity = new Vector2(); Player oldPlayer = gs.players[i]; ActionsTypes tempChosenActionPlayer = 0; if (i == 0) { tempChosenActionPlayer = chosenPlayer1Actions; } else if (i == 1) { tempChosenActionPlayer = chosenPlayer2Actions; } //Debug.LogFormat("chosenPlayer1Actions : {0} / chosenPlayer2Actions : {1}", chosenPlayer1Actions, chosenPlayer2Actions); switch (tempChosenActionPlayer) { case ActionsTypes.Nothing: { DecelerateVelocity(ref gameParameters, ref gs, ref oldPlayer, i); DecelerateRotation(ref gameParameters, ref gs, ref oldPlayer, i); gs.players[i] = oldPlayer; break; } case ActionsTypes.RotateRightNS: { RotateRightAgent(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); DecelerateVelocity(ref gameParameters, ref gs, ref oldPlayer, i); gs.players[i] = oldPlayer; break; } case ActionsTypes.RotateRightS: { RotateRightAgent(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); DecelerateVelocity(ref gameParameters, ref gs, ref oldPlayer, i); Shoot(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); gs.players[i] = oldPlayer; break; } case ActionsTypes.RotateLeftNS: { RotateLeftAgent(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); DecelerateVelocity(ref gameParameters, ref gs, ref oldPlayer, i); gs.players[i] = oldPlayer; break; } case ActionsTypes.RotateLeftS: { RotateLeftAgent(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); DecelerateVelocity(ref gameParameters, ref gs, ref oldPlayer, i); Shoot(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); gs.players[i] = oldPlayer; break; } case ActionsTypes.MoveUpNS: { MoveUpAgent(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); DecelerateRotation(ref gameParameters, ref gs, ref oldPlayer, i); gs.players[i] = oldPlayer; break; } case ActionsTypes.MoveUpS: { MoveUpAgent(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); DecelerateRotation(ref gameParameters, ref gs, ref oldPlayer, i); Shoot(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); gs.players[i] = oldPlayer; break; } case ActionsTypes.MoveDownNS: { MoveDownAgent(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); DecelerateRotation(ref gameParameters, ref gs, ref oldPlayer, i); gs.players[i] = oldPlayer; break; } case ActionsTypes.MoveDownS: { MoveDownAgent(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); DecelerateRotation(ref gameParameters, ref gs, ref oldPlayer, i); Shoot(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); gs.players[i] = oldPlayer; break; } case ActionsTypes.NothingS: { DecelerateRotation(ref gameParameters, ref gs, ref oldPlayer, i); DecelerateVelocity(ref gameParameters, ref gs, ref oldPlayer, i); Shoot(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); gs.players[i] = oldPlayer; break; } case ActionsTypes.RotateRightUp: { RotateRightAgent(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); MoveUpAgent(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); gs.players[i] = oldPlayer; break; } case ActionsTypes.RotateLeftUp: { RotateLeftAgent(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); MoveUpAgent(ref gameParameters, ref gs, rotationVelocity, velocity, ref oldPlayer, i); gs.players[i] = oldPlayer; break; } } } for (int i = 0; i < 2; i++) { Player oldPlayer = gs.players[i]; Vector2 position = oldPlayer.position; Vector2 velocity = oldPlayer.velocity; if (AllowMovement(ref gameParameters, ref gs, position + gs.players[i].velocity)) { position += gs.players[i].velocity; } else { velocity = new Vector2(0, 0); } Vector2 lookDirection = Quaternion.Euler(0, 0, gs.players[i].rotationVelocity) * gs.players[i].lookDirection; velocity = Vector2.ClampMagnitude(velocity, gameParameters.MaxVelocity); float rotationVelocity = Mathf.Clamp(gs.players[i].rotationVelocity, -2, 2); gs.players[i] = createPlayer(oldPlayer.score, oldPlayer.speed, position, oldPlayer.lastShootStep, oldPlayer.isGameOver, velocity, rotationVelocity, lookDirection); } }
public static void Step(ref GameParametersStruct gameParameters, ref GameState gs, ActionsTypes actionPlayer1, ActionsTypes actionPlayer2) { UpdateAsteroids(ref gameParameters, ref gs); UpdateProjectiles(ref gs); HandleAgentInputs(ref gameParameters, ref gs, actionPlayer1, actionPlayer2); if (gs.players[0].isGameOver || gs.players[1].isGameOver) { return; } else { HandleCollisions(ref gameParameters, ref gs); } gs.currentGameStep += 1; if (gs.currentGameStep - gs.scoreStepDelay < 100) { return; } gs.scoreStepDelay = gs.currentGameStep; }