From 64a9105d21820117e723fee86998029f2e397316 Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Mon, 18 Dec 2017 15:26:56 +0100 Subject: [PATCH] Action texts on player and some refactoring --- src/main.c | 13 ++++-------- src/monster.c | 4 +++- src/player.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ src/player.h | 10 +++++++++- src/roommatrix.c | 10 ++++++++++ src/roommatrix.h | 2 ++ 6 files changed, 79 insertions(+), 11 deletions(-) diff --git a/src/main.c b/src/main.c index 242f8bc..9278486 100644 --- a/src/main.c +++ b/src/main.c @@ -111,7 +111,7 @@ bool init() static void loadMedia() { - gPlayer = player_create(WARRIOR, gRenderer); + gPlayer = player_create(ROGUE, gRenderer); } static @@ -152,20 +152,15 @@ void run() roommatrix_build_lightmap(gRoomMatrix); if (gPlayer->steps == gPlayer->stats.speed) { - player_print(gPlayer); - gPlayer->steps = 0; - Position rp = - position_to_matrix_coords(&gPlayer->sprite->pos); - gRoomMatrix->spaces[rp.x][rp.y].occupied = true; - gRoomMatrix->spaces[rp.x][rp.y].player = gPlayer; - gRoomMatrix->playerRoomPos = rp; + player_reset_steps(gPlayer); + roommatrix_update_with_player(gRoomMatrix, gPlayer); map_move_monsters(gMap, gRoomMatrix); } SDL_RenderClear(gRenderer); map_render(gMap, &gCamera); - sprite_render(gPlayer->sprite, &gCamera); + player_render(gPlayer, &gCamera); roommatrix_render_lightmap(gRoomMatrix, &gCamera); SDL_RenderPresent(gRenderer); diff --git a/src/monster.c b/src/monster.c index b24a2a5..27547f0 100644 --- a/src/monster.c +++ b/src/monster.c @@ -60,7 +60,9 @@ has_collided(Monster *monster, RoomMatrix *matrix) RoomSpace *space = &matrix->spaces[roomPos.x][roomPos.y]; if (space->player) { - stats_fight(&monster->stats, &space->player->stats); + unsigned int dmg = stats_fight(&monster->stats, + &space->player->stats); + player_hit(space->player, dmg); } return space->occupied; diff --git a/src/player.c b/src/player.c index e73a10a..ccf42a7 100644 --- a/src/player.c +++ b/src/player.c @@ -43,6 +43,8 @@ player_step(Player *p, RoomMatrix* m) { p->total_steps++; p->steps++; + p->missText->pos = p->sprite->pos; + p->hitText->pos = p->sprite->pos; } static void @@ -121,6 +123,24 @@ void handle_player_input(Player *player, RoomMatrix *matrix, SDL_Event *event) } } +static void +player_load_texts(Player *p, SDL_Renderer *renderer) +{ + ActionText *t = actiontext_create(); + actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14); + t->color = (SDL_Color) { 255, 100, 0 }; + actiontext_set_text(t, "HIT", renderer); + t->pos = p->sprite->pos; + p->hitText = t; + + t = actiontext_create(); + actiontext_load_font(t, "assets/GUI/SDS_6x6.ttf", 14); + t->color = (SDL_Color) { 255, 255, 0 }; + actiontext_set_text(t, "MISS", renderer); + t->pos = p->sprite->pos; + p->missText = t; +} + Player* player_create(class_t class, SDL_Renderer *renderer) { @@ -161,10 +181,32 @@ player_create(class_t class, SDL_Renderer *renderer) TILE_DIMENSION, TILE_DIMENSION }; player->handle_event = &handle_player_input; + player_load_texts(player, renderer); + return player; } void +player_hit(Player *p, unsigned int dmg) +{ + if (dmg > 0) { + p->hitText->active = true; + p->missText->active = false; + } else { + p->missText->active = true; + p->hitText->active = false; + } +} + +void +player_render(Player *player, Camera *cam) +{ + sprite_render(player->sprite, cam); + actiontext_render(player->hitText, cam); + actiontext_render(player->missText, cam); +} + +static void player_print(Player *p) { Position roomPos = position_to_matrix_coords(&p->sprite->pos); @@ -179,11 +221,20 @@ player_print(Player *p) printf("------------------------------------------\n"); } +void +player_reset_steps(Player *p) +{ + p->steps = 0; + player_print(p); +} + void player_destroy(Player *player) { if (player->sprite) sprite_destroy(player->sprite); + actiontext_destroy(player->hitText); + actiontext_destroy(player->missText); free(player); } diff --git a/src/player.h b/src/player.h index 6fed0b2..74f4902 100644 --- a/src/player.h +++ b/src/player.h @@ -4,12 +4,16 @@ #include #include "sprite.h" #include "stats.h" +#include "actiontext.h" +#include "camera.h" enum PlayerClass { ENGINEER, MAGE, PALADIN, ROGUE, WARRIOR }; typedef enum PlayerClass class_t; typedef struct Player_t { Sprite *sprite; + ActionText *hitText; + ActionText *missText; Stats stats; unsigned int xp; unsigned int total_steps; @@ -19,7 +23,11 @@ typedef struct Player_t { Player* player_create(class_t, SDL_Renderer*); -void player_print(Player*); +void player_hit(Player*, unsigned int dmg); + +void player_reset_steps(Player*); + +void player_render(Player*, Camera*); void player_destroy(Player*); diff --git a/src/roommatrix.c b/src/roommatrix.c index 90298e6..157c606 100644 --- a/src/roommatrix.c +++ b/src/roommatrix.c @@ -2,6 +2,7 @@ #include "roommatrix.h" #include "util.h" #include "map.h" +#include "player.h" RoomMatrix* roommatrix_create() { @@ -72,6 +73,15 @@ max(int a, int b) return a > b ? a : b; } +void +roommatrix_update_with_player(RoomMatrix *rm, Player *p) +{ + Position rp = position_to_matrix_coords(&p->sprite->pos); + rm->spaces[rp.x][rp.y].occupied = true; + rm->spaces[rp.x][rp.y].player = p; + rm->playerRoomPos = rp; +} + void roommatrix_add_lightsource(RoomMatrix *matrix, Position *pos) { diff --git a/src/roommatrix.h b/src/roommatrix.h index af7317d..03db0bc 100644 --- a/src/roommatrix.h +++ b/src/roommatrix.h @@ -29,6 +29,8 @@ RoomMatrix* roommatrix_create(); void roommatrix_populate_from_map(RoomMatrix*, Map*); +void roommatrix_update_with_player(RoomMatrix*, Player*); + void roommatrix_add_lightsource(RoomMatrix*, Position*); void roommatrix_build_lightmap(RoomMatrix*);