예제 #1
0
        //*****************************************************************************************
        //Method name:  IsClose(MovingObj other)
        //Description:  Checks if two objects are close enough to begin actual hit detection
        //Parameters:   MovingObj other - the other object to be checked
        //Returns:      bool - if they are close or not
        //*****************************************************************************************
        public bool IsClose(MovingObj other)
        {
            bool retVal = false;

            other.AllLocs.ForEach(o =>
            {
                if (Math.Sqrt(Math.Pow(_location.X - o.X, 2) + Math.Pow(_location.Y - o.Y, 2)) < _size * 2 + other._size * 2)
                {
                    retVal = true;
                }
            });
            return(retVal);
        }
예제 #2
0
        //*****************************************************************************************
        //Method name:  IsColliding(MovingObj other, Graphics gr)
        //Description:  Checks if two object's regions are colliding.
        //Parameters:   MovingObj other - the object to check against
        //              Graphics gr - the graphics area on which they are drawn
        //Returns:      bool - if they are colliding or not
        //*****************************************************************************************
        public bool IsColliding(MovingObj other, Graphics gr)
        {
            Region myRegion;
            Region othRegion;
            bool   retVal = false;

            other.AllLocs.ForEach(o =>
            {
                myRegion  = new Region(GetPath(_location));
                othRegion = new Region(other.GetPath(o));
                myRegion.Intersect(othRegion);
                if (!myRegion.IsEmpty(gr))
                {
                    retVal = true;
                }
            });
            return(retVal);
        }
예제 #3
0
        //************************************************************************
        //Method name:  HandleGameLoop(BufferedGraphics bg)
        //Purpose:      Runs the game and applies all rules to the game objects.
        //Parameters:   BufferedGraphics bg - the graphics to check regions against
        //Returns:      void
        //************************************************************************
        private void HandleGameLoop(BufferedGraphics bg)
        {
            //rotates the ship on rotate controls
            if (_gameInputs.IsA_Down)
            {
                _myShip.Rotate(RotationType.Left);
            }
            if (_gameInputs.IsD_Down)
            {
                _myShip.Rotate(RotationType.Right);
            }

            //checks for forward or backward movement.
            if (_gameInputs.IsW_Down || _gameInputs.IsS_Down)
            {
                _myShip.Move((_gameInputs.IsW_Down) ? Direction.Forward : Direction.Backward);
            }
            else
            {
                _myShip.Move(Direction.None);
            }

            //shoots ship's weapon. Only shoots on intervals of fireRate.
            if (_gameInputs.IsSpace_Down)
            {
                if (shotsFired % fireRate == 0)
                {
                    shotsFired = 0;
                    _allRenderable.Add(new Bullet(new PointF(_myShip.Pos.X, _myShip.Pos.Y), _myShip.Heading));
                    _soundEng.Play2D(@"..\..\..\laserGun.wav");
                }
                shotsFired++;
            }
            else
            {
                shotsFired = 0;
            }

            //checks if pause button is pressed.
            if (_gameInputs.IsEsc_Down)
            {
                _currentState = GameState.PauseGame;
                _currentMenu  = new Menu(_currentState, new PointF(_client.Width / 2, _client.Height / 2));
                _allRenderable.Add(_currentMenu);
            }

            //checks to see if a new rock should be spawned.
            if (_rockTimer.Elapsed.Seconds > _secondsBetweenRockSpawns)
            {
                _rockTimer.Reset();
                _rockTimer.Start();
                _allRenderable.Add(new Rock(Rock.RockSize.Big, new PointF(_rng.Next(0, _client.Width), _rng.Next(0, _client.Height)), true));
            }

            //applies game rules to all objects.
            _allRenderable.ForEach(o =>
            {
                if (o is MovingObj)
                {
                    //move all objects one tick
                    MovingObj temp = (MovingObj)o;
                    temp.Tick(_client);

                    //applies rules to bullets. Checks for collisions and removes if necessary.
                    if (temp is Bullet)
                    {
                        Bullet currBullet = (Bullet)temp;
                        CheckBulletCollisionOnRocks(currBullet, bg.Graphics);
                        if (currBullet.FlaggedForRemoval)
                        {
                            _killList.Add(currBullet);
                        }
                    }

                    //applies rules to rocks. Checks for collisions with ships and ends game if necessary,
                    if (temp is Rock)
                    {
                        Rock currRock = (Rock)temp;
                        if (currRock.FlaggedForRemoval)
                        {
                            _killList.Add(currRock);
                        }

                        //Checks for ship collisions.
                        if (!_myShip.IsInvincible &&
                            !currRock.IsFadingIn &&
                            _myShip.IsClose(currRock) &&
                            _myShip.IsColliding(currRock, bg.Graphics))
                        {
                            _ld.TookDamage();
                            _myShip.SetPosition(new Point(_client.Width / 2, _client.Height / 2));
                            _soundEng.Play2D(@"..\..\..\takeDamage.wav");
                            if (_ld.CurrentLives > 0)
                            {
                                _myShip.ActivateInvincibility();
                            }

                            //ends game here if no more lives.
                            else
                            {
                                _currentState = GameState.PostGame;
                                _currentMenu  = new Menu(GameState.PostGame, new PointF(_client.Width / 2, _client.Height / 2));
                                _newObjects.Add(_currentMenu);
                            }
                        }
                    }
                }
            });
        }