Player.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using Microsoft.Xna.Framework.Input;
  9. using Tutorial030.Models;
  10. namespace Tutorial030.Sprites
  11. {
  12. public class Player : Sprite
  13. {
  14. private KeyboardState _previousKey;
  15. private KeyboardState _currentKey;
  16. private bool _isOnGround = false;
  17. private bool _jumping = false;
  18. public Vector2 Velocity;
  19. /// <summary>
  20. /// These are the types of attributes to only change on level-up
  21. /// </summary>
  22. public Attributes BaseAttributes { get; set; }
  23. /// <summary>
  24. /// These are extra attributes that can be gained from different sources (equipment, power-ups, spells etc)
  25. /// </summary>
  26. public List<Attributes> AttributeModifiers { get; set; }
  27. public Attributes TotalAttributes
  28. {
  29. get
  30. {
  31. return BaseAttributes + AttributeModifiers.Sum();
  32. }
  33. }
  34. public Player(Dictionary<string, Animation> animations) : base(animations)
  35. {
  36. BaseAttributes = new Attributes();
  37. AttributeModifiers = new List<Attributes>();
  38. }
  39. public override void Update(GameTime gameTime)
  40. {
  41. _previousKey = _currentKey;
  42. _currentKey = Keyboard.GetState();
  43. Velocity.X = TotalAttributes.Speed;
  44. if (Velocity.Y >= 0)
  45. _jumping = false;
  46. if (_isOnGround)
  47. {
  48. if (_previousKey.IsKeyUp(Keys.Space) && _currentKey.IsKeyDown(Keys.Space))
  49. {
  50. Velocity.Y = -12f;
  51. _jumping = true;
  52. }
  53. }
  54. else
  55. {
  56. Velocity.Y += 0.50f;
  57. }
  58. SetAnimation();
  59. _animationManager.Update(gameTime);
  60. _isOnGround = false;
  61. }
  62. public void ApplyVelocity(GameTime gameTime)
  63. {
  64. //Position = new Vector2(Position.X, Position.Y + Velocity.Y);
  65. this.Y += Velocity.Y;
  66. }
  67. private void SetAnimation()
  68. {
  69. if (Velocity.Y < 0)
  70. {
  71. _animationManager.Play(_animations["Jumping"]);
  72. }
  73. else if (Velocity.Y > 0)
  74. {
  75. _animationManager.Play(_animations["Falling"]);
  76. }
  77. else
  78. {
  79. _animationManager.Play(_animations["Running"]);
  80. }
  81. }
  82. public override void OnCollide(Sprite sprite)
  83. {
  84. {
  85. var resultP1 = GetMinMax(this.Dots, GetNormals()[1]);
  86. var resultP2 = GetMinMax(sprite.Dots, GetNormals()[1]);
  87. var resultQ1 = GetMinMax(this.Dots, GetNormals()[0]);
  88. var resultQ2 = GetMinMax(sprite.Dots, GetNormals()[0]);
  89. var resultR1 = GetMinMax(this.Dots, sprite.GetNormals()[1]);
  90. var resultR2 = GetMinMax(sprite.Dots, sprite.GetNormals()[1]);
  91. var resultS1 = GetMinMax(this.Dots, sprite.GetNormals()[0]);
  92. var resultS2 = GetMinMax(sprite.Dots, sprite.GetNormals()[0]);
  93. float p1Min = resultP1[0];
  94. float p1Max = resultP1[1];
  95. float p2Min = resultP2[0];
  96. float p2Max = resultP2[1];
  97. float q1Min = resultQ1[0];
  98. float q1Max = resultQ1[1];
  99. float q2Min = resultQ2[0];
  100. float q2Max = resultQ2[1];
  101. float r1Min = resultR1[0];
  102. float r1Max = resultR1[1];
  103. float r2Min = resultR2[0];
  104. float r2Max = resultR2[1];
  105. float s1Min = resultS1[0];
  106. float s1Max = resultS1[1];
  107. float s2Min = resultS2[0];
  108. float s2Max = resultS2[1];
  109. var separate_P = p1Max < p2Min || p2Max < p1Min;
  110. var separate_Q = q1Max < q2Min || q2Max < q1Min;
  111. var separate_R = r1Max < r2Min || r2Max < r1Min;
  112. var separate_S = s1Max < s2Min || s2Max < s1Min;
  113. var isSeperated = separate_P || separate_Q || separate_R || separate_S;
  114. if (isSeperated)
  115. {
  116. }
  117. else
  118. {
  119. }
  120. }
  121. var test = sprite.Centre - (this.Centre);// + new Vector2(10, 25));
  122. var rotation = (float)Math.Atan2(test.Y, test.X);
  123. var rotation2 = Math.Abs(MathHelper.ToDegrees(rotation));
  124. if (rotation2 > 89 || rotation2 < 91)
  125. {
  126. }
  127. if(sprite.Y == 640)
  128. {
  129. }
  130. bool onLeft = false;
  131. bool onRight = false;
  132. bool onTop = false;
  133. bool onBotton = false;
  134. int index = 0;
  135. for (int i = -45; i <= 315; i += 90)
  136. {
  137. if (rotation2 >= i && rotation2 < i + 90)
  138. {
  139. switch (index)
  140. {
  141. case 0:
  142. onLeft = true;
  143. break;
  144. case 1:
  145. onTop = true;
  146. break;
  147. case 2:
  148. onRight = true;
  149. break;
  150. case 3:
  151. onBotton = true;
  152. break;
  153. default:
  154. break;
  155. }
  156. }
  157. index++;
  158. }
  159. switch (sprite)
  160. {
  161. case Platform platform:
  162. if (onLeft)
  163. {
  164. this.X = platform.Rectangle.Left - this.Rectangle.Width;
  165. }
  166. if (onTop)
  167. {
  168. if (!_jumping)
  169. {
  170. this._position.Y = platform.Rectangle.Top - this.Rectangle.Height;
  171. Velocity.Y = 0;
  172. _isOnGround = true;
  173. }
  174. }
  175. break;
  176. case PowerUp powerUp:
  177. break;
  178. default:
  179. throw new Exception("Unexpected sprite: " + sprite.ToString());
  180. }
  181. }
  182. private List<float> GetMinMax(List<Vector2> dots, Vector2 axis)
  183. {
  184. var minProj = Vector2.Dot(dots[1], axis);
  185. var maxProj = Vector2.Dot(dots[1], axis);
  186. var minDot = 1;
  187. var maxDot = 1;
  188. for (int i = 2; i < dots.Count; i++)
  189. {
  190. var currProj = Vector2.Dot(dots[i], axis);
  191. if (minProj > currProj)
  192. {
  193. minProj = currProj;
  194. minDot = i;
  195. }
  196. if (currProj > maxProj)
  197. {
  198. maxProj = currProj;
  199. maxDot = i;
  200. }
  201. }
  202. return new List<float>()
  203. {
  204. minProj,
  205. maxProj,
  206. };
  207. }
  208. }
  209. }