-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
145 lines (116 loc) · 4.32 KB
/
game.cpp
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
#define is_down(b) input->buttons[b].is_down
#define pressed(b) (input->buttons[b].is_down && input->buttons[b].changed)
#define released(b) (!input->buttons[b].is_down && input->buttons[b].changed)
float player_1_p, player_1_dp, player_2_p, player_2_dp;
float arena_half_size_x = 85, arena_half_size_y = 45;
float player_half_size_x = 2.5, player_half_size_y = 12;
float ball_p_x, ball_p_y, ball_dp_x = 130, ball_dp_y, ball_half_size = 1;
int player_1_score, player_2_score;
internal void
simulate_player(float *p, float *dp, float ddp, float dt) {
ddp -= *dp * 10.f;
*p = *p + *dp * dt + ddp * dt * dt * .5f;
*dp = *dp + ddp * dt;
if (*p + player_half_size_y > arena_half_size_y) {
*p = arena_half_size_y - player_half_size_y;
*dp = 0;
}
else if (*p - player_half_size_y < -arena_half_size_y) {
*p = -arena_half_size_y + player_half_size_y;
*dp = 0;
}
}
internal bool
aabb_vs_aabb(float p1x, float p1y, float hs1x, float hs1y,
float p2x, float p2y, float hs2x, float hs2y) {
return (p1x + hs1x > p2x - hs2x &&
p1x - hs1x < p2x + hs2x &&
p1y + hs1y > p2y - hs2y &&
p1y + hs1y < p2y + hs2y);
}
enum Gamemode {
GM_MENU,
GM_GAMEPLAY,
};
Gamemode current_gamemode;
int hot_button;
bool enemy_is_ai;
internal void
simulate_game(Input* input, float dt) {
draw_rect(0, 0, arena_half_size_x, arena_half_size_y, 0xffaa33);
draw_arena_borders(arena_half_size_x, arena_half_size_y, 0xff5500);
if (current_gamemode == GM_GAMEPLAY) {
float player_1_ddp = 0.f;
if (!enemy_is_ai) {
if (is_down(BUTTON_UP)) player_1_ddp += 2000;
if (is_down(BUTTON_DOWN)) player_1_ddp -= 2000;
} else {
//if (ball_p_y > player_1_p+2.f) player_1_ddp += 1300;
//if (ball_p_y < player_1_p-2.f) player_1_ddp -= 1300;
player_1_ddp = (ball_p_y - player_1_p) * 100;
if (player_1_ddp > 1300) player_1_ddp = 1300;
if (player_1_ddp < -1300) player_1_ddp = -1300;
}
float player_2_ddp = 0.f;
if (is_down(BUTTON_W)) player_2_ddp += 2000;
if (is_down(BUTTON_S)) player_2_ddp -= 2000;
simulate_player(&player_1_p, &player_1_dp, player_1_ddp, dt);
simulate_player(&player_2_p, &player_2_dp, player_2_ddp, dt);
// Simulate Ball
{
ball_p_x += ball_dp_x * dt;
ball_p_y += ball_dp_y * dt;
if (aabb_vs_aabb(ball_p_x, ball_p_y, ball_half_size, ball_half_size, 80, player_1_p, player_half_size_x, player_half_size_y)) {
ball_p_x = 80 - player_half_size_x - ball_half_size;
ball_dp_x *= -1;
ball_dp_y = (ball_p_y - player_1_p) * 2 + player_1_dp * .75f;
} else if (aabb_vs_aabb(ball_p_x, ball_p_y, ball_half_size, ball_half_size, -80, player_2_p, player_half_size_x, player_half_size_y)) {
ball_p_x = -80 + player_half_size_x + ball_half_size;
ball_dp_x *= -1;
ball_dp_y = (ball_p_y - player_2_p) * 2 + player_2_dp * .75f;
}
if (ball_p_y + ball_half_size > arena_half_size_y) {
ball_p_y = arena_half_size_y - ball_half_size;
ball_dp_y *= -1;
} else if (ball_p_y - ball_half_size < -arena_half_size_y) {
ball_p_y = -arena_half_size_y + ball_half_size;
ball_dp_y *= -1;
}
if (ball_p_x + ball_half_size > arena_half_size_x) {
ball_dp_x *= -1;
ball_dp_y = 0;
ball_p_x = 0;
ball_p_y = 0;
player_1_score++;
} else if (ball_p_x - ball_half_size < -arena_half_size_x) {
ball_dp_x *= -1;
ball_dp_y = 0;
ball_p_x = 0;
ball_p_y = 0;
player_2_score++;
}
}
draw_number(player_1_score, -10, 40, 1.f, 0xbbffbb);
draw_number(player_2_score, 10, 40, 1.f, 0xbbffbb);
// Rendering
draw_rect(ball_p_x, ball_p_y, ball_half_size, ball_half_size, 0xffffff);
draw_rect(80, player_1_p, player_half_size_x, player_half_size_y, 0xff0000);
draw_rect(-80, player_2_p, player_half_size_x, player_half_size_y, 0xff0000);
} else {
if (pressed(BUTTON_LEFT) || pressed(BUTTON_RIGHT)) {
hot_button = !hot_button;
}
if (pressed(BUTTON_ENTER)) {
current_gamemode = GM_GAMEPLAY;
enemy_is_ai = hot_button ? 0 : 1;
}
if (hot_button == 0) {
draw_text("SINGLE PLAYER", -80, -10, 1, 0xff0000);
draw_text("MULTIPLAYER", 20, -10, 1, 0xaaaaaa);
} else {
draw_text("SINGLE PLAYER", -80, -10, 1, 0xaaaaaa);
draw_text("MULTIPLAYER", 20, -10, 1, 0xff0000);
}
draw_text("PANG BONG SOURCE", -73, 40, 2, 0xffffff);
}
}