Example #1
0
        /// <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);
        }