player: update code

This commit is contained in:
KikooDX 2021-06-11 13:53:22 +02:00
parent d8f5b63500
commit 4cfcd6b29c
7 changed files with 41 additions and 10 deletions

View File

@ -19,6 +19,7 @@ set(SOURCES
src/entity/init.c
src/entity/move.c
src/wall/init.c
src/player/update.c
src/player/init.c
src/util.c
)

View File

@ -9,4 +9,5 @@ struct Player {
float rem_y;
};
void player_init(struct Player *restrict, int x, int y);
void player_init(struct Player *, int x, int y);
void player_update(struct Player *);

View File

@ -5,4 +5,4 @@ struct Wall {
struct Entity;
};
void wall_init(struct Wall *restrict, int x, int y, int width, int height);
void wall_init(struct Wall *, int x, int y, int width, int height);

View File

@ -1,5 +1,4 @@
#include "entity.h"
#include "layers.h"
#include "player.h"
#include "wall.h"
#include <gint/display.h>
@ -11,7 +10,6 @@ main(void)
struct Player player;
struct Player newb;
struct Wall walls[5];
int collided = 0;
player_init(&player, 16, 32);
player_init(&newb, 64, 128);
@ -24,12 +22,9 @@ main(void)
do {
dclear(C_BLACK);
entity_grid_draw_hitboxes();
dprint(2, 2, C_WHITE, "%d", collided);
dupdate();
clearevents();
collided = entity_move(&player, L_SOLID,
keydown(KEY_RIGHT) - keydown(KEY_LEFT),
keydown(KEY_DOWN) - keydown(KEY_UP));
player_update(&player);
} while (!keydown(KEY_EXIT));
return;

View File

@ -3,7 +3,7 @@
#include <gint/display.h>
void
player_init(struct Player *restrict p, int x, int y)
player_init(struct Player *p, int x, int y)
{
entity_init(p, x, y, 2, 2, 12, 12, L_PUSHABLE, C_BLUE);
p->spd_x = 0.0;

34
src/player/update.c Normal file
View File

@ -0,0 +1,34 @@
#include "entity.h"
#include "layers.h"
#include "player.h"
#include <gint/keyboard.h>
void
player_update(struct Player *p)
{
int collided;
const int dir_x = keydown(KEY_RIGHT) - keydown(KEY_LEFT);
const int dir_y = keydown(KEY_DOWN) - keydown(KEY_UP);
p->spd_x += dir_x * 0.1;
p->spd_y += dir_y * 0.1;
/* rem */
const float spd_n_rem_x = p->spd_x + p->rem_x;
const int spd_x = spd_n_rem_x;
p->rem_x = spd_n_rem_x - spd_x;
const float spd_n_rem_y = p->spd_y + p->rem_y;
const int spd_y = spd_n_rem_y;
p->rem_y = spd_n_rem_y - spd_y;
/* move */
collided = entity_move(p, L_SOLID, spd_x, spd_y);
if (collided & 1) {
p->spd_x = 0.0;
p->rem_x = 0.0;
}
if (collided & 2) {
p->spd_y = 0.0;
p->rem_y = 0.0;
}
}

View File

@ -3,7 +3,7 @@
#include <gint/display.h>
void
wall_init(struct Wall *restrict w, int x, int y, int width, int height)
wall_init(struct Wall *w, int x, int y, int width, int height)
{
entity_init(w, x, y, 0, 0, width, height, L_SOLID, C_DARK);
}