-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidle.h
176 lines (156 loc) · 4.58 KB
/
idle.h
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#ifndef CO_OP_BALL_GAME_IDLE_H
#define CO_OP_BALL_GAME_IDLE_H
#include "manager_methods.h"
bool goal_control() {
if (ballX + ballRadius > goal2X2 && ballY >= goal2Y1 && ballY <= goal2Y2) {
player1_won();
return true;
}
if (ballX - ballRadius < goal1X2 && ballY >= goal1Y1 && ballY <= goal1Y2) {
player2_won();
return true;
}
return false;
}
bool collide_with_player_rectangles() {
if (ballX - ballRadius <= player1.getPlayerX2() && ballY >= player1.getPlayerY1() && ballY <= player1.getPlayerY2()) {
ballX = player1.getPlayerX2() + ballRadius;
ballSpeedX = -ballSpeedX;
return true;
}
if (ballX + ballRadius >= player2.getPlayerX1() && ballY >= player2.getPlayerY1() && ballY <= player2.getPlayerY2()) {
ballX = player2.getPlayerX1() - ballRadius;
ballSpeedX = -ballSpeedX;
return true;
}
return false;
}
bool collide_with_top_bottom() {
if(ballSpeedY > 0) {
if (ballY + ballRadius + ballSpeedY >= WINDOW_HEIGHT/2 - padding - paddingTop) {
ballY = WINDOW_HEIGHT/2 - padding - paddingTop - ballRadius;
ballSpeedY = -ballSpeedY;
return true;
}
} else {
if (ballY - ballRadius - ballSpeedY <= -WINDOW_HEIGHT/2 + padding) {
ballY = -WINDOW_HEIGHT/2 + padding;
ballSpeedY = -ballSpeedY;
return true;
}
}
return false;
}
bool collide_with_left_right() {
if ((ballY >= goal2Y1 && ballY <= goal2Y2) || (ballY >= goal1Y1 && ballY <= goal1Y2)) {
return false;
}
if(ballSpeedX > 0) {
if (ballX + ballRadius + ballSpeedX >= WINDOW_WIDTH/2 - padding) {
ballX = WINDOW_WIDTH/2 - padding;
ballSpeedX = -ballSpeedX;
return true;
}
} else {
if (ballX - ballRadius - ballSpeedX <= -WINDOW_WIDTH/2 + padding) {
ballX = -WINDOW_WIDTH/2 + padding;
ballSpeedX = -ballSpeedX;
return true;
}
}
return false;
}
bool collide_with_goal_rear_lines() {
if (ballY + ballRadius >= goal2Y2 && ballY + ballRadius <= goal2Y2 + 10 && ballX >= goal2X1 && ballX <= goal2X2) {
ballSpeedY = -ballSpeedY;
return true;
}
if (ballY - ballRadius <= goal2Y1 && ballY + ballRadius >= goal2Y1 - 10 && ballX >= goal2X1 && ballX <= goal2X2) {
ballSpeedY = -ballSpeedY;
return true;
}
if (ballY + ballRadius >= goal1Y2 && ballY + ballRadius <= goal2Y2 + 10 && ballX >= goal1X2 && ballX <= goal1X1) {
ballSpeedY = -ballSpeedY;
return true;
}
if (ballY - ballRadius <= goal1Y1 && ballY + ballRadius >= goal2Y1 - 10 && ballX >= goal1X2 && ballX <= goal1X1) {
ballSpeedY = -ballSpeedY;
return true;
}
return false;
}
void ball_movement() {
ballX += ballSpeedX;
ballY += ballSpeedY;
if (goal_control()) {
return;
}
if (collide_with_player_rectangles()) {
return;
}
if (collide_with_top_bottom()) {
return;
}
if (collide_with_left_right()) {
return;
}
if (collide_with_goal_rear_lines()) {
return;
}
}
void speedUpBall() {
#ifdef __APPLE_CC__
if (ballSpeedX > 0) {
ballSpeedX += returnRandomFloatBetween(0.001, 0.005);
} else {
ballSpeedX -= returnRandomFloatBetween(0.001, 0.005);
}
if (ballSpeedY > 0) {
ballSpeedY += returnRandomFloatBetween(0.001, 0.005);
} else {
ballSpeedY -= returnRandomFloatBetween(0.001, 0.005);
}
#else
if (ballSpeedX > 0) {
ballSpeedX += returnRandomFloatBetween(0.0001, 0.0005);
} else {
ballSpeedX -= returnRandomFloatBetween(0.0001, 0.0005);
}
if (ballSpeedY > 0) {
ballSpeedY += returnRandomFloatBetween(0.0001, 0.0005);
} else {
ballSpeedY -= returnRandomFloatBetween(0.0001, 0.0005);
}
#endif
}
void key_controls() {
if (keyStates['w']) {
if (player1.getPlayerY2() <= goal1Y2) {
player1.movePlayerUp();
}
}
if (keyStates['s']) {
if (player1.getPlayerY1() >= goal1Y1) {
player1.movePlayerDown();
}
}
if (keyStates[GLUT_KEY_UP]) {
if (player2.getPlayerY2() <= goal2Y2) {
player2.movePlayerUp();
}
}
if (keyStates[GLUT_KEY_DOWN]) {
if (player2.getPlayerY1() >= goal2Y1) {
player2.movePlayerDown();
}
}
}
void idle() {
if (gameStarted) {
key_controls();
ball_movement();
speedUpBall();
}
glutPostRedisplay();
}
#endif //CO_OP_BALL_GAME_IDLE_H