Mover.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Urho2D/AnimatedSprite2D.h>
  23. #include <Urho3D/Urho2D/AnimationSet2D.h>
  24. #include <Urho3D/Core/Context.h>
  25. #include <Urho3D/IO/MemoryBuffer.h>
  26. #include <Urho3D/Scene/Scene.h>
  27. #include <Urho3D/Scene/SceneEvents.h>
  28. #include <Urho3D/DebugNew.h>
  29. #include "Mover.h"
  30. Mover::Mover(Context* context) :
  31. LogicComponent(context),
  32. speed_(0.8f),
  33. currentPathID_(1),
  34. emitTime_(0.0f),
  35. fightTimer_(0.0f),
  36. flip_(0.0f)
  37. {
  38. // Only the scene update event is needed: unsubscribe from the rest for optimization
  39. SetUpdateEventMask(USE_UPDATE);
  40. }
  41. void Mover::RegisterObject(Context* context)
  42. {
  43. context->RegisterFactory<Mover>();
  44. // These macros register the class attribute to the Context for automatic load / save handling.
  45. // We specify the Default attribute mode which means it will be used both for saving into file, and network replication.
  46. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Path", GetPathAttr, SetPathAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_DEFAULT);
  47. URHO3D_ATTRIBUTE("Speed", float, speed_, 0.8f, AM_DEFAULT);
  48. URHO3D_ATTRIBUTE("Current Path ID", int, currentPathID_, 1, AM_DEFAULT);
  49. URHO3D_ATTRIBUTE("Emit Time", float, emitTime_, 0.0f, AM_DEFAULT);
  50. URHO3D_ATTRIBUTE("Fight Timer", float, fightTimer_, 0.0f, AM_DEFAULT);
  51. URHO3D_ATTRIBUTE("Flip Animation", float, flip_, 0.0f, AM_DEFAULT);
  52. }
  53. void Mover::SetPathAttr(const PODVector<unsigned char>& value)
  54. {
  55. if (value.Empty())
  56. return;
  57. MemoryBuffer buffer(value);
  58. while (!buffer.IsEof())
  59. path_.Push(buffer.ReadVector2());
  60. }
  61. PODVector<unsigned char> Mover::GetPathAttr() const
  62. {
  63. VectorBuffer buffer;
  64. for (unsigned i = 0; i < path_.Size(); ++i)
  65. buffer.WriteVector2(path_[i]);
  66. return buffer.GetBuffer();
  67. }
  68. void Mover::Update(float timeStep)
  69. {
  70. if (path_.Size() < 2)
  71. return;
  72. // Handle Orc states (idle/wounded/fighting)
  73. if (node_->GetName() == "Orc")
  74. {
  75. auto* animatedSprite = node_->GetComponent<AnimatedSprite2D>();
  76. String anim = "run";
  77. // Handle wounded state
  78. if (emitTime_ > 0.0f)
  79. {
  80. emitTime_ += timeStep;
  81. anim = "dead";
  82. // Handle dead
  83. if (emitTime_ >= 3.0f)
  84. {
  85. node_->Remove();
  86. return;
  87. }
  88. }
  89. else
  90. {
  91. // Handle fighting state
  92. if (fightTimer_ > 0.0f)
  93. {
  94. anim = "attack";
  95. flip_ = GetScene()->GetChild("Imp", true)->GetPosition().x_ - node_->GetPosition().x_;
  96. fightTimer_ += timeStep;
  97. if (fightTimer_ >= 3.0f)
  98. fightTimer_ = 0.0f; // Reset
  99. }
  100. // Flip Orc animation according to speed, or player position when fighting
  101. animatedSprite->SetFlipX(flip_ >= 0.0f);
  102. }
  103. // Animate
  104. if (animatedSprite->GetAnimation() != anim)
  105. animatedSprite->SetAnimation(anim);
  106. }
  107. // Don't move if fighting or wounded
  108. if (fightTimer_ > 0.0f || emitTime_ > 0.0f)
  109. return;
  110. // Set direction and move to target
  111. Vector2 dir = path_[currentPathID_] - node_->GetPosition2D();
  112. Vector2 dirNormal = dir.Normalized();
  113. node_->Translate(Vector3(dirNormal.x_, dirNormal.y_, 0.0f) * Abs(speed_) * timeStep);
  114. flip_ = dir.x_;
  115. // Check for new target to reach
  116. if (Abs(dir.Length()) < 0.1f)
  117. {
  118. if (speed_ > 0.0f)
  119. {
  120. if (currentPathID_ + 1 < path_.Size())
  121. currentPathID_ = currentPathID_ + 1;
  122. else
  123. {
  124. // If loop, go to first waypoint, which equates to last one (and never reverse)
  125. if (path_[currentPathID_] == path_[0])
  126. {
  127. currentPathID_ = 1;
  128. return;
  129. }
  130. // Reverse path if not looping
  131. currentPathID_ = currentPathID_ - 1;
  132. speed_ = -speed_;
  133. }
  134. }
  135. else
  136. {
  137. if (currentPathID_ - 1 >= 0)
  138. currentPathID_ = currentPathID_ - 1;
  139. else
  140. {
  141. currentPathID_ = 1;
  142. speed_ = -speed_;
  143. }
  144. }
  145. }
  146. }