Separated player vars, weird stuff happens.

This commit is contained in:
KikooDX 2021-01-09 14:28:14 +01:00
parent bffaf264dd
commit e1f6d108e2
7 changed files with 66 additions and 15 deletions

View File

@ -4,6 +4,7 @@
#include <stdbool.h>
#include "vec.h"
#include "player_vars.h"
typedef struct Player {
Vec pos;
@ -11,16 +12,18 @@ 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 sprite (offset) */
uint grace; /* coyot jump */
unsigned int grace; /* coyot jump */
bool jump_held; /* used to control jump height */
bool dead; /* set to true the player will die */
Player_vars vars;
} Player;
#include "level.h"
#include "camera.h"
#include "input.h"
void player_init(Player *player);
void player_init(Player *player, const Level *level);
void player_set_vars(Player *player, const Level *level);
void player_move(Player *player, const Level *level);
void player_step(Player *player, Input *input, const Level *level, uint step);
void player_draw(Player *player, Camera *camera);

View File

@ -0,0 +1,6 @@
/* tile modifiers on player vars */
#ifndef _DEF_PLAYER_MODIFIERS
#define _DEF_PLAYER_MODIFIERS
void player_mod_ice(Player *player);
void player_mod_glue(Player *player);
#endif

11
include/player_vars.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef _DEF_PLAYER_VARS
#define _DEF_PLAYER_VARS
typedef struct Player_vars {
float friction;
int acceleration;
int gravity;
int jump_spd;
} Player_vars;
#endif /* _DEF_PLAYER_VARS */

View File

@ -8,8 +8,9 @@ typedef unsigned char Tile_flags; /* the tile properties (bitmask) */
/* define flags */
#define F_SOLID 0b1
#define F_SLIPPERY 0b10
#define F_SPIKY 0b100
#define F_SPIKY 0b10
#define F_ICE 0b100
#define F_GLUE 0b1000
/* define properties */
#define P_AIR (0)

View File

@ -27,14 +27,14 @@ int main(void) {
}
int play_level(uint level_id) {
/* create player */
Player player;
player_init(&player);
/* set level */
const Level *level;
level_set(&level, level_id);
/* create player */
Player player;
player_init(&player, level);
/* create camera */
Camera camera;
camera_init(&camera, &player, level);

View File

@ -2,6 +2,7 @@
#include <stdbool.h>
#include "player.h"
#include "player_vars.h"
#include "vec.h"
#include "conf.h"
#include "camera.h"
@ -16,15 +17,15 @@
#define GRAVITY PXS
#define FAST_FALL_FACTOR 3
#define JUMP_SPD (-128 * PXS)
#define GRACE_UNITS (int)(UPS / 5)
#define GRACE_UNITS (int)(UPS / 10)
#define EARLY_UNITS (int)(UPS / 5)
#define H_CLIP_MARGIN (TILE_SIZE / 4)
#define V_CLIP_MARGIN (TILE_SIZE / 3)
#define SGN(x) (((x) > 0) ? (1) : (((x) < 0) ? (-1) : (0)))
#define PLAYER_COLLIDE_SOLID(pos) (player_collide_or(player, pos, level) & F_SOLID)
void player_init(Player *player) {
player->pos = (Vec){TILE_SIZE, TILE_SIZE};
void player_init(Player *player, const Level *level) {
player->pos = (Vec){TILE_SIZE * 2, TILE_SIZE * 2};
player->spd = (Vec){0, 0};
player->hbox = (Vec){TILE_SIZE - 1, TILE_SIZE - 1};
player->vbox = (Vec){7, 7};
@ -34,7 +35,25 @@ void player_init(Player *player) {
player->dead = false;
}
void player_set_vars(Player *player, const Level *level) {
player->vars.friction = FRICTION;
player->vars.acceleration = ACCELERATION;
player->vars.gravity = GRAVITY;
player->vars.jump_spd = JUMP_SPD;
/* apply modifiers */
Vec pos = (Vec){ player->pos.x, player->pos.y + 1 };
Tile_flags flags = player_collide_or(player, pos, level);
pos.y -= 2;
flags |= player_collide_or(player, pos, level);
pos.y += 1;
pos.x += 1;
flags |= player_collide_or(player, pos, level);
pos.x -= 2;
flags |= player_collide_or(player, pos, level);
}
void player_move(Player *player, const Level *level) {
player_set_vars(player, level);
/* TODO: Take into account player's hitbox */
const int sgn_spd_x = SGN(player->spd.x);
const int sgn_spd_y = SGN(player->spd.y);
@ -90,16 +109,16 @@ void player_step(Player *player, Input *input, const Level *level, uint step) {
};
/* other keys */
bool k_jump = INPUT_DOWN(K_JUMP);
int xacc = move.x * ACCELERATION; /* calculate horizontal acceleration */
player->spd.x *= FRICTION; /* apply horizontal friction */
int xacc = move.x * player->vars.acceleration; /* calculate horizontal acceleration */
player->spd.x *= player->vars.friction; /* apply horizontal friction */
player->spd.x += xacc; /* apply horizontal acceleration */
/* apply gravity */
if (player->spd.y < 0 && !player->jump_held) {
/* The player is rising and let go the jump key,
* accelerate gravity until they reach 0. */
player->spd.y += GRAVITY * FAST_FALL_FACTOR;
player->spd.y += player->vars.gravity * FAST_FALL_FACTOR;
} else {
player->spd.y += GRAVITY;
player->spd.y += player->vars.gravity;
}
/* Grace frames allow the player to jump a short
* time after leaving a platform. */

11
src/player_modifiers.c Normal file
View File

@ -0,0 +1,11 @@
#include "player.h"
void player_mod_ice(Player *player) {
player->vars.acceleration /= 2;
player->vars.friction /= 2;
}
void player_mod_glue(Player *player) {
player->vars.acceleration = 0;
player->vars.friction = 1;
}