From b793ce4a7387ce9084b72743f3ede5f7fe039b28 Mon Sep 17 00:00:00 2001 From: Massena Date: Sun, 9 Jan 2022 17:29:44 +0100 Subject: [PATCH] hook part 4 -- ok now it won't work >:o --- CMakeLists.txt | 1 + assets-cg/hooksel.png | Bin 0 -> 114 bytes include/player.h | 2 +- levels/test.lua | 4 ++-- levels/test.tmx | 2 +- src/hook.c | 10 +++++++-- src/level.c | 2 +- src/player.c | 51 +++++++++++++++++++++++------------------- 8 files changed, 42 insertions(+), 30 deletions(-) create mode 100644 assets-cg/hooksel.png diff --git a/CMakeLists.txt b/CMakeLists.txt index d92e5cc..11c1cef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ set(SOURCES set(ASSETS_cg assets-cg/tileset.png + assets-cg/hooksel.png ) fxconv_declare_assets(${ASSETS} ${ASSETS_fx} ${ASSETS_cg} WITH_METADATA) diff --git a/assets-cg/hooksel.png b/assets-cg/hooksel.png new file mode 100644 index 0000000000000000000000000000000000000000..7226062469a5559f19228bf05d4af5123689e0f7 GIT binary patch literal 114 zcmeAS@N?(olHy`uVBq!ia0vp^AT}2V8<6ZZI=>f4F%}28J29*~C-V}>(eZR~43U_c z{OAAw|MhGd2`NC}z;vQ$g^bJtpwR!B9GX%=4X^g4shAyLl%6L4zU|7RE}%XJPgg&e IbxsLQ0O(sKod5s; literal 0 HcmV?d00001 diff --git a/include/player.h b/include/player.h index 01d2349..26ee226 100644 --- a/include/player.h +++ b/include/player.h @@ -7,7 +7,7 @@ struct Player { struct FVec2 rem; struct Vec2 spawn; struct Vec2 hook_pos; - struct FVec2 hook_mom; + struct Vec2 hook_near; int jumping, airbreak, hooking, locked; }; diff --git a/levels/test.lua b/levels/test.lua index d279607..131e90b 100644 --- a/levels/test.lua +++ b/levels/test.lua @@ -16,7 +16,7 @@ return { { name = "tileset", firstgid = 1, - filename = "../levels/tileset.tsx" + filename = "tileset.tsx" } }, layers = { @@ -47,7 +47,7 @@ return { 19, 22, 17, 25, 0, 0, 0, 0, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 58, 0, 57, 58, 57, 58, 57, 58, 57, 0, 58, 0, 0, 0, 0, 0, 0, 57, 0, 0, 26, 18, 21, 20, 20, 19, 17, 25, 0, 0, 0, 0, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 58, 58, 57, 58, 57, 58, 57, 58, 57, 58, 58, 0, 0, 0, 0, 0, 0, 57, 0, 0, 26, 18, 20, 20, 19, 22, 17, 25, 0, 0, 0, 0, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 57, 58, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 18, 22, 19, - 22, 21, 17, 25, 0, 0, 0, 0, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 42, 41, 42, 41, 42, 41, 42, 41, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 18, 22, 21, + 22, 21, 17, 25, 0, 0, 0, 0, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 42, 41, 42, 41, 42, 41, 42, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 18, 22, 21, 19, 21, 17, 25, 0, 0, 0, 0, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 18, 21, 22, 22, 21, 17, 25, 0, 0, 0, 0, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 58, 57, 58, 57, 58, 57, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 18, 21, 19, 19, 19, 17, 25, 0, 0, 0, 0, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 18, 21, 21, diff --git a/levels/test.tmx b/levels/test.tmx index ca7027b..c8812ae 100644 --- a/levels/test.tmx +++ b/levels/test.tmx @@ -16,7 +16,7 @@ 19,22,17,25,0,0,0,0,57,58,57,58,57,0,0,0,0,0,0,58,0,0,0,0,0,0,58,0,57,58,57,58,57,58,57,0,58,0,0,0,0,0,0,57,0,0,26,18,21,20, 20,19,17,25,0,0,0,0,57,58,57,58,57,0,0,0,0,0,0,58,0,0,0,0,0,0,58,58,57,58,57,58,57,58,57,58,58,0,0,0,0,0,0,57,0,0,26,18,20,20, 19,22,17,25,0,0,0,0,57,58,57,58,57,0,0,0,0,0,0,9,0,0,0,0,0,0,0,0,57,58,57,58,57,58,57,0,0,0,0,0,0,0,0,0,0,0,26,18,22,19, -22,21,17,25,0,0,0,0,57,58,57,58,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,42,41,42,41,42,41,42,41,0,0,0,0,0,0,0,0,0,0,26,18,22,21, +22,21,17,25,0,0,0,0,57,58,57,58,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0,41,42,41,42,41,42,41,42,9,0,0,0,0,0,0,0,0,0,0,26,18,22,21, 19,21,17,25,0,0,0,0,57,58,57,58,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,57,58,57,58,57,0,0,0,0,0,0,0,0,0,0,0,0,26,18,21,22, 22,21,17,25,0,0,0,0,57,58,57,58,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,58,57,58,57,58,57,58,0,0,0,0,0,0,0,0,0,0,0,26,18,21,19, 19,19,17,25,0,0,0,0,57,58,57,58,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,57,58,57,58,57,0,0,0,0,0,0,0,0,0,0,0,0,26,18,21,21, diff --git a/src/hook.c b/src/hook.c index 8402ab9..7d2b720 100644 --- a/src/hook.c +++ b/src/hook.c @@ -64,6 +64,12 @@ void hook_debug_draw(void) { dprint(10, 20, C_WHITE, "%d", hook_table.n); - dprint(30, 20, C_WHITE, "%d", hook_table.hooks[0].pos); - dprint(50, 20, C_WHITE, "%d", hook_table.hooks[0].pos); + dprint(30, 20, C_WHITE, "%d", hook_table.hooks[0].pos.x); + dprint(50, 20, C_WHITE, "%d", hook_table.hooks[1].pos.x); + dprint(30, 40, C_WHITE, "%d", + (int)length(hook_table.hooks[0].pos.x, hook_table.hooks[0].pos.y, + 192, 192)); + dprint(10, 40, C_WHITE, "%d", + (int)length(hook_table.hooks[1].pos.x, hook_table.hooks[1].pos.y, + 102, 192)); } diff --git a/src/level.c b/src/level.c index 10da37b..f6cb031 100644 --- a/src/level.c +++ b/src/level.c @@ -148,7 +148,7 @@ level_load(void) 0, 0, 57, 58, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 18, 22, 19, 22, 21, 17, 25, 0, 0, 0, 0, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 41, 42, 41, 42, 41, 42, 41, 42, 41, 0, 0, 0, 0, 0, 0, 0, 0, + 41, 42, 41, 42, 41, 42, 41, 42, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 18, 22, 21, 19, 21, 17, 25, 0, 0, 0, 0, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57, 58, 57, 58, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, diff --git a/src/player.c b/src/player.c index 843ebbe..c80259a 100644 --- a/src/player.c +++ b/src/player.c @@ -17,6 +17,8 @@ static struct Vec2 player_update_rem(); static int player_collide_pixel(int x, int y, tile_t id); static int player_collide(struct Vec2 pos, tile_t id, int h); +extern bopti_image_t img_hooksel; + void player_init(void) { @@ -27,7 +29,8 @@ player_init(void) player.airbreak = 1; player.hooking = 0; player.locked = 0; - player.hook_pos = (struct Vec2){50, 50}; + player.hook_pos = (struct Vec2){0, 0}; + player.hook_near = hook_closest(&player.pos); } void @@ -74,15 +77,16 @@ player_hook(void) { struct Vec2 player_middle = {player.pos.x + PLAYER_S / 2, player.pos.y + PLAYER_S / 2}; - + player.hook_near = hook_closest(&player_middle); /* get the nearest hook */ if (!player.locked) { - player.hook_pos = hook_closest(&player_middle); + player.hook_pos = player.hook_near; } const float dist = length(player_middle.x, player_middle.y, player.hook_pos.x, player.hook_pos.y); + /* if the nearest hook is in range */ if (dist < 100 && player_middle.y > player.hook_pos.y) { player.hooking = 1; player.locked = 1; @@ -90,31 +94,24 @@ player_hook(void) } if (player.hooking && dist) { + /* determining hook force */ float dist_x = player.hook_pos.x - player_middle.x; float dist_y = player.hook_pos.y - player_middle.y; - /*if (abs(dist_x) < 1) { - dist_x = 1 * sign(dist_x); - } - if (abs(dist_y) < 1) { - dist_y = 1 * sign(dist_y); - }*/ struct FVec2 force = {}; - if (dist_x) { - force.x = dist_x / dist; - } - if (dist_y) { - force.y = dist_y / dist; - } + force.x = dist_x / dist; + force.y = dist_y / dist; + /* apply hook force */ player.spd.x += force.x; player.spd.y += force.y; + /* cap speed */ if (abs(player.spd.x) > 16) { - player.spd.x = 8 * sign(player.spd.x); + player.spd.x = 16 * sign(player.spd.x); } - if (abs(player.spd.y) > 8) { - player.spd.y = 8 * sign(player.spd.y); + if (abs(player.spd.y) > 16) { + player.spd.y = 16 * sign(player.spd.y); } } } @@ -122,18 +119,26 @@ player_hook(void) void player_draw(void) { - if (player.hooking) { - player_hook_draw(); - } + player_hook_draw(); draw_rectangle(player.pos.x, player.pos.y, PLAYER_S, PLAYER_S, C_WHITE); dprint(10, 20, C_WHITE, "%f", player.spd.y); + dprint(70, 30, C_WHITE, "%d", hook_closest(&player.pos).x); } void player_hook_draw(void) { - dline(player.pos.x + PLAYER_S / 2, player.pos.y + PLAYER_S / 2, - player.hook_pos.x, player.hook_pos.y, C_RED); + if (length(player.pos.x + PLAYER_S / 2, player.pos.y + PLAYER_S / 2, + player.hook_near.x, player.hook_near.y) < 100 && + player.pos.y + PLAYER_S / 2 > player.hook_near.y) { + dimage(player.hook_near.x - 5, player.hook_near.y - 5, + &img_hooksel); + }; + + if (player.hooking) { + dline(player.pos.x + PLAYER_S / 2, player.pos.y + PLAYER_S / 2, + player.hook_pos.x, player.hook_pos.y, C_RED); + } } static void