hook part 4 -- ok now it won't work >:o

This commit is contained in:
Masséna Fezard | Nounouille 2022-01-09 17:29:44 +01:00
parent 5a23856a3f
commit b793ce4a73
8 changed files with 42 additions and 30 deletions

View File

@ -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)

BIN
assets-cg/hooksel.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 B

View File

@ -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;
};

View File

@ -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,

View File

@ -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,

View File

@ -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));
}

View File

@ -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,

View File

@ -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