|
@@ -4,19 +4,38 @@
|
|
|
#include "scufeng/engine.h"
|
|
#include "scufeng/engine.h"
|
|
|
#include "scufeng/util.h"
|
|
#include "scufeng/util.h"
|
|
|
|
|
|
|
|
|
|
+// static const float paddles_w = 0.01f;
|
|
|
|
|
+// static const float paddles_h = 0.10f;
|
|
|
|
|
+// static const float paddle_l_x = (1.0f / 16);
|
|
|
|
|
+// static const float ball_w = paddles_w * 0.5f;
|
|
|
|
|
+// static vec2 ball_pos = { 0.5f, 0.5f };
|
|
|
|
|
+// static const float paddle_speed = 0.7f;
|
|
|
|
|
+// static const float ball_speed = 0.7f;
|
|
|
|
|
+
|
|
|
typedef enum {
|
|
typedef enum {
|
|
|
BCR_PADDLE_L_GOAL,
|
|
BCR_PADDLE_L_GOAL,
|
|
|
BCR_PADDLE_R_GOAL,
|
|
BCR_PADDLE_R_GOAL,
|
|
|
} BALL_COLLISION_RESULT;
|
|
} BALL_COLLISION_RESULT;
|
|
|
|
|
|
|
|
|
|
+const GameStateParameters game_default_params = {
|
|
|
|
|
+ .paddles_w = 0.01f,
|
|
|
|
|
+ .paddles_h = 0.10f,
|
|
|
|
|
+ .paddle_l_x = (1.0f / 16),
|
|
|
|
|
+ .ball_w = 0.005f,
|
|
|
|
|
+ .ball_pos = { 0.5f, 0.5f },
|
|
|
|
|
+ .paddle_speed = 0.7f,
|
|
|
|
|
+ .ball_speed = 0.7f,
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
GameState GameState_new(
|
|
GameState GameState_new(
|
|
|
- const float paddles_w,
|
|
|
|
|
- const float paddles_h,
|
|
|
|
|
- const float paddle_l_x,
|
|
|
|
|
- const float paddle_speed,
|
|
|
|
|
- const float ball_w,
|
|
|
|
|
- const vec2 ball_pos,
|
|
|
|
|
- const float ball_speed
|
|
|
|
|
|
|
+ // const float paddles_w,
|
|
|
|
|
+ // const float paddles_h,
|
|
|
|
|
+ // const float paddle_l_x,
|
|
|
|
|
+ // const float paddle_speed,
|
|
|
|
|
+ // const float ball_w,
|
|
|
|
|
+ // const vec2 ball_pos,
|
|
|
|
|
+ // const float ball_speed
|
|
|
|
|
+ const GameStateParameters params
|
|
|
) {
|
|
) {
|
|
|
|
|
|
|
|
#define BALL_START_DIR { -1.0f, 0.0f }
|
|
#define BALL_START_DIR { -1.0f, 0.0f }
|
|
@@ -24,28 +43,28 @@ GameState GameState_new(
|
|
|
// clang-format off
|
|
// clang-format off
|
|
|
GameState state = {
|
|
GameState state = {
|
|
|
.paddle_l = {
|
|
.paddle_l = {
|
|
|
- .pos = { paddle_l_x, 0.5f },
|
|
|
|
|
- .w = paddles_w,
|
|
|
|
|
- .h = paddles_h,
|
|
|
|
|
|
|
+ .pos = { params.paddle_l_x, 0.5f },
|
|
|
|
|
+ .w = params.paddles_w,
|
|
|
|
|
+ .h = params.paddles_h,
|
|
|
.dirv = GLM_VEC2_ZERO_INIT,
|
|
.dirv = GLM_VEC2_ZERO_INIT,
|
|
|
- .speed = paddle_speed
|
|
|
|
|
|
|
+ .speed = params.paddle_speed
|
|
|
},
|
|
},
|
|
|
.paddle_r = {
|
|
.paddle_r = {
|
|
|
- .pos = { 1.0f - paddle_l_x, 0.5f },
|
|
|
|
|
- .w = paddles_w,
|
|
|
|
|
- .h = paddles_h,
|
|
|
|
|
|
|
+ .pos = { 1.0f - params.paddle_l_x, 0.5f },
|
|
|
|
|
+ .w = params.paddles_w,
|
|
|
|
|
+ .h = params.paddles_h,
|
|
|
.dirv = GLM_VEC2_ZERO_INIT,
|
|
.dirv = GLM_VEC2_ZERO_INIT,
|
|
|
- .speed = paddle_speed
|
|
|
|
|
|
|
+ .speed = params.paddle_speed
|
|
|
},
|
|
},
|
|
|
.ball = {
|
|
.ball = {
|
|
|
// .pos = { 0.5f, 0.5f },
|
|
// .pos = { 0.5f, 0.5f },
|
|
|
- .pos = { ball_pos[0], ball_pos[1] },
|
|
|
|
|
- .w = ball_w,
|
|
|
|
|
- .h = ball_w,
|
|
|
|
|
|
|
+ .pos = { params.ball_pos[0], params.ball_pos[1] },
|
|
|
|
|
+ .w = params.ball_w,
|
|
|
|
|
+ .h = params.ball_w,
|
|
|
.dirv = BALL_START_DIR,
|
|
.dirv = BALL_START_DIR,
|
|
|
- .speed = ball_speed
|
|
|
|
|
|
|
+ .speed = params.ball_speed
|
|
|
},
|
|
},
|
|
|
- .ball_start_pos = { ball_pos[0], ball_pos[1] },
|
|
|
|
|
|
|
+ .ball_start_pos = { params.ball_pos[0], params.ball_pos[1] },
|
|
|
.ball_start_dir = BALL_START_DIR
|
|
.ball_start_dir = BALL_START_DIR
|
|
|
};
|
|
};
|
|
|
// clang-format on
|
|
// clang-format on
|
|
@@ -53,7 +72,7 @@ GameState GameState_new(
|
|
|
return state;
|
|
return state;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static void paddle_collision_check(MovingObject* const paddle) {
|
|
|
|
|
|
|
+static void paddle_collision_check(MovingObject *const paddle) {
|
|
|
if (paddle->pos[1] - paddle->h / 2.0f < 0.0f) {
|
|
if (paddle->pos[1] - paddle->h / 2.0f < 0.0f) {
|
|
|
paddle->pos[1] = paddle->h / 2.0f;
|
|
paddle->pos[1] = paddle->h / 2.0f;
|
|
|
}
|
|
}
|
|
@@ -62,7 +81,7 @@ static void paddle_collision_check(MovingObject* const paddle) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static void paddle_r_move(GameState* const state) {
|
|
|
|
|
|
|
+static void paddle_r_move(GameState *const state) {
|
|
|
const float ball_y = state->ball.pos[1];
|
|
const float ball_y = state->ball.pos[1];
|
|
|
|
|
|
|
|
const float target_zone_range = state->paddle_r.h / 2.0f;
|
|
const float target_zone_range = state->paddle_r.h / 2.0f;
|
|
@@ -85,13 +104,13 @@ static void paddle_r_move(GameState* const state) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static void game_next_round(GameState* const state) {
|
|
|
|
|
|
|
+static void game_next_round(GameState *const state) {
|
|
|
glmc_vec2_copy(state->ball_start_pos, state->ball.pos);
|
|
glmc_vec2_copy(state->ball_start_pos, state->ball.pos);
|
|
|
glmc_vec2_copy(state->ball_start_dir, state->ball.dirv);
|
|
glmc_vec2_copy(state->ball_start_dir, state->ball.dirv);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static bool ball_bounds_collision_check(GameState* const state, vec2 new_pos) {
|
|
|
|
|
- MovingObject* const ball = &state->ball;
|
|
|
|
|
|
|
+static bool ball_bounds_collision_check(GameState *const state, vec2 new_pos) {
|
|
|
|
|
+ MovingObject *const ball = &state->ball;
|
|
|
|
|
|
|
|
const float wd2 = ball->w / 2.0f;
|
|
const float wd2 = ball->w / 2.0f;
|
|
|
const float hd2 = ball->h / 2.0f;
|
|
const float hd2 = ball->h / 2.0f;
|
|
@@ -118,16 +137,16 @@ static bool ball_bounds_collision_check(GameState* const state, vec2 new_pos) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static void ball_paddles_collision_check(GameState* const state, vec2 new_pos) {
|
|
|
|
|
|
|
+static void ball_paddles_collision_check(GameState *const state, vec2 new_pos) {
|
|
|
static const float possible_y_directions[] = {
|
|
static const float possible_y_directions[] = {
|
|
|
0.0f,
|
|
0.0f,
|
|
|
0.5f,
|
|
0.5f,
|
|
|
-0.5f
|
|
-0.5f
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- MovingObject* const ball = &state->ball;
|
|
|
|
|
- const MovingObject* const paddle_l = &state->paddle_l;
|
|
|
|
|
- const MovingObject* const paddle_r = &state->paddle_r;
|
|
|
|
|
|
|
+ MovingObject *const ball = &state->ball;
|
|
|
|
|
+ const MovingObject *const paddle_l = &state->paddle_l;
|
|
|
|
|
+ const MovingObject *const paddle_r = &state->paddle_r;
|
|
|
|
|
|
|
|
const float ball_wd2 = ball->w / 2.0f;
|
|
const float ball_wd2 = ball->w / 2.0f;
|
|
|
const float ball_hd2 = ball->h / 2.0f;
|
|
const float ball_hd2 = ball->h / 2.0f;
|
|
@@ -221,7 +240,7 @@ static void ball_paddles_collision_check(GameState* const state, vec2 new_pos) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static void ball_move(GameState* const state, const double delta) {
|
|
|
|
|
|
|
+static void ball_move(GameState *const state, const double delta) {
|
|
|
vec2 ball_movement;
|
|
vec2 ball_movement;
|
|
|
const float ball_movement_scale = delta * state->ball.speed;
|
|
const float ball_movement_scale = delta * state->ball.speed;
|
|
|
// glmc_vec2_normalize(state->ball.dirv);
|
|
// glmc_vec2_normalize(state->ball.dirv);
|
|
@@ -238,7 +257,7 @@ static void ball_move(GameState* const state, const double delta) {
|
|
|
glmc_vec2_copy(new_pos, state->ball.pos);
|
|
glmc_vec2_copy(new_pos, state->ball.pos);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void GameState_move_paddle_l(GameState* const state, const SEDirection dir) {
|
|
|
|
|
|
|
+void GameState_move_paddle_l(GameState *const state, const SEDirection dir) {
|
|
|
switch (dir) {
|
|
switch (dir) {
|
|
|
case SE_UP:
|
|
case SE_UP:
|
|
|
state->paddle_l.dirv[1] = -1.0f;
|
|
state->paddle_l.dirv[1] = -1.0f;
|
|
@@ -253,7 +272,7 @@ void GameState_move_paddle_l(GameState* const state, const SEDirection dir) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void GameState_update(GameState* const state, const double delta) {
|
|
|
|
|
|
|
+void GameState_update(GameState *const state, const double delta) {
|
|
|
// Paddles
|
|
// Paddles
|
|
|
vec2 paddle_movement;
|
|
vec2 paddle_movement;
|
|
|
const float paddle_movement_scale = delta * state->paddle_l.speed;
|
|
const float paddle_movement_scale = delta * state->paddle_l.speed;
|
|
@@ -269,3 +288,13 @@ void GameState_update(GameState* const state, const double delta) {
|
|
|
// Ball
|
|
// Ball
|
|
|
ball_move(state, delta);
|
|
ball_move(state, delta);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+SEDirection GameState_paddle_l_dir_get(const GameState *const state) {
|
|
|
|
|
+ if (state->paddle_l.dirv[1] == -1.0f) {
|
|
|
|
|
+ return SE_UP;
|
|
|
|
|
+ } else if (state->paddle_l.dirv[1] == 1.0f) {
|
|
|
|
|
+ return SE_DOWN;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return SE_NONE;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|