2023-07-06 22:02:37 +02:00
|
|
|
#include "player.h"
|
|
|
|
#include "map.h"
|
2023-07-07 14:20:13 +02:00
|
|
|
#include <gint/display.h>
|
2023-07-06 22:02:37 +02:00
|
|
|
|
2023-07-08 20:07:34 +02:00
|
|
|
#ifdef FXCG50
|
|
|
|
#define P_WIDTH 16
|
|
|
|
#define P_HEIGHT 16
|
|
|
|
#else
|
|
|
|
#define P_WIDTH 8
|
|
|
|
#define P_HEIGHT 8
|
|
|
|
#endif
|
2023-07-07 14:20:13 +02:00
|
|
|
|
2023-07-08 15:55:06 +02:00
|
|
|
/* SPEED should NOT be 8 or bigger: it this may cause bugs when handling
|
|
|
|
* collisions! */
|
2023-07-07 19:26:14 +02:00
|
|
|
#ifdef FXCG50
|
2023-07-08 20:07:34 +02:00
|
|
|
#define SPEED 2
|
2023-07-07 19:26:14 +02:00
|
|
|
#else
|
|
|
|
#define SPEED 1
|
|
|
|
#endif
|
|
|
|
|
2023-07-08 15:55:06 +02:00
|
|
|
const char one_px_mov[8] = {
|
|
|
|
0, -1, /* Up */
|
|
|
|
0, 1, /* Down */
|
|
|
|
-1, 0, /* Left */
|
|
|
|
1, 0 /* Right */
|
|
|
|
};
|
2023-07-07 19:26:14 +02:00
|
|
|
|
2023-07-08 15:55:06 +02:00
|
|
|
/* TODO: Search for all hard tiles in the tileset. hard_tiles is a list of their
|
|
|
|
* IDs */
|
2023-07-08 16:57:04 +02:00
|
|
|
/* The tiles where the player can't go trough. */
|
2023-07-08 15:55:06 +02:00
|
|
|
#define HARD_TILES_AMOUNT 5
|
|
|
|
const short int hard_tiles[HARD_TILES_AMOUNT] = {
|
|
|
|
MAP_OUTSIDE, 124, 148, 125, 149
|
|
|
|
};
|
2023-07-06 22:02:37 +02:00
|
|
|
|
2023-07-08 15:55:06 +02:00
|
|
|
extern bopti_image_t demo_player_img;
|
2023-07-07 18:22:39 +02:00
|
|
|
|
2023-07-08 15:55:06 +02:00
|
|
|
void player_draw(Player *player) {
|
|
|
|
dimage(player->px-P_WIDTH/2, player->py-P_HEIGHT/2, &demo_player_img);
|
2023-07-07 14:20:13 +02:00
|
|
|
}
|
2023-07-06 22:02:37 +02:00
|
|
|
|
2023-07-08 15:55:06 +02:00
|
|
|
void player_move(Map *map_level, Player *player, Direction direction) {
|
|
|
|
/* How this player movement will modify the player x and y. */
|
|
|
|
const char dx = one_px_mov[direction*2]*SPEED;
|
|
|
|
const char dy = one_px_mov[direction*2+1]*SPEED;
|
2023-07-08 16:57:04 +02:00
|
|
|
/* If the player will collide with a hard tile. */
|
2023-07-08 15:55:06 +02:00
|
|
|
if(player_collision(map_level, player, direction)){
|
2023-07-08 16:57:04 +02:00
|
|
|
/* I fix his position so he won't be partially in the tile. */
|
2023-07-08 20:07:34 +02:00
|
|
|
player_fix_position(player, 1, 1);
|
2023-07-08 15:55:06 +02:00
|
|
|
}else{
|
2023-07-08 16:57:04 +02:00
|
|
|
/* If he won't collide I just move him normally */
|
2023-07-08 15:55:06 +02:00
|
|
|
player->x += dx;
|
|
|
|
player->y += dy;
|
2023-07-07 14:50:30 +02:00
|
|
|
}
|
2023-07-06 22:02:37 +02:00
|
|
|
}
|
|
|
|
|
2023-07-08 15:55:06 +02:00
|
|
|
void player_action(Player *player) {
|
|
|
|
/**/
|
2023-07-06 22:02:37 +02:00
|
|
|
}
|
|
|
|
|
2023-07-08 15:55:06 +02:00
|
|
|
bool player_collision(Map *map_level, Player *player, Direction direction) {
|
|
|
|
/* What's the tile the player is going to. */
|
|
|
|
short int i;
|
2023-07-08 16:57:04 +02:00
|
|
|
/* Where is the tile where he will go to from his position. */
|
2023-07-08 15:55:06 +02:00
|
|
|
const char dx = one_px_mov[direction*2];
|
|
|
|
const char dy = one_px_mov[direction*2+1];
|
2023-07-08 16:57:04 +02:00
|
|
|
/* The tile he will go to. */
|
2023-07-08 15:55:06 +02:00
|
|
|
int player_tile_x = player->x/T_WIDTH;
|
2023-07-08 16:40:25 +02:00
|
|
|
int player_tile_y = player->y/T_HEIGHT;
|
2023-07-08 15:55:06 +02:00
|
|
|
for(i=0;i<map_level->nblayers;i++){
|
2023-07-08 16:57:04 +02:00
|
|
|
/* if he's on a hard tile */
|
2023-07-08 16:40:25 +02:00
|
|
|
if(is_in((short int*)hard_tiles, HARD_TILES_AMOUNT,
|
2023-07-08 15:55:06 +02:00
|
|
|
get_tile(map_level, player_tile_x+dx, player_tile_y+dy, i))){
|
2023-07-08 16:57:04 +02:00
|
|
|
return true; /* He will collide with it. */
|
2023-07-08 15:55:06 +02:00
|
|
|
}
|
2023-07-07 14:50:30 +02:00
|
|
|
}
|
2023-07-08 16:57:04 +02:00
|
|
|
return false; /* He won't collide with a hard tile. */
|
2023-07-06 22:02:37 +02:00
|
|
|
}
|
|
|
|
|
2023-07-08 15:55:06 +02:00
|
|
|
void player_fix_position(Player *player, bool fix_x, bool fix_y) {
|
2023-07-08 16:57:04 +02:00
|
|
|
/* I fix his poition on x or/and on y if y need to, so that he won't be over
|
|
|
|
* the hard tile that he collided with. */
|
2023-07-08 16:40:25 +02:00
|
|
|
if(fix_x) player->x = player->x/T_WIDTH*T_WIDTH+P_WIDTH/2;
|
|
|
|
if(fix_y) player->y = player->y/T_HEIGHT*T_HEIGHT+P_HEIGHT/2;
|
2023-07-06 22:02:37 +02:00
|
|
|
}
|
|
|
|
|