a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit 71d684de3b226f7d186ecf86f644e0d4a4e26286
parent ae19785db0f7a44b4deb4aedae6dd74c620fa5c4
Author: amin <dev@aminmesbah.com>
Date:   Sat, 20 Apr 2019 21:09:41 +0000

Do super janky collision resolution

FossilOrigin-Name: 13c03856e1a5460ca8dbf6e261ba832ae3682f0bd31c97dd67aeacdb0417bbaa
Diffstat:
Msrc/game.c | 57++++++++++++++++++++++++++++++++++++---------------------
Msrc/glmth.h | 11+++++++++++
2 files changed, 47 insertions(+), 21 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -14,7 +14,7 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) struct GameState *game_state = game_memory->game_state; // init player - game_state->player.pos = (v2) {1.0f, 1.0f}; + game_state->player.pos = (v2) {10.0f, 5.0f}; // In Knytt, the player is 9 by 14 texels and a tile is 24 by 24 texels. // These dimensions are relative to a square 'meter', one tile @@ -124,6 +124,24 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga // Screen origin is in the upper left m4 projection = glmth_projection_ortho(0.0f, framebuffer.width, framebuffer.height, 0.0f, -1.0f, 0.0f); + // the little hanging fly village from knytt + u32 tiles[ROOM_TILE_DIM_Y][ROOM_TILE_DIM_X] = { + { 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }, + { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }, + { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }, + { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }, + + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, + }; + + // One tile is one square meter + f32 tile_size = 1.0f; + // update player { struct Entity *player = &game_state->player; @@ -175,27 +193,10 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga glBindVertexArray(game_state->tiles.vao); shader_use(&game_state->tiles.shader); - // the little hanging fly village from knytt - u32 tiles[ROOM_TILE_DIM_Y][ROOM_TILE_DIM_X] = { - { 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }, - { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }, - { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }, - { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }, - - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, - }; - for (size_t y = 0; y < ROOM_TILE_DIM_Y; y++) { for (size_t x = 0; x < ROOM_TILE_DIM_X; x++) { - // One tile is one square meter - f32 tile_size = 1.0f; v2 tile_pos = { x, ROOM_TILE_DIM_Y - 1.0f - y, @@ -221,10 +222,12 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga } { - struct Entity player = game_state->player; + struct Entity *player = &game_state->player; + f32 half_w = 0.5f * player->dimensions.width; + f32 half_h = 0.5f * player->dimensions.height; rect player_aabb = { - .min = {player.pos.x - (0.5f * player.dimensions.width), player.pos.y - (0.5f * player.dimensions.height)}, - .max = {player.pos.x + (0.5f * player.dimensions.width), player.pos.y + (0.5f * player.dimensions.height)}, + .min = {player->pos.x - half_w, player->pos.y - half_h}, + .max = {player->pos.x + half_w, player->pos.y + half_h}, }; rect tile_aabb = { .min = tile_pos, @@ -238,6 +241,18 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga if (player_is_in_tile && tile_id > 0) { color = (v3) {0.8f, 0.4f, 0.4f}; + v2 tile_center = glmth_v2_a(tile_aabb.min, glmth_v2f_m(glmth_v2_s(tile_aabb.max, tile_aabb.min), 0.5f)); + v2 diff = glmth_v2_s(player->pos, tile_center); + if (x == 13 && y == 3) + { + printf("***"); + glmth_v2_print(player->pos); + printf("- "); + glmth_v2_print(tile_center); + printf("= "); + glmth_v2_print(diff); + } + player->pos = glmth_v2_a(player->pos, diff); } } diff --git a/src/glmth.h b/src/glmth.h @@ -134,6 +134,17 @@ internal inline v2 glmth_v2_a(v2 vec1, v2 vec2) return r; } +internal inline v2 glmth_v2_negate(v2 v) +{ + v2 r = {-v.x, -v.y}; + return r; +} + +internal inline v2 glmth_v2_s(v2 vec1, v2 vec2) +{ + return glmth_v2_a(vec1, glmth_v2_negate(vec2)); +} + internal inline v2 glmth_v2f_m(v2 v, f32 s) { v2 r = {v.x * s, v.y * s};