forked from kdx/jtmm2-old
Jump (yay) and grace frames support.
This commit is contained in:
parent
c8bd190ec1
commit
a5c414dd4c
|
@ -4,13 +4,14 @@
|
|||
#include <gint/defs/types.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define KEYS_COUNT 5
|
||||
#define KEYS_COUNT 6
|
||||
|
||||
enum {
|
||||
K_LEFT,
|
||||
K_RIGHT,
|
||||
K_UP,
|
||||
K_DOWN,
|
||||
K_JUMP,
|
||||
K_EXIT
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef _DEF_PLAYER
|
||||
#define _DEF_PLAYER
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "vec.h"
|
||||
|
||||
typedef struct Player {
|
||||
|
@ -9,6 +11,7 @@ typedef struct Player {
|
|||
Vec hbox; /* the bottom left corner of the player's hitbox */
|
||||
Vec vbox; /* the bottom left corner of the player's visual box */
|
||||
Vec origin; /* the origin of the player (offset) */
|
||||
uint grace; /* coyot jump */
|
||||
} Player;
|
||||
|
||||
#include "level.h"
|
||||
|
|
|
@ -28,6 +28,7 @@ void input_init(Input *input) {
|
|||
input->keys[K_RIGHT] = KEY_RIGHT;
|
||||
input->keys[K_UP] = KEY_UP;
|
||||
input->keys[K_DOWN] = KEY_DOWN;
|
||||
input->keys[K_JUMP] = KEY_SHIFT;
|
||||
input->keys[K_EXIT] = KEY_EXIT;
|
||||
for (int i = 0; i < KEYS_COUNT; ++i) {
|
||||
input->states[i] = S_UP;
|
||||
|
|
|
@ -33,7 +33,8 @@ int play_level(uint level_id) {
|
|||
.spd = {0, 0},
|
||||
.hbox = {TILE_SIZE - 1, TILE_SIZE - 1},
|
||||
.vbox = {7, 7},
|
||||
.origin = {0 * VEC_PRECISION, 0 * VEC_PRECISION}
|
||||
.origin = {0 * VEC_PRECISION, 0 * VEC_PRECISION},
|
||||
.grace = 0
|
||||
};
|
||||
vec_cpy(&player.pos, player.origin); /* place the player at "0/0"*/
|
||||
|
||||
|
|
25
src/player.c
25
src/player.c
|
@ -1,4 +1,5 @@
|
|||
#include <gint/display.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "player.h"
|
||||
#include "vec.h"
|
||||
|
@ -12,6 +13,8 @@
|
|||
#define FRICTION 0.9
|
||||
#define ACCELERATION (MAX_SPD * (1 - FRICTION))
|
||||
#define GRAVITY PXS
|
||||
#define JUMP_SPD (-128 * PXS)
|
||||
#define GRACE_UNITS (int)(UPS / 5)
|
||||
#define SGN(x) ((x > 0) ? (1) : ((x < 0) ? (-1) : (0)))
|
||||
#define PLAYER_COLLIDE(pos) player_collide(player, pos, level, level->solid_layer)
|
||||
|
||||
|
@ -42,6 +45,9 @@ void player_move(Player *player, const Level *level) {
|
|||
destination.y += TILE_SIZE * sgn_spd_y;
|
||||
}
|
||||
destination.y -= TILE_SIZE * sgn_spd_y;
|
||||
if (sgn_spd_y > 0) { /* the player was falling */
|
||||
player->grace = GRACE_UNITS;
|
||||
}
|
||||
player->spd.y = 0;
|
||||
}
|
||||
/* move the player to their new position */
|
||||
|
@ -49,15 +55,32 @@ void player_move(Player *player, const Level *level) {
|
|||
}
|
||||
|
||||
void player_step(Player *player, Input *input, const Level *level) {
|
||||
/* Get directionnal input and assign it to move.x/move.y:
|
||||
* i.e., if the player hold left and down move will have
|
||||
* move.x = -1 and move.y = 1. */
|
||||
Vec move = {
|
||||
(input_is_down(input, K_RIGHT) - input_is_down(input, K_LEFT)),
|
||||
(input_is_down(input, K_DOWN) - input_is_down(input, K_UP))
|
||||
};
|
||||
/* other keys */
|
||||
bool kp_jump = input_is_pressed(input, K_JUMP);
|
||||
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 */
|
||||
player_move(player, level);
|
||||
/* Grace frames allow the player to jump a short
|
||||
* time after leaving a platform. */
|
||||
if (player->grace) {
|
||||
player->grace -= 1;
|
||||
if (kp_jump) {
|
||||
/* If the player try to jump and can, prevent
|
||||
* them to do it again and assign them the
|
||||
* corresponding y speed. */
|
||||
player->grace = 0;
|
||||
player->spd.y = JUMP_SPD;
|
||||
}
|
||||
}
|
||||
player_move(player, level); /* move the player according to their speed */
|
||||
}
|
||||
|
||||
void player_draw(Player *player, Camera *camera) {
|
||||
|
|
Loading…
Reference in New Issue