Action texts on player and some refactoring

This commit is contained in:
Linus Probert 2017-12-18 15:26:56 +01:00
parent 1c48f1dd9a
commit 64a9105d21
6 changed files with 79 additions and 11 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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);
}

View File

@ -4,12 +4,16 @@
#include <SDL2/SDL.h>
#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*);

View File

@ -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)
{

View File

@ -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*);