/Assets/Scripts/Game/MovingAction.cs

https://bitbucket.org/gorkasg/boxoban · C# · 169 lines · 61 code · 40 blank · 68 comment · 2 complexity · 639c65ab9b23a24381281147e5e41c22 MD5 · raw file

  1. //******************************************************************************************
  2. // Boxoban: A retro pixel-art puzzle 2D game made with Unity 4.5
  3. // Copyright (C) 2015 Gorka Suárez García
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //******************************************************************************************
  18. using UnityEngine;
  19. /// <summary>
  20. /// This class represents a moving action donne by an entity in the world.
  21. /// </summary>
  22. public class MovingAction {
  23. //--------------------------------------------------------------------------------------
  24. // Constants:
  25. //--------------------------------------------------------------------------------------
  26. #region Constants
  27. public const float MAX_TIME = 0.2f;
  28. #endregion
  29. //--------------------------------------------------------------------------------------
  30. // Properties:
  31. //--------------------------------------------------------------------------------------
  32. #region EntityController entity
  33. /// <summary>
  34. /// The entity controller.
  35. /// </summary>
  36. private EntityController entity;
  37. #endregion
  38. #region CellData destinationCell
  39. /// <summary>
  40. /// The destination cell.
  41. /// </summary>
  42. private CellData destinationCell;
  43. #endregion
  44. #region Vector3 originPoint
  45. /// <summary>
  46. /// The origin point.
  47. /// </summary>
  48. private Vector3 originPoint;
  49. #endregion
  50. #region Vector3 destinationPoint
  51. /// <summary>
  52. /// The destination point.
  53. /// </summary>
  54. private Vector3 destinationPoint;
  55. #endregion
  56. #region Vector3 offsetVector
  57. /// <summary>
  58. /// The offset vector.
  59. /// </summary>
  60. private Vector3 offsetVector;
  61. #endregion
  62. #region float currentTime
  63. /// <summary>
  64. /// The current time in the movement.
  65. /// </summary>
  66. private float currentTime;
  67. #endregion
  68. #region bool Finished
  69. /// <summary>
  70. /// Gets if the movement is finished or not.
  71. /// </summary>
  72. public bool Finished {
  73. get { return currentTime >= MAX_TIME; }
  74. }
  75. #endregion
  76. #region MoveDirection Direction
  77. /// <summary>
  78. /// Gets the movement direction.
  79. /// </summary>
  80. public MoveDirection Direction { get; private set; }
  81. #endregion
  82. //--------------------------------------------------------------------------------------
  83. // Methods:
  84. //--------------------------------------------------------------------------------------
  85. #region void Update()
  86. /// <summary>
  87. /// Updates the action.
  88. /// </summary>
  89. public void Update() {
  90. if (!Finished) {
  91. currentTime += Time.deltaTime;
  92. var delta = currentTime / MAX_TIME;
  93. var deltaVector = offsetVector * delta;
  94. var nextPoint = originPoint + deltaVector;
  95. SceneUtil.SetPosition(entity.gameObject, ref nextPoint);
  96. } else {
  97. SceneUtil.SetPosition(entity.gameObject, ref destinationPoint);
  98. }
  99. }
  100. #endregion
  101. #region void Finish()
  102. /// <summary>
  103. /// Finishes the action.
  104. /// </summary>
  105. public void Finish() {
  106. entity.EnterCell(destinationCell);
  107. }
  108. #endregion
  109. //--------------------------------------------------------------------------------------
  110. // Constructors:
  111. //--------------------------------------------------------------------------------------
  112. #region MovingAction(EntityController, CellData, MoveDirection)
  113. /// <summary>
  114. /// Constructs a new object.
  115. /// </summary>
  116. /// <param name="controller">The entity controller.</param>
  117. /// <param name="destination">The destination cell.</param>
  118. /// <param name="direction">The movement direction.</param>
  119. public MovingAction(EntityController controller, CellData destination, MoveDirection direction) {
  120. entity = controller;
  121. destinationCell = destination;
  122. originPoint = entity.transform.localPosition;
  123. destinationPoint = destinationCell.TerrainObject.transform.localPosition;
  124. offsetVector = destinationPoint - originPoint;
  125. currentTime = 0.0f;
  126. Direction = direction;
  127. }
  128. #endregion
  129. }