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