about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCharadon <dev@iotib.net>2022-06-09 11:56:21 -0400
committerCharadon <dev@iotib.net>2022-06-09 11:56:21 -0400
commit5621db21d9896ab648b70a1a882e0b9f64cf003f (patch)
tree5342138efcbcdc4428bb56a1c02bc7c7f79d9cdb
parent68401ca778ca0a1ee4fbb68b7be8dd47b63a2738 (diff)
downloadPong-C-5621db21d9896ab648b70a1a882e0b9f64cf003f.tar.gz
Done seperating game logic from framerate
-rw-r--r--src/ball.c99
-rw-r--r--src/enemy.c67
-rw-r--r--src/marathon.c4
-rw-r--r--src/versus.c1
4 files changed, 98 insertions, 73 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;
diff --git a/src/enemy.c b/src/enemy.c
index 0f73a55..47e2438 100644
--- a/src/enemy.c
+++ b/src/enemy.c
@@ -1,36 +1,49 @@
 #include "pong.h"
+#include <SDL2/SDL_atomic.h>
 
 void enemy(struct Players *Enemy, struct Balls ball) {
-	if (Enemy->NextTick <= SDL_AtomicGet(&Ticks)) {
-		if (!CheckCollisionRecs(ball.HitBox, Enemy->BallDetector)) {
-			if (Enemy->Y+120 > ball.Y) {
-				Enemy->Direction = 0;
-			} else if (Enemy->Y < ball.Y) {
-				Enemy->Direction = 1;
-			}
-		} else {
-			Enemy->Direction = 3;
+	int CurrentTick = SDL_AtomicGet(&Ticks);
+	int RunThisManyTimes;
+	if (Enemy->NextTick <= CurrentTick) {
+		if(CurrentTick > Enemy->NextTick) {
+			RunThisManyTimes = (CurrentTick - Enemy->NextTick);
 		}
-		switch(Enemy->Direction) {
-			case 0:
-				Enemy->Y -= 15;
-				break;
-			case 1:
-				Enemy->Y += 15;
-				break;
-			default:
-				break;
+		for (int i = 0; i <= RunThisManyTimes; i++) {
+			// Changes direction
+			if (!CheckCollisionRecs(ball.HitBox, Enemy->BallDetector)) {
+				if (Enemy->Y+120 > ball.Y) {
+					Enemy->Direction = 0;
+				} else if (Enemy->Y < ball.Y) {
+					Enemy->Direction = 1;
+				}
+			} else {
+				Enemy->Direction = 3;
+			}
+
+			// Moves
+			switch(Enemy->Direction) {
+				case 0:
+					Enemy->Y -= 15;
+					break;
+				case 1:
+					Enemy->Y += 15;
+					break;
+				default:
+					break;
+			}
+
+			// Prevents from going off screen.
+			if ( Enemy->Y > 480 ) {
+				Enemy->Y = 480;
+			} else if (Enemy->Y < 0) {
+				Enemy->Y = 0;
+			}
+			
+			// Updates hitbox and detector
+			Enemy->HitBox.y = Enemy->Y;
+			Enemy->BallDetector.y = Enemy->Y+80;
 		}
 		Enemy->NextTick = SDL_AtomicGet(&Ticks)+1;
 	}
-
-	// Prevents from going off screen.
-	if ( Enemy->Y > 480 ) {
-		Enemy->Y = 480;
-	} else if (Enemy->Y < 0) {
-		Enemy->Y = 0;
-	}
-	Enemy->HitBox.y = Enemy->Y;
-	Enemy->BallDetector.y = Enemy->Y+80;
 	return;
 }
diff --git a/src/marathon.c b/src/marathon.c
index 209fb52..319b425 100644
--- a/src/marathon.c
+++ b/src/marathon.c
@@ -1,5 +1,9 @@
 #include "pong.h"
 
+void leaderboard_record() {
+    
+}
+
 void marathon_main() {
 
     // Init Music
diff --git a/src/versus.c b/src/versus.c
index 33fa944..6523974 100644
--- a/src/versus.c
+++ b/src/versus.c
@@ -133,7 +133,6 @@ void versus_main() {
 		
 		BeginDrawing();
 			ClearBackground(BLACK);
-			DrawFPS(100, 100);
 			BeginMode2D(MainCamera);
 				DrawRectangle(0, 0, 1280, 720, (Color){20, 20, 20, 255});
 				DrawTexture(PaddleSprite, 0, Player.Y, WHITE);