Esempio n. 1
0
        /// <summary>
        /// Update the game based on events, objects and other things
        /// </summary>
        public void Update()
        {
            randomhit++;

            //spawn rate - jeremy
            if (randomhit >= 200)
            {
                Init();
                randomhit = 0;
            }

            // JY- detect projectile hits, projectile and enemies will be deleted
            foreach (Entity e in Objects)
            {
                if (e is Projectile)
                {
                    if (_twoplayer)
                    {
                        if (_player.PlayerHitbyProjectile((Projectile)e))
                        {
                            _gameStates.Push(GameState.PlayerTwoWins);
                            ControlGameState();
                        }
                        else if (_player2.PlayerHitbyProjectile((Projectile)e))
                        {
                            _gameStates.Push(GameState.PlayerOneWins);
                            ControlGameState();
                        }
                    }
                    else
                    {
                        if (_player.PlayerHitbyProjectile((Projectile)e))
                        {
                            _gameStates.Push(GameState.SinglePlayerEndGame);
                            ControlGameState();
                        }
                    }

                    if (SpawnedEnemies.Count > 0)
                    {
                        foreach (Enemy _e in SpawnedEnemies)
                        {
                            if (((Projectile)e).CheckObjectHit(_world, _e))
                            {
                                ProjectilesToBeRemoved.Add((Projectile)e);
                            }

                            if (_e.CheckHit((Projectile)e))
                            {
                                EnemiesToBeRemoved.Add(_e);
                            }
                        }
                    }
                    else
                    if (((Projectile)e).CheckObjectHit(_world, _enemy))
                    {
                        ProjectilesToBeRemoved.Add((Projectile)e);
                    }
                }
                //JY- detect enemies hitting players
                else if (e is Enemy)
                {
                    if (_twoplayer)
                    {
                        if (_player.PlayerHitbyEnemy((Enemy)e))
                        {
                            _gameStates.Push(GameState.PlayerTwoWins);
                            //_player.PlayerHitbyEnemy((Enemy)e) = !_player.PlayerHitbyEnemy((Enemy)e);
                            ControlGameState();
                        }
                        else if (_player2.PlayerHitbyEnemy((Enemy)e))
                        {
                            _gameStates.Push(GameState.PlayerOneWins);
                            ControlGameState();
                        }
                    }
                    else
                    {
                        if (_player.PlayerHitbyEnemy((Enemy)e))
                        {
                            _gameStates.Push(GameState.SinglePlayerEndGame);
                            ControlGameState();
                        }
                    }
                }
            }

            foreach (Projectile p in ProjectilesToBeRemoved)
            {
                Objects.Remove(p);
            }

            foreach (Enemy en in EnemiesToBeRemoved)
            {
                if (SpawnedEnemies.Contains(en))
                {
                    SpawnedEnemies.Remove(en);
                }

                Objects.Remove(en);
            }

            foreach (Entity obj in Objects)
            {
                obj.Update();
            }


            //PLAYER 1 MINE/BUILD ROCKS INPUT
            //mine rocks/minerals right
            // IA - Rewritten the mining logic for the single player
            if (SwinGame.KeyDown(KeyCode.FKey))
            {
                int x = _player.Location.X;
                int y = _player.Location.Y;
                if (SwinGame.KeyReleased(KeyCode.DKey))
                {
                    x += 1;
                }
                else if (SwinGame.KeyReleased(KeyCode.SKey))
                {
                    y += 1;
                }
                else if (SwinGame.KeyReleased(KeyCode.AKey))
                {
                    x -= 1;
                }
                else if (SwinGame.KeyReleased(KeyCode.WKey))
                {
                    y -= 1;
                }

                try
                {
                    if (_world.Map[x, y].Type == TileType.Rock && x != 0 && y != 0 && x != 52 && y != 36)
                    {
                        _world.Map[x, y].Type = TileType.Air;
                        if (_world.Map[x, y].Mineral != null)
                        {
                            GetPlayer().Inventory.AddItem(_world.Map[x, y].Mineral);
                        }
                    }
                }
                catch (Exception e)
                {
                    // IA - in case of an exception, force the reassignment of x and y using the player's location.
                    x = _player.Location.X;
                    y = _player.Location.Y;
                }
            }

            //build rock right
            if (SwinGame.KeyDown(KeyCode.DKey) && SwinGame.KeyDown(KeyCode.GKey))
            {
                //GetEnvironment ().HandleGuiEvent (GuiEvent.MouseRight, new Location (_player.Location.X+1, _player.Location.Y));
                GetEnvironment();
                int   x = (_player.Location.X + 1);
                int   y = (_player.Location.Y);
                Frame f = GuiEnvironment.GetRenderer().GetActiveFrame(new Location(x, y));
                if (f == null)
                {
                    Tile tile = _world.Map[x, y];
                    if (tile.Type == TileType.Air)
                    {
                        _world.Map[x, y] = new Tile(TileType.Rock);
                    }
                }
            }

            //build rock left
            if (SwinGame.KeyDown(KeyCode.AKey) && SwinGame.KeyDown(KeyCode.GKey))
            {
                //GetEnvironment ().HandleGuiEvent (GuiEvent.MouseRight, new Location (_player.Location.X+1, _player.Location.Y));
                GetEnvironment();
                int   x = (_player.Location.X - 1);
                int   y = (_player.Location.Y);
                Frame f = GuiEnvironment.GetRenderer().GetActiveFrame(new Location(x, y));
                if (f == null)
                {
                    Tile tile = _world.Map[x, y];
                    if (tile.Type == TileType.Air)
                    {
                        _world.Map[x, y] = new Tile(TileType.Rock);
                    }
                }
            }

            //build rock up
            if (SwinGame.KeyDown(KeyCode.WKey) && SwinGame.KeyDown(KeyCode.GKey))
            {
                //GetEnvironment ().HandleGuiEvent (GuiEvent.MouseRight, new Location (_player.Location.X+1, _player.Location.Y));
                GetEnvironment();
                int   x = (_player.Location.X);
                int   y = (_player.Location.Y - 1);
                Frame f = GuiEnvironment.GetRenderer().GetActiveFrame(new Location(x, y));
                if (f == null)
                {
                    Tile tile = _world.Map[x, y];
                    if (tile.Type == TileType.Air)
                    {
                        _world.Map[x, y] = new Tile(TileType.Rock);
                    }
                }
            }

            //build rock down
            if (SwinGame.KeyDown(KeyCode.SKey) && SwinGame.KeyDown(KeyCode.GKey))
            {
                //GetEnvironment ().HandleGuiEvent (GuiEvent.MouseRight, new Location (_player.Location.X+1, _player.Location.Y));
                GetEnvironment();
                int   x = (_player.Location.X);
                int   y = (_player.Location.Y + 1);
                Frame f = GuiEnvironment.GetRenderer().GetActiveFrame(new Location(x, y));
                if (f == null)
                {
                    Tile tile = _world.Map[x, y];
                    if (tile.Type == TileType.Air)
                    {
                        _world.Map[x, y] = new Tile(TileType.Rock);
                    }
                }
            }

            if (_twoplayer == true)
            {
                //PLAYER 2 MINE/BUILD ROCKS INPUT
                //mine rocks/minerals right
                if (SwinGame.KeyDown(KeyCode.KKey))
                {
                    int i = _player2.Location.X;
                    int j = _player2.Location.Y;
                    if (SwinGame.KeyReleased(KeyCode.RightKey))
                    {
                        i += 1;
                    }
                    else if (SwinGame.KeyReleased(KeyCode.DownKey))
                    {
                        j += 1;
                    }
                    else if (SwinGame.KeyReleased(KeyCode.LeftKey))
                    {
                        i -= 1;
                    }
                    else if (SwinGame.KeyReleased(KeyCode.UpKey))
                    {
                        j -= 1;
                    }

                    try
                    {
                        if (_world.Map[i, j].Type == TileType.Rock && i != 0 && j != 0 && i != 52 && j != 36)
                        {
                            _world.Map[i, j].Type = TileType.Air;
                            if (_world.Map[i, j].Mineral != null)
                            {
                                _player2.Inventory.AddItem(_world.Map[i, j].Mineral);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        // IA - in case of an exception, force the reassignment of x and y using the player's location.
                        i = _player2.Location.X;
                        j = _player2.Location.Y;
                    }
                }

                //build rock right
                if (SwinGame.KeyDown(KeyCode.RightKey) && SwinGame.KeyDown(KeyCode.LKey))
                {
                    //GetEnvironment ().HandleGuiEvent (GuiEvent.MouseRight, new Location (_player.Location.X+1, _player.Location.Y));
                    GetEnvironment();
                    int   x = (_player2.Location.X + 1);
                    int   y = (_player2.Location.Y);
                    Frame f = GuiEnvironment.GetRenderer().GetActiveFrame(new Location(x, y));
                    if (f == null)
                    {
                        Tile tile = _world.Map[x, y];
                        if (tile.Type == TileType.Air)
                        {
                            _world.Map[x, y] = new Tile(TileType.Rock);
                        }
                    }
                }

                //build rock left
                if (SwinGame.KeyDown(KeyCode.LeftKey) && SwinGame.KeyDown(KeyCode.LKey))
                {
                    //GetEnvironment ().HandleGuiEvent (GuiEvent.MouseRight, new Location (_player.Location.X+1, _player.Location.Y));
                    GetEnvironment();
                    int   x = (_player2.Location.X - 1);
                    int   y = (_player2.Location.Y);
                    Frame f = GuiEnvironment.GetRenderer().GetActiveFrame(new Location(x, y));
                    if (f == null)
                    {
                        Tile tile = _world.Map[x, y];
                        if (tile.Type == TileType.Air)
                        {
                            _world.Map[x, y] = new Tile(TileType.Rock);
                        }
                    }
                }

                //build rock up
                if (SwinGame.KeyDown(KeyCode.UpKey) && SwinGame.KeyDown(KeyCode.LKey))
                {
                    //GetEnvironment ().HandleGuiEvent (GuiEvent.MouseRight, new Location (_player.Location.X+1, _player.Location.Y));
                    GetEnvironment();
                    int   x = (_player2.Location.X);
                    int   y = (_player2.Location.Y - 1);
                    Frame f = GuiEnvironment.GetRenderer().GetActiveFrame(new Location(x, y));
                    if (f == null)
                    {
                        Tile tile = _world.Map[x, y];
                        if (tile.Type == TileType.Air)
                        {
                            _world.Map[x, y] = new Tile(TileType.Rock);
                        }
                    }
                }

                //build rock down
                if (SwinGame.KeyDown(KeyCode.DownKey) && SwinGame.KeyDown(KeyCode.LKey))
                {
                    //GetEnvironment ().HandleGuiEvent (GuiEvent.MouseRight, new Location (_player.Location.X+1, _player.Location.Y));
                    GetEnvironment();
                    int   x = (_player2.Location.X);
                    int   y = (_player2.Location.Y + 1);
                    Frame f = GuiEnvironment.GetRenderer().GetActiveFrame(new Location(x, y));
                    if (f == null)
                    {
                        Tile tile = _world.Map[x, y];
                        if (tile.Type == TileType.Air)
                        {
                            _world.Map[x, y] = new Tile(TileType.Rock);
                        }
                    }
                }
            }

            if (SwinGame.KeyDown(KeyCode.EscapeKey))
            {
                Environment.Exit(0);
            }

            //PLAYER 1 KEY MOVEMENT/WEAPON INPUT
            //changes player input using keyboard - jeremy
            //Allowed player to attack,
            //else if if used so that the player can spam attack and move at the same time - Jonathan

            // buy weapon
            if (SwinGame.KeyDown(KeyCode.VKey) && SwinGame.KeyDown(KeyCode.WKey))
            {
                if (_player.Weapon != null && _player.Weapon.Ammunition > 0)
                {
                    _player.DeployWeapon(AttackDirection.Up);
                    Objects.Add(_player.Weapon.Projectile);
                }
            }
            else if (SwinGame.KeyDown(KeyCode.WKey) && _world.Map[_player.Location.X, _player.Location.Y - 1].Type != TileType.Rock && _player.Location.Y - 1 != 0)
            {
                _player.Location.Y -= 1;
            }

            if (SwinGame.KeyDown(KeyCode.VKey) && SwinGame.KeyDown(KeyCode.SKey))
            {
                if (_player.Weapon != null && _player.Weapon.Ammunition > 0)
                {
                    _player.DeployWeapon(AttackDirection.Down);
                    Objects.Add(_player.Weapon.Projectile);
                }
            }
            else if (SwinGame.KeyDown(KeyCode.SKey) && _world.Map[_player.Location.X, _player.Location.Y + 1].Type != TileType.Rock && _player.Location.Y + 1 != 36)
            {
                _player.Location.Y += 1;
            }

            if (SwinGame.KeyDown(KeyCode.VKey) && SwinGame.KeyDown(KeyCode.AKey))
            {
                if (_player.Weapon != null && _player.Weapon.Ammunition > 0)
                {
                    _player.DeployWeapon(AttackDirection.Left);
                    Objects.Add(_player.Weapon.Projectile);
                }
            }
            else if (SwinGame.KeyDown(KeyCode.AKey) && _world.Map[_player.Location.X - 1, _player.Location.Y].Type != TileType.Rock && _player.Location.X - 1 != 0)
            {
                _player.Location.X -= 1;
            }

            if (SwinGame.KeyDown(KeyCode.VKey) && SwinGame.KeyDown(KeyCode.DKey))
            {
                if (_player.Weapon != null && _player.Weapon.Ammunition > 0)
                {
                    _player.DeployWeapon(AttackDirection.Right);
                    Objects.Add(_player.Weapon.Projectile);
                }
            }
            else if (SwinGame.KeyDown(KeyCode.DKey) && _world.Map[_player.Location.X + 1, _player.Location.Y].Type != TileType.Rock && _player.Location.X + 1 != 52)
            {
                _player.Location.X += 1;
            }

            if (SwinGame.KeyTyped(KeyCode.BKey) && !SwinGame.KeyDown(KeyCode.LeftShiftKey))
            {
                if (GetPlayer().Inventory.GetMineralPoints() >= 20)
                {
                    if (_player.Weapon == null)
                    {
                        _player.BuyWeapon(_player.Location, WeaponType.Normal);
                    }
                    if (_player.Weapon.Type == WeaponType.Super)
                    {
                        _player.BuyWeapon(_player.Location, WeaponType.Normal);
                    }
                    _player.Weapon.Ammunition += 30;
                    // Objects.Add(_player.Weapon);
                    _player.Inventory.DeductMineralPoints(WeaponType.Normal);
                }
            }

            if (SwinGame.KeyTyped(KeyCode.BKey) && SwinGame.KeyDown(KeyCode.LeftShiftKey))
            {
                if (GetPlayer().Inventory.GetMineralPoints() >= 30)
                {
                    if (_player.Weapon == null)
                    {
                        _player.BuyWeapon(_player.Location, WeaponType.Super);
                    }
                    if (_player.Weapon.Type == WeaponType.Normal)
                    {
                        _player.BuyWeapon(_player.Location, WeaponType.Super);
                    }
                    _player.Weapon.Ammunition += 30;
                    // Objects.Add(_player.Weapon);
                    _player.Inventory.DeductMineralPoints(WeaponType.Super);
                }
            }

            if (_twoplayer == true)
            {
                //PLAYER 2 MOVEMENT/WEAPON KEY INPUT
                if (SwinGame.KeyDown(KeyCode.OKey) && SwinGame.KeyDown(KeyCode.UpKey))
                {
                    if (_player2.Weapon != null && _player2.Weapon.Ammunition > 0)
                    {
                        _player2.DeployWeapon(AttackDirection.Up);
                        Objects.Add(_player2.Weapon.Projectile);
                    }
                }
                else if (SwinGame.KeyDown(KeyCode.UpKey) && _world.Map[_player2.Location.X, _player2.Location.Y - 1].Type != TileType.Rock && _player2.Location.Y - 1 != 0)
                {
                    _player2.Location.Y -= 1;
                }

                if (SwinGame.KeyDown(KeyCode.OKey) && SwinGame.KeyDown(KeyCode.DownKey))
                {
                    if (_player2.Weapon != null && _player2.Weapon.Ammunition > 0)
                    {
                        _player2.DeployWeapon(AttackDirection.Down);
                        Objects.Add(_player2.Weapon.Projectile);
                    }
                }
                else if (SwinGame.KeyDown(KeyCode.DownKey) && _world.Map[_player2.Location.X, _player2.Location.Y + 1].Type != TileType.Rock && _player2.Location.Y + 1 != 36)
                {
                    _player2.Location.Y += 1;
                }

                if (SwinGame.KeyDown(KeyCode.OKey) && SwinGame.KeyDown(KeyCode.LeftKey))
                {
                    if (_player2.Weapon != null && _player2.Weapon.Ammunition > 0)
                    {
                        _player2.DeployWeapon(AttackDirection.Left);
                        Objects.Add(_player2.Weapon.Projectile);
                    }
                }
                else if (SwinGame.KeyDown(KeyCode.LeftKey) && _world.Map[_player2.Location.X - 1, _player2.Location.Y].Type != TileType.Rock && _player2.Location.X - 1 != 0)
                {
                    _player2.Location.X -= 1;
                }

                if (SwinGame.KeyDown(KeyCode.OKey) && SwinGame.KeyDown(KeyCode.RightKey))
                {
                    if (_player2.Weapon != null && _player2.Weapon.Ammunition > 0)
                    {
                        _player2.DeployWeapon(AttackDirection.Right);
                        Objects.Add(_player2.Weapon.Projectile);
                    }
                }
                else if (SwinGame.KeyDown(KeyCode.RightKey) && _world.Map[_player2.Location.X + 1, _player2.Location.Y].Type != TileType.Rock && _player2.Location.X + 1 != 52)
                {
                    _player2.Location.X += 1;
                }

                // Player 2 buys a weapon
                if (SwinGame.KeyTyped(KeyCode.PKey) && !SwinGame.KeyDown(KeyCode.JKey))
                {
                    if (_player2.Inventory.GetMineralPoints() >= 20)
                    {
                        if (_player2.Weapon == null)
                        {
                            _player2.BuyWeapon(_player.Location, WeaponType.Normal);
                        }
                        if (_player2.Weapon.Type == WeaponType.Super)
                        {
                            _player2.BuyWeapon(_player.Location, WeaponType.Normal);
                        }
                        _player2.Weapon.Ammunition += 20;
                        // Objects.Add(_player.Weapon);
                        _player2.Inventory.DeductMineralPoints(WeaponType.Normal);
                    }
                }


                if (SwinGame.KeyTyped(KeyCode.PKey) && SwinGame.KeyDown(KeyCode.JKey))
                {
                    if (_player2.Inventory.GetMineralPoints() >= 30)
                    {
                        if (_player2.Weapon == null)
                        {
                            _player2.BuyWeapon(_player.Location, WeaponType.Super);
                        }
                        if (_player2.Weapon.Type == WeaponType.Normal)
                        {
                            _player2.BuyWeapon(_player.Location, WeaponType.Super);
                        }
                        _player2.Weapon.Ammunition += 40;
                        // Objects.Add(_player.Weapon);
                        _player2.Inventory.DeductMineralPoints(WeaponType.Super);
                    }
                }
            }

            // Accept the spawning of minerals on demand - Useful for demonstration purposes only (to make it easier to collect mineral points, buy food, weapons, etc.)
            // if (SwinGame.KeyTyped(KeyCode.EKey))
            // {
            // GetWorld().PutMinerals();
            // }

            // IA - After computing everything, determine when/if the level should be increased.
            ControlLevels();
            MetaHandler.DisplayFoodExchange(GetPlayer().Inventory);

            // IA - End the game when the player's energy level reaches 0.
            if (MetaHandler.GetEnergyLevel() == 0)
            {
                EndGame("you_died");
            }
        }