Collab_RPG/src/player.c

87 lines
2.6 KiB
C
Raw Normal View History

#include "player.h"
#include "map.h"
#include <gint/display.h>
#ifdef FXCG50
#define P_WIDTH 16
#define P_HEIGHT 16
#else
#define P_WIDTH 8
#define P_HEIGHT 8
#endif
/* 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
#define SPEED 2
2023-07-07 19:26:14 +02:00
#else
#define SPEED 1
#endif
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
/* 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. */
#define HARD_TILES_AMOUNT 5
const short int hard_tiles[HARD_TILES_AMOUNT] = {
MAP_OUTSIDE, 124, 148, 125, 149
};
extern bopti_image_t demo_player_img;
void player_draw(Player *player) {
dimage(player->px-P_WIDTH/2, player->py-P_HEIGHT/2, &demo_player_img);
}
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. */
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. */
player_fix_position(player, 1, 1);
}else{
2023-07-08 16:57:04 +02:00
/* If he won't collide I just move him normally */
player->x += dx;
player->y += dy;
2023-07-07 14:50:30 +02:00
}
}
void player_action(Player *player) {
/**/
}
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. */
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. */
int player_tile_x = player->x/T_WIDTH;
int player_tile_y = player->y/T_HEIGHT;
for(i=0;i<map_level->nblayers;i++){
2023-07-08 16:57:04 +02:00
/* if he's on a hard tile */
if(is_in((short int*)hard_tiles, HARD_TILES_AMOUNT,
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-07 14:50:30 +02:00
}
2023-07-08 16:57:04 +02:00
return false; /* He won't collide with a hard tile. */
}
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. */
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;
}