GameObject.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Tile_Engine;
  8. namespace Gemstone_Hunter
  9. {
  10. public class GameObject
  11. {
  12. protected Vector2 worldLocation;
  13. protected Vector2 velocity;
  14. protected int frameWidth;
  15. protected int frameHeight;
  16. protected bool enabled;
  17. protected bool flipped = false;
  18. protected bool onGround;
  19. protected Rectangle collisionRectangle;
  20. protected int collideWidth;
  21. protected int collideHeight;
  22. protected bool codeBasedBlocks = true;
  23. protected float drawDepth = 0.85f;
  24. protected Dictionary<string, AnimationStrip> animations =
  25. new Dictionary<string, AnimationStrip>();
  26. protected string currentAnimation;
  27. public bool Enabled
  28. {
  29. get { return enabled; }
  30. set { enabled = value; }
  31. }
  32. public Vector2 WorldLocation
  33. {
  34. get { return worldLocation; }
  35. set { worldLocation = value; }
  36. }
  37. public Vector2 WorldCenter
  38. {
  39. get
  40. {
  41. return new Vector2(
  42. (int)worldLocation.X + (int)(frameWidth / 2),
  43. (int)worldLocation.Y + (int)(frameHeight / 2));
  44. }
  45. }
  46. public Rectangle WorldRectangle
  47. {
  48. get
  49. {
  50. return new Rectangle(
  51. (int)worldLocation.X,
  52. (int)worldLocation.Y,
  53. frameWidth,
  54. frameHeight);
  55. }
  56. }
  57. public Rectangle CollisionRectangle
  58. {
  59. get
  60. {
  61. return new Rectangle(
  62. (int)worldLocation.X + collisionRectangle.X,
  63. (int)WorldRectangle.Y + collisionRectangle.Y,
  64. collisionRectangle.Width,
  65. collisionRectangle.Height);
  66. }
  67. set { collisionRectangle = value; }
  68. }
  69. private void updateAnimation(GameTime gameTime)
  70. {
  71. if (animations.ContainsKey(currentAnimation))
  72. {
  73. if (animations[currentAnimation].FinishedPlaying)
  74. {
  75. PlayAnimation(animations[currentAnimation].NextAnimation);
  76. }
  77. else
  78. {
  79. animations[currentAnimation].Update(gameTime);
  80. }
  81. }
  82. }
  83. private Vector2 horizontalCollisionTest(Vector2 moveAmount)
  84. {
  85. if (moveAmount.X == 0)
  86. return moveAmount;
  87. Rectangle afterMoveRect = CollisionRectangle;
  88. afterMoveRect.Offset((int)moveAmount.X, 0);
  89. Vector2 corner1, corner2;
  90. if (moveAmount.X < 0)
  91. {
  92. corner1 = new Vector2(afterMoveRect.Left,
  93. afterMoveRect.Top + 1);
  94. corner2 = new Vector2(afterMoveRect.Left,
  95. afterMoveRect.Bottom - 1);
  96. }
  97. else
  98. {
  99. corner1 = new Vector2(afterMoveRect.Right,
  100. afterMoveRect.Top + 1);
  101. corner2 = new Vector2(afterMoveRect.Right,
  102. afterMoveRect.Bottom - 1);
  103. }
  104. Vector2 mapCell1 = TileMap.GetCellByPixel(corner1);
  105. Vector2 mapCell2 = TileMap.GetCellByPixel(corner2);
  106. if (!TileMap.CellIsPassable(mapCell1) ||
  107. !TileMap.CellIsPassable(mapCell2))
  108. {
  109. moveAmount.X = 0;
  110. velocity.X = 0;
  111. }
  112. if (codeBasedBlocks)
  113. {
  114. if (TileMap.CellCodeValue(mapCell1) == "BLOCK" ||
  115. TileMap.CellCodeValue(mapCell2) == "BLOCK")
  116. {
  117. moveAmount.X = 0;
  118. velocity.X = 0;
  119. }
  120. }
  121. return moveAmount;
  122. }
  123. private Vector2 verticalCollisionTest(Vector2 moveAmount)
  124. {
  125. if (moveAmount.Y == 0)
  126. return moveAmount;
  127. Rectangle afterMoveRect = CollisionRectangle;
  128. afterMoveRect.Offset((int)moveAmount.X, (int)moveAmount.Y);
  129. Vector2 corner1, corner2;
  130. if (moveAmount.Y < 0)
  131. {
  132. corner1 = new Vector2(afterMoveRect.Left + 1,
  133. afterMoveRect.Top);
  134. corner2 = new Vector2(afterMoveRect.Right - 1,
  135. afterMoveRect.Top);
  136. }
  137. else
  138. {
  139. corner1 = new Vector2(afterMoveRect.Left + 1,
  140. afterMoveRect.Bottom);
  141. corner2 = new Vector2(afterMoveRect.Right - 1,
  142. afterMoveRect.Bottom);
  143. }
  144. Vector2 mapCell1 = TileMap.GetCellByPixel(corner1);
  145. Vector2 mapCell2 = TileMap.GetCellByPixel(corner2);
  146. if (!TileMap.CellIsPassable(mapCell1) ||
  147. !TileMap.CellIsPassable(mapCell2))
  148. {
  149. if (moveAmount.Y > 0)
  150. onGround = true;
  151. moveAmount.Y = 0;
  152. velocity.Y = 0;
  153. }
  154. if (codeBasedBlocks)
  155. {
  156. if (TileMap.CellCodeValue(mapCell1) == "BLOCK" ||
  157. TileMap.CellCodeValue(mapCell2) == "BLOCK")
  158. {
  159. if (moveAmount.Y > 0)
  160. onGround = true;
  161. moveAmount.Y = 0;
  162. velocity.Y = 0;
  163. }
  164. }
  165. return moveAmount;
  166. }
  167. public void PlayAnimation(string name)
  168. {
  169. if (!(name == null) && animations.ContainsKey(name))
  170. {
  171. currentAnimation = name;
  172. animations[name].Play();
  173. }
  174. }
  175. public virtual void Update(GameTime gameTime)
  176. {
  177. if (!enabled)
  178. return;
  179. float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  180. updateAnimation(gameTime);
  181. if (velocity.Y != 0)
  182. {
  183. onGround = false;
  184. }
  185. Vector2 moveAmount = velocity * elapsed;
  186. moveAmount = horizontalCollisionTest(moveAmount);
  187. moveAmount = verticalCollisionTest(moveAmount);
  188. Vector2 newPosition = worldLocation + moveAmount;
  189. newPosition = new Vector2(
  190. MathHelper.Clamp(newPosition.X, 0,
  191. Camera.WorldRectangle.Width - frameWidth),
  192. MathHelper.Clamp(newPosition.Y, 2 * (-TileMap.TileHeight),
  193. Camera.WorldRectangle.Height - frameHeight));
  194. worldLocation = newPosition;
  195. }
  196. public virtual void Draw(SpriteBatch spriteBatch)
  197. {
  198. if (!enabled)
  199. return;
  200. if (animations.ContainsKey(currentAnimation))
  201. {
  202. SpriteEffects effect = SpriteEffects.None;
  203. if (flipped)
  204. {
  205. effect = SpriteEffects.FlipHorizontally;
  206. }
  207. spriteBatch.Draw(
  208. animations[currentAnimation].Texture,
  209. Camera.WorldToScreen(WorldRectangle),
  210. animations[currentAnimation].FrameRectangle,
  211. Color.White, 0.0f, Vector2.Zero, effect, drawDepth);
  212. }
  213. }
  214. }
  215. }