123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Tile_Engine;
- namespace Gemstone_Hunter
- {
- public class GameObject
- {
- #region Declarations
- protected Vector2 worldLocation;
- protected Vector2 velocity;
- protected int frameWidth;
- protected int frameHeight;
- protected bool enabled;
- protected bool flipped = false;
- protected bool onGround;
- protected Rectangle collisionRectangle;
- protected int collideWidth;
- protected int collideHeight;
- protected bool codeBasedBlocks = true;
- protected float drawDepth = 0.85f;
- protected Dictionary<string, AnimationStrip> animations =
- new Dictionary<string, AnimationStrip>();
- protected string currentAnimation;
- #endregion
- #region Properties
- public bool Enabled
- {
- get { return enabled; }
- set { enabled = value; }
- }
- public Vector2 WorldLocation
- {
- get { return worldLocation; }
- set { worldLocation = value; }
- }
- public Vector2 WorldCenter
- {
- get
- {
- return new Vector2(
- (int)worldLocation.X + (int)(frameWidth / 2),
- (int)worldLocation.Y + (int)(frameHeight / 2));
- }
- }
- public Rectangle WorldRectangle
- {
- get
- {
- return new Rectangle(
- (int)worldLocation.X,
- (int)worldLocation.Y,
- frameWidth,
- frameHeight);
- }
- }
- public Rectangle CollisionRectangle
- {
- get
- {
- return new Rectangle(
- (int)worldLocation.X + collisionRectangle.X,
- (int)WorldRectangle.Y + collisionRectangle.Y,
- collisionRectangle.Width,
- collisionRectangle.Height);
- }
- set { collisionRectangle = value; }
- }
- #endregion
- #region Helper Methods
- private void updateAnimation(GameTime gameTime)
- {
- if (animations.ContainsKey(currentAnimation))
- {
- if (animations[currentAnimation].FinishedPlaying)
- {
- PlayAnimation(animations[currentAnimation].NextAnimation);
- }
- else
- {
- animations[currentAnimation].Update(gameTime);
- }
- }
- }
- #endregion
- #region Map-Based Collision Detection Methods
- private Vector2 horizontalCollisionTest(Vector2 moveAmount)
- {
- if (moveAmount.X == 0)
- return moveAmount;
- Rectangle afterMoveRect = CollisionRectangle;
- afterMoveRect.Offset((int)moveAmount.X, 0);
- Vector2 corner1, corner2;
- if (moveAmount.X < 0)
- {
- corner1 = new Vector2(afterMoveRect.Left,
- afterMoveRect.Top + 1);
- corner2 = new Vector2(afterMoveRect.Left,
- afterMoveRect.Bottom - 1);
- }
- else
- {
- corner1 = new Vector2(afterMoveRect.Right,
- afterMoveRect.Top + 1);
- corner2 = new Vector2(afterMoveRect.Right,
- afterMoveRect.Bottom - 1);
- }
- Vector2 mapCell1 = TileMap.GetCellByPixel(corner1);
- Vector2 mapCell2 = TileMap.GetCellByPixel(corner2);
- if (!TileMap.CellIsPassable(mapCell1) ||
- !TileMap.CellIsPassable(mapCell2))
- {
- moveAmount.X = 0;
- velocity.X = 0;
- }
- if (codeBasedBlocks)
- {
- if (TileMap.CellCodeValue(mapCell1) == "BLOCK" ||
- TileMap.CellCodeValue(mapCell2) == "BLOCK")
- {
- moveAmount.X = 0;
- velocity.X = 0;
- }
- }
- return moveAmount;
- }
- private Vector2 verticalCollisionTest(Vector2 moveAmount)
- {
- if (moveAmount.Y == 0)
- return moveAmount;
- Rectangle afterMoveRect = CollisionRectangle;
- afterMoveRect.Offset((int)moveAmount.X, (int)moveAmount.Y);
- Vector2 corner1, corner2;
- if (moveAmount.Y < 0)
- {
- corner1 = new Vector2(afterMoveRect.Left + 1,
- afterMoveRect.Top);
- corner2 = new Vector2(afterMoveRect.Right - 1,
- afterMoveRect.Top);
- }
- else
- {
- corner1 = new Vector2(afterMoveRect.Left + 1,
- afterMoveRect.Bottom);
- corner2 = new Vector2(afterMoveRect.Right - 1,
- afterMoveRect.Bottom);
- }
- Vector2 mapCell1 = TileMap.GetCellByPixel(corner1);
- Vector2 mapCell2 = TileMap.GetCellByPixel(corner2);
- if (!TileMap.CellIsPassable(mapCell1) ||
- !TileMap.CellIsPassable(mapCell2))
- {
- if (moveAmount.Y > 0)
- onGround = true;
- moveAmount.Y = 0;
- velocity.Y = 0;
- }
- if (codeBasedBlocks)
- {
- if (TileMap.CellCodeValue(mapCell1) == "BLOCK" ||
- TileMap.CellCodeValue(mapCell2) == "BLOCK")
- {
- if (moveAmount.Y > 0)
- onGround = true;
- moveAmount.Y = 0;
- velocity.Y = 0;
- }
- }
- return moveAmount;
- }
- #endregion
- #region Public Methods
- public void PlayAnimation(string name)
- {
- if (!(name == null) && animations.ContainsKey(name))
- {
- currentAnimation = name;
- animations[name].Play();
- }
- }
- public virtual void Update(GameTime gameTime)
- {
- if (!enabled)
- return;
- float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
- updateAnimation(gameTime);
- if (velocity.Y != 0)
- {
- onGround = false;
- }
- Vector2 moveAmount = velocity * elapsed;
- moveAmount = horizontalCollisionTest(moveAmount);
- moveAmount = verticalCollisionTest(moveAmount);
- Vector2 newPosition = worldLocation + moveAmount;
- newPosition = new Vector2(
- MathHelper.Clamp(newPosition.X, 0,
- Camera.WorldRectangle.Width - frameWidth),
- MathHelper.Clamp(newPosition.Y, 2 * (-TileMap.TileHeight),
- Camera.WorldRectangle.Height - frameHeight));
- worldLocation = newPosition;
- }
- public virtual void Draw(SpriteBatch spriteBatch)
- {
- if (!enabled)
- return;
- if (animations.ContainsKey(currentAnimation))
- {
- SpriteEffects effect = SpriteEffects.None;
- if (flipped)
- {
- effect = SpriteEffects.FlipHorizontally;
- }
- spriteBatch.Draw(
- animations[currentAnimation].Texture,
- Camera.WorldToScreen(WorldRectangle),
- animations[currentAnimation].FrameRectangle,
- Color.White, 0.0f, Vector2.Zero, effect, drawDepth);
- }
- }
- #endregion
- }
- }
|