PlayerPosition.cs 5.0 KB

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