/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // IO events start Random random = new Random(); KeyboardState lastKeyboardState = KeyboardState; KeyboardState = Keyboard.GetState(); MouseState lastMouseState = MouseState; MouseState = Mouse.GetState(); MousePos.X = MouseState.X; MousePos.Y = MouseState.Y; CursorMapPos = coordinateConverter.ScreenToMap(MousePos, DisplayShift); if (KeyboardState.IsKeyDown(Keys.Right)) { DisplayShift.X -= 5; } if (KeyboardState.IsKeyDown(Keys.Left)) { DisplayShift.X += 5; } if (KeyboardState.IsKeyDown(Keys.Up)) { DisplayShift.Y += 5; } if (KeyboardState.IsKeyDown(Keys.Down)) { DisplayShift.Y -= 5; } if (KeyboardState.IsKeyDown(Keys.Escape)) { Exit(); } if (KeyboardState.IsKeyUp(Keys.P) && lastKeyboardState.IsKeyDown(Keys.P)) { DisplayPaths = !DisplayPaths; } if (KeyboardState.IsKeyUp(Keys.S) && lastKeyboardState.IsKeyDown(Keys.S)) { DisplayScore = !DisplayScore; } if (MouseState.LeftButton == ButtonState.Released && lastMouseState.LeftButton == ButtonState.Pressed) { Point mapCoords = coordinateConverter.ScreenToMap(MousePos, DisplayShift); if (gameMap.IsPointInside(mapCoords) && gameMap.game_tiles[(int)mapCoords.Y, (int)mapCoords.X].IsPathable()) { LumberCamp newLumberCamp = new LumberCamp(mapCoords); gameMap.game_tiles[(int)mapCoords.Y, (int)mapCoords.X].Building = newLumberCamp; gameMap.GetBuildings().Add(newLumberCamp); List <Point> validNeighbors = gameMap.GetValidNeighbors(mapCoords); List <Point> spawnableNeighbors = new List <Point>(); foreach (Point validNeighbor in validNeighbors) { if (gameMap.game_tiles[(int)validNeighbor.Y, (int)validNeighbor.X].IsPathable()) { spawnableNeighbors.Add(validNeighbor); } } for (int x = 0; x < 3; x++) { Point spawnTile = spawnableNeighbors[random.Next(spawnableNeighbors.Count)]; Laborer newLaborer = new Laborer(spawnTile); gameMap.GetUnits().Add(newLaborer); GameTile theTile = gameMap.game_tiles[(int)spawnTile.Y, (int)spawnTile.X]; theTile.Unit = newLaborer; } } } // IO events end // game logic start List <Entity> expiredEntities = new List <Entity>(); foreach (Unit unitObject in gameMap.GetUnits()) { unitObject.Tick(gameMap, Player1, pathfinder); } foreach (Terrain terrainObject in gameMap.GetTerrain()) { terrainObject.Tick(gameMap, Player1, pathfinder); if (terrainObject.IsExpired()) { expiredEntities.Add(terrainObject); } } foreach (Entity entityObject in expiredEntities) { entityObject.OnDeath(gameMap); gameMap.GetTerrain().Remove(entityObject); gameMap.GetBuildings().Remove(entityObject); } // game logic ends base.Update(gameTime); }
public override void OnDeath(GameMap gameMap) { Stump newStump = new Stump(coordinates); gameMap.GetTerrain().Add(newStump); }