about summary refs log tree commit diff stats
path: root/src/enemy.c
blob: 5a7e99b19ba860a01e655bbf0a83c16e9b6809c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "pong.h"

void enemy(struct Players *Enemy, struct Balls ball) {
	if (SDL_AtomicGet(&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;
}