diff --git a/src/game.c b/src/game.c index 975e8aa..9a5577c 100644 --- a/src/game.c +++ b/src/game.c @@ -13,28 +13,28 @@ extern bopti_image_t SignAction_img; extern Dialog *dialogRPG; -//extern NPC *npcRPG; -//extern uint32_t nbNPC; +// extern NPC *npcRPG; +// extern uint32_t nbNPC; -#define MAX_INTERACTION_DISTANCE 12 +#define MAX_INTERACTION_DISTANCE 12 -void interaction_available(Game *game) -{ +void interaction_available(Game *game) { uint32_t i; /*NPCs take priority over signs*/ - for(uint32_t i=0; imap_level->nbNPC; i++){ - if(!game->map_level->npcs[i].has_dialogue) continue; + for(uint32_t i = 0; i < game->map_level->nbNPC; i++) { + if(!game->map_level->npcs[i].has_dialogue) + continue; /* simple distance check along X and Y axis */ /* Be careful to use world coordinates, not local (i.e.map) ones */ - if ((abs((int) game->player.wx - - (int) game->map_level->npcs[i].curx*PXSIZE ) - < MAX_INTERACTION_DISTANCE*PXSIZE) - && (abs((int) game->player.wy - - (int) game->map_level->npcs[i].cury*PXSIZE ) - < MAX_INTERACTION_DISTANCE*PXSIZE)){ + if((abs((int)game->player.wx - + (int)game->map_level->npcs[i].curx * PXSIZE) < + MAX_INTERACTION_DISTANCE * PXSIZE) && + (abs((int)game->player.wy - + (int)game->map_level->npcs[i].cury * PXSIZE) < + MAX_INTERACTION_DISTANCE * PXSIZE)) { /* the player can do something */ game->player.canDoSomething = true; /* we mark the action for futur treatment in player_action() */ @@ -45,16 +45,15 @@ void interaction_available(Game *game) } } - for(i = 0; i < game->map_level->nbSign; i++){ - /* simple distance check along X and Y axis */ + for(i = 0; i < game->map_level->nbSign; i++) { + /* simple distance check along X and Y axis */ /* Be careful to use world coordinates, not local (i.e.map) ones */ - if ((abs((int) game->player.wx - - (int) game->map_level->signs[i].x*PXSIZE ) - < MAX_INTERACTION_DISTANCE*PXSIZE) - && (abs((int) game->player.wy - - (int) game->map_level->signs[i].y*PXSIZE ) - < MAX_INTERACTION_DISTANCE*PXSIZE)) - { + if((abs((int)game->player.wx - + (int)game->map_level->signs[i].x * PXSIZE) < + MAX_INTERACTION_DISTANCE * PXSIZE) && + (abs((int)game->player.wy - + (int)game->map_level->signs[i].y * PXSIZE) < + MAX_INTERACTION_DISTANCE * PXSIZE)) { /* the player can do something */ game->player.canDoSomething = true; /* we mark the action for future treatment in player_action() */ @@ -74,14 +73,13 @@ void interaction_available(Game *game) void game_logic(Game *game) { - update_npcs( game ); + update_npcs(game); /* we check if interactions are possible close to the player */ interaction_available(game); } - void game_render_indicator(Game *game) { /* nothing to do for the player so we quit */ if(game->player.canDoSomething == false) diff --git a/src/game.h b/src/game.h index b2fead6..4e8627e 100644 --- a/src/game.h +++ b/src/game.h @@ -9,16 +9,11 @@ /* The direction where the player is going to. */ typedef enum { D_UP, D_DOWN, D_LEFT, D_RIGHT } Direction; -typedef enum { - P_LEFTUP = -1, - P_CENTER = 0, - P_RIGHTDOWN = 1 -} Checkpos; +typedef enum { P_LEFTUP = -1, P_CENTER = 0, P_RIGHTDOWN = 1 } Checkpos; -typedef struct -{ - uint32_t x,y; - uint32_t w,h; +typedef struct { + uint32_t x, y; + uint32_t w, h; } Collider; @@ -75,12 +70,12 @@ typedef struct { uint32_t needAction; } Sign; -typedef struct -{ +typedef struct { /* current coordinates of the NPC */ float curx, cury; - /* initial coordinates of the NPC (needed to get absolute coordinates of path) */ + /* initial coordinates of the NPC (needed to get absolute coordinates of + * path) */ uint32_t x; uint32_t y; /* id of it's face */ @@ -112,7 +107,7 @@ typedef struct uint16_t __padding; } NPC; -typedef struct{ +typedef struct { Collider collider; /*if the portal tps to an interior or exterior map*/ uint16_t tp_interior; diff --git a/src/main.c b/src/main.c index 3e6824a..b705b5f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,10 +1,9 @@ +#include "config.h" + +#include #include #include #include -#include - - -#include "config.h" #if USB_FEATURE #include @@ -12,11 +11,11 @@ #endif // USB_FEATURE #if GRAYMODEOK - #include -#endif //GRAYMODEOK +#include +#endif // GRAYMODEOK #if DEBUGMODE - #include +#include #endif /*DEBUGMODE*/ #include "dialogs.h" @@ -25,7 +24,6 @@ #include #include - #include extern bopti_image_t player_face_img; @@ -94,10 +92,10 @@ int update_time(void) { } int main(void) { - - #if DEBUGMODE - gdb_start_on_exception(); - #endif /*DEBUGMODE*/ + +#if DEBUGMODE + gdb_start_on_exception(); +#endif /*DEBUGMODE*/ //__printf_enable_fp(); @@ -113,18 +111,18 @@ int main(void) { events_bind_variable(&game.handler, (int *)&game.player.life, "life"); events_bind_variable(&game.handler, &game.mana, "mana"); - //reload_npc(&game); + // reload_npc(&game); - #if USB_FEATURE - usb_interface_t const *interfaces[] = {&usb_ff_bulk, NULL}; - usb_open(interfaces, GINT_CALL_NULL); - #endif +#if USB_FEATURE + usb_interface_t const *interfaces[] = {&usb_ff_bulk, NULL}; + usb_open(interfaces, GINT_CALL_NULL); +#endif /* start grayscale engine */ - #if GRAYMODEOK - dgray(DGRAY_ON); - #endif +#if GRAYMODEOK + dgray(DGRAY_ON); +#endif dupdate(); getkey(); @@ -147,7 +145,7 @@ int main(void) { dprint(10, 20, C_RED, "Map[%d] : Xmn %d Ymn %d Xmx %d Ymx %d", 1, worldRPG[1]->xmin, worldRPG[1]->ymin, worldRPG[1]->xmax, worldRPG[1]->ymax); - dprint(10, 30, C_RED, "Map[%d] : Xmn %d Ymn %d Xmx %d Ymx %d", + dprint(10, 30, C_RED, "Map[%d] : Xmn %d Ymn %d Xmx %d Ymx %d", 2, worldRPG[2]->xmin, worldRPG[2]->ymin, worldRPG[2]->xmax, worldRPG[2]->ymax); dprint(10, 40, C_RED, "Map[%d] : Xmn %d Ymn %d Xmx %d Ymx %d", @@ -166,12 +164,13 @@ int main(void) { { dfont( NULL ); for (int i=0; inbextradata; i++ ) - dprint( 10, 90+i*15, C_RED, "X= %d - Y= %d - T: %d - ID: %d - S: %c", - game.map_level->extradata[i].x, + dprint( 10, 90+i*15, C_RED, "X= %d - Y= %d - T: %d - ID: %d + - S: %c", game.map_level->extradata[i].x, game.map_level->extradata[i].y, game.map_level->extradata[i].dialogID, - game.map_level->dialogs[ game.map_level->extradata[i].dialogID ].ID, - game.map_level->dialogs[ game.map_level->extradata[i].dialogID ].conclusion1[0] ); + game.map_level->dialogs[ + game.map_level->extradata[i].dialogID ].ID, game.map_level->dialogs[ + game.map_level->extradata[i].dialogID ].conclusion1[0] ); } #endif*/ diff --git a/src/npc.c b/src/npc.c index ddab831..a7559b2 100644 --- a/src/npc.c +++ b/src/npc.c @@ -16,8 +16,8 @@ extern bopti_image_t tiny_npc_female; extern bopti_image_t tiny_npc_milkman; extern bopti_image_t tiny_npc_police; -//NPC *npcRPG; -//uint32_t nbNPC = 0; +// NPC *npcRPG; +// uint32_t nbNPC = 0; float length(float x, float y) { return sqrtf(x * x + y * y); } @@ -225,11 +225,9 @@ int npc_pathfind(int32_t dest_x, int32_t dest_y, Map *full_map, NPC *npc) { memmove(npc, &npc[1], (nbNPC - pos - 1) * sizeof(NPC)); }*/ -//Refactoring to make adding complexity cleaner -void update_npcs([[maybe_unused]] Game *game) -{ - for( uint32_t u=0; umap_level->nbNPC; u++ ) - { +// Refactoring to make adding complexity cleaner +void update_npcs([[maybe_unused]] Game *game) { + for(uint32_t u = 0; u < game->map_level->nbNPC; u++) { update_npc(&game->map_level->npcs[u]); } } @@ -303,40 +301,37 @@ void npc_draw(Game *game) { Player *pl = &game->player; size_t i; const bopti_image_t *npc_sprites[FACES] = { - &tiny_npc_male, - &tiny_npc_female, - &tiny_npc_milkman, - &tiny_npc_police - }; + &tiny_npc_male, &tiny_npc_female, &tiny_npc_milkman, &tiny_npc_police}; - for (uint32_t u=0; umap_level->nbNPC; u++) - { + for(uint32_t u = 0; u < game->map_level->nbNPC; u++) { NPC *Data = &game->map_level->npcs[u]; - - /* Render the path if in debug*/ - #if DEBUGMODE - if(!Data->hasPath) continue; /* this NPC has a trajectory */ - int NbPoints = Data->path_length+1; - for(int v=0; vx + - Data->xpath[v % NbPoints]) * PXSIZE) - -(int16_t) pl->wx; - int16_t deltaY1=((int16_t) (Data->y + - Data->ypath[v % NbPoints]) * PXSIZE) - -(int16_t) pl->wy; - int16_t deltaX2=((int16_t) (Data->x + - Data->xpath[(v+1) % NbPoints]) * PXSIZE) - -(int16_t) pl->wx; - int16_t deltaY2=((int16_t) (Data->y + - Data->ypath[(v+1) % NbPoints]) * PXSIZE) - -(int16_t) pl->wy; - - dline( pl->px + deltaX1, pl->py + deltaY1,pl->px + deltaX2, - pl->py + deltaY2,PATH_COLOR); - } - #endif // DEBUGMODE + +/* Render the path if in debug*/ +#if DEBUGMODE + if(!Data->hasPath) + continue; /* this NPC has a trajectory */ + int NbPoints = Data->path_length + 1; + for(int v = 0; v < NbPoints; v++) { + + int16_t deltaX1 = + ((int16_t)(Data->x + Data->xpath[v % NbPoints]) * PXSIZE) - + (int16_t)pl->wx; + int16_t deltaY1 = + ((int16_t)(Data->y + Data->ypath[v % NbPoints]) * PXSIZE) - + (int16_t)pl->wy; + int16_t deltaX2 = + ((int16_t)(Data->x + Data->xpath[(v + 1) % NbPoints]) * + PXSIZE) - + (int16_t)pl->wx; + int16_t deltaY2 = + ((int16_t)(Data->y + Data->ypath[(v + 1) % NbPoints]) * + PXSIZE) - + (int16_t)pl->wy; + + dline(pl->px + deltaX1, pl->py + deltaY1, pl->px + deltaX2, + pl->py + deltaY2, PATH_COLOR); + } +#endif // DEBUGMODE int16_t delX = ((int16_t)(Data->curx * PXSIZE)) - (int16_t)pl->wx; int16_t delY = ((int16_t)(Data->cury * PXSIZE)) - (int16_t)pl->wy; diff --git a/src/player.c b/src/player.c index 3ae8e77..e6aa200 100644 --- a/src/player.c +++ b/src/player.c @@ -17,12 +17,8 @@ extern bopti_image_t npc_police; extern bopti_image_t SGN_Icon_img; extern bopti_image_t INFO_Icon_img; -const bopti_image_t *faces[FACES] = { - &npc_male, - &npc_female, - &npc_milkman, - &npc_police -}; +const bopti_image_t *faces[FACES] = {&npc_male, &npc_female, &npc_milkman, + &npc_police}; const char one_px_mov[8] = { 0, -1, /* Up */ @@ -99,10 +95,10 @@ void player_action(Game *game) { size_t i; /* already doing something, or can't do anything*/ - if(game->player.isDoingAction || !game->player.canDoSomething) return; + if(game->player.isDoingAction || !game->player.canDoSomething) + return; - if(!game->player.isInteractingWithNPC) - { + if(!game->player.isInteractingWithNPC) { /* we can do something */ /* we indicate that the player is occupied */ game->player.isDoingAction = true; @@ -123,9 +119,7 @@ void player_action(Game *game) { /* when done we release the occupied status of the player */ game->player.isDoingAction = false; - } - else - { + } else { /* we can do something (action IS with an NPC) */ /* we indicate that the player is occupied */ game->player.isDoingAction = true; @@ -198,11 +192,12 @@ bool player_collision(Game *game, Direction direction, player->x = (worldX - map->xmin) * PXSIZE; player->y = (worldY - map->ymin) * PXSIZE; - int on_walkable = map_get_walkable(game, player->x/T_WIDTH, - player->y/T_HEIGHT); + int on_walkable = map_get_walkable(game, player->x / T_WIDTH, + player->y / T_HEIGHT); - int speed = (on_walkable >= 0 && on_walkable < WALKABLE_TILE_MAX) ? - walkable_speed[on_walkable] : 0; + int speed = (on_walkable >= 0 && on_walkable < WALKABLE_TILE_MAX) + ? walkable_speed[on_walkable] + : 0; /* if he's on a hard tile and we need to revert the changes as */ /* tile on the next side of the border is not walkable */ @@ -218,7 +213,7 @@ bool player_collision(Game *game, Direction direction, /* we update the list of NPCs in the current map */ /* to follow the trajectories */ - //reload_npc(game); + // reload_npc(game); return false; } diff --git a/src/player.h b/src/player.h index 1cf4785..a34bd75 100644 --- a/src/player.h +++ b/src/player.h @@ -34,7 +34,8 @@ void player_draw(Game *game); */ void player_move(Game *game, Direction direction); -/*Tries to do an action based on previously set flags (called if the shift key is pressed)*/ +/*Tries to do an action based on previously set flags (called if the shift key + * is pressed)*/ void player_action(Game *game); /* player_collision()