public bool IntersectsWith(GameObject gameObject) { if (BoundingRectangle.IntersectsWith(gameObject.BoundingRectangle)) { if (PixelsIntersect(BoundingRectangle.Rectangle, TextureToArray(Sprite), gameObject.BoundingRectangle.Rectangle, TextureToArray(gameObject.Sprite))) return true; } return false; }
public bool Visible(GameObject gobj) { return _boundingRectangle.IntersectsWith(gobj.BoundingRectangle); }
public void LockToObject(GameObject gameObject) { _focusObject = gameObject; _mode = CameraMode.Locked; }
/// <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); }
internal Level LoadLevelFromFile(string filename) { var level = new Level(); using(var sr = new StreamReader(filename)) { while (!sr.EndOfStream) { string s = sr.ReadLine(); if (s != null && (s.StartsWith("//") || s == "")) continue; if (s != null) { string[] split = s.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries); if (split[2] == "startzone") { Texture2D sprite = _startZoneTexture; var startZone = new GameObject( new Vector2(float.Parse(split[0]), float.Parse(split[1])), Vector2.Zero, sprite, GameObject.ObjectType.StartZone); level.StartZone = startZone; } else if (split[2] == "finishzone") { Texture2D sprite = _finishZoneTexture; var finishZone = new GameObject( new Vector2(float.Parse(split[0]), float.Parse(split[1])), Vector2.Zero, sprite, GameObject.ObjectType.FinishZone); level.FinishZone = finishZone; } else if(split[2] == "enemy") { Texture2D sprite = _enemyTexture; var enemy = new Enemy(new Vector2(float.Parse(split[0]), float.Parse(split[1])), Vector2.Zero, sprite); level.GameObjects.Add(enemy); } else { var sprite = Content.Load<Texture2D>(split[2]); sprite.Name = split[2]; var gameObject = new GameObject( new Vector2(float.Parse(split[0]), float.Parse(split[1])), new Vector2(0, 0), sprite, GameObject.ObjectType.Block); level.GameObjects.Add(gameObject); } } } } return level; //X,Y,spritename //first line = X //second line = Y //third line = spritename }