-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPongEnemy.cpp
More file actions
65 lines (51 loc) · 2.18 KB
/
PongEnemy.cpp
File metadata and controls
65 lines (51 loc) · 2.18 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
int enemyPosY;
float enemySpeedY;
float enemyAcceleration;
float visibilityDistance; // Enemy is blind beyond visibilityDistance and will just move towards the center
float reactionDistance; // The distance at which the enemy will start decelerating when it's approaching it's target
const int windowX = 1920;
const int windowY = 1080;
const float wallMargin = 210;
const float topSpeed = 30;
const float dampSpeed = 0.8f;
const float dampAcceleration = 0.3f;
float clamp(float num, float min, float max) {
return std::max(min, std::min(num, max));
}
void EnemyOnStart() {
enemyPosY = windowY * 0.5f;
enemySpeedY = 0;
enemyAcceleration = 7;
visibilityDistance = windowX * 0.75f;
reactionDistance = 200;
}
// An enemy 10 points behind is almost unbeatable, an enemy 10 points ahead will fail to reach any less than easy ball
void EnemyOnScore(int playerScore, int enemyScore) {
enemyPosY = windowY * 0.5f;
enemySpeedY = 0;
visibilityDistance += playerScore > enemyScore ? windowX * 0.04f : -windowX * 0.04f;
visibilityDistance = clamp(visibilityDistance, windowX * 0.5f, windowX);
reactionDistance += playerScore > enemyScore ? 10 : -10;
reactionDistance = clamp(reactionDistance, 50, 300);
}
//"ballDistX" is input as enemyPosX - ballPosX because enemy is on the right side and has an offset away from the side of the screen
float EnemyUpdate(float ballDistX, float ballPosY, float ballSpeedX, float ballSpeedY, float dt) {
float timeX = ballDistX / std::abs(ballSpeedX);
float targetY = ballPosY + ballSpeedY * timeX;
targetY = clamp(targetY, wallMargin, windowY - wallMargin);
if (ballDistX > visibilityDistance || ballSpeedX < 0)
targetY = windowY * 0.5f;
float deltaY = targetY - enemyPosY;
int dir = deltaY > 0 ? 1 : -1;
if (enemySpeedY / std::abs(enemySpeedY) != dir && std::abs(deltaY) < reactionDistance) {
enemySpeedY *= dampSpeed;
enemySpeedY -= enemyAcceleration * dampAcceleration * dir * dt;
}
else {
enemySpeedY += enemyAcceleration * dir * dt;
}
enemySpeedY = clamp(enemySpeedY, -topSpeed, topSpeed);
enemyPosY += enemySpeedY;
return enemyPosY;
}