예제 #1
0
 public GameObject(Vector2 position,Vector2 velocity,Texture2D sprite,ObjectType objectType)
 {
     _position = position;
     Velocity = velocity;
     Sprite = sprite;
     Type = objectType;
     BoundingRectangle = new BoundingRectangle((int) _position.X, (int) _position.Y, sprite.Width, sprite.Height);
 }
예제 #2
0
 public bool IntersectsWith(BoundingRectangle boundingRectangle)
 {
     return _rectangle.Intersects(boundingRectangle._rectangle);
 }
예제 #3
0
 public Camera(int x, int y, int width, int height)
 {
     _viewport = new Viewport(x, y, width, height);
     _boundingRectangle = new BoundingRectangle(x, y, width, height);
 }
예제 #4
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)
                Exit();

            KeyboardState newKeyboardState = Keyboard.GetState();

            if (_oldKeyboardState.IsKeyUp(Keys.R) && newKeyboardState.IsKeyDown(Keys.R))
            {
                LoadLevel(_currentLevel.Name);
                return;
            }

            #if DEBUG
            MouseState newMouseState = Mouse.GetState();
            if (_oldKeyboardState.IsKeyUp(Keys.P) && newKeyboardState.IsKeyDown(Keys.P))
            {
                _logWindow.AddMessage(_currentLevel.ToLevelString());
            }
            if (_oldKeyboardState.IsKeyUp(Keys.N) && newKeyboardState.IsKeyDown(Keys.N))
            {
                _noclip = !_noclip;
                _camera.LockToPlayingArea = !_noclip;
            }
            if (_oldKeyboardState.IsKeyUp(Keys.L) && newKeyboardState.IsKeyDown(Keys.L))
            {
                _currentLevel.SaveLevelToFile("customlevel.txt");
                LoadLevel("customlevel.txt");
            }
            if(_oldKeyboardState.IsKeyUp(Keys.E) &&  newKeyboardState.IsKeyDown(Keys.E))
            {
                LoadLevel("");
            }

            #region MouseInputInDeBugMode

            if (_form.Focused && newMouseState.LeftButton == ButtonState.Pressed
                || newMouseState.RightButton == ButtonState.Pressed)
            {
                int mousePositionX = (int) (newMouseState.X + _camera.Position.X);
                int mousePositionY = (int) (newMouseState.Y + _camera.Position.Y);
                int x = (mousePositionX)/32*32;
                int y = (mousePositionY)/32*32;
                if (mousePositionX <= 0)
                    x -= 32;
                GameObject block = null;
                var boundingRectangle = new BoundingRectangle(mousePositionX - 1, mousePositionY - 1,
                                                              2, 2);
                var temp =
                    _currentLevel.GameObjects.Where(gobj => gobj.BoundingRectangle.IntersectsWith(boundingRectangle));

                while (temp.Count() > 1)
                {
                    _currentLevel.GameObjects.Remove(temp.First());
                    block = temp.First();
                    temp =
                        _currentLevel.GameObjects.Where(gobj => gobj.BoundingRectangle.IntersectsWith(boundingRectangle));
                }
                if (temp.Count() == 1)
                    block = temp.First();

                if (newMouseState.LeftButton == ButtonState.Pressed && _oldMouseState.LeftButton == ButtonState.Released)
                {
                    if (block != null)
                    {
                        int nextBlockIndex = _blocks.IndexOf(block.Sprite) + 1;
                        if (nextBlockIndex >= _blocks.Count())
                        {
                            _currentLevel.GameObjects.Remove(block);
                        }
                        else
                        {
                            block.Sprite = _blocks[nextBlockIndex];
                        }
                    }
                    else
                    {
                        block = new GameObject(new Vector2(x, y), Vector2.Zero, _blocks[0],
                                               GameObject.ObjectType.Block);
                        _currentLevel.GameObjects.Add(block);
                    }
                }
                if (newMouseState.RightButton == ButtonState.Pressed
                    && _oldMouseState.RightButton == ButtonState.Released)
                {
                    if (block == null)
                    {
                        var newEnemy = new Enemy(new Vector2(x, y), Vector2.Zero, _enemyTexture);
                        bool emptyPosition = true;
                        foreach (GameObject gobj in _currentLevel.GameObjects)
                        {
                            if (gobj.Type == GameObject.ObjectType.Enemy)
                            {
                                var existingEnemy = (Enemy) gobj;
                                if (existingEnemy.SpawnLocation == newEnemy.SpawnLocation)
                                {
                                    emptyPosition = false;
                                }
                            }
                        }
                        if (emptyPosition)
                            _currentLevel.GameObjects.Add(newEnemy);
                    }
                    else
                    {
                        if (block.Type == GameObject.ObjectType.Enemy)
                            _currentLevel.GameObjects.Remove(block);

                    }
                }
            }
            _oldMouseState = newMouseState;

            #endregion

            if (_noclip)
            {
                if (newKeyboardState.IsKeyDown(Keys.Left))
                {
                    Vector2 newPos = _player.Position;
                    newPos.X -= 10;
                    _player.Position = newPos;
                }
                else if (newKeyboardState.IsKeyDown(Keys.Right))
                {
                    Vector2 newPos = _player.Position;
                    newPos.X += 10;
                    _player.Position = newPos;
                }
                if (newKeyboardState.IsKeyDown(Keys.Up))
                {
                    Vector2 newPos = _player.Position;
                    newPos.Y -= 10;
                    _player.Position = newPos;
                }
                if (newKeyboardState.IsKeyDown(Keys.Down))
                {
                    Vector2 newPos = _player.Position;
                    newPos.Y += 10;
                    _player.Position = newPos;
                }
            }
            else
            {
            #endif
                if (newKeyboardState.IsKeyDown(Keys.Left))
                {
                    _player.Move(MovingObject.Direction.Left);
                }
                else if (newKeyboardState.IsKeyDown(Keys.Right))
                {
                    _player.Move(MovingObject.Direction.Right);
                }
                if (newKeyboardState.IsKeyDown(Keys.Up))
                {
                    _player.Jump();
                }
                foreach (GameObject gobj in _currentLevel.GameObjects)
                {
                    gobj.Update(_currentLevel);
                }
                if (_currentLevel.Finished)
                {
                    if (DateTime.Now >= _currentLevel.LevelFinishTime.AddSeconds(3))
                    {
                        NextLevel();
                    }
                }
                else
                {
                    if (_player.HasFinished(_currentLevel))
                    {
                        _currentLevel.Finished = true;
                        _currentLevel.LevelFinishTime = DateTime.Now;
                        _lastScore = _lastScore + _player.Score;
                        _player.Score = 0;
                    }
                }
            #if DEBUG
            }
            #endif
            _camera.Update();
            _totalScore = _lastScore + _player.Score;

            _oldKeyboardState = newKeyboardState;

            base.Update(gameTime);
        }