summaryrefslogtreecommitdiff
path: root/game/src/plugins/pong.c
blob: 070cc6f0ca3b87b6b8606f21ee0b8e9d4bad21b1 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "plugin.h"

#include <gfx/app.h>
#include <gfx/gfx.h>
#include <gfx/renderer.h>

#include <math/mat4.h>
#include <math/vec2.h>
#include <math/vec4.h>

#include <stdlib.h>

static const vec2 PAD_SIZE        = (vec2){120, 20};
static const R    PLAYER_Y_OFFSET = 50;
static const R    PLAYER_SPEED    = 800;

static const R ENEMY_SPEED = 2;

static const R BALL_SIZE  = 18;
static const R BALL_SPEED = 360; // In each dimension.

static const R EPS = (R)1e-3;

typedef struct Player {
  vec2 position;
} Player;

typedef struct Ball {
  vec2 position;
  vec2 velocity;
} Ball;

typedef struct State {
  bool   game_started;
  Player human;
  Player enemy;
  Ball   ball;
  mat4   viewProjection;
} State;

bool init(Game* game, State** pp_state) {
  assert(game);

  State* state = calloc(1, sizeof(State));
  if (!state) {
    return false;
  }

  *pp_state = state;
  return true;

cleanup:
  free(state);
  return false;
}

void shutdown(Game* game, State* state) {
  assert(game);
  assert(state);
}

static void move_ball(Ball* ball, R dt, int width, int height) {
  assert(ball);

  const R offset = BALL_SIZE / 2;

  ball->position = vec2_add(ball->position, vec2_scale(ball->velocity, dt));

  // Right wall.
  if (ball->position.x + offset > (R)width) {
    ball->position.x = (R)width - offset - EPS;
    ball->velocity.x = -ball->velocity.x;
  }
  // Left wall.
  else if (ball->position.x - offset < 0) {
    ball->position.x = offset + EPS;
    ball->velocity.x = -ball->velocity.x;
  }
  // Top wall.
  if (ball->position.y + offset > (R)height) {
    ball->position.y = (R)height - offset - EPS;
    ball->velocity.y = -ball->velocity.y;
  }
  // Bottom wall.
  else if (ball->position.y - offset < 0) {
    ball->position.y = offset + EPS;
    ball->velocity.y = -ball->velocity.y;
  }
}

void move_enemy_player(int width, Player* player, R t) {
  const R half_width = (R)width / 2;
  const R amplitude  = half_width - (PAD_SIZE.x / 2);
  player->position.x = half_width + amplitude * sinf(t * ENEMY_SPEED);
}

void move_human_player(Player* player, R dt) {
  assert(player);

  R speed = 0;
  if (gfx_is_key_pressed('a')) {
    speed -= PLAYER_SPEED;
  }
  if (gfx_is_key_pressed('d')) {
    speed += PLAYER_SPEED;
  }

  player->position.x += speed * dt;
}

void clamp_player(Player* player, int width) {
  assert(player);

  const R offset = PAD_SIZE.x / 2;

  // Left wall.
  if (player->position.x + offset > (R)width) {
    player->position.x = (R)width - offset;
  }
  // Right wall.
  else if (player->position.x - offset < 0) {
    player->position.x = offset;
  }
}

void collide_ball(vec2 old_ball_position, const Player* player, Ball* ball) {
  assert(player);
  assert(ball);

  // Discrete but simple collision. Checks for intersection and moves the ball
  // back by a small epsilon.

  // Player bounding box.
  const vec2 player_pmin = vec2_make(
      player->position.x - PAD_SIZE.x / 2, player->position.y - PAD_SIZE.y / 2);
  const vec2 player_pmax = vec2_make(
      player->position.x + PAD_SIZE.x / 2, player->position.y + PAD_SIZE.y / 2);

  // Ball bounding box.
  const vec2 ball_pmin = vec2_make(
      ball->position.x - BALL_SIZE / 2, ball->position.y - BALL_SIZE / 2);
  const vec2 ball_pmax = vec2_make(
      ball->position.x + BALL_SIZE / 2, ball->position.y + BALL_SIZE / 2);

  // Check for intersection and update ball.
  if (!((ball_pmax.x < player_pmin.x) || (ball_pmin.x > player_pmax.x) ||
        (ball_pmax.y < player_pmin.y) || (ball_pmin.y > player_pmax.y))) {
    ball->position =
        vec2_add(old_ball_position, vec2_scale(ball->velocity, -EPS));
    ball->velocity.y = -ball->velocity.y;
  }
}

void update(Game* game, State* state, double t, double dt) {
  assert(game);
  assert(state);

  // TODO: Move game width/height to GfxApp query functions?
  const vec2 old_ball_position = state->ball.position;
  move_ball(&state->ball, (R)dt, game->width, game->height);
  move_human_player(&state->human, (R)dt);
  move_enemy_player(game->width, &state->enemy, (R)t);
  clamp_player(&state->human, game->width);
  collide_ball(old_ball_position, &state->human, &state->ball);
  collide_ball(old_ball_position, &state->enemy, &state->ball);
}

static void draw_player(ImmRenderer* imm, const Player* player) {
  assert(imm);
  assert(player);

  const vec2 half_box = vec2_div(PAD_SIZE, vec2_make(2, 2));

  const vec2  pmin = vec2_sub(player->position, half_box);
  const vec2  pmax = vec2_add(player->position, half_box);
  const aabb2 box  = aabb2_make(pmin, pmax);

  gfx_imm_draw_aabb2(imm, box);
}

static void draw_ball(ImmRenderer* imm, const Ball* ball) {
  assert(imm);
  assert(ball);

  const vec2  half_box = vec2_make(BALL_SIZE / 2, BALL_SIZE / 2);
  const vec2  pmin     = vec2_sub(ball->position, half_box);
  const vec2  pmax     = vec2_add(ball->position, half_box);
  const aabb2 box      = aabb2_make(pmin, pmax);

  gfx_imm_draw_aabb2(imm, box);
}

void render(const Game* game, const State* state) {
  assert(game);
  assert(state);

  ImmRenderer* imm = gfx_get_imm_renderer(game->gfx);
  gfx_imm_start(imm);
  gfx_imm_set_view_projection_matrix(imm, &state->viewProjection);
  gfx_imm_load_identity(imm);
  gfx_imm_set_colour(imm, vec4_make(1, 1, 1, 1));
  draw_player(imm, &state->human);
  draw_player(imm, &state->enemy);
  draw_ball(imm, &state->ball);
  gfx_imm_end(imm);
}

static R clamp_to_width(int width, R x, R extent) {
  return min(x, (R)width - extent);
}

void resize(Game* game, State* state, int width, int height) {
  assert(game);
  assert(state);

  state->viewProjection = mat4_ortho(0, (R)width, 0, (R)height, -1, 1);

  state->human.position.y = PLAYER_Y_OFFSET;
  state->enemy.position.y = (R)height - PLAYER_Y_OFFSET;

  if (!state->game_started) {
    state->human.position.x = (R)width / 2;
    state->enemy.position.x = (R)width / 2;

    state->ball.position =
        vec2_div(vec2_make((R)width, (R)height), vec2_make(2, 2));

    state->ball.velocity = vec2_make(BALL_SPEED, BALL_SPEED);

    state->game_started = true;
  } else {
    state->human.position.x =
        clamp_to_width(width, state->human.position.x, PAD_SIZE.x / 2);
    state->enemy.position.x =
        clamp_to_width(width, state->enemy.position.x, PAD_SIZE.x / 2);
  }
}