PlayerPosition.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // PlayerPosition.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework;
  12. using RolePlayingGameData;
  13. #endregion
  14. namespace RolePlaying
  15. {
  16. /// <summary>
  17. /// The position of a player in the tile engine.
  18. /// </summary>
  19. /// <remarks>Players are the only objects that move between tiles.</remarks>
  20. public class PlayerPosition
  21. {
  22. #region Position
  23. /// <summary>
  24. /// Position in map coordinates (tiles).
  25. /// </summary>
  26. public Point TilePosition = Point.Zero;
  27. /// <summary>
  28. /// The offset into the tile, in pixels.
  29. /// </summary>
  30. public Vector2 TileOffset = Vector2.Zero;
  31. /// <summary>
  32. /// The position in screen coordinates.
  33. /// </summary>
  34. public Vector2 ScreenPosition
  35. {
  36. get
  37. {
  38. return TileEngine.GetScreenPosition(TilePosition) + TileOffset;
  39. }
  40. }
  41. #endregion
  42. #region Direction
  43. /// <summary>
  44. /// The direction that the player is facing.
  45. /// </summary>
  46. public Direction Direction = Direction.South;
  47. #endregion
  48. #region Movement
  49. /// <summary>
  50. /// If true, the position moved on the last update.
  51. /// </summary>
  52. /// <remarks>Used to control animation.</remarks>
  53. private bool isMoving = false;
  54. /// <summary>
  55. /// If true, the position moved on the last update.
  56. /// </summary>
  57. /// <remarks>Used to control animation.</remarks>
  58. public bool IsMoving
  59. {
  60. get { return isMoving; }
  61. }
  62. /// <summary>
  63. /// Move the player by the given amount.
  64. /// </summary>
  65. public void Move(Vector2 movement)
  66. {
  67. isMoving = (movement != Vector2.Zero);
  68. CalculateMovement(movement, ref TilePosition, ref TileOffset);
  69. // if the position is moving, up the direction
  70. if (IsMoving)
  71. {
  72. Direction = CalculateDirection(movement);
  73. }
  74. }
  75. #endregion
  76. #region Calculation
  77. /// <summary>
  78. /// Calculates the effect of movement on the position.
  79. /// </summary>
  80. /// <param name="movement">
  81. /// The movement to be used to calculate the new tile position.
  82. /// </param>
  83. /// <param name="TileOffset">The map position (tiles).</param>
  84. /// <param name="TilePosition">The offset into the current tile.</param>
  85. public static void CalculateMovement(Vector2 movement, ref Point TilePosition,
  86. ref Vector2 TileOffset)
  87. {
  88. // add the movement
  89. TileOffset += movement;
  90. while (TileOffset.X > TileEngine.Map.TileSize.X / 2f)
  91. {
  92. TilePosition.X++;
  93. TileOffset.X -= TileEngine.Map.TileSize.X;
  94. }
  95. while (TileOffset.X < -TileEngine.Map.TileSize.X / 2f)
  96. {
  97. TilePosition.X--;
  98. TileOffset.X += TileEngine.Map.TileSize.X;
  99. }
  100. while (TileOffset.Y > TileEngine.Map.TileSize.Y / 2f)
  101. {
  102. TilePosition.Y++;
  103. TileOffset.Y -= TileEngine.Map.TileSize.Y;
  104. }
  105. while (TileOffset.Y < -TileEngine.Map.TileSize.Y / 2f)
  106. {
  107. TilePosition.Y--;
  108. TileOffset.Y += TileEngine.Map.TileSize.Y;
  109. }
  110. }
  111. /// <summary>
  112. /// Determine the direction based on the given movement vector.
  113. /// </summary>
  114. /// <param name="vector">The vector that the player is moving.</param>
  115. /// <returns>The calculated direction.</returns>
  116. public static Direction CalculateDirection(Vector2 vector)
  117. {
  118. if (vector.X > 0)
  119. {
  120. if (vector.Y > 0)
  121. {
  122. return Direction.SouthEast;
  123. }
  124. else if (vector.Y < 0)
  125. {
  126. return Direction.NorthEast;
  127. }
  128. else // y == 0
  129. {
  130. return Direction.East;
  131. }
  132. }
  133. else if (vector.X < 0)
  134. {
  135. if (vector.Y > 0)
  136. {
  137. return Direction.SouthWest;
  138. }
  139. else if (vector.Y < 0)
  140. {
  141. return Direction.NorthWest;
  142. }
  143. else // y == 0
  144. {
  145. return Direction.West;
  146. }
  147. }
  148. else // x == 0
  149. {
  150. if (vector.Y > 0)
  151. {
  152. return Direction.South;
  153. }
  154. else if (vector.Y < 0)
  155. {
  156. return Direction.North;
  157. }
  158. }
  159. // x == 0 && y == 0, so... south?
  160. return Direction.South;
  161. }
  162. #endregion
  163. }
  164. }