The player can now vary their jump height + cleaning

This commit is contained in:
KikooDX 2021-01-01 02:22:48 +01:00
parent 8011f9d126
commit d4c1575ca9
4 changed files with 22 additions and 6 deletions

View File

@ -1,6 +1,8 @@
#ifndef _DEF_PLAYER
#define _DEF_PLAYER
#include <stdbool.h>
#include "vec.h"
typedef struct Player {
@ -10,6 +12,7 @@ typedef struct Player {
Vec vbox; /* the bottom left corner of the player's visual box */
Vec origin; /* the origin of the sprite (offset) */
uint grace; /* coyot jump */
bool jump_held; /* used to control jump height */
} Player;
#include "level.h"

View File

@ -24,10 +24,10 @@ void layer_draw(const Level *level, Camera *camera, uint layer_id) {
vec_add(&display_br, VEC_PRECISE_HALF_DISP);
vec_div(&display_tl, TILE_SIZE);
vec_div(&display_br, TILE_SIZE);
int start_x = (display_tl.x > 0) ? display_tl.x : 0;
int start_y = (display_tl.y > 0) ? display_tl.y : 0;
int end_x = (display_br.x < level->width) ? display_br.x + 1 : level->width;
int end_y = (display_br.y < level->height) ? display_br.y + 1 : level->height;
int start_x = (display_tl.x > 0) ? (display_tl.x) : (0);
int start_y = (display_tl.y > 0) ? (display_tl.y) : (0);
int end_x = (display_br.x < level->width) ? (display_br.x + 1) : (level->width);
int end_y = (display_br.y < level->height) ? (display_br.y + 1) : (level->height);
for (int y = start_y; y < end_y; ++y) {
for (int x = start_x; x < end_x; ++x) {
const uint8_t cell = layer[x + y * level->width];

View File

@ -34,7 +34,8 @@ int play_level(uint level_id) {
.hbox = {TILE_SIZE - 1, TILE_SIZE - 1},
.vbox = {7, 7},
.origin = {0 * VEC_PRECISION, 0 * VEC_PRECISION},
.grace = 0
.grace = 0,
.jump_held = false
};
vec_cpy(&player.pos, (Vec){0, 0}); /* place the player at "0/0"*/

View File

@ -13,6 +13,7 @@
#define FRICTION 0.99
#define ACCELERATION (int)(MAX_SPD * (1 - FRICTION))
#define GRAVITY PXS
#define FAST_FALL_FACTOR 3
#define JUMP_SPD (-128 * PXS)
#define GRACE_UNITS (int)(UPS / 5)
#define EARLY_UNITS (int)(UPS / 5)
@ -68,7 +69,12 @@ void player_step(Player *player, Input *input, const Level *level, uint step) {
int xacc = move.x * ACCELERATION; /* calculate horizontal acceleration */
player->spd.x *= FRICTION; /* apply horizontal friction */
player->spd.x += xacc; /* apply horizontal acceleration */
player->spd.y += GRAVITY; /* apply gravity */
/* apply gravity */
if (player->spd.y < 0 && !player->jump_held) {
player->spd.y += GRAVITY * FAST_FALL_FACTOR;
} else {
player->spd.y += GRAVITY;
}
/* Grace frames allow the player to jump a short
* time after leaving a platform. */
if (player->grace) {
@ -79,8 +85,14 @@ void player_step(Player *player, Input *input, const Level *level, uint step) {
* corresponding y speed. */
player->grace = 0;
player->spd.y = JUMP_SPD;
player->jump_held = true;
}
}
/* See if the player is still holding their jump button, this is
* usefull for jump height and gravity manipulation. */
if (player->jump_held && !k_jump) {
player->jump_held = false;
}
player_move(player, level); /* move the player according to their speed */
}