Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace brickbreakerRewrite.Asset.Game
- {
- public enum BallState
- {
- Active,
- Inactive
- }
- public class Ball : GameObj
- {
- float speed;
- public Vector2 direction;
- Court court;
- public BallState CurrentState;
- protected void ReverseDirX() { direction.X *= -1; }
- protected void ReverseDirY() { direction.Y *= -1; }
- protected void BounceLeft() { direction.X = -Math.Abs(direction.X); }
- protected void BounceRight() { direction.X = Math.Abs(direction.X); }
- protected void BounceUp() { direction.Y = -Math.Abs(direction.Y); }
- protected void BounceDown() { direction.Y = Math.Abs(direction.Y); }
- public Ball(Vector2 Position, float speed, Court court): base(Position)
- {
- List<Texture2D> images = new();
- images.Add(GetContent.GetTexture("ball_0"));
- images.Add(GetContent.GetTexture("ball_1"));
- images.Add(GetContent.GetTexture("ball_2"));
- images.Add(GetContent.GetTexture("ball_3"));
- images.Add(GetContent.GetTexture("ball_4"));
- Image = images[1];
- this.speed = speed;
- this.court = court;
- direction = new Vector2 (0, 1);
- }
- public Ball(Vector2 Position, float speed, Court court, BallState startingstate): base(Position)
- {
- List<Texture2D> images = new();
- images.Add(GetContent.GetTexture("ball_0"));
- images.Add(GetContent.GetTexture("ball_1"));
- images.Add(GetContent.GetTexture("ball_2"));
- images.Add(GetContent.GetTexture("ball_3"));
- images.Add(GetContent.GetTexture("ball_4"));
- Image = images[1];
- this.speed = speed;
- this.court = court;
- direction = new Vector2 (0, -1);
- CurrentState = startingstate;
- }
- protected Vector2 Velocity(float deltaTime)
- {
- Vector2 Velocity = new Vector2(speed * direction.X * deltaTime,
- speed * direction.Y * deltaTime);
- return Velocity;
- }
- protected Vector2 ProjectPosition(float deltaTime)
- {
- Vector2 projectedPosition = (new Vector2(CollideBox.Center.X, CollideBox.Center.Y) + Velocity(deltaTime));
- return projectedPosition;
- }
- private void ResolveBrickCollision(Brick brick, int damage)
- {
- brick.Weaken(damage);
- }
- private CollidePoint GetClosestCollidePoint(float deltaTime, out float closestMagnitude)
- {
- List<CollidePoint> collidePoints = new();
- foreach (GameObj item in court.ObjectList)
- {
- if ((item != this) &&
- (GetCollisions.DoesLineIntersectRect(CollideBox.Center.X, CollideBox.Center.Y,
- ProjectPosition(deltaTime).X, ProjectPosition(deltaTime).Y, item.CollideBox)))
- {
- Collision collisionType = GetCollisions.LineRectCollision(CollideBox.Center.X, CollideBox.Center.Y,
- ProjectPosition(deltaTime).X, ProjectPosition(deltaTime).Y,
- item.CollideBox, out Vector2 intersectPoint);
- collidePoints.Add(new CollidePoint(intersectPoint, item, collisionType));
- }
- }
- CollidePoint closest = null;
- closestMagnitude = float.MaxValue;
- foreach (CollidePoint point in collidePoints)
- {
- float current = ExtendedMath.Magnitude(point.Position.X - Position.X, point.Position.Y - Position.Y);
- if (current < closestMagnitude)
- {
- closestMagnitude = current;
- closest = point;
- }
- }
- return closest;
- }
- protected void CheckCollisions(float deltaTime)
- {
- switch(CurrentState)
- {
- case BallState.Active:
- CollidePoint current = GetClosestCollidePoint(deltaTime, out var magnitudeClosest);
- if (current != null)
- {
- switch (current.CollisionType)
- {
- case Collision.Left:
- Position.X = current.GameObj.CollideBox.Left - CollideBox.Width;
- Console.WriteLine("L");
- BounceLeft();
- break;
- case Collision.Right:
- Position.X = current.GameObj.CollideBox.Right;
- BounceRight();
- Console.WriteLine("R");
- break;
- case Collision.Top:
- Position.Y = current.GameObj.CollideBox.Top - CollideBox.Height;
- BounceUp();
- Console.WriteLine("T");
- break;
- case Collision.Bottom:
- Position.Y = current.GameObj.CollideBox.Bottom;
- BounceDown();
- Console.WriteLine("B");
- break;
- }
- if (current.GameObj is Paddle)
- {
- direction.X = (CollideBox.Center.X - current.GameObj.CollideBox.Center.X) / 15f;
- direction.Normalize();
- }
- if (current.GameObj is Brick)
- {
- ResolveBrickCollision((Brick)current.GameObj, 1);
- }
- float udt = deltaTime * (magnitudeClosest / ExtendedMath.Magnitude(ProjectPosition(deltaTime).X, ProjectPosition(deltaTime).Y));
- Update(deltaTime - udt);
- }
- break;
- }
- }
- protected void Move(float deltaTime)
- {
- switch (CurrentState)
- {
- case BallState.Active:
- Position += Velocity(deltaTime);
- break;
- case BallState.Inactive:
- Position = new Vector2(court.Player.Position.X + (court.Player.CollideBox.Width / 2) - CollideBox.Width / 2, court.Player.CollideBox.Y - 6);
- break;
- }
- }
- public override void Update(float deltaTime)
- {
- ProjectPosition(deltaTime);
- CheckCollisions(deltaTime);
- Move(deltaTime);
- if(Position.Y > 260)
- court.KillList.Add(this);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement