2021-03-30 01:42:11 +02:00
|
|
|
/* 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;
|
|
|
|
|
2021-03-30 22:57:49 +02:00
|
|
|
static Tile collide_single(int x, int y);
|
2021-03-30 01:42:11 +02:00
|
|
|
|
2021-03-30 22:57:49 +02:00
|
|
|
void player_collide(Tile collisions[COLLIDE_POINTS], int x, int y)
|
2021-03-30 01:42:11 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-03-30 22:57:49 +02:00
|
|
|
int player_collide_tile(Tile collisions[COLLIDE_POINTS], int x, int y,
|
|
|
|
Tile tile, int update)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (update)
|
|
|
|
player_collide(collisions, x, y);
|
|
|
|
for (i = 0; i < COLLIDE_POINTS; i += 1)
|
|
|
|
if (collisions[i] == tile)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int player_collide_solid(int x, int y)
|
|
|
|
{
|
|
|
|
Tile collisions[COLLIDE_POINTS];
|
|
|
|
return player_collide_tile(collisions, x, y, TILE_SOLID, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Tile collide_single(int x, int y)
|
2021-03-30 01:42:11 +02:00
|
|
|
{
|
|
|
|
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];
|
|
|
|
}
|