mirror of
https://git.planet-casio.com/Slyvtt/Collab_RPG.git
synced 2025-01-04 07:53:39 +01:00
85 lines
2.7 KiB
C
85 lines
2.7 KiB
C
#include "player.h"
|
|
#include "map.h"
|
|
#include <gint/display.h>
|
|
|
|
/* (Mibi88) TODO: Upscale the player for the CG50. */
|
|
/* The player should not be bigger than a tile because it may cause problems
|
|
* with the collisions. If it's a problem please ask me (Mibi88) to adapt that.
|
|
*/
|
|
#define P_WIDTH 8
|
|
#define P_HEIGHT 8
|
|
|
|
/* SPEED should NOT be 8 or bigger: it this may cause bugs when handling
|
|
* collisions! */
|
|
#ifdef FXCG50
|
|
#define SPEED 3
|
|
#else
|
|
#define SPEED 1
|
|
#endif
|
|
|
|
const char one_px_mov[8] = {
|
|
0, -1, /* Up */
|
|
0, 1, /* Down */
|
|
-1, 0, /* Left */
|
|
1, 0 /* Right */
|
|
};
|
|
|
|
/* TODO: Search for all hard tiles in the tileset. hard_tiles is a list of their
|
|
* IDs */
|
|
/* 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;
|
|
/* If the player will collide with a hard tile. */
|
|
if(player_collision(map_level, player, direction)){
|
|
/* I fix his position so he won't be partially in the tile. */
|
|
player_fix_position(player, dx, dy);
|
|
}else{
|
|
/* If he won't collide I just move him normally */
|
|
player->x += dx;
|
|
player->y += dy;
|
|
}
|
|
}
|
|
|
|
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;
|
|
/* 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];
|
|
/* 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++){
|
|
/* 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))){
|
|
return true; /* He will collide with it. */
|
|
}
|
|
}
|
|
return false; /* He won't collide with a hard tile. */
|
|
}
|
|
|
|
void player_fix_position(Player *player, bool fix_x, bool fix_y) {
|
|
/* 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;
|
|
}
|
|
|