Collab_RPG/src/game.c
2024-07-30 18:21:22 +02:00

159 lines
4.4 KiB
C

#include "game.h"
#include "config.h"
#include "map.h"
#include "npc.h"
#include <gint/cpu.h>
#include <gint/display.h>
#include <gint/keyboard.h>
#include <stdlib.h>
#include <string.h>
extern bopti_image_t SignAction_img;
extern Dialog *dialogRPG;
// extern NPC *npcRPG;
// extern uint32_t nbNPC;
#define MAX_INTERACTION_DISTANCE 12
void interaction_available(Game *game) {
uint32_t i;
/*NPCs take priority over signs*/
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)) {
/* the player can do something */
game->player.canDoSomething = true;
/* we mark the action for futur treatment in player_action() */
game->player.whichAction = i;
/* this is an interraction with a NPC */
game->player.isInteractingWithNPC = true;
return;
}
}
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)) {
/* the player can do something */
game->player.canDoSomething = true;
/* we mark the action for future treatment in player_action() */
game->player.whichAction = i;
/* this is not an interraction with a NPC */
game->player.isInteractingWithNPC = false;
return;
}
}
/* else nothing to be done here */
game->player.canDoSomething = false;
game->player.whichAction = -1;
game->player.isInteractingWithNPC = false;
return;
}
void game_logic(Game *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)
return;
/* else we draw a small indicator on the screen */
dimage(5, 5, &SignAction_img);
}
void game_draw(Game *game) {
/* Draw everything. */
dclear(C_WHITE);
map_render_by_layer(game, BACKGROUND);
npc_draw(game);
player_draw(game);
map_render_by_layer(game, FOREGROUND);
game_render_indicator(game);
/*DEBUG*/
dprint(8, 8, C_BLACK, "Lifes: %d", game->player.life);
dprint(8, 16, C_BLACK, "Mana: %d", game->mana);
}
/* Key management */
void game_get_inputs(Game *game) {
clearevents();
/* Key binding for the Player action */
/*************************************/
if(keydown(KEY_EXIT))
game->exittoOS = true;
/* Player actions - Prototypes in player.h and implementation in player.c */
if(keydown(KEY_LEFT))
player_move(game, D_LEFT);
if(keydown(KEY_RIGHT))
player_move(game, D_RIGHT);
if(keydown(KEY_UP))
player_move(game, D_UP);
if(keydown(KEY_DOWN))
player_move(game, D_DOWN);
if(keydown(KEY_SHIFT))
player_action(game);
if(keydown(KEY_OPTN)) {
game->player.is_male = !game->player.is_male;
/* TODO: Make something cleaner */
while(keydown(KEY_OPTN)) {
clearevents();
sleep();
}
}
/* Display Debug Information on screen */
#if DEBUGMODE
if(keydown(KEY_F1)) {
game->debug_map = !game->debug_map;
}
if(keydown(KEY_F2)) {
game->debug_player = !game->debug_player;
}
if(keydown(KEY_F3)) {
game->debug_extra = !game->debug_extra;
}
#endif
/* if USB is enabled - keybinding for screencapture */
#if USB_FEATURE
if(keydown(KEY_7))
game->screenshot = true;
if(keydown(KEY_8))
game->record = !game->record;
#endif // USB_FEATURE
}