shatter by jumping check

This commit is contained in:
KikooDX 2021-11-10 23:29:12 +01:00
parent b21def5052
commit 91ad5ce1f5
11 changed files with 62 additions and 10 deletions

View File

@ -5,10 +5,13 @@
#define PLAYER_WIDTH 14
#define PLAYER_HEIGHT 14
#define GRAVITY 0.2f
#define AIR_RESISTANCE 0.02f
#define AIR_RESISTANCE 0.03f
#define MAX_WALK_SPEED 2.0f
#define ACCELERATION_AIR 0.1f
#define ACCELERATION_GROUND 0.4f
#define FRICTION_AIR (ACCELERATION_AIR / MAX_WALK_SPEED)
#define FRICTION_GROUND (ACCELERATION_GROUND / MAX_WALK_SPEED)
#define FRICTION_BREAK (0.6f / MAX_WALK_SPEED)
#define JUMP_GRACE 4
#define JUMP_BUFFER 12
#define JUMP_SPD -5.0

View File

@ -1,7 +1,7 @@
#pragma once
/* use this to get user input, NOT gint/keyboard.h */
enum Key { K_A, K_B, K_UP, K_DOWN, K_LEFT, K_RIGHT, K_EXIT, K_COUNT };
enum Key { K_JUMP, K_PUNCH, K_UP, K_DOWN, K_LEFT, K_RIGHT, K_EXIT, K_COUNT };
enum KeyState { KS_UP, KS_RELEASE, KS_DOWN, KS_PRESS };
struct Input {

View File

@ -7,3 +7,4 @@ void particles_init(void);
void particles_add(struct Anim);
void particles_update(void);
void particles_draw(void);
int particles_here(int x, int y);

View File

@ -5,6 +5,8 @@ struct Player {
struct Vec pos;
struct VecF spd;
struct VecF rem;
int jump_grace;
int jump_buffer;
};
void player_init(struct Vec pos);

View File

@ -1,3 +1,9 @@
#pragma once
enum Tile { TILE_VOID, TILE_SOLID, TILE_PLAYER, TILE_OOB = TILE_SOLID };
enum Tile {
TILE_VOID,
TILE_SOLID,
TILE_PLAYER,
TILE_SHATTERED,
TILE_OOB = TILE_SOLID
};

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.7 KiB

After

Width:  |  Height:  |  Size: 9.8 KiB

View File

@ -10,8 +10,8 @@ input_init(void)
while (i-- > 0) {
input.states[i] = KS_UP;
}
input.keys[K_A] = KEY_SHIFT;
input.keys[K_B] = KEY_ALPHA;
input.keys[K_JUMP] = KEY_SHIFT;
input.keys[K_PUNCH] = KEY_ALPHA;
input.keys[K_LEFT] = KEY_LEFT;
input.keys[K_RIGHT] = KEY_RIGHT;
input.keys[K_UP] = KEY_UP;

View File

@ -31,7 +31,7 @@ main(void)
player_init(level_find(TILE_PLAYER));
camera_init(player_pos());
while (!input_pressed(K_EXIT)) {
while (!input_down(K_EXIT)) {
int i;
draw();
for (i = 0; i < frameskip; i++) {

View File

@ -37,3 +37,14 @@ particles_draw(void)
while (i-- > 0)
anim_draw(&particles[i]);
}
int
particles_here(int x, int y)
{
int i = MAX_PARTICLES;
while (i-- > 0) {
if (particles[i].pos.x == x && particles[i].pos.y == y)
return 1;
}
return 0;
}

View File

@ -10,6 +10,7 @@
static struct Player self;
static void jump(int on_ground);
static int collide_solid(int x, int y);
void
@ -18,6 +19,8 @@ player_init(struct Vec pos)
self.pos = pos;
self.spd = VECFZ;
self.rem = VECFZ;
self.jump_grace = 0;
self.jump_buffer = 0;
}
void
@ -42,12 +45,37 @@ player_update(void)
self.spd.y *= 1.0f - AIR_RESISTANCE;
self.spd.y += GRAVITY;
if (input_pressed(K_DOWN))
shatter(self.pos.x, self.pos.y + PLAYER_HEIGHT);
jump(on_ground);
player_move(player_update_rem());
}
static void
jump(int on_ground)
{
/* grace */
if (on_ground)
self.jump_grace = JUMP_GRACE;
else if (self.jump_grace)
self.jump_grace--;
/* buffer */
if (input_pressed(K_JUMP))
self.jump_buffer = JUMP_BUFFER;
else if (self.jump_buffer)
self.jump_buffer--;
/* jump */
if (self.jump_grace && self.jump_buffer && input_down(K_JUMP)) {
struct Vec shatter_pos = VEC(self.pos.x, self.pos.y);
self.jump_grace = 0;
self.jump_buffer = 0;
self.spd.y = JUMP_SPD;
shatter_pos.y += PLAYER_HEIGHT;
shatter(shatter_pos.x, shatter_pos.y);
shatter_pos.x += PLAYER_WIDTH - 1;
shatter(shatter_pos.x, shatter_pos.y);
}
}
void
player_draw(void)
{

View File

@ -17,11 +17,12 @@ shatter(int x, int y)
const int ay = (int)(y / TILE_SIZE) * TILE_SIZE;
struct Anim p = anim_new(&bimg_shatter, ax, ay, TILE_SIZE, 2, false);
p.callback = callback;
particles_add(p);
if (level_get_px(ax, ay) == TILE_SOLID && !particles_here(ax, ay))
particles_add(p);
}
static void
callback(struct Anim *a)
{
level_set_px(a->pos.x, a->pos.y, TILE_PLAYER);
level_set_px(a->pos.x, a->pos.y, TILE_SHATTERED);
}