typedef & PascalCase for ALL structs and enums
This commit is contained in:
@ -13,9 +13,9 @@
|
||||
void game_tick(void) {
|
||||
if (ctx.initialization_needed) {
|
||||
if (!ctx.udata) {
|
||||
ctx.udata = ccalloc(1, sizeof (struct state));
|
||||
ctx.udata = ccalloc(1, sizeof (State));
|
||||
|
||||
struct state *state = ctx.udata;
|
||||
State *state = ctx.udata;
|
||||
state->ctx = &ctx;
|
||||
state->scene = title_scene(state);
|
||||
}
|
||||
@ -48,7 +48,7 @@ void game_tick(void) {
|
||||
input_bind_action_scancode(&ctx.input, "mouse_capture_toggle", SDL_SCANCODE_ESCAPE);
|
||||
}
|
||||
|
||||
struct state *state = ctx.udata;
|
||||
State *state = ctx.udata;
|
||||
|
||||
if (input_is_action_just_pressed(&ctx.input, "debug_toggle")) {
|
||||
ctx.debug = !ctx.debug;
|
||||
@ -67,7 +67,7 @@ void game_tick(void) {
|
||||
|
||||
|
||||
void game_end(void) {
|
||||
struct state *state = ctx.udata;
|
||||
State *state = ctx.udata;
|
||||
state->scene->end(state);
|
||||
free(state);
|
||||
}
|
||||
|
@ -10,14 +10,14 @@
|
||||
#include <tgmath.h>
|
||||
|
||||
|
||||
static void update_timers(struct player *player) {
|
||||
static void update_timers(Player *player) {
|
||||
tick_timer(&player->jump_air_timer);
|
||||
tick_timer(&player->jump_coyote_timer);
|
||||
tick_timer(&player->jump_buffer_timer);
|
||||
}
|
||||
|
||||
|
||||
static void input_move(struct input_state *input, struct player *player) {
|
||||
static void input_move(InputState *input, Player *player) {
|
||||
/* apply horizontal damping when the player stops moving */
|
||||
/* in other words, make it decelerate to a standstill */
|
||||
if (!input_is_action_pressed(input, "player_left") &&
|
||||
@ -44,7 +44,7 @@ static void input_move(struct input_state *input, struct player *player) {
|
||||
}
|
||||
|
||||
|
||||
static void jump(struct player *player) {
|
||||
static void jump(Player *player) {
|
||||
player->jump_coyote_timer = 0;
|
||||
player->jump_buffer_timer = 0;
|
||||
player->dy = player->jump_force_initial;
|
||||
@ -53,7 +53,7 @@ static void jump(struct player *player) {
|
||||
}
|
||||
|
||||
|
||||
static void input_jump(struct input_state *input, struct player *player) {
|
||||
static void input_jump(InputState *input, Player *player) {
|
||||
player->current_gravity_multiplier = player->jump_default_multiplier;
|
||||
|
||||
if (input_is_action_just_pressed(input, "player_jump")) {
|
||||
@ -74,7 +74,7 @@ static void input_jump(struct input_state *input, struct player *player) {
|
||||
}
|
||||
|
||||
|
||||
static void update_collider_x(struct player *player) {
|
||||
static void update_collider_x(Player *player) {
|
||||
player->collider_x.w = player->rect.w;
|
||||
player->collider_x.h = player->rect.h - 8;
|
||||
player->collider_x.x = player->rect.x;
|
||||
@ -82,7 +82,7 @@ static void update_collider_x(struct player *player) {
|
||||
}
|
||||
|
||||
|
||||
static void update_collider_y(struct player *player) {
|
||||
static void update_collider_y(Player *player) {
|
||||
player->collider_y.w = player->rect.w;
|
||||
player->collider_y.h = player->rect.h;
|
||||
player->collider_y.x = player->rect.x + ((player->rect.w - player->collider_y.w) / 2);
|
||||
@ -90,7 +90,7 @@ static void update_collider_y(struct player *player) {
|
||||
}
|
||||
|
||||
|
||||
static void apply_gravity(struct player *player, float gravity) {
|
||||
static void apply_gravity(Player *player, float gravity) {
|
||||
player->dy -= gravity * player->current_gravity_multiplier;
|
||||
player->dy = fmax(player->dy, -player->terminal_velocity);
|
||||
|
||||
@ -106,7 +106,7 @@ static void apply_gravity(struct player *player, float gravity) {
|
||||
|
||||
|
||||
/* returns whether or not a correction was applied */
|
||||
static bool corner_correct(struct player *player, t_frect collision) {
|
||||
static bool corner_correct(Player *player, Rect collision) {
|
||||
/*
|
||||
* somewhat of a hack here. we only want to do corner correction
|
||||
* if the corner in question really is the corner of a "platform,"
|
||||
@ -145,16 +145,16 @@ static bool corner_correct(struct player *player, t_frect collision) {
|
||||
}
|
||||
|
||||
|
||||
static void calc_collisions_x(struct player *player) {
|
||||
t_frect collision;
|
||||
static void calc_collisions_x(Player *player) {
|
||||
Rect collision;
|
||||
bool is_colliding = world_find_intersect_frect(player->world, player->collider_x, &collision);
|
||||
if (!is_colliding) return;
|
||||
|
||||
float player_center_x = player->collider_x.x + (player->collider_x.w / 2);
|
||||
float collision_center_x = collision.x + (collision.w / 2);
|
||||
|
||||
enum collision_direction { COLLISION_LEFT = -1, COLLISION_RIGHT = 1 };
|
||||
enum collision_direction dir_x =
|
||||
typedef enum CollisionDirection { COLLISION_LEFT = -1, COLLISION_RIGHT = 1 } CollisionDirection;
|
||||
CollisionDirection dir_x =
|
||||
player_center_x > collision_center_x ? COLLISION_LEFT : COLLISION_RIGHT;
|
||||
|
||||
player->rect.x -= collision.w * (float)dir_x;
|
||||
@ -162,16 +162,16 @@ static void calc_collisions_x(struct player *player) {
|
||||
}
|
||||
|
||||
|
||||
static void calc_collisions_y(struct player *player) {
|
||||
t_frect collision;
|
||||
static void calc_collisions_y(Player *player) {
|
||||
Rect collision;
|
||||
bool is_colliding = world_find_intersect_frect(player->world, player->collider_y, &collision);
|
||||
if (!is_colliding) return;
|
||||
|
||||
float player_center_y = player->collider_y.y + (player->collider_y.h / 2);
|
||||
float collision_center_y = collision.y + (collision.h / 2);
|
||||
|
||||
enum collision_direction { COLLISION_ABOVE = -1, COLLISION_BELOW = 1 };
|
||||
enum collision_direction dir_y =
|
||||
typedef enum CollisionDirection { COLLISION_ABOVE = -1, COLLISION_BELOW = 1 } CollisionDirection;
|
||||
CollisionDirection dir_y =
|
||||
player_center_y > collision_center_y ? COLLISION_ABOVE : COLLISION_BELOW;
|
||||
|
||||
/* before the resolution */
|
||||
@ -202,16 +202,16 @@ static void calc_collisions_y(struct player *player) {
|
||||
}
|
||||
|
||||
|
||||
struct player *player_create(struct world *world) {
|
||||
struct player *player = cmalloc(sizeof *player);
|
||||
Player *player_create(World *world) {
|
||||
Player *player = cmalloc(sizeof *player);
|
||||
|
||||
*player = (struct player) {
|
||||
*player = (Player) {
|
||||
.world = world,
|
||||
|
||||
.sprite_w = 48,
|
||||
.sprite_h = 48,
|
||||
|
||||
.rect = (t_frect) {
|
||||
.rect = (Rect) {
|
||||
.x = 92,
|
||||
.y = 200,
|
||||
.w = 16,
|
||||
@ -243,22 +243,22 @@ struct player *player_create(struct world *world) {
|
||||
}
|
||||
|
||||
|
||||
static void drawdef(struct player *player) {
|
||||
static void drawdef(Player *player) {
|
||||
m_sprite("/assets/player/baron-walk.png",
|
||||
(t_frect) {
|
||||
(Rect) {
|
||||
.x = player->rect.x + ((player->rect.w - player->sprite_w) / 2),
|
||||
.y = player->rect.y - 8,
|
||||
.w = player->sprite_w,
|
||||
.h = player->sprite_h,
|
||||
});
|
||||
|
||||
push_circle((t_fvec2) { 256, 128 },
|
||||
push_circle((Vec2) { 256, 128 },
|
||||
24,
|
||||
(t_color) { 255, 0, 0, 255 });
|
||||
(Color) { 255, 0, 0, 255 });
|
||||
}
|
||||
|
||||
|
||||
static void drawdef_debug(struct player *player) {
|
||||
static void drawdef_debug(Player *player) {
|
||||
if (!ctx.debug)
|
||||
return;
|
||||
|
||||
@ -269,19 +269,19 @@ static void drawdef_debug(struct player *player) {
|
||||
/* }; */
|
||||
|
||||
push_rectangle(player->collider_x,
|
||||
(t_color){ 0, 0, 255, 128 });
|
||||
(Color){ 0, 0, 255, 128 });
|
||||
|
||||
push_rectangle(player->collider_y,
|
||||
(t_color){ 0, 0, 255, 128 });
|
||||
(Color){ 0, 0, 255, 128 });
|
||||
}
|
||||
|
||||
|
||||
void player_destroy(struct player *player) {
|
||||
void player_destroy(Player *player) {
|
||||
free(player);
|
||||
}
|
||||
|
||||
|
||||
void player_calc(struct player *player) {
|
||||
void player_calc(Player *player) {
|
||||
update_timers(player);
|
||||
|
||||
input_move(&ctx.input, player);
|
||||
|
@ -4,32 +4,32 @@
|
||||
#include "twn_game_api.h"
|
||||
|
||||
|
||||
struct world;
|
||||
typedef struct World World;
|
||||
|
||||
|
||||
enum player_action {
|
||||
typedef enum PlayerAction {
|
||||
PLAYER_ACTION_GROUND,
|
||||
PLAYER_ACTION_FALL,
|
||||
PLAYER_ACTION_JUMP,
|
||||
};
|
||||
} PlayerAction;
|
||||
|
||||
|
||||
struct player {
|
||||
struct world *world;
|
||||
typedef struct Player {
|
||||
World *world;
|
||||
|
||||
/* visual */
|
||||
float sprite_w;
|
||||
float sprite_h;
|
||||
|
||||
/* body */
|
||||
t_frect rect;
|
||||
Rect rect;
|
||||
|
||||
/* state */
|
||||
enum player_action action;
|
||||
PlayerAction action;
|
||||
|
||||
/* physics */
|
||||
t_frect collider_x;
|
||||
t_frect collider_y;
|
||||
Rect collider_x;
|
||||
Rect collider_y;
|
||||
int collider_thickness;
|
||||
float dx;
|
||||
float dy;
|
||||
@ -54,12 +54,12 @@ struct player {
|
||||
float jump_boosted_multiplier;
|
||||
|
||||
float jump_corner_correction_offset; /* from center */
|
||||
};
|
||||
} Player;
|
||||
|
||||
|
||||
struct player *player_create(struct world *world);
|
||||
void player_destroy(struct player *player);
|
||||
void player_calc(struct player *player);
|
||||
Player *player_create(World *world);
|
||||
void player_destroy(Player *player);
|
||||
void player_calc(Player *player);
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -8,8 +8,8 @@
|
||||
#include <stb_perlin.h>
|
||||
|
||||
|
||||
static void ingame_tick(struct state *state) {
|
||||
struct scene_ingame *scn = (struct scene_ingame *)state->scene;
|
||||
static void ingame_tick(State *state) {
|
||||
SceneIngame *scn = (SceneIngame *)state->scene;
|
||||
|
||||
world_drawdef(scn->world);
|
||||
player_calc(scn->player);
|
||||
@ -23,26 +23,26 @@ static void ingame_tick(struct state *state) {
|
||||
const float yaw_rad = scn->yaw * (float)DEG2RAD;
|
||||
const float pitch_rad = scn->pitch * (float)DEG2RAD;
|
||||
|
||||
scn->cam.target = m_vec_norm(((t_fvec3){
|
||||
scn->cam.target = m_vec_norm(((Vec3){
|
||||
cosf(yaw_rad) * cosf(pitch_rad),
|
||||
sinf(pitch_rad),
|
||||
sinf(yaw_rad) * cosf(pitch_rad)
|
||||
}));
|
||||
}
|
||||
|
||||
const t_fvec3 right = m_vec_norm(m_vec_cross(scn->cam.target, scn->cam.up));
|
||||
const Vec3 right = m_vec_norm(m_vec_cross(scn->cam.target, scn->cam.up));
|
||||
const float speed = 0.04f; /* TODO: put this in a better place */
|
||||
if (input_is_action_pressed(&ctx.input, "player_left"))
|
||||
scn->cam.pos = fvec3_sub(scn->cam.pos, m_vec_scale(right, speed));
|
||||
scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(right, speed));
|
||||
|
||||
if (input_is_action_pressed(&ctx.input, "player_right"))
|
||||
scn->cam.pos = fvec3_add(scn->cam.pos, m_vec_scale(right, speed));
|
||||
scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(right, speed));
|
||||
|
||||
if (input_is_action_pressed(&ctx.input, "player_forward"))
|
||||
scn->cam.pos = fvec3_add(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
|
||||
scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
|
||||
|
||||
if (input_is_action_pressed(&ctx.input, "player_backward"))
|
||||
scn->cam.pos = fvec3_sub(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
|
||||
scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
|
||||
|
||||
if (input_is_action_pressed(&ctx.input, "player_jump"))
|
||||
scn->cam.pos.y += speed;
|
||||
@ -56,27 +56,27 @@ static void ingame_tick(struct state *state) {
|
||||
}
|
||||
|
||||
m_sprite(m_set(path, "/assets/9slice.png"),
|
||||
m_set(rect, ((t_frect){ 16, 16, 128, 128 })),
|
||||
m_opt(texture_region, ((t_frect){ 0, 0, (float)(ctx.tick_count % 48), (float)(ctx.tick_count % 48) })));
|
||||
m_set(rect, ((Rect){ 16, 16, 128, 128 })),
|
||||
m_opt(texture_region, ((Rect){ 0, 0, (float)(ctx.tick_count % 48), (float)(ctx.tick_count % 48) })));
|
||||
|
||||
m_sprite(m_set(path, "/assets/light.png"),
|
||||
m_set(rect, ((t_frect){ 48, 64, 64, 64 })),
|
||||
m_opt(color, ((t_color){ 255, 0, 0, 255 })));
|
||||
m_set(rect, ((Rect){ 48, 64, 64, 64 })),
|
||||
m_opt(color, ((Color){ 255, 0, 0, 255 })));
|
||||
|
||||
m_sprite(m_set(path, "/assets/light.png"),
|
||||
m_set(rect, ((t_frect){ 64, 64, 64, 64 })),
|
||||
m_opt(color, ((t_color){ 0, 255, 0, 255 })));
|
||||
m_set(rect, ((Rect){ 64, 64, 64, 64 })),
|
||||
m_opt(color, ((Color){ 0, 255, 0, 255 })));
|
||||
|
||||
m_sprite(m_set(path, "/assets/light.png"),
|
||||
m_set(rect, ((t_frect){ 80, 64, 64, 64 })),
|
||||
m_opt(color, ((t_color){ 0, 0, 255, 255 })));
|
||||
m_set(rect, ((Rect){ 80, 64, 64, 64 })),
|
||||
m_opt(color, ((Color){ 0, 0, 255, 255 })));
|
||||
|
||||
m_sprite(m_set(path, "/assets/player/baron-walk.png"),
|
||||
m_set(rect, ((t_frect){ 32, 32, 64, 64 })),
|
||||
m_set(rect, ((Rect){ 32, 32, 64, 64 })),
|
||||
m_opt(rotation, (float)M_PI * 2 * (float)(ctx.tick_count % 64) / 64 ));
|
||||
|
||||
m_sprite(m_set(path, "/assets/player/baron-walk.png"),
|
||||
m_set(rect, ((t_frect){ 128, 32, 128, 64 })),
|
||||
m_set(rect, ((Rect){ 128, 32, 128, 64 })),
|
||||
m_opt(rotation, (float)M_PI * 2 * (float)(ctx.tick_count % 64) / 64 ));
|
||||
|
||||
set_camera(&scn->cam);
|
||||
@ -91,51 +91,51 @@ static void ingame_tick(struct state *state) {
|
||||
float d3 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 20 - 6;
|
||||
|
||||
unfurl_triangle("/assets/grass.gif",
|
||||
(t_fvec3){ (float)x, d0, (float)y },
|
||||
(t_fvec3){ (float)x + 1, d1, (float)y },
|
||||
(t_fvec3){ (float)x, d3, (float)y - 1 },
|
||||
(t_shvec2){ 1024, 768 },
|
||||
(t_shvec2){ 1024, 0 },
|
||||
(t_shvec2){ 0, 768 });
|
||||
(Vec3){ (float)x, d0, (float)y },
|
||||
(Vec3){ (float)x + 1, d1, (float)y },
|
||||
(Vec3){ (float)x, d3, (float)y - 1 },
|
||||
(Vec2sh){ 1024, 768 },
|
||||
(Vec2sh){ 1024, 0 },
|
||||
(Vec2sh){ 0, 768 });
|
||||
|
||||
unfurl_triangle("/assets/grass.gif",
|
||||
(t_fvec3){ (float)x + 1, d1, (float)y },
|
||||
(t_fvec3){ (float)x + 1, d2, (float)y - 1 },
|
||||
(t_fvec3){ (float)x, d3, (float)y - 1 },
|
||||
(t_shvec2){ 1024, 0 },
|
||||
(t_shvec2){ 0, 0 },
|
||||
(t_shvec2){ 0, 768 });
|
||||
(Vec3){ (float)x + 1, d1, (float)y },
|
||||
(Vec3){ (float)x + 1, d2, (float)y - 1 },
|
||||
(Vec3){ (float)x, d3, (float)y - 1 },
|
||||
(Vec2sh){ 1024, 0 },
|
||||
(Vec2sh){ 0, 0 },
|
||||
(Vec2sh){ 0, 768 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void ingame_end(struct state *state) {
|
||||
struct scene_ingame *scn = (struct scene_ingame *)state->scene;
|
||||
static void ingame_end(State *state) {
|
||||
SceneIngame *scn = (SceneIngame *)state->scene;
|
||||
player_destroy(scn->player);
|
||||
world_destroy(scn->world);
|
||||
free(state->scene);
|
||||
}
|
||||
|
||||
|
||||
struct scene *ingame_scene(struct state *state) {
|
||||
Scene *ingame_scene(State *state) {
|
||||
(void)state;
|
||||
|
||||
struct scene_ingame *new_scene = ccalloc(1, sizeof *new_scene);
|
||||
SceneIngame *new_scene = ccalloc(1, sizeof *new_scene);
|
||||
new_scene->base.tick = ingame_tick;
|
||||
new_scene->base.end = ingame_end;
|
||||
|
||||
new_scene->world = world_create();
|
||||
new_scene->player = player_create(new_scene->world);
|
||||
|
||||
new_scene->cam = (t_camera){ .pos = { 32, 0, 1 }, .up = { 0, 1, 0 }, .fov = (float)M_PI_2 };
|
||||
new_scene->cam = (Camera){ .pos = { 32, 0, 1 }, .up = { 0, 1, 0 }, .fov = (float)M_PI_2 };
|
||||
|
||||
play_audio_ex("music/mod65.xm", "soundtrack", (t_play_audio_args){
|
||||
audio_play_ex("music/mod65.xm", "soundtrack", (PlayAudioArgs){
|
||||
.repeat = true,
|
||||
.volume = 1.0f
|
||||
});
|
||||
|
||||
input_set_mouse_captured(&ctx.input, true);
|
||||
|
||||
return (struct scene *)new_scene;
|
||||
return (Scene *)new_scene;
|
||||
}
|
||||
|
@ -9,22 +9,22 @@
|
||||
#include "../world.h"
|
||||
|
||||
|
||||
struct scene_ingame {
|
||||
struct scene base;
|
||||
typedef struct SceneIngame {
|
||||
Scene base;
|
||||
|
||||
struct world *world;
|
||||
struct player *player;
|
||||
World *world;
|
||||
Player *player;
|
||||
|
||||
t_camera cam;
|
||||
Camera cam;
|
||||
|
||||
/* TODO: put this in a better place */
|
||||
float yaw;
|
||||
float pitch;
|
||||
float roll;
|
||||
};
|
||||
} SceneIngame;
|
||||
|
||||
|
||||
struct scene *ingame_scene(struct state *state);
|
||||
Scene *ingame_scene(State *state);
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -2,7 +2,7 @@
|
||||
#include "../state.h"
|
||||
|
||||
|
||||
void switch_to(struct state *state, struct scene *(*scene_func)(struct state *)) {
|
||||
void switch_to(State *state, Scene *(*scene_func)(State *)) {
|
||||
state->next_scene = scene_func(state);
|
||||
state->is_scene_switching = true;
|
||||
}
|
||||
|
@ -2,15 +2,15 @@
|
||||
#define SCENE_H
|
||||
|
||||
|
||||
struct state;
|
||||
struct scene {
|
||||
typedef struct State State;
|
||||
typedef struct Scene {
|
||||
char *id;
|
||||
void (*tick)(struct state *);
|
||||
void (*end)(struct state *);
|
||||
};
|
||||
void (*tick)(State *);
|
||||
void (*end)(State *);
|
||||
} Scene;
|
||||
|
||||
|
||||
void switch_to(struct state *state, struct scene *(*scene_func)(struct state *));
|
||||
void switch_to(State *state, Scene *(*scene_func)(State *));
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -8,8 +8,8 @@
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
static void title_tick(struct state *state) {
|
||||
struct scene_title *scn = (struct scene_title *)state->scene;
|
||||
static void title_tick(State *state) {
|
||||
SceneTitle *scn = (SceneTitle *)state->scene;
|
||||
(void)scn;
|
||||
|
||||
if (input_is_action_just_pressed(&state->ctx->input, "ui_accept")) {
|
||||
@ -17,7 +17,7 @@ static void title_tick(struct state *state) {
|
||||
}
|
||||
|
||||
|
||||
m_sprite("/assets/title.png", ((t_frect) {
|
||||
m_sprite("/assets/title.png", ((Rect) {
|
||||
((float)RENDER_BASE_WIDTH / 2) - ((float)320 / 2), 64, 320, 128 }));
|
||||
|
||||
|
||||
@ -31,42 +31,42 @@ static void title_tick(struct state *state) {
|
||||
int text_w = get_text_width(text_str, text_h, font);
|
||||
|
||||
push_rectangle(
|
||||
(t_frect) {
|
||||
(Rect) {
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.w = (float)text_w,
|
||||
.h = (float)text_h,
|
||||
},
|
||||
(t_color) { 0, 0, 0, 255 }
|
||||
(Color) { 0, 0, 0, 255 }
|
||||
);
|
||||
push_text(
|
||||
text_str,
|
||||
(t_fvec2){ 0, 0 },
|
||||
(Vec2){ 0, 0 },
|
||||
text_h,
|
||||
(t_color) { 255, 255, 255, 255 },
|
||||
(Color) { 255, 255, 255, 255 },
|
||||
font
|
||||
);
|
||||
free(text_str);
|
||||
}
|
||||
|
||||
|
||||
static void title_end(struct state *state) {
|
||||
struct scene_title *scn = (struct scene_title *)state->scene;
|
||||
static void title_end(State *state) {
|
||||
SceneTitle *scn = (SceneTitle *)state->scene;
|
||||
player_destroy(scn->player);
|
||||
world_destroy(scn->world);
|
||||
free(state->scene);
|
||||
}
|
||||
|
||||
|
||||
struct scene *title_scene(struct state *state) {
|
||||
Scene *title_scene(State *state) {
|
||||
(void)state;
|
||||
|
||||
struct scene_title *new_scene = ccalloc(1, sizeof *new_scene);
|
||||
SceneTitle *new_scene = ccalloc(1, sizeof *new_scene);
|
||||
new_scene->base.tick = title_tick;
|
||||
new_scene->base.end = title_end;
|
||||
|
||||
new_scene->world = world_create();
|
||||
new_scene->player = player_create(new_scene->world);
|
||||
|
||||
return (struct scene *)new_scene;
|
||||
return (Scene *)new_scene;
|
||||
}
|
||||
|
@ -7,15 +7,15 @@
|
||||
#include "../world.h"
|
||||
|
||||
|
||||
struct scene_title {
|
||||
struct scene base;
|
||||
typedef struct SceneTitle {
|
||||
Scene base;
|
||||
|
||||
struct world *world;
|
||||
struct player *player;
|
||||
};
|
||||
World *world;
|
||||
Player *player;
|
||||
} SceneTitle;
|
||||
|
||||
|
||||
struct scene *title_scene(struct state *state);
|
||||
Scene *title_scene(State *state);
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -6,12 +6,15 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
struct state {
|
||||
t_ctx *ctx;
|
||||
struct scene *scene;
|
||||
struct scene *next_scene;
|
||||
|
||||
typedef struct Scene Scene;
|
||||
|
||||
typedef struct State {
|
||||
Context *ctx;
|
||||
Scene *scene;
|
||||
Scene *next_scene;
|
||||
bool is_scene_switching;
|
||||
};
|
||||
} State;
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -8,11 +8,11 @@
|
||||
#include <tgmath.h>
|
||||
|
||||
|
||||
static void update_tiles(struct world *world) {
|
||||
static void update_tiles(struct World *world) {
|
||||
for (size_t row = 0; row < world->tilemap_height; ++row) {
|
||||
for (size_t col = 0; col < world->tilemap_width; ++col) {
|
||||
world->tiles[(row * world->tilemap_width) + col] = (struct tile) {
|
||||
.rect = (t_rect) {
|
||||
world->tiles[(row * world->tilemap_width) + col] = (struct Tile) {
|
||||
.rect = (Recti) {
|
||||
.x = (int)col * world->tile_size,
|
||||
.y = (int)row * world->tile_size,
|
||||
.w = world->tile_size,
|
||||
@ -25,30 +25,30 @@ static void update_tiles(struct world *world) {
|
||||
}
|
||||
|
||||
|
||||
static t_vec2 to_grid_location(struct world *world, float x, float y) {
|
||||
return (t_vec2) {
|
||||
static Vec2i to_grid_location(struct World *world, float x, float y) {
|
||||
return (Vec2i) {
|
||||
.x = (int)floor(x / (float)world->tile_size),
|
||||
.y = (int)floor(y / (float)world->tile_size),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
static void drawdef_debug(struct world *world) {
|
||||
static void drawdef_debug(struct World *world) {
|
||||
if (!ctx.debug) return;
|
||||
|
||||
for (size_t i = 0; i < world->tilemap_height * world->tilemap_width; ++i) {
|
||||
if (world->tiles[i].type == TILE_TYPE_VOID) continue;
|
||||
|
||||
push_rectangle(to_frect(world->tiles[i].rect),
|
||||
(t_color) { 255, 0, 255, 128 });
|
||||
(Color) { 255, 0, 255, 128 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct world *world_create(void) {
|
||||
struct world *world = cmalloc(sizeof *world);
|
||||
struct World *world_create(void) {
|
||||
struct World *world = cmalloc(sizeof *world);
|
||||
|
||||
*world = (struct world) {
|
||||
*world = (struct World) {
|
||||
.tiles = NULL,
|
||||
.tile_size = 42,
|
||||
.tilemap_width = 20,
|
||||
@ -89,7 +89,7 @@ struct world *world_create(void) {
|
||||
}
|
||||
|
||||
|
||||
void world_destroy(struct world *world) {
|
||||
void world_destroy(struct World *world) {
|
||||
free(world->tiles);
|
||||
|
||||
for (size_t i = 0; i < world->tilemap_height; ++i) {
|
||||
@ -101,7 +101,7 @@ void world_destroy(struct world *world) {
|
||||
}
|
||||
|
||||
|
||||
void world_drawdef(struct world *world) {
|
||||
void world_drawdef(struct World *world) {
|
||||
for (size_t i = 0; i < world->tilemap_height * world->tilemap_width; ++i) {
|
||||
if (world->tiles[i].type == TILE_TYPE_VOID)
|
||||
continue;
|
||||
@ -113,7 +113,7 @@ void world_drawdef(struct world *world) {
|
||||
}
|
||||
|
||||
|
||||
bool world_find_intersect_frect(struct world *world, t_frect rect, t_frect *intersection) {
|
||||
bool world_find_intersect_frect(struct World *world, Rect rect, Rect *intersection) {
|
||||
bool is_intersecting = false;
|
||||
|
||||
const size_t tile_count = world->tilemap_height * world->tilemap_width;
|
||||
@ -121,7 +121,7 @@ bool world_find_intersect_frect(struct world *world, t_frect rect, t_frect *inte
|
||||
if (world->tiles[i].type == TILE_TYPE_VOID)
|
||||
continue;
|
||||
|
||||
t_frect tile_frect = {
|
||||
Rect tile_frect = {
|
||||
.x = (float)(world->tiles[i].rect.x),
|
||||
.y = (float)(world->tiles[i].rect.y),
|
||||
.w = (float)(world->tiles[i].rect.w),
|
||||
@ -129,7 +129,7 @@ bool world_find_intersect_frect(struct world *world, t_frect rect, t_frect *inte
|
||||
};
|
||||
|
||||
if (intersection == NULL) {
|
||||
t_frect temp;
|
||||
Rect temp;
|
||||
is_intersecting = overlap_frect(&rect, &tile_frect, &temp);
|
||||
} else {
|
||||
is_intersecting = overlap_frect(&rect, &tile_frect, intersection);
|
||||
@ -143,7 +143,7 @@ bool world_find_intersect_frect(struct world *world, t_frect rect, t_frect *inte
|
||||
}
|
||||
|
||||
|
||||
bool world_find_intersect_rect(struct world *world, t_rect rect, t_rect *intersection) {
|
||||
bool world_find_intersect_rect(struct World *world, Recti rect, Recti *intersection) {
|
||||
bool is_intersecting = false;
|
||||
|
||||
const size_t tile_count = world->tilemap_height * world->tilemap_width;
|
||||
@ -151,10 +151,10 @@ bool world_find_intersect_rect(struct world *world, t_rect rect, t_rect *interse
|
||||
if (world->tiles[i].type == TILE_TYPE_VOID)
|
||||
continue;
|
||||
|
||||
t_rect *tile_rect = &world->tiles[i].rect;
|
||||
Recti *tile_rect = &world->tiles[i].rect;
|
||||
|
||||
if (intersection == NULL) {
|
||||
t_rect temp;
|
||||
Recti temp;
|
||||
is_intersecting = overlap_rect(&rect, tile_rect, &temp);
|
||||
} else {
|
||||
is_intersecting = overlap_rect(&rect, tile_rect, intersection);
|
||||
@ -168,21 +168,21 @@ bool world_find_intersect_rect(struct world *world, t_rect rect, t_rect *interse
|
||||
}
|
||||
|
||||
|
||||
bool world_is_tile_at(struct world *world, float x, float y) {
|
||||
t_vec2 position_in_grid = to_grid_location(world, x, y);
|
||||
bool world_is_tile_at(struct World *world, float x, float y) {
|
||||
Vec2i position_in_grid = to_grid_location(world, x, y);
|
||||
return world->tilemap[position_in_grid.y][position_in_grid.x] != TILE_TYPE_VOID;
|
||||
}
|
||||
|
||||
|
||||
void world_place_tile(struct world *world, float x, float y) {
|
||||
t_vec2 position_in_grid = to_grid_location(world, x, y);
|
||||
void world_place_tile(struct World *world, float x, float y) {
|
||||
Vec2i position_in_grid = to_grid_location(world, x, y);
|
||||
world->tilemap[position_in_grid.y][position_in_grid.x] = TILE_TYPE_SOLID;
|
||||
update_tiles(world);
|
||||
}
|
||||
|
||||
|
||||
void world_remove_tile(struct world *world, float x, float y) {
|
||||
t_vec2 position_in_grid = to_grid_location(world, x, y);
|
||||
void world_remove_tile(struct World *world, float x, float y) {
|
||||
Vec2i position_in_grid = to_grid_location(world, x, y);
|
||||
world->tilemap[position_in_grid.y][position_in_grid.x] = TILE_TYPE_VOID;
|
||||
update_tiles(world);
|
||||
}
|
||||
|
@ -1,42 +1,44 @@
|
||||
#ifndef WORLD_H
|
||||
#define WORLD_H
|
||||
|
||||
#include "twn_game_api.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
enum tile_type {
|
||||
typedef enum TileType {
|
||||
TILE_TYPE_VOID,
|
||||
TILE_TYPE_SOLID,
|
||||
};
|
||||
} TileType;
|
||||
|
||||
|
||||
struct tile {
|
||||
t_rect rect;
|
||||
enum tile_type type;
|
||||
};
|
||||
typedef struct Tile {
|
||||
Recti rect;
|
||||
TileType type;
|
||||
} Tile;
|
||||
|
||||
|
||||
struct world {
|
||||
enum tile_type **tilemap;
|
||||
struct tile *tiles;
|
||||
typedef struct World {
|
||||
TileType **tilemap;
|
||||
Tile *tiles;
|
||||
|
||||
int tile_size;
|
||||
unsigned int tilemap_width;
|
||||
unsigned int tilemap_height;
|
||||
size_t tile_nonvoid_count;
|
||||
float gravity;
|
||||
};
|
||||
} World;
|
||||
|
||||
|
||||
struct world *world_create(void);
|
||||
void world_destroy(struct world *world);
|
||||
void world_drawdef(struct world *world);
|
||||
bool world_find_intersect_frect(struct world *world, t_frect rect, t_frect *intersection);
|
||||
bool world_find_intersect_rect(struct world *world, t_rect rect, t_rect *intersection);
|
||||
bool world_is_tile_at(struct world *world, float x, float y);
|
||||
void world_place_tile(struct world *world, float x, float y);
|
||||
void world_remove_tile(struct world *world, float x, float y);
|
||||
World *world_create(void);
|
||||
void world_destroy(World *world);
|
||||
void world_drawdef(World *world);
|
||||
bool world_find_intersect_frect(World *world, Rect rect, Rect *intersection);
|
||||
bool world_find_intersect_rect(World *world, Recti rect, Recti *intersection);
|
||||
bool world_is_tile_at(World *world, float x, float y);
|
||||
void world_place_tile(World *world, float x, float y);
|
||||
void world_remove_tile(World *world, float x, float y);
|
||||
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user