Added speed value to the player, separated collision/movement from it's main step event. TODO in player.c.

This commit is contained in:
KikooDX 2020-12-21 17:50:00 +01:00
parent f49f6719d3
commit baa1a72703
3 changed files with 30 additions and 15 deletions

View File

@ -5,6 +5,7 @@
typedef struct Player {
Vec pos;
Vec spd; /* velocity */
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) */
@ -14,6 +15,7 @@ typedef struct Player {
#include "camera.h"
#include "input.h"
void player_move(Player *player, const Level *level);
void player_step(Player *player, Input *input, const Level *level);
void player_draw(Player *player, Camera *camera);
void player_draw_debug(Player *player, uint step, const Level *level, uint layer_id);

View File

@ -30,6 +30,7 @@ int play_level(uint level_id) {
/* create player */
Player player = {
//.pos = {TILE_SIZE, TILE_SIZE},
.spd = {0, 0},
.hbox = {7 * VEC_PRECISION - 1, 7 * VEC_PRECISION - 1},
.vbox = {6, 6},
.origin = {4 * VEC_PRECISION, 4 * VEC_PRECISION}

View File

@ -1,12 +1,36 @@
#include <gint/display.h>
#include "player.h"
#include "vec.h"
#include "conf.h"
#include "camera.h"
#include "input.h"
#include "collide.h"
#define SGN(x) ((x > 0) ? (1) : ((x < 0) ? (-1) : (0)))
void player_move(Player *player, const Level *level) {
/* Try to move the player to a destination, and stop their
* momentum if they hit a wall. The player can move if they're
* stuck in a block, to avoid softlock in some situations. If
* the destination is solid and they aren't in a block, then we
* use the costy player_move function to snap them and reset
* their speed accordingly.
*/
Vec destination;
vec_cpy(&destination, player->pos);
vec_add(&destination, player->spd);
if (collide_full(player, player->pos, level, level->solid_layer) ||
!collide_full(player, destination, level, level->solid_layer)) {
vec_cpy(&player->pos, destination);
}
else {
/* TODO
* Player can't move to destination, snap them. First
* move on x axis, then y axis.
*/
uint i = 0;
}
}
void player_step(Player *player, Input *input, const Level *level) {
@ -14,21 +38,9 @@ void player_step(Player *player, Input *input, const Level *level) {
(input_is_down(input, K_RIGHT) - input_is_down(input, K_LEFT)),
(input_is_down(input, K_DOWN) - input_is_down(input, K_UP))
};
vec_mul(&move, 32 * PXS); /* set speed */
Vec destination;
vec_cpy(&destination, player->pos);
vec_add(&destination, move);
/* The player can move if they're stuck in a block, to avoid
* softlock in some situations. If the destination is solid and
* they aren't in a block, then we use the costy player_move
* function to snap them and reset their speed accordingly.
*/
if (collide_full(player, player->pos, level, level->solid_layer) ||
!collide_full(player, destination, level, level->solid_layer)) {
vec_cpy(&player->pos, destination);
}
else {
}
vec_mul(&move, 32 * PXS); /* calculate speed */
vec_cpy(&player->spd, move);
player_move(player, level);
}
void player_draw(Player *player, Camera *camera) {