#include "player.h" #include "map.h" #include #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! */ #ifdef FXCG50 #define SPEED 2 #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, P_CENTER)){ player_fix_position(player, dx, dy); }else{ /* If he won't collide I just move him normally */ if(player_collision(map_level, player, direction, P_RIGHTDOWN) || player_collision(map_level, player, direction, P_LEFTUP)){ /* I fix his position so he won't be partially in the tile. */ player_fix_position(player, dx==0, dy==0); } player->x += dx; player->y += dy; } } void player_action(Player *player) { /**/ } bool player_collision(Map *map_level, Player *player, Direction direction, Checkpos nomov_axis_check) { /* What's the tile the player is going to. */ short int i; /* Where is the tile where he will go to from his position. */ char dx = one_px_mov[direction*2]; char dy = one_px_mov[direction*2+1]; if(!dx){ dx += nomov_axis_check; dx = dx*(P_WIDTH/2+(nomov_axis_check == P_CENTER)); }else if(!dy){ dy += nomov_axis_check; dy = dy*(P_HEIGHT/2+(nomov_axis_check == P_CENTER)); } /* The tile he will go to. */ int player_tile_x = (player->x+dx)/T_WIDTH; int player_tile_y = (player->y+dy)/T_HEIGHT; for(i=0;inblayers;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, player_tile_y, 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; }