[new] Collision system base + miscellaneous code improvements and small bugfixes

This commit is contained in:
KikooDX 2020-09-22 13:20:16 +02:00
parent bc5273ed77
commit 62cdb1ec7f
9 changed files with 53 additions and 16 deletions

13
include/collide.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef _DEF_PLAYER_COLLIDE
#define _DEF_PLAYER_COLLIDE
#include <gint/defs/types.h>
#include "conf.h"
#include "level.h"
/* return tile at the given position
* -1 is out of bound */
uint8_t collide_point(Vec point, const Level *level, uint layer);
#endif /* _DEF_PLAYER_COLLIDE */

View File

@ -1 +1 @@
//#define DEBUG
#define DEBUG

View File

@ -7,10 +7,10 @@
typedef struct Level
{
uint width; /* in tiles */
uint height; /* in tiles */
int width; /* in tiles */
int height; /* in tiles */
const uint8_t **layers; /* points toward the level content */
uint layers_count;
int layers_count;
} Level;
void level_step(const Level *level);

View File

@ -2,6 +2,7 @@
#define _DEF_PLAYER
#include "vec.h"
#include "level.h"
#include "camera.h"
#include "input.h"
@ -15,6 +16,6 @@ typedef struct Player
void player_step(Player *player, Input *input);
void player_draw(Player *player, Camera *camera);
void player_draw_debug(Player *player, uint step);
void player_draw_debug(Player *player, uint step, const Level *level, uint layer_id);
#endif /* _DEF_PLAYER */

View File

@ -14,7 +14,7 @@ void camera_step(Camera *camera)
vec_cpy(offset, camera->pos);
vec_div(offset, VEC_PRECISION);
Vec scaled_dcenter = VEC_DCENTER;
vec_div(&scaled_dcenter, 2);
vec_div(&scaled_dcenter, SCALE);
vec_sub(offset, scaled_dcenter);
}

21
src/collide.c Normal file
View File

@ -0,0 +1,21 @@
#include <gint/defs/types.h>
#include "collide.h"
#include "conf.h"
#include "level.h"
uint8_t collide_point(Vec point, const Level *level, uint layer_id)
{
Vec cursor; /* the final position to test */
vec_cpy(&cursor, point);
vec_div(&cursor, VEC_PRECISION * TILE_SIZE); /* get the expected tile ID */
if (point.x < 0 || point.y < 0 ||
cursor.x >= level->width || cursor.y >= level->height)
{
return -1; /* the point is out of bounds */
}
else
{
return level->layers[layer_id][cursor.x + cursor.y * level->width];
}
}

View File

@ -16,13 +16,13 @@ void level_draw(const Level *level, Camera *camera)
void layer_draw(const Level *level, Camera *camera, uint layer_id)
{
const uint8_t *layer = level->layers[layer_id];
for (uint y = 0; y < level->height; ++y)
for (int y = 0; y < level->height; ++y)
{
for (uint x = 0; x < level->width; ++x)
for (int x = 0; x < level->width; ++x)
{
const uint8_t cell = layer[x + y * level->width];
#ifdef FX9860G
const int color = C_BLACK;
const int color = C_LIGHT;
#endif /* FX9860G */
#ifdef FXCG50
const int color = C_GREEN;

View File

@ -98,9 +98,9 @@ void draw_event(Player *player, const Level *level, Camera *camera, Input *input
level_draw(level, camera);
player_draw(player, camera);
#ifdef DEBUG
camera_draw_debug(camera);
//camera_draw_debug(camera);
//input_draw_debug(input);
player_draw_debug(player, step);
player_draw_debug(player, step, level, 0);
#endif
dupdate();
}

View File

@ -4,6 +4,7 @@
#include "conf.h"
#include "camera.h"
#include "input.h"
#include "collide.h"
void player_step(Player *player, Input *input)
{
@ -32,12 +33,13 @@ void player_draw(Player *player, Camera *camera)
vec_drect(tl, br, C_BLACK);
}
void player_draw_debug(Player *player, uint step)
void player_draw_debug(Player *player, uint step, const Level *level, uint layer_id)
{
dprint(0, 0, C_BLACK, "x: %d", player->pos.x);
dprint(0, 10, C_BLACK, "y: %d", player->pos.y);
dprint(0, 20, C_BLACK, "dx: %d", player->pos.x / SCALE * VEC_PRECISION);
dprint(0, 30, C_BLACK, "dy: %d", player->pos.y / SCALE * VEC_PRECISION);
dprint(0, 40, C_BLACK, "vp: %d", VEC_PRECISION);
dprint(0, 50, C_BLACK, "st: %u", step);
dprint(0, 20, C_BLACK, "vp: %d", VEC_PRECISION);
dprint(0, 30, C_BLACK, "st: %u", step);
dprint(0, 40, C_BLACK, "cx: %d", player->pos.x / (VEC_PRECISION * TILE_SIZE));
dprint(0, 50, C_BLACK, "cy: %d", player->pos.y / (VEC_PRECISION * TILE_SIZE));
dprint(0, 60, C_BLACK, "cl: %d", collide_point(player->pos, level, layer_id));
}