private void constructItem() { List <Tuple <resource.resourceTypes, int> > costs = new List <Tuple <resource.resourceTypes, int> >(); foreach (constructionItem item in items) { if (item.selected) { costs = item.cost; foreach (Tuple <resource.resourceTypes, int> cost in costs) { if (!playerInv.contains(cost.Item1, cost.Item2)) { //do nothing return; } } foreach (Tuple <resource.resourceTypes, int> cost in costs) { playerInv.decreaseResource(cost.Item1, cost.Item2); } if (item.appearsInInventory) { playerInv.addToInventory(item.type, 1); } else if (item.appearsOnMap) { Vector2 location = new Vector2(); location.X = currPlayer.getCurrentPos().X + currPlayer.getTexture().Width + currMap.getViewport().X; location.Y = currPlayer.getCurrentPos().Y + currPlayer.getTexture().Height + currMap.getViewport().Y; //add spike pits and trees to the world. if (item.type == resource.resourceTypes.Tree) { currMap.addTree(location); } else if (item.type == resource.resourceTypes.SpikePit) { currMap.addTrapToMap(location); } } break; } } }
/// <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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } // TODO: Add your update logic here if (attacking) { //RotationAngle += elapsed; //float circle = MathHelper.Pi * 300; //RotationAngle = RotationAngle % circle; RotationAngle += 0.1f; AttackTimeElapsed += 1; if (AttackTimeElapsed > maxAttackTime) { attacking = false; } checkZombieWeaponCollision(); } double elapsed = gameTime.TotalGameTime.TotalSeconds; if (elapsed >= nextZombieSpawnTime) { nextZombieSpawnTime += intervalBetweenSpawns; spawnZombies(); } if (!constructionMenuOpen && !currentPlayer.isInventoryOpen()) { List <Zombie> deleteZeds = new List <Zombie>(); List <Vector2> deleteTraps = new List <Vector2>(); foreach (Zombie zed in zombies) { Vector2 viewport = currentMap.getViewport(); Vector2 PlayerRealPos = currentPlayer.getCurrentPos(); PlayerRealPos.X += viewport.X; PlayerRealPos.Y += viewport.Y; zed.moveTowardsPlayer(currentPlayer, viewport); Matrix zombieMatrix = Matrix.CreateTranslation(zed.getLocation().X, zed.getLocation().Y, 0); Color[,] zombieColours = Helpers.TextureTo2DArray(zed.getTexture()); foreach (Vector2 trap in currentMap.getTraps()) { //Vector2 trapPos = new Vector2(trap.X, trap.Y); Matrix trapMatrix = Matrix.CreateTranslation(trap.X, trap.Y, 0); Color[,] trapColours = Helpers.TextureTo2DArray(currentMap.getTrapTexture()); Vector2 collisionPoint = Helpers.TexturesCollide(zombieColours, zombieMatrix, trapColours, trapMatrix); if (collisionPoint.X != -1) { zed.setDead(); deleteZeds.Add(zed); deleteTraps.Add(trap); } } } foreach (Zombie deadZed in deleteZeds) { zombies.Remove(deadZed); } foreach (Vector2 trap in deleteTraps) { currentMap.removeTrap(trap); } checkZombiePlayerCollision(); } else { //delay the spawn timer nextZombieSpawnTime += gameTime.ElapsedGameTime.TotalSeconds; } checkKeyboard(); base.Update(gameTime); }