Enemy.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.Content;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using Tile_Engine;
  9. namespace Gemstone_Hunter
  10. {
  11. public class Enemy : GameObject
  12. {
  13. private Vector2 fallSpeed = new Vector2(0, 20);
  14. private float walkSpeed = 60.0f;
  15. private bool facingLeft = true;
  16. public bool Dead = false;
  17. #region Constructor
  18. public Enemy(ContentManager content, int cellX, int cellY)
  19. {
  20. animations.Add("idle",
  21. new AnimationStrip(
  22. content.Load<Texture2D>(
  23. @"Textures\Sprites\MonsterC\Idle"),
  24. 48,
  25. "idle"));
  26. animations["idle"].LoopAnimation = true;
  27. animations.Add("run",
  28. new AnimationStrip(
  29. content.Load<Texture2D>(
  30. @"Textures\Sprites\MonsterC\Run"),
  31. 48,
  32. "run"));
  33. animations["run"].FrameLength = 0.1f;
  34. animations["run"].LoopAnimation = true;
  35. animations.Add("die",
  36. new AnimationStrip(
  37. content.Load<Texture2D>(
  38. @"Textures\Sprites\MonsterC\Die"),
  39. 48,
  40. "die"));
  41. animations["die"].LoopAnimation = false;
  42. frameWidth = 48;
  43. frameHeight = 48;
  44. CollisionRectangle = new Rectangle(9, 1, 30, 46);
  45. worldLocation = new Vector2(
  46. cellX * TileMap.TileWidth,
  47. cellY * TileMap.TileHeight);
  48. enabled = true;
  49. codeBasedBlocks = true;
  50. PlayAnimation("run");
  51. }
  52. #endregion
  53. #region Public Methods
  54. public override void Update(GameTime gameTime)
  55. {
  56. Vector2 oldLocation = worldLocation;
  57. if (!Dead)
  58. {
  59. velocity = new Vector2(0, velocity.Y);
  60. Vector2 direction = new Vector2(1, 0);
  61. flipped = true;
  62. if (facingLeft)
  63. {
  64. direction = new Vector2(-1, 0);
  65. flipped = false;
  66. }
  67. direction *= walkSpeed;
  68. velocity += direction;
  69. velocity += fallSpeed;
  70. }
  71. base.Update(gameTime);
  72. if (!Dead)
  73. {
  74. if (oldLocation == worldLocation)
  75. {
  76. facingLeft = !facingLeft;
  77. }
  78. }
  79. else
  80. {
  81. if (animations[currentAnimation].FinishedPlaying)
  82. {
  83. enabled = false;
  84. }
  85. }
  86. }
  87. #endregion
  88. }
  89. }