Nicer gui and stored potions.

This commit is contained in:
Linus Probert 2018-02-03 13:02:39 +01:00
parent 5b8a8030bd
commit e7e412cc33
8 changed files with 115 additions and 46 deletions

View File

@ -43,6 +43,8 @@ if (NOT WIN32)
) )
endif (NOT WIN32) endif (NOT WIN32)
set(CMAKE_C_FLAGS_DEBUG "-DDEBUG")
# PROGRAMS: # PROGRAMS:
add_executable(breakhack add_executable(breakhack
src/main src/main

View File

@ -9,7 +9,9 @@
#include "map.h" #include "map.h"
#define DEFAULT_LOG { NULL, 50, 0, 200 } #define DEFAULT_LOG { NULL, 50, 0, 200 }
#define POS_Y_XPBAR 96
#define POS_Y_COLLECTABLES 64
#define POS_Y_XPBAR 112
static SDL_Rect frame_top_left = { 16, 160, 16, 16 }; static SDL_Rect frame_top_left = { 16, 160, 16, 16 };
static SDL_Rect frame_top_right = { 48, 160, 16, 16 }; static SDL_Rect frame_top_right = { 48, 160, 16, 16 };
@ -116,6 +118,23 @@ init_sprites(Gui *gui, SDL_Renderer *renderer)
(Position) { 16 + (i * 16), POS_Y_XPBAR } (Position) { 16 + (i * 16), POS_Y_XPBAR }
)); ));
} }
Sprite *s;
t = add_texture(gui, "assets/Items/Potion.png", renderer);
s = sprite_create();
s->fixed = true;
sprite_set_texture(s, t, 0);
s->clip = (SDL_Rect) { 0, 0, 16, 16 };
s->pos = (Position) { 16, POS_Y_COLLECTABLES };
linkedlist_append(&gui->sprites, s);
t = add_texture(gui, "assets/Items/Money.png", renderer);
s = sprite_create();
s->fixed = true;
sprite_set_texture(s, t, 0);
s->clip = (SDL_Rect) { 16, 16, 16, 16 };
s->pos = (Position) { 16, POS_Y_COLLECTABLES + 16 };
linkedlist_append(&gui->sprites, s);
} }
Gui* Gui*
@ -136,10 +155,11 @@ gui_create(SDL_Renderer *renderer)
gui->log_lines[i] = t; gui->log_lines[i] = t;
} }
gui->labels[CURRENT_XP_LABEL] = create_label_sprite((Position) { 16, 116 }); gui->labels[CURRENT_XP_LABEL] = create_label_sprite((Position) { 16, POS_Y_XPBAR + 18 });
gui->labels[LEVEL_LABEL] = create_label_sprite((Position) { 16, 128 }); gui->labels[LEVEL_LABEL] = create_label_sprite((Position) { 16, POS_Y_XPBAR + 18 + 14 });
gui->labels[DUNGEON_LEVEL_LABEL] = create_label_sprite((Position) { 16, 156 }); gui->labels[DUNGEON_LEVEL_LABEL] = create_label_sprite((Position) { 16, POS_Y_XPBAR + 18 + (2*14) });
gui->labels[GOLD_LABEL] = create_label_sprite((Position) { 16, 142 }); gui->labels[HEALTH_POTION_LABEL] = create_label_sprite((Position) { 32, POS_Y_COLLECTABLES + 5 });
gui->labels[GOLD_LABEL] = create_label_sprite((Position) { 32, POS_Y_COLLECTABLES + 16 + 5 });
gui_malloc_log(); gui_malloc_log();
@ -213,6 +233,7 @@ gui_update_player_stats(Gui *gui, Player *player, Map *map, SDL_Renderer *render
static unsigned int dungeon_level = 0; static unsigned int dungeon_level = 0;
static int max_health = -1; static int max_health = -1;
static int current_health = -1; static int current_health = -1;
static int current_potion_sips = -1;
static SDL_Color color = { 255, 255, 255, 255 }; static SDL_Color color = { 255, 255, 255, 255 };
@ -270,8 +291,14 @@ gui_update_player_stats(Gui *gui, Player *player, Map *map, SDL_Renderer *render
dungeon_level = (unsigned int) map->level; dungeon_level = (unsigned int) map->level;
} }
if (current_potion_sips != (int) player->potion_sips) {
m_sprintf(buffer, 200, "x %u", (unsigned int) player->potion_sips);
texture_load_from_text(gui->labels[HEALTH_POTION_LABEL]->textures[0], buffer, color, renderer);
current_potion_sips = player->potion_sips;
}
if (last_gold != player->gold) { if (last_gold != player->gold) {
m_sprintf(buffer, 200, "Gold: %.2f", player->gold); m_sprintf(buffer, 200, "x %.2f", player->gold);
texture_load_from_text(gui->labels[GOLD_LABEL]->textures[0], buffer, color, renderer); texture_load_from_text(gui->labels[GOLD_LABEL]->textures[0], buffer, color, renderer);
last_gold = player->gold; last_gold = player->gold;
} }
@ -402,7 +429,7 @@ gui_render_log(Gui *gui, unsigned int width, unsigned int height, Camera *cam)
for (i = 0; i < render_count; ++i) { for (i = 0; i < render_count; ++i) {
Texture *t; Texture *t;
p.y = 16 + ((LOG_FONT_SIZE+1) * i); p.y = 16 + ((LOG_FONT_SIZE+5) * i);
t = gui->log_lines[i]; t = gui->log_lines[i];
texture_load_from_text(t, log_data.log[i], color, cam->renderer); texture_load_from_text(t, log_data.log[i], color, cam->renderer);
texture_render(t, &p, cam); texture_render(t, &p, cam);

View File

@ -1,7 +1,7 @@
#ifndef GUI_H_ #ifndef GUI_H_
#define GUI_H_ #define GUI_H_
#define LOG_LINES_COUNT 15 #define LOG_LINES_COUNT 10
#define LOG_FONT_SIZE 8 #define LOG_FONT_SIZE 8
#define LABEL_FONT_SIZE 8 #define LABEL_FONT_SIZE 8
@ -16,6 +16,7 @@ typedef enum Label_e {
CURRENT_XP_LABEL, CURRENT_XP_LABEL,
GOLD_LABEL, GOLD_LABEL,
DUNGEON_LEVEL_LABEL, DUNGEON_LEVEL_LABEL,
HEALTH_POTION_LABEL,
LABEL_COUNT LABEL_COUNT
} LabelIndex; } LabelIndex;

View File

@ -51,13 +51,9 @@ eat_flesh(Item *item, Player *player)
static void static void
drink_health(Item *item, Player *player) drink_health(Item *item, Player *player)
{ {
int original_hp = player->stats.hp; player->potion_sips += item->value;
player->stats.hp += (int) item->value * player->stats.lvl;
if (player->stats.hp > player->stats.maxhp)
player->stats.hp = player->stats.maxhp;
gui_log("You drink a health potion and gain %d health", gui_log("You collect %u sips of health", (unsigned int) item->value);
player->stats.hp - original_hp);
} }
static Item * static Item *
@ -87,14 +83,16 @@ pickup_gold(Item *item, Player *player)
static Item * static Item *
create_treasure(int current_level) create_treasure(int current_level)
{ {
double amt = (unsigned int) rand() % 40; double amt;
char label[50]; char label[50];
unsigned int highest_treasure; unsigned int highest_treasure;
unsigned int value; unsigned int value;
if (current_level > 15) { amt = (unsigned int) (rand() + (5*current_level)) % 40;
if (current_level > 9) {
highest_treasure = TREASURE_COUNT; highest_treasure = TREASURE_COUNT;
} else if (current_level > 5) { } else if (current_level > 3) {
highest_treasure = PLATINUM; highest_treasure = PLATINUM;
} else { } else {
highest_treasure = GOLD; highest_treasure = GOLD;

View File

@ -155,38 +155,73 @@ move_down(Player *player, RoomMatrix *matrix)
} }
static void static void
handle_player_input(Player *player, RoomMatrix *matrix, SDL_Event *event) sip_health(Player *player)
{
if (player->potion_sips > 0) {
--player->potion_sips;
++player->stats.hp;
gui_log("You take a sip of health potion");
} else {
gui_log("You have nothing to sip");
}
}
static void
handle_movement_input(Player *player, RoomMatrix *matrix, Uint32 key)
{ {
static unsigned int step = 1; static unsigned int step = 1;
bool moved = false;
if (event->type == SDL_KEYDOWN) { switch (key) {
switch (event->key.keysym.sym) { case SDLK_LEFT:
case SDLK_LEFT: case SDLK_h:
case SDLK_h: case SDLK_a:
case SDLK_a: move_left(player, matrix);
move_left(player, matrix); moved = true;
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
case SDLK_l: case SDLK_l:
case SDLK_d: case SDLK_d:
move_right(player, matrix); move_right(player, matrix);
break; moved = true;
case SDLK_UP: break;
case SDLK_k: case SDLK_UP:
case SDLK_w: case SDLK_k:
move_up(player, matrix); case SDLK_w:
break; move_up(player, matrix);
case SDLK_DOWN: moved = true;
case SDLK_j: break;
case SDLK_s: case SDLK_DOWN:
move_down(player, matrix); case SDLK_j:
break; case SDLK_s:
} move_down(player, matrix);
moved = true;
break;
default:
break;
}
if (moved) {
player->sprite->clip.x = 16*step; player->sprite->clip.x = 16*step;
if (step == 3) ++step;
step = 0; step = step % 4;
else }
++step; }
static void
handle_player_input(Player *player, RoomMatrix *matrix, SDL_Event *event)
{
if (event->type != SDL_KEYDOWN)
return;
bool shift = event->key.keysym.mod & (KMOD_RSHIFT | KMOD_LSHIFT);
Uint32 key = event->key.keysym.sym;
if (!shift)
handle_movement_input(player, matrix, key);
if (shift && key == SDLK_h) {
sip_health(player);
} }
} }
@ -220,6 +255,7 @@ player_create(class_t class, SDL_Renderer *renderer)
player->kills = 0; player->kills = 0;
player->misses = 0; player->misses = 0;
player->gold = 0; player->gold = 0;
player->potion_sips = 0;
char asset[100]; char asset[100];
switch (class) { switch (class) {

View File

@ -10,7 +10,6 @@
enum PlayerClass { ENGINEER, MAGE, PALADIN, ROGUE, WARRIOR }; enum PlayerClass { ENGINEER, MAGE, PALADIN, ROGUE, WARRIOR };
typedef enum PlayerClass class_t; typedef enum PlayerClass class_t;
typedef struct ExperienceData_t { typedef struct ExperienceData_t {
unsigned int previousLevel; unsigned int previousLevel;
unsigned int current; unsigned int current;
@ -30,6 +29,7 @@ typedef struct Player_t {
unsigned int kills; unsigned int kills;
unsigned int misses; unsigned int misses;
double gold; double gold;
unsigned int potion_sips;
void (*handle_event)(struct Player_t*, RoomMatrix*, SDL_Event*); void (*handle_event)(struct Player_t*, RoomMatrix*, SDL_Event*);
} Player; } Player;

View File

@ -25,6 +25,7 @@ pointer_handle_event(Pointer *p, SDL_Event *event)
if (event->type == SDL_MOUSEMOTION) { if (event->type == SDL_MOUSEMOTION) {
p->sprite->pos.x = event->motion.x; p->sprite->pos.x = event->motion.x;
p->sprite->pos.y = event->motion.y; p->sprite->pos.y = event->motion.y;
debug("Pointer pos: %dx%d", p->sprite->pos.x, p->sprite->pos.y);
} }
} }

View File

@ -57,6 +57,7 @@ m_sprintf(char * dest, size_t destsz, const char * format, ...)
void void
debug(const char *fmt, ...) debug(const char *fmt, ...)
{ {
#ifdef DEBUG
va_list args; va_list args;
char tstamp[10]; char tstamp[10];
@ -66,6 +67,9 @@ debug(const char *fmt, ...)
vprintf(fmt, args); vprintf(fmt, args);
va_end(args); va_end(args);
printf("\n"); printf("\n");
#else // DEBUG
UNUSED (fmt);
#endif // DEBUG
} }
void void