hook part 2 -- electric boogaloo
This commit is contained in:
parent
9cd2b08884
commit
d553ec9bb1
|
@ -14,3 +14,4 @@ tile_t level_get_tile(int x, int y);
|
|||
int level_count(tile_t tile);
|
||||
int level_search_i(tile_t tile, int occ);
|
||||
struct Vec2 level_search_s(tile_t tile, int occ);
|
||||
struct Vec2 level_get_dim(void);
|
||||
|
|
|
@ -7,7 +7,8 @@ struct Player {
|
|||
struct FVec2 rem;
|
||||
struct Vec2 spawn;
|
||||
struct Vec2 hook_pos;
|
||||
int jumping, airbreak, hooking;
|
||||
struct FVec2 hook_mom;
|
||||
int jumping, airbreak, hooking, locked;
|
||||
};
|
||||
|
||||
void player_init(void);
|
||||
|
|
|
@ -55,7 +55,8 @@ hook_closest(struct Vec2 *pos)
|
|||
}
|
||||
}
|
||||
}
|
||||
shortest = (struct Vec2){shortest.x * TILE_S + TILE_S / 2, shortest.y * TILE_S + TILE_S / 2};
|
||||
shortest = (struct Vec2){shortest.x * TILE_S + TILE_S / 2,
|
||||
shortest.y * TILE_S + TILE_S / 2};
|
||||
return shortest;
|
||||
}
|
||||
|
||||
|
|
|
@ -99,6 +99,12 @@ level_search_s(tile_t tile, int occ)
|
|||
return (struct Vec2){0, 0};
|
||||
}
|
||||
|
||||
struct Vec2
|
||||
level_get_dim(void)
|
||||
{
|
||||
return (struct Vec2){level.w, level.h};
|
||||
}
|
||||
|
||||
static void
|
||||
level_free(void)
|
||||
{
|
||||
|
|
51
src/player.c
51
src/player.c
|
@ -5,6 +5,7 @@
|
|||
#include "input.h"
|
||||
#include "level.h"
|
||||
#include "tools.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static int k_up, k_down, k_left, k_right, k_jump, k_jumpdown, k_hook;
|
||||
static struct Player player;
|
||||
|
@ -25,6 +26,7 @@ player_init(void)
|
|||
player.jumping = 1;
|
||||
player.airbreak = 1;
|
||||
player.hooking = 0;
|
||||
player.locked = 0;
|
||||
player.hook_pos = (struct Vec2){50, 50};
|
||||
}
|
||||
|
||||
|
@ -47,11 +49,15 @@ player_update(void)
|
|||
player.hooking = 0;
|
||||
if (k_hook) {
|
||||
player_hook();
|
||||
} else {
|
||||
/* unbind the player from the hook */
|
||||
player.locked = 0;
|
||||
}
|
||||
|
||||
player.spd.x *= (1 - PLAYER_FRIC);
|
||||
player.spd.x += dir * PLAYER_ACC;
|
||||
player.spd.y += GRAVITY + GRAVITY * player.airbreak;
|
||||
player.spd.y +=
|
||||
GRAVITY + GRAVITY * (player.airbreak && !player.hooking);
|
||||
|
||||
player_move(player_update_rem());
|
||||
|
||||
|
@ -64,8 +70,28 @@ player_update(void)
|
|||
void
|
||||
player_hook(void)
|
||||
{
|
||||
player.hook_pos = hook_closest(&player.pos);
|
||||
player.hooking = 1;
|
||||
if (!player.locked) {
|
||||
player.hook_pos = hook_closest(&player.pos);
|
||||
}
|
||||
|
||||
const float dist = length(player.pos.x, player.pos.y, player.hook_pos.x,
|
||||
player.hook_pos.y);
|
||||
|
||||
if (dist < 100 && player.pos.y > player.hook_pos.y) {
|
||||
player.hooking = 1;
|
||||
player.locked = 1;
|
||||
}
|
||||
|
||||
if (player.hooking && dist) {
|
||||
const float dist_x = player.hook_pos.x - player.pos.x;
|
||||
const float dist_y = player.hook_pos.y - player.pos.y;
|
||||
if (abs(player.spd.x) < 5) {
|
||||
player.spd.x += dist_x * (0.5f / dist);
|
||||
}
|
||||
if (abs(player.spd.y) < 5) {
|
||||
player.spd.y += dist_y * (1.0f / dist);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -75,7 +101,7 @@ player_draw(void)
|
|||
player_hook_draw();
|
||||
}
|
||||
draw_rectangle(player.pos.x, player.pos.y, PLAYER_S, PLAYER_S, C_WHITE);
|
||||
dprint(10, 10, C_BLACK, "%d", player_collide(player.pos, 1, 0));
|
||||
dprint(10, 20, C_WHITE, "%f", player.spd.y);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -116,11 +142,19 @@ player_move(struct Vec2 spd)
|
|||
return;
|
||||
}
|
||||
|
||||
player_update_rem();
|
||||
/* OOB */
|
||||
const struct Vec2 level_dim = level_get_dim();
|
||||
|
||||
if (spd.x) {
|
||||
const int sign_x = sign(spd.x);
|
||||
player.pos.x += spd.x;
|
||||
if (player.pos.x < 0) {
|
||||
player.pos.x = 1;
|
||||
}
|
||||
if (player.pos.x > level_dim.x * TILE_S) {
|
||||
player.pos.x = level_dim.x * TILE_S - 1;
|
||||
}
|
||||
|
||||
if (player_collide(player.pos, 1, 0)) {
|
||||
player.spd.x = 0.0f;
|
||||
player.rem.x = 0.0f;
|
||||
|
@ -133,6 +167,13 @@ player_move(struct Vec2 spd)
|
|||
if (spd.y) {
|
||||
const int sign_y = sign(spd.y);
|
||||
player.pos.y += spd.y;
|
||||
if (player.pos.y < 0) {
|
||||
player.pos.y = 1;
|
||||
}
|
||||
if (player.pos.y > level_dim.y * TILE_S) {
|
||||
player.pos.y = level_dim.y * TILE_S - 1;
|
||||
}
|
||||
|
||||
if (player_collide(player.pos, 1, 0)) {
|
||||
player.spd.y = 0.0f;
|
||||
player.rem.y = 0.0f;
|
||||
|
|
Loading…
Reference in New Issue