diff --git a/inc/conf.h b/inc/conf.h index f4eebe6..a635acc 100644 --- a/inc/conf.h +++ b/inc/conf.h @@ -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 diff --git a/inc/input.h b/inc/input.h index d73de49..39eb248 100644 --- a/inc/input.h +++ b/inc/input.h @@ -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 { diff --git a/inc/particles.h b/inc/particles.h index 51fbd0f..96f8d46 100644 --- a/inc/particles.h +++ b/inc/particles.h @@ -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); diff --git a/inc/player.h b/inc/player.h index bcd9025..5b47114 100644 --- a/inc/player.h +++ b/inc/player.h @@ -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); diff --git a/inc/tile.h b/inc/tile.h index 4519eb9..2a238ed 100644 --- a/inc/tile.h +++ b/inc/tile.h @@ -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 +}; diff --git a/res/tileset.png b/res/tileset.png index 47b7789..6e2ea9e 100644 Binary files a/res/tileset.png and b/res/tileset.png differ diff --git a/src/input.c b/src/input.c index 4f3cbda..bd87fc4 100644 --- a/src/input.c +++ b/src/input.c @@ -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; diff --git a/src/main.c b/src/main.c index 9a5beab..401a37a 100644 --- a/src/main.c +++ b/src/main.c @@ -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++) { diff --git a/src/particles.c b/src/particles.c index 3cce7ac..7a0212d 100644 --- a/src/particles.c +++ b/src/particles.c @@ -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; +} diff --git a/src/player.c b/src/player.c index 752dd4d..b5166a8 100644 --- a/src/player.c +++ b/src/player.c @@ -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) { diff --git a/src/shatter.c b/src/shatter.c index 4fd0edd..bdf6578 100644 --- a/src/shatter.c +++ b/src/shatter.c @@ -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); }