Advertisement
Guest User

asdfd

a guest
Jun 2nd, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1.  
  2. namespace brickbreakerRewrite.Asset.Game
  3. {
  4. public enum BallState
  5. {
  6. Active,
  7. Inactive
  8. }
  9.  
  10. public class Ball : GameObj
  11. {
  12. float speed;
  13. public Vector2 direction;
  14. Court court;
  15.  
  16. public BallState CurrentState;
  17.  
  18. protected void ReverseDirX() { direction.X *= -1; }
  19. protected void ReverseDirY() { direction.Y *= -1; }
  20. protected void BounceLeft() { direction.X = -Math.Abs(direction.X); }
  21. protected void BounceRight() { direction.X = Math.Abs(direction.X); }
  22. protected void BounceUp() { direction.Y = -Math.Abs(direction.Y); }
  23. protected void BounceDown() { direction.Y = Math.Abs(direction.Y); }
  24.  
  25. public Ball(Vector2 Position, float speed, Court court): base(Position)
  26. {
  27. List<Texture2D> images = new();
  28. images.Add(GetContent.GetTexture("ball_0"));
  29. images.Add(GetContent.GetTexture("ball_1"));
  30. images.Add(GetContent.GetTexture("ball_2"));
  31. images.Add(GetContent.GetTexture("ball_3"));
  32. images.Add(GetContent.GetTexture("ball_4"));
  33. Image = images[1];
  34.  
  35. this.speed = speed;
  36. this.court = court;
  37. direction = new Vector2 (0, 1);
  38. }
  39.  
  40. public Ball(Vector2 Position, float speed, Court court, BallState startingstate): base(Position)
  41. {
  42. List<Texture2D> images = new();
  43. images.Add(GetContent.GetTexture("ball_0"));
  44. images.Add(GetContent.GetTexture("ball_1"));
  45. images.Add(GetContent.GetTexture("ball_2"));
  46. images.Add(GetContent.GetTexture("ball_3"));
  47. images.Add(GetContent.GetTexture("ball_4"));
  48. Image = images[1];
  49.  
  50. this.speed = speed;
  51. this.court = court;
  52. direction = new Vector2 (0, -1);
  53.  
  54. CurrentState = startingstate;
  55. }
  56.  
  57. protected Vector2 Velocity(float deltaTime)
  58. {
  59. Vector2 Velocity = new Vector2(speed * direction.X * deltaTime,
  60. speed * direction.Y * deltaTime);
  61. return Velocity;
  62. }
  63.  
  64. protected Vector2 ProjectPosition(float deltaTime)
  65. {
  66. Vector2 projectedPosition = (new Vector2(CollideBox.Center.X, CollideBox.Center.Y) + Velocity(deltaTime));
  67. return projectedPosition;
  68. }
  69.  
  70. private void ResolveBrickCollision(Brick brick, int damage)
  71. {
  72. brick.Weaken(damage);
  73. }
  74.  
  75. private CollidePoint GetClosestCollidePoint(float deltaTime, out float closestMagnitude)
  76. {
  77. List<CollidePoint> collidePoints = new();
  78. foreach (GameObj item in court.ObjectList)
  79. {
  80. if ((item != this) &&
  81. (GetCollisions.DoesLineIntersectRect(CollideBox.Center.X, CollideBox.Center.Y,
  82. ProjectPosition(deltaTime).X, ProjectPosition(deltaTime).Y, item.CollideBox)))
  83. {
  84. Collision collisionType = GetCollisions.LineRectCollision(CollideBox.Center.X, CollideBox.Center.Y,
  85. ProjectPosition(deltaTime).X, ProjectPosition(deltaTime).Y,
  86. item.CollideBox, out Vector2 intersectPoint);
  87. collidePoints.Add(new CollidePoint(intersectPoint, item, collisionType));
  88. }
  89. }
  90.  
  91. CollidePoint closest = null;
  92. closestMagnitude = float.MaxValue;
  93. foreach (CollidePoint point in collidePoints)
  94. {
  95. float current = ExtendedMath.Magnitude(point.Position.X - Position.X, point.Position.Y - Position.Y);
  96. if (current < closestMagnitude)
  97. {
  98. closestMagnitude = current;
  99. closest = point;
  100. }
  101. }
  102. return closest;
  103. }
  104.  
  105. protected void CheckCollisions(float deltaTime)
  106. {
  107. switch(CurrentState)
  108. {
  109. case BallState.Active:
  110. CollidePoint current = GetClosestCollidePoint(deltaTime, out var magnitudeClosest);
  111.  
  112. if (current != null)
  113. {
  114. switch (current.CollisionType)
  115. {
  116. case Collision.Left:
  117. Position.X = current.GameObj.CollideBox.Left - CollideBox.Width;
  118. Console.WriteLine("L");
  119. BounceLeft();
  120. break;
  121. case Collision.Right:
  122. Position.X = current.GameObj.CollideBox.Right;
  123. BounceRight();
  124. Console.WriteLine("R");
  125. break;
  126. case Collision.Top:
  127. Position.Y = current.GameObj.CollideBox.Top - CollideBox.Height;
  128. BounceUp();
  129. Console.WriteLine("T");
  130. break;
  131. case Collision.Bottom:
  132. Position.Y = current.GameObj.CollideBox.Bottom;
  133. BounceDown();
  134. Console.WriteLine("B");
  135. break;
  136. }
  137. if (current.GameObj is Paddle)
  138. {
  139. direction.X = (CollideBox.Center.X - current.GameObj.CollideBox.Center.X) / 15f;
  140. direction.Normalize();
  141. }
  142. if (current.GameObj is Brick)
  143. {
  144. ResolveBrickCollision((Brick)current.GameObj, 1);
  145. }
  146. float udt = deltaTime * (magnitudeClosest / ExtendedMath.Magnitude(ProjectPosition(deltaTime).X, ProjectPosition(deltaTime).Y));
  147. Update(deltaTime - udt);
  148. }
  149. break;
  150. }
  151. }
  152.  
  153. protected void Move(float deltaTime)
  154. {
  155. switch (CurrentState)
  156. {
  157. case BallState.Active:
  158. Position += Velocity(deltaTime);
  159. break;
  160. case BallState.Inactive:
  161. Position = new Vector2(court.Player.Position.X + (court.Player.CollideBox.Width / 2) - CollideBox.Width / 2, court.Player.CollideBox.Y - 6);
  162. break;
  163. }
  164. }
  165.  
  166. public override void Update(float deltaTime)
  167. {
  168. ProjectPosition(deltaTime);
  169. CheckCollisions(deltaTime);
  170. Move(deltaTime);
  171. if(Position.Y > 260)
  172. court.KillList.Add(this);
  173. }
  174. }
  175. }
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement