From 13744457164e5da1a26fa9b625143bde6e7276b5 Mon Sep 17 00:00:00 2001 From: Massena Date: Sun, 9 Jan 2022 18:54:15 +0100 Subject: [PATCH] hook part 5 -- nyooom --- include/player.h | 1 + include/tools.h | 3 +- src/hook.c | 24 +++++++------ src/player.c | 91 ++++++++++++++++++++++++++---------------------- src/tools.c | 6 ++-- 5 files changed, 68 insertions(+), 57 deletions(-) diff --git a/include/player.h b/include/player.h index 26ee226..22dde4f 100644 --- a/include/player.h +++ b/include/player.h @@ -14,3 +14,4 @@ struct Player { void player_init(void); void player_update(void); void player_draw(void); +struct Vec2 player_get_pos(void); diff --git a/include/tools.h b/include/tools.h index cc4db86..de1a414 100644 --- a/include/tools.h +++ b/include/tools.h @@ -1,4 +1,5 @@ #pragma once +#include "type.h" int sign(int x); -int length(int x1, int y1, int x2, int y2); +int length(struct Vec2 *a, struct Vec2 *b); diff --git a/src/hook.c b/src/hook.c index 7d2b720..53f4291 100644 --- a/src/hook.c +++ b/src/hook.c @@ -1,6 +1,7 @@ #include "hook.h" #include "conf.h" #include "level.h" +#include "player.h" #include "tools.h" #include #include @@ -19,6 +20,10 @@ hook_table_init(void) for (int i = 0; i < hook_table.n; ++i) { hook_table.hooks[i].pos = level_search_s(HOOK_T, i + 1); + hook_table.hooks[i].pos.x = + hook_table.hooks[i].pos.x * TILE_S + TILE_S / 2; + hook_table.hooks[i].pos.y = + hook_table.hooks[i].pos.y * TILE_S + TILE_S / 2; } /*hook_table.hooks[0].pos = level_search_s(HOOK_T, 1);*/ } @@ -44,19 +49,17 @@ hook_closest(struct Vec2 *pos) struct Vec2 shortest = {0, 0}; if (hook_table.n) { shortest = hook_table.hooks[0].pos; - float sh_dist = length(shortest.x, shortest.y, pos->x, pos->y); + float sh_dist = length(&shortest, pos); for (int i = 0; i < hook_table.n; ++i) { - const struct Hook h = hook_table.hooks[i]; - const float dist = - length(h.pos.x, h.pos.y, pos->x, pos->y); + struct Hook h = hook_table.hooks[i]; + const float dist = length(&h.pos, pos); if (dist < sh_dist) { shortest = h.pos; sh_dist = dist; } } } - shortest = (struct Vec2){shortest.x * TILE_S + TILE_S / 2, - shortest.y * TILE_S + TILE_S / 2}; + shortest = (struct Vec2){shortest.x, shortest.y}; return shortest; } @@ -66,10 +69,9 @@ hook_debug_draw(void) dprint(10, 20, C_WHITE, "%d", hook_table.n); 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)); + struct Vec2 player_pos = player_get_pos(); dprint(10, 40, C_WHITE, "%d", - (int)length(hook_table.hooks[1].pos.x, hook_table.hooks[1].pos.y, - 102, 192)); + (int)length(&hook_table.hooks[0].pos, &player_pos)); + dprint(50, 40, C_WHITE, "%d", + (int)length(&hook_table.hooks[1].pos, &player_pos)); } diff --git a/src/player.c b/src/player.c index c80259a..e096e44 100644 --- a/src/player.c +++ b/src/player.c @@ -19,6 +19,12 @@ static int player_collide(struct Vec2 pos, tile_t id, int h); extern bopti_image_t img_hooksel; +struct Vec2 +player_get_pos(void) +{ + return player.pos; +} + void player_init(void) { @@ -49,13 +55,7 @@ player_update(void) player.airbreak = 0; } - player.hooking = 0; - if (k_hook) { - player_hook(); - } else { - /* unbind the player from the hook */ - player.locked = 0; - } + player_hook(); if (!player.hooking) { player.spd.x *= (1 - PLAYER_FRIC); @@ -75,44 +75,50 @@ player_update(void) void player_hook(void) { + player.hooking = 0; 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 = 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; - player.jumping = 1; - } - - 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; - - struct FVec2 force = {}; - 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 = 16 * sign(player.spd.x); + if (k_hook) { + /* get the nearest hook */ + if (!player.locked) { + player.hook_pos = player.hook_near; } - if (abs(player.spd.y) > 16) { - player.spd.y = 16 * sign(player.spd.y); + + const float dist = length(&player_middle, &player.hook_pos); + + /* if the nearest hook is in range */ + if (dist < 100 && player_middle.y > player.hook_pos.y) { + player.hooking = 1; + player.locked = 1; + player.jumping = 1; } + + 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; + + struct FVec2 force = {}; + 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 = 16 * sign(player.spd.x); + } + if (abs(player.spd.y) > 16) { + player.spd.y = 16 * sign(player.spd.y); + } + } + } else { + /* unbind the player from the hook */ + player.locked = 0; } } @@ -128,9 +134,10 @@ player_draw(void) void player_hook_draw(void) { - 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) { + struct Vec2 player_middle = {player.pos.x + PLAYER_S / 2, + player.pos.y + PLAYER_S / 2}; + if (length(&player_middle, &player.hook_near) < 100 && + player_middle.y > player.hook_near.y) { dimage(player.hook_near.x - 5, player.hook_near.y - 5, &img_hooksel); }; diff --git a/src/tools.c b/src/tools.c index 35f4043..b45b648 100644 --- a/src/tools.c +++ b/src/tools.c @@ -9,10 +9,10 @@ sign(int x) } int -length(int x1, int y1, int x2, int y2) +length(struct Vec2 *a, struct Vec2 *b) { - const int dist_x = abs(x1 - x2); - const int dist_y = abs(y1 - y2); + const int dist_x = abs(a->x - b->x); + const int dist_y = abs(a->y - b->y); const float dist = sqrt(dist_x * dist_x + dist_y * dist_y); return dist; }