/// <summary> /// Default constructor /// </summary> /// <param name="name">Name of the level.</param> /// <param name="levelMap">The map object to instantiate the level with.</param> /// <param name="bgMusic">Background music for the level.</param> /// <param name="enemies">Enemies located in the level.</param> /// <param name="levelObjects">Objects located in the level.</param> /// <param name="playerCharacter">Player's character in the level.</param> public GameLevel(string name, Map levelMap, Song bgMusic, MainCharacter playerCharacter, List<EnemyCharacter> enemies, List<LevelObject> levelObjects) { this.LevelMap = levelMap; this.bgMusic = bgMusic; this.Enemies = enemies; this.name = name; this.LevelObjects = levelObjects; this.PlayerCharacter = playerCharacter; }
/// <summary> /// This uses input of the player to move the character /// </summary> /// <param name="direction">The direction to move the character</param> /// <param name="currentMap">The current map the character is on</param> /// <param name="hasCollided">Has it collided with another object</param> /// <returns>This returns true if the character has moved (i.e no collisson)</returns> public override bool Move(MoveDirection direction, Map currentMap, out bool hasCollided) { bool hasMoved = base.Move(direction, currentMap, out hasCollided); Rectangle newCameraView = CameraView; if (hasMoved) { // center x coord of camera if (Position.X + (CharacterSprite.FrameSize.X / 2) <= (CameraView.Width / 2)) { // center camera to left edge newCameraView.X = 0; } else if (Position.X + (CharacterSprite.FrameSize.X / 2) >= currentMap.Size.X - (CameraView.Width / 2)) { // center camera to right edge newCameraView.X = currentMap.Size.X - CameraView.Width; } else { // center camera x coord to player position newCameraView.X = (int)Position.X - (CameraView.Width / 2) + (CharacterSprite.FrameSize.X / 2); } // center y cood of camera if (Position.Y + (CharacterSprite.FrameSize.Y / 2) <= (CameraView.Height / 2)) { // center camera to top edge newCameraView.Y = 0; } else if (Position.Y + (CharacterSprite.FrameSize.Y / 2) >= currentMap.Size.Y - (CameraView.Height / 2)) { // center camera to bottom edge newCameraView.Y = currentMap.Size.Y - CameraView.Height; } else { // center camera y coord to player position newCameraView.Y = (int)Position.Y - (CameraView.Height / 2) + (CharacterSprite.FrameSize.Y / 2); } } CameraView = newCameraView; return hasMoved; }
public void MoveTowardTarget(Vector2 destination, Map currentMap, GameTime gameTime, out bool hasMoved, out bool hasCollided) { MoveDirection direction = MoveDirection.None; if (DirectionStuckIn == MoveDirection.None) { if ((destination.X < Position.X) && (Position.X - speed.X) > destination.X) { direction = MoveDirection.Left; } else if ((destination.X > Position.X) && (Position.X + speed.X) < destination.X) { direction = MoveDirection.Right; } else if ((destination.Y < Position.Y) && (Position.Y - speed.Y) > destination.Y) { direction = MoveDirection.Up; } else if ((destination.Y > Position.Y) && (Position.Y + speed.Y) < destination.Y) { direction = MoveDirection.Down; } } else { // get unstuck switch (DirectionStuckIn) { case MoveDirection.Up: direction = MoveDirection.Left; break; case MoveDirection.Left: direction = MoveDirection.Down; break; case MoveDirection.Down: direction = MoveDirection.Right; break; case MoveDirection.Right: direction = MoveDirection.Up; break; } UnstickAttempts++; } hasMoved = base.Move(direction, currentMap, out hasCollided); base.Update(gameTime, direction, currentMap); // cheap way to try to get enemies unstuck if (hasCollided) { // randomly pick the direction stuck in int randMove = randomMover.Next(3); switch (direction) { case MoveDirection.Up: DirectionStuckIn = randMove < 2 ? MoveDirection.Up : MoveDirection.Down; break; case MoveDirection.Left: DirectionStuckIn = randMove < 2 ? MoveDirection.Left : MoveDirection.Right; break; case MoveDirection.Down: DirectionStuckIn = randMove < 2 ? MoveDirection.Down : MoveDirection.Up; break; case MoveDirection.Right: DirectionStuckIn = randMove < 2 ? MoveDirection.Right : MoveDirection.Left; break; } } else { if (UnstickAttempts > 100) { DirectionStuckIn = MoveDirection.None; UnstickAttempts = 0; } } }
public void MoveTowardPlayer(Map currentMap, GameTime gameTime, out bool hasMoved, out bool hasCollided) { MoveDirection direction = MoveDirection.None; if (PlayerCharacter.Position.X < Position.X && (Position.X - speed.X) > PlayerCharacter.Position.X) { direction = MoveDirection.Left; } else if (PlayerCharacter.Position.X > Position.X && (Position.X + speed.X) < PlayerCharacter.Position.X) { direction = MoveDirection.Right; } else if (PlayerCharacter.Position.Y < Position.Y && (Position.Y - speed.Y) > PlayerCharacter.Position.Y) { direction = MoveDirection.Up; } else if (PlayerCharacter.Position.Y > Position.Y && (Position.Y + speed.Y) < PlayerCharacter.Position.Y) { direction = MoveDirection.Down; } hasMoved = base.Move(direction, currentMap, out hasCollided); base.Update(gameTime, direction, currentMap); }
public void SetPosition(Vector2 newPosition, Map map) { Position = newPosition; Rectangle newCameraView = CameraView; int cx = (int)Position.X - (CameraView.Width / 2) + (CharacterSprite.FrameSize.X / 2); int cy = (int)Position.Y - (CameraView.Height / 2) + (CharacterSprite.FrameSize.Y / 2); if ((Position.Y + (CharacterSprite.FrameSize.Y / 2) >= map.Size.Y - (CameraView.Height / 2))) { newCameraView.Y = map.Size.Y - CameraView.Height; } if ((Position.X + (CharacterSprite.FrameSize.X / 2) >= map.Size.X - (CameraView.Width / 2))) { newCameraView.X = map.Size.X - CameraView.Width; } // center y cood of camera if (Position.Y + (CharacterSprite.FrameSize.Y / 2) <= (CameraView.Height / 2)) { // center camera to top edge newCameraView.Y = 0; } if (Position.X + (CharacterSprite.FrameSize.X / 2) <= (CameraView.Width / 2)) { // center camera to left edge newCameraView.X = 0; } CameraView = newCameraView; }
/// <summary> /// This updates the character's state /// </summary> /// <param name="gameTime">The game time to use for animation stuff</param> /// <param name="moveDirection">The movement direction (I.E input from the player)</param> /// <param name="currentMap">The map the currect character is on</param> public virtual void Update(GameTime gameTime, MoveDirection moveDirection, Map currentMap) { bool hasCollided; bool hasMoved = Move(moveDirection, currentMap, out hasCollided); CharacterSprite.Update(gameTime, moveDirection, hasCollided); }
/// <summary> /// This moves a character if there were no collissions within the map. /// </summary> /// <param name="direction">The direction to move the character (uses the enum below)</param> /// <param name="currentMap">The map to check if the character has collided with an object</param> /// <param name="hasCollided">This is the value it returns if the character collided</param> /// <returns>True if the character collided, false otherwise</returns> public virtual bool Move(MoveDirection direction, Map currentMap, out bool hasCollided) { bool hasMoved = false; hasCollided = false; if (direction != MoveDirection.None) { Vector2 movedPosition = Position; // caculate position if character were to move switch (direction) { case MoveDirection.Up: movedPosition.Y -= speed.Y; break; case MoveDirection.Down: movedPosition.Y += speed.Y; break; case MoveDirection.Left: movedPosition.X -= speed.X; break; case MoveDirection.Right: movedPosition.X += speed.X; break; } // check if movement would take character out of bounds bool outOfBounds = false; if ((movedPosition.X < 0) || (movedPosition.Y < 0) || (movedPosition.X > currentMap.Size.X - CharacterSprite.FrameSize.X) || (movedPosition.Y > currentMap.Size.Y - CharacterSprite.FrameSize.Y)) { outOfBounds = true; } // TODO: use better collision offsets // check if movement would result in boundary collision Rectangle movedRectangle = new Rectangle( (int)movedPosition.X, (int)movedPosition.Y, CharacterSprite.FrameSize.X, CharacterSprite.FrameSize.Y); hasCollided = currentMap.CollidesWithBoundary(movedRectangle); if (!(outOfBounds || hasCollided)) { Position = movedPosition; hasMoved = true; } } return hasMoved; }