Player update base and collision code

This commit is contained in:
KikooDX 2021-03-30 01:42:11 +02:00
parent 8fa7f702d6
commit 1c7bd74837
6 changed files with 59 additions and 0 deletions

View File

@ -16,6 +16,8 @@ set(SOURCES
src/level/draw.c
src/player/init.c
src/player/draw.c
src/player/update.c
src/player/collide.c
)
set(ASSETS

View File

@ -12,5 +12,11 @@ struct Player {
float rem_y;
};
/* used for collisions */
enum { UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT };
#define COLLIDE_POINTS 4
struct Player player_init(void);
void player_draw(struct Player player);
void player_update(struct Player *player);
void player_collide(int collisions[COLLIDE_POINTS], int x, int y);

View File

@ -58,6 +58,7 @@ int main(void)
has_ticked = 0;
/* update */
clearevents();
player_update(&player);
}
/* draw */
dclear(C_BLACK);

31
src/player/collide.c Normal file
View File

@ -0,0 +1,31 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */
#include "conf.h"
#include "level.h"
#include "player.h"
#include "tiles.h"
extern struct Level level;
static int collide_single(int x, int y);
void player_collide(int collisions[COLLIDE_POINTS], int x, int y)
{
const int rx = x + PLAYER_WIDTH - 1;
const int dy = y + PLAYER_HEIGHT - 1;
collisions[UP_LEFT] = collide_single(x, y);
collisions[UP_RIGHT] = collide_single(rx, y);
collisions[DOWN_LEFT] = collide_single(x, dy);
collisions[DOWN_RIGHT] = collide_single(rx, dy);
}
static int collide_single(int x, int y)
{
const int tx = x / TILE_WIDTH;
const int ty = y / TILE_WIDTH;
/* out of bounds handling */
if (x < 0 || y < 0 || tx >= level.width || ty >= level.height)
return TILE_OOB;
return level.data[tx + ty * level.width];
}

View File

@ -8,5 +8,10 @@ extern bopti_image_t bimg_player;
void player_draw(struct Player player)
{
int collisions[COLLIDE_POINTS];
int i;
player_collide(collisions, player.x, player.y);
for (i = 0; i < COLLIDE_POINTS; i += 1)
dprint(0, i * 20, C_WHITE, "%d: %d", i, collisions[i]);
dimage(player.x, player.y, &bimg_player);
}

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

@ -0,0 +1,14 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */
#include "player.h"
#include <gint/keyboard.h>
void player_update(struct Player *player)
{
/* process input */
const int move_x = keydown(KEY_RIGHT) - keydown(KEY_LEFT);
const int move_y = keydown(KEY_DOWN) - keydown(KEY_UP);
player->x += move_x;
player->y += move_y;
}