about summary refs log tree commit diff stats
path: root/src/ball.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ball.c')
-rw-r--r--src/ball.c99
1 files changed, 54 insertions, 45 deletions
diff --git a/src/ball.c b/src/ball.c
index d7a6b1b..33b2d40 100644
--- a/src/ball.c
+++ b/src/ball.c
@@ -7,56 +7,65 @@ void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerSc
 		NoEnemy = true;
 	}
 
-	// Moves ball
-	if(Ball->NextTick <= SDL_AtomicGet(&Ticks)){
-		Ball->Y += Ball->Angle;
-		if (Ball->Direction == LEFT) {
-			Ball->X -= Ball->Speed;
-		} else {
-			Ball->X += Ball->Speed;
+	// Moves 
+	int CurrentTick = SDL_AtomicGet(&Ticks);
+	if(Ball->NextTick <= CurrentTick){
+		int RunThisManyTimes = 1;
+		if (CurrentTick > Ball->NextTick) {
+			RunThisManyTimes = (CurrentTick - Ball->NextTick);
+			printf("%d\n",RunThisManyTimes);
 		}
-		// Moves hitbox with ball.
-		Ball->HitBox.x = Ball->X;
-		Ball->HitBox.y = Ball->Y;
-		Ball->NextTick = SDL_AtomicGet(&Ticks)+1;
-		printf("%d,%d\n", SDL_AtomicGet(&Ticks), Ball->NextTick);
-	}
-	printf("%d,%d\n", SDL_AtomicGet(&Ticks), Ball->NextTick);
-	// Check collisions against players.
-	if (CheckCollisionRecs(*Player, Ball->HitBox) && Ball->Direction == LEFT) {
-		Ball->Direction = RIGHT;
-		Ball->Speed *= 1.5f;
-        if (Ball->Speed > 40) {
-            Ball->Speed = 40;
-        }
-        if (Ball->Speed != 40) {
-		    Ball->Angle = GetRandomValue(-10, 10);
-        } else {
-            Ball->Angle = GetRandomValue(-30, 30);
-        }
-        play_audio(0);
-	}
-	if (NoEnemy == false) {
-		if (CheckCollisionRecs(*Enemy, Ball->HitBox) && Ball->Direction == RIGHT) {
-			Ball->Direction = LEFT;
-			Ball->Speed *= 1.5f;
-			if (Ball->Speed > 40) {
-				Ball->Speed = 40;
-			}
-			if (Ball->Speed != 40) {
-				Ball->Angle = GetRandomValue(-10, 10);
+		for (int i = 0; i <= RunThisManyTimes; i++) {
+			Ball->Y += Ball->Angle;
+			if (Ball->Direction == LEFT) {
+				Ball->X -= Ball->Speed;
 			} else {
-				Ball->Angle = GetRandomValue(-30, 30);
+				Ball->X += Ball->Speed;
+			}
+
+			// Moves hitbox with ball.
+			Ball->HitBox.x = Ball->X;
+			Ball->HitBox.y = Ball->Y;
+
+			// Check collisions against players.
+			if (CheckCollisionRecs(*Player, Ball->HitBox) && Ball->Direction == LEFT) {
+				Ball->Direction = RIGHT;
+				Ball->Speed *= 1.5f;
+				if (Ball->Speed > 40) {
+					Ball->Speed = 40;
+				}
+				if (Ball->Speed != 40) {
+					Ball->Angle = GetRandomValue(-10, 10);
+				} else {
+					Ball->Angle = GetRandomValue(-30, 30);
+				}
+				play_audio(0);
+			}
+			if (NoEnemy == false) {
+				if (CheckCollisionRecs(*Enemy, Ball->HitBox) && Ball->Direction == RIGHT) {
+					Ball->Direction = LEFT;
+					Ball->Speed *= 1.5f;
+					if (Ball->Speed > 40) {
+						Ball->Speed = 40;
+					}
+					if (Ball->Speed != 40) {
+						Ball->Angle = GetRandomValue(-10, 10);
+					} else {
+						Ball->Angle = GetRandomValue(-30, 30);
+					}
+					play_audio(SOUND_BOUNCE);
+				}
+			}
+			// Bounce ball if touches top or bottom of screen.
+			if ( (Ball->Y+32 >= 720 && Ball->Angle >=1) || (Ball->Y <= 0 && Ball->Angle <= -1) ) {
+				Ball->Angle *= -1;
+				play_audio(SOUND_BOUNCE);
 			}
-			play_audio(SOUND_BOUNCE);
 		}
+		Ball->NextTick = SDL_AtomicGet(&Ticks)+1;
+//		printf("%d,%d\n", SDL_AtomicGet(&Ticks), Ball->NextTick);
 	}
-	
-	// Bounce ball if touches top or bottom of screen.
-	if ( (Ball->Y+32 >= 720 && Ball->Angle >=1) || (Ball->Y <= 0 && Ball->Angle <= -1) ) {
-		Ball->Angle *= -1;
-        play_audio(SOUND_BOUNCE);
-	}
+//	printf("%d,%d\n", SDL_AtomicGet(&Ticks), Ball->NextTick);
 
 	// Calculates score and resets ball.
 	bool Scored = false;