private void AssembleMap() { Map = new List <List <Piece> >(); for (int y = 0; y < Height; y++) { Map.Add(new List <Piece>()); for (int x = 0; x < Width; x++) { if (Walls.Any(p => p.X == x && p.Y == y)) { Map[y].Add(new Piece(x, y, PieceType.Wall, Color.Blue)); } else if (Snakes.Any(s => s.snakePieces.Any(p => p.X == x && p.Y == y && p.Type == PieceType.SnakePiece))) { Map[y].Add(new Piece(x, y, PieceType.SnakePiece, Snakes.Where(s => s.IsAtPos(x, y)).FirstOrDefault().snakePieces.Where(p => p.X == x && p.Y == y).FirstOrDefault().Color)); } else if (Snakes.Any(s => s.snakePieces.Any(p => p.X == x && p.Y == y && p.Type == PieceType.HeadPiece))) { Map[y].Add(new Piece(x, y, PieceType.HeadPiece, Snakes.Where(s => s.IsAtPos(x, y)).FirstOrDefault().snakePieces.Where(p => p.X == x && p.Y == y).FirstOrDefault().Color)); } else if (FoodPieces.Any(p => p.X == x && p.Y == y)) { Piece piece = FoodPieces.Where(p => p.X == x && p.Y == y).FirstOrDefault(); Map[y].Add(piece); } else { Map[y].Add(new Piece(x, y, PieceType.Empty)); } } } }
/// <summary> /// Send everyone the command for make a step (authoritarian) /// </summary> void MakeStepGlobal() { var directions = Snakes.Select(s => s.GetCurrentInputDirection()).ToArray(); RaiseEventOptions raiseEventOptions = new RaiseEventOptions { Receivers = ReceiverGroup.All }; SendOptions sendOptions = new SendOptions { Reliability = true }; PhotonNetwork.RaiseEvent(RemoteEventNames.RegularStep, directions, raiseEventOptions, sendOptions); //Try respawn for deactivate snakes foreach (var snake in Snakes.Where(s => !s.Disconnect)) { if (snake.GetCurrentLenght() == 0) { snake.CreateRespawnEvent(); } } }
public void OnTick() { AssembleMap(); if (Running) { foreach (ComputerSnake snake in Snakes.Where(s => s.GetType() == typeof(ComputerSnake))) { snake.Map = Map; snake.Move(); } // HACK foreach (Snake snake in Snakes.Where(s => s.GetType() != typeof(ComputerSnake))) { snake.Move(); } } CheckCollisions(); }
public Map(int width, int height, int worldTick, SnakePlayer mySnake, IEnumerable <SnakePlayer> snakeInfos, IEnumerable <MapCoordinate> foodPositions, IEnumerable <MapCoordinate> obstaclePositions) { Tick = worldTick; MySnake = mySnake; FoodPositions = new HashSet <MapCoordinate>(foodPositions); ObstaclePositions = new HashSet <MapCoordinate>(obstaclePositions); Snakes = snakeInfos.ToList(); OpponentSnakes = Snakes.Where(s => s.Id != MySnake.Id).ToList(); TileGrid = new TileGridBuilder(width, height) .WithFood(FoodPositions) .WithObstacles(ObstaclePositions) .WithMySnake(MySnake) .WithOpponentSnakes(OpponentSnakes) .WithOpponentHeadNeighbors(OpponentSnakes) .Build(); OpponentsTailsPositions = OpponentSnakes.Where(s => s.IsAlive) .ToDictionary(s => s.TailPosition, s => s); Width = width; Height = height; }