Mover.as 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Mover script object class
  2. // - handles entity (enemy, platform...) translation along a path (set of Vector2 points)
  3. // - supports looping paths and animation flip
  4. // - default speed is 0.8 if 'Speed' property is not set in the tmx file objects
  5. class Mover : ScriptObject
  6. {
  7. float speed = 0.8f;
  8. Array<Vector2> path;
  9. int currentPathID = 1;
  10. float emitTime = 0.0f;
  11. float fightTimer = 0.0f;
  12. float flip = 0.0f;
  13. uint bufferSize = 0;
  14. void Load(Deserializer& deserializer)
  15. {
  16. bufferSize = deserializer.ReadUInt(); // Get buffer size
  17. SetPathAttr(deserializer.ReadVectorBuffer(bufferSize));
  18. }
  19. void Save(Serializer& serializer)
  20. {
  21. serializer.WriteVectorBuffer(GetPathAttr(serializer));
  22. }
  23. void SetPathAttr(VectorBuffer buffer)
  24. {
  25. if (buffer.size == 0)
  26. return;
  27. while (!buffer.eof)
  28. path.Push(buffer.ReadVector2());
  29. }
  30. VectorBuffer GetPathAttr(Serializer& serializer)
  31. {
  32. VectorBuffer buffer = VectorBuffer();
  33. for (uint i=0; i < path.length; ++i)
  34. buffer.WriteVector2(path[i]);
  35. bufferSize = buffer.size;
  36. serializer.WriteUInt(bufferSize);
  37. return buffer;
  38. }
  39. void Update(float timeStep)
  40. {
  41. if (path.length < 2)
  42. return;
  43. // Handle Orc states (idle/wounded/fighting)
  44. if (node.name == "Orc")
  45. {
  46. AnimatedSprite2D@ animatedSprite = node.GetComponent("AnimatedSprite2D");
  47. String anim = "run";
  48. // Handle wounded state
  49. if (emitTime > 0.0f)
  50. {
  51. emitTime += timeStep;
  52. anim = "dead";
  53. // Handle dead
  54. if (emitTime >= 3.0f)
  55. {
  56. node.Remove();
  57. return;
  58. }
  59. }
  60. else
  61. {
  62. // Handle fighting state
  63. if (fightTimer > 0.0f)
  64. {
  65. anim = "attack";
  66. flip = character2DNode.position.x - node.position.x;
  67. fightTimer += timeStep;
  68. if (fightTimer >= 3.0f)
  69. fightTimer = 0.0f; // Reset
  70. }
  71. // Flip Orc animation according to speed, or player position when fighting
  72. animatedSprite.flipX = flip >= 0.0f;
  73. }
  74. // Animate
  75. if (animatedSprite.animation != anim)
  76. animatedSprite.SetAnimation(anim);
  77. }
  78. // Don't move if fighting or wounded
  79. if (fightTimer > 0.0f || emitTime > 0.0f)
  80. return;
  81. // Set direction and move to target
  82. Vector2 dir = path[currentPathID] - node.position2D;
  83. Vector2 dirNormal = dir.Normalized();
  84. node.Translate(Vector3(dirNormal.x, dirNormal.y, 0.0f) * Abs(speed) * timeStep);
  85. flip = dir.x;
  86. // Check for new target to reach
  87. if (Abs(dir.length) < 0.1f)
  88. {
  89. if (speed > 0.0f)
  90. {
  91. if (currentPathID + 1 < path.length)
  92. currentPathID = currentPathID + 1;
  93. else
  94. {
  95. // If loop, go to first waypoint, which equates to last one (and never reverse)
  96. if (path[currentPathID] == path[0])
  97. {
  98. currentPathID = 1;
  99. return;
  100. }
  101. // Reverse path if not looping
  102. currentPathID = currentPathID - 1;
  103. speed = -speed;
  104. }
  105. }
  106. else
  107. {
  108. if (currentPathID - 1 >= 0)
  109. currentPathID = currentPathID - 1;
  110. else
  111. {
  112. currentPathID = 1;
  113. speed = -speed;
  114. }
  115. }
  116. }
  117. }
  118. }