public void move(SnakeType snake, FoodType food, LevelType level) { PortalType portal; random = RandomGenerator.generator.Next(); deduceThreat(snake, food); deduceMove(); if (moveFood) { deduceOrientation(snake, food); if (level.tryGetPortal(food.Vector, out portal)) { if (orientation == portal.Entrance) { if (!portal.Link.IsFull) { portal.teleport(); portal.clearGrid(level); } } else { movement(snake, food, level); } } else { movement(snake, food, level); } } }
//Returns an orientation facing the snake, giving the food a predatory movement private void attackOrientation(SnakeType snake, FoodType food) { switch (snake.Orientation) { case OrientationType.up: case OrientationType.down: if (snake.Vector.X < food.Vector.X) { orientation = OrientationType.left; } else { orientation = OrientationType.right; } break; case OrientationType.left: case OrientationType.right: if (snake.Vector.Y < food.Vector.Y) { orientation = OrientationType.up; } else { orientation = OrientationType.down; } break; } }
//This region contains all in-game events #region Events void EatenFood(object sender, FoodArgs args) { SnakeType snake = sender as SnakeType; FoodType foodEaten = args.getFood(snake.Vector); snake.addSegment(foodEaten.Growth); foodEaten.spawn(level.Grid); SoundManager.playSound("Munch"); updateScore(foodEaten); }
private void deduceOrientation(SnakeType snake, FoodType food) { int number = random % upperBound; switch (threat) { case ThreatLevel.low: if (number == 0) { randomOrientation(); } else if (number == 1) { directionalOrientation(food); } else { attackOrientation(snake, food); } break; case ThreatLevel.medium: if (number == 0 || number == 1) { randomOrientation(); } else if (number == 2) { attackOrientation(snake, food); } else { smartOrientation(snake); } break; case ThreatLevel.high: if (number == 0 || number == 1) { attackOrientation(snake, food); } else { smartOrientation(snake); } break; } }
//Returns an orientation that allows the AI to get in front of the snake private void smartOrientation(SnakeType snake) { int number = random % 2; switch (snake.Orientation) { case OrientationType.up: case OrientationType.down: orientation = (OrientationType)(2 + number); break; case OrientationType.left: case OrientationType.right: orientation = (OrientationType)(number); break; } }
//Returns the same orientation as the snake, simulating the food running from the snake private void runOrientation(SnakeType snake, FoodType food) { bool temp = false; //Determine if food is in front of snake switch (snake.Orientation) { case OrientationType.up: if (snake.Vector.Y > food.Vector.Y) { temp = true; } break; case OrientationType.down: if (snake.Vector.Y < food.Vector.Y) { temp = true; } break; case OrientationType.left: if (snake.Vector.X > food.Vector.X) { temp = true; } break; case OrientationType.right: if (snake.Vector.X < food.Vector.X) { temp = true; } break; } //If the food is in front of snake, run, else random if (temp) { orientation = snake.Orientation; } else { randomOrientation(); } }
private void deduceThreat(SnakeType snake, FoodType food) { int yClose = (int)(snake.Vector.Y - food.Vector.Y); int xClose = (int)(snake.Vector.X - food.Vector.X); if ((Math.Abs(yClose) <= innerZone) || (Math.Abs(xClose) <= innerZone)) { threat = ThreatLevel.high; } else if ((Math.Abs(yClose) <= outerZone) || (Math.Abs(xClose) <= outerZone)) { threat = ThreatLevel.medium; } else if ((Math.Abs(yClose) > outerZone) || (Math.Abs(xClose) > outerZone)) { threat = ThreatLevel.low; } }
private void movement(SnakeType snake, FoodType food, LevelType level) { PortalType portal; //If the food is in a portal, clear the portal if (level.tryGetPortal(food.Vector, out portal)) { portal.clearGrid(level); } //Else empty the old grid cell else { level.Grid.GetCell(food.Vector).emptyCell(); } //Add the movement vector to the current mouse vector Vector2 newLocation = food.Vector + MovementFunctions.calculateLocation(orientation); if (level.tryGetPortal(newLocation, out portal)) { if (!portal.IsFull) { food.Vector = newLocation; portal.fillPortal(food); } } else if (level.Grid.GetCell(newLocation).content == CellContent.snake) { if (classification == FoodClassification.threat) { snake.GameOver(this, new EventArgs()); } } else if (level.Grid.GetCell(newLocation).content == CellContent.empty) { food.Vector = newLocation; } food.fillGrid(level); }
public void update(SnakeType snake, LevelType level) { if (counter == food.Count) { counter = 0; } food[counter].update(snake, level); if (food.Count > 7) { if (counter + 7 >= food.Count) { food[(counter + 7) - food.Count].update(snake, level); } else { food[counter + 7].update(snake, level); } } counter++; }
/// <summary> /// Updates the food item's AI agent, which moves the food. /// </summary> /// <param name="snake">The snake, which is controlled by the user.</param> /// <param name="level">The current level.</param> public void update(SnakeType snake, LevelType level) { ai.move(snake, this, level); }