private void ApplyMoveToAgentIfValid(SimplifiedAgent agent, SimplifiedAgent opponent, Vector3 nextPos, bool headCollision) { if (IsCrash(nextPos) || headCollision) { if (agent.powerTurns < 1) { Hurt(agent); } else if (opponent.bodyPositions.Contains(nextPos) && agent.powerTurns > 0) //checks if attacking opponent { Hurt(opponent); } } else { //move is valid if (foods.Contains(nextPos)) { agent.length++; foods.Remove(nextPos); } if (powerups.Contains(nextPos)) { agent.powerTurns += 15; // GAME CONSTANT WAS LAZY powerups.Remove(nextPos); } agent.bodyPositions.Add(agent.headPosition); agent.headPosition = nextPos; agent.bodyPositions.Add(nextPos); } }
public SimplifiedAgent(SimplifiedAgent copy) { bodyPositions = new List <Vector3>(); foreach (Vector3 position in copy.bodyPositions) { bodyPositions.Add(position); } headPosition = copy.headPosition; length = copy.length; powerTurns = copy.powerTurns; }
public GameState(HashSet <Vector3> walls, HashSet <Vector3> powerups, HashSet <Vector3> foods, SimplifiedAgent agent1, SimplifiedAgent agent2) { this.walls = new HashSet <Vector3>(walls); this.powerups = new HashSet <Vector3>(powerups); this.foods = new HashSet <Vector3>(foods); this.player1 = new SimplifiedAgent(agent1);; this.player2 = new SimplifiedAgent(agent2);; }
private void Hurt(SimplifiedAgent agent) { if (agent.powerTurns < 1) { if (agent.length <= 1) { // simulated death agent.length = 0; } if (agent.length <= 3) { agent.length = 1; } agent.length = agent.length - agent.length / 3; } }
//should only take in the SimplifiedAgents that are fields of this GameState public SimplifiedAgent GetOpponent(SimplifiedAgent agent) { if (agent == this.player1) { return(player2); } else if (agent == this.player2) { return(player1); } else { Debug.LogError("Invalid input into GetOpponent()"); return(null); } }
// find the closest target to a player private Vector3 FindTarget(GameState state, SimplifiedAgent player) { Vector3 target = new Vector3(0, 0, 0); Vector3 head = player.headPosition; // food and powerups are goals HashSet <Vector3> goals = new HashSet <Vector3>(state.foods); goals.UnionWith(state.powerups); // if currently powered up, so is the other player's body if (player.powerTurns > 1) { goals.UnionWith(state.GetOpponent(player).bodyPositions); } foreach (Vector3 goal in goals) { if (target == Vector3.zero || MDist(target, head) > MDist(goal, head)) { target = goal; } } return(target); }
private float DistToTarget(GameState state, SimplifiedAgent player) { Vector3 target = FindTarget(state, player); return(MDist(target, player.headPosition)); }