// Returns an instance of Player public Player createPlayer(Vector2 pos) { Player p = new Player(textures[MAINPLAYER], textures[MAINPLAYER]); p.Position = pos; return p; }
public HUD createHUD(Player player) { return new HUD(textures[INFECT_RANGE], textures[HEALTH_BAR]); }
/* * Handle collision between player and item */ private bool CheckItemCollision(Player player, Item item) { if (player.distance(item) < (player.Size + Item.ITEM_SIZE)/2) { player.PickupItem(item); return true; } return false; }
/* * Calculates and processes all collisions that occur, * using the collision cell optimization structure */ public void Update(List<GameUnit> units, Player player, Level level, ItemController item_controller) { ConstructCollisionGrids(units, item_controller.Items, player, level); foreach (GameUnit unit in units) { ProcessCollisions(unit, level.Map); } if (player != null) { ProcessCollisions(player, level.Map); ProcessItems(player, item_controller); } }
/* * Process item pickups */ public void ProcessItems(Player player, ItemController item_controller) { int x_indexp = (int)MathHelper.Clamp((player.Position.X / CELL_SIZE), 0, itemGrid.GetLength(1) - 1); int y_indexp = (int)MathHelper.Clamp((player.Position.Y / CELL_SIZE), 0, itemGrid.GetLength(0) - 1); List<Point> adjacent = getAdjacent(new Point(x_indexp, y_indexp)); foreach (Point loc in adjacent) { if (itemGrid[loc.Y, loc.X] != null) { foreach (Item it in itemGrid[loc.Y, loc.X]) { if (CheckItemCollision(player, it)) { item_controller.RemoveItem(it); } } } } }
/* * Populates the collision grid by bucketizing each unit on the map into a cell * around which collisions will be processed */ public void ConstructCollisionGrids(List<GameUnit> units, List<Item> items, Player player, Level level) { // Construct unit collision grid List<GameUnit>[,] grid = new List<GameUnit>[ (int)level.Width / CELL_SIZE, (int)level.Height / CELL_SIZE]; foreach (GameUnit unit in units) { int x_index = (int)MathHelper.Clamp((unit.Position.X / CELL_SIZE), 0, grid.GetLength(1)-1); int y_index = (int)MathHelper.Clamp((unit.Position.Y / CELL_SIZE), 0, grid.GetLength(0)-1); if (grid[y_index, x_index] == null) { grid[y_index, x_index] = new List<GameUnit>(); } grid[y_index, x_index].Add(unit); } if (player != null) { int x_indexp = (int)MathHelper.Clamp((player.Position.X / CELL_SIZE), 0, grid.GetLength(1) - 1); int y_indexp = (int)MathHelper.Clamp((player.Position.Y / CELL_SIZE), 0, grid.GetLength(0) - 1); if (grid[y_indexp, x_indexp] == null) { grid[y_indexp, x_indexp] = new List<GameUnit>(); } grid[y_indexp, x_indexp].Add(player); cellGrid = grid; } // Contruct item collision grid itemGrid = new List<Item>[(int)level.Width / CELL_SIZE, (int)level.Height / CELL_SIZE]; foreach (Item item in items) { int x_index = (int)MathHelper.Clamp((item.Position.X / CELL_SIZE), 0, grid.GetLength(1) - 1); int y_index = (int)MathHelper.Clamp((item.Position.Y / CELL_SIZE), 0, grid.GetLength(0) - 1); if (itemGrid[y_index, x_index] == null) { itemGrid[y_index, x_index] = new List<Item>(); } itemGrid[y_index, x_index].Add(item); } }