about summary refs log tree commit diff stats
path: root/src/enemy.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/enemy.c')
-rw-r--r--src/enemy.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/enemy.c b/src/enemy.c
new file mode 100644
index 0000000..e5e0a2b
--- /dev/null
+++ b/src/enemy.c
@@ -0,0 +1,37 @@
+#include "raylib.h"
+#include "pong.h"
+
+void enemy(struct Players *Enemy, struct Balls ball) {
+	if (atomic_load(&Ticks) % 1 == 0) {
+		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;
+		}
+	}
+
+	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;
+	}
+	Enemy->HitBox.y = Enemy->Y;
+	Enemy->BallDetector.y = Enemy->Y+80;
+	return;
+}
\ No newline at end of file