PlayerSprite.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using Godot;
  2. #if REAL_T_IS_DOUBLE
  3. using real_t = System.Double;
  4. #else
  5. using real_t = System.Single;
  6. #endif
  7. [Tool]
  8. public partial class PlayerSprite : Sprite2D
  9. {
  10. private static Texture2D _stand = ResourceLoader.Load<Texture2D>("res://assets/player/textures/stand.png");
  11. private static Texture2D _jump = ResourceLoader.Load<Texture2D>("res://assets/player/textures/jump.png");
  12. private static Texture2D _run = ResourceLoader.Load<Texture2D>("res://assets/player/textures/run.png");
  13. private const int FRAMERATE = 15;
  14. private int _direction;
  15. private float _progress;
  16. private Node25D _parent;
  17. private PlayerMath25D _parentMath;
  18. public override void _Ready()
  19. {
  20. _parent = GetParent<Node25D>();
  21. _parentMath = _parent.GetChild<PlayerMath25D>(0);
  22. }
  23. public override void _Process(real_t delta)
  24. {
  25. if (Engine.EditorHint)
  26. {
  27. return; // Don't run this in the editor.
  28. }
  29. SpriteBasis();
  30. bool movement = CheckMovement(); // Always run to get direction, but don't always use return bool.
  31. // Test-only move and collide, check if the player is on the ground.
  32. var k = _parentMath.MoveAndCollide(Vector3.Down * 10 * delta, true, true, true);
  33. if (k != null)
  34. {
  35. if (movement)
  36. {
  37. // TODO: https://github.com/godotengine/godot/issues/28748
  38. Hframes = 6;
  39. Texture = _run;
  40. if (Input.IsActionPressed("movement_modifier"))
  41. {
  42. delta /= 2;
  43. }
  44. _progress = (_progress + FRAMERATE * delta) % 6;
  45. Frame = _direction * 6 + (int)_progress;
  46. }
  47. else
  48. {
  49. Hframes = 1;
  50. Texture = _stand;
  51. _progress = 0;
  52. Frame = _direction;
  53. }
  54. }
  55. else
  56. {
  57. Hframes = 2;
  58. Texture = _jump;
  59. _progress = 0;
  60. int jumping = _parentMath.verticalSpeed < 0 ? 1 : 0;
  61. Frame = _direction * 2 + jumping;
  62. }
  63. }
  64. public void SetViewMode(int viewModeIndex)
  65. {
  66. Transform2D t = Transform;
  67. switch (viewModeIndex)
  68. {
  69. case 0:
  70. t.x = new Vector2(1, 0);
  71. t.y = new Vector2(0, 0.75f);
  72. break;
  73. case 1:
  74. t.x = new Vector2(1, 0);
  75. t.y = new Vector2(0, 1);
  76. break;
  77. case 2:
  78. t.x = new Vector2(1, 0);
  79. t.y = new Vector2(0, 0.5f);
  80. break;
  81. case 3:
  82. t.x = new Vector2(1, 0);
  83. t.y = new Vector2(0, 1);
  84. break;
  85. case 4:
  86. t.x = new Vector2(1, 0);
  87. t.y = new Vector2(0.75f, 0.75f);
  88. break;
  89. case 5:
  90. t.x = new Vector2(1, 0.25f);
  91. t.y = new Vector2(0, 1);
  92. break;
  93. }
  94. Transform = t;
  95. }
  96. /// <summary>
  97. /// Change the basis of the sprite to try and make it fit multiple view modes.
  98. /// </summary>
  99. private void SpriteBasis()
  100. {
  101. if (!Engine.EditorHint)
  102. {
  103. if (Input.IsActionPressed("forty_five_mode"))
  104. {
  105. SetViewMode(0);
  106. }
  107. else if (Input.IsActionPressed("isometric_mode"))
  108. {
  109. SetViewMode(1);
  110. }
  111. else if (Input.IsActionPressed("top_down_mode"))
  112. {
  113. SetViewMode(2);
  114. }
  115. else if (Input.IsActionPressed("front_side_mode"))
  116. {
  117. SetViewMode(3);
  118. }
  119. else if (Input.IsActionPressed("oblique_y_mode"))
  120. {
  121. SetViewMode(4);
  122. }
  123. else if (Input.IsActionPressed("oblique_z_mode"))
  124. {
  125. SetViewMode(5);
  126. }
  127. }
  128. }
  129. // There might be a more efficient way to do this, but I can't think of it.
  130. private bool CheckMovement()
  131. {
  132. // Gather player input and store movement to these int variables. Note: These indeed have to be integers.
  133. int x = 0;
  134. int z = 0;
  135. if (Input.IsActionPressed("move_right"))
  136. {
  137. x++;
  138. }
  139. if (Input.IsActionPressed("move_left"))
  140. {
  141. x--;
  142. }
  143. if (Input.IsActionPressed("move_forward"))
  144. {
  145. z--;
  146. }
  147. if (Input.IsActionPressed("move_back"))
  148. {
  149. z++;
  150. }
  151. // Check for isometric controls and add more to movement accordingly.
  152. // For efficiency, only check the X axis since this X axis value isn't used anywhere else.
  153. if (!_parentMath.isometricControls && _parent.Basis25D.x.IsEqualApprox(Basis25D.Isometric.x * Node25D.SCALE))
  154. {
  155. if (Input.IsActionPressed("move_right"))
  156. {
  157. z++;
  158. }
  159. if (Input.IsActionPressed("move_left"))
  160. {
  161. z--;
  162. }
  163. if (Input.IsActionPressed("move_forward"))
  164. {
  165. x++;
  166. }
  167. if (Input.IsActionPressed("move_back"))
  168. {
  169. x--;
  170. }
  171. }
  172. // Set the direction based on which inputs were pressed.
  173. if (x == 0)
  174. {
  175. if (z == 0)
  176. {
  177. return false; // No movement
  178. }
  179. else if (z > 0)
  180. {
  181. _direction = 0;
  182. }
  183. else
  184. {
  185. _direction = 4;
  186. }
  187. }
  188. else if (x > 0)
  189. {
  190. if (z == 0)
  191. {
  192. _direction = 2;
  193. FlipH = true;
  194. }
  195. else if (z > 0)
  196. {
  197. _direction = 1;
  198. FlipH = true;
  199. }
  200. else
  201. {
  202. _direction = 3;
  203. FlipH = true;
  204. }
  205. }
  206. else
  207. {
  208. if (z == 0)
  209. {
  210. _direction = 2;
  211. FlipH = false;
  212. }
  213. else if (z > 0)
  214. {
  215. _direction = 1;
  216. FlipH = false;
  217. }
  218. else
  219. {
  220. _direction = 3;
  221. FlipH = false;
  222. }
  223. }
  224. return true; // There is movement
  225. }
  226. }