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)
set(CMAKE_C_FLAGS_DEBUG "-DDEBUG")
# PROGRAMS:
add_executable(breakhack
src/main

View File

@ -9,7 +9,9 @@
#include "map.h"
#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_right = { 48, 160, 16, 16 };
@ -116,6 +118,23 @@ init_sprites(Gui *gui, SDL_Renderer *renderer)
(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*
@ -136,10 +155,11 @@ gui_create(SDL_Renderer *renderer)
gui->log_lines[i] = t;
}
gui->labels[CURRENT_XP_LABEL] = create_label_sprite((Position) { 16, 116 });
gui->labels[LEVEL_LABEL] = create_label_sprite((Position) { 16, 128 });
gui->labels[DUNGEON_LEVEL_LABEL] = create_label_sprite((Position) { 16, 156 });
gui->labels[GOLD_LABEL] = create_label_sprite((Position) { 16, 142 });
gui->labels[CURRENT_XP_LABEL] = create_label_sprite((Position) { 16, POS_Y_XPBAR + 18 });
gui->labels[LEVEL_LABEL] = create_label_sprite((Position) { 16, POS_Y_XPBAR + 18 + 14 });
gui->labels[DUNGEON_LEVEL_LABEL] = create_label_sprite((Position) { 16, POS_Y_XPBAR + 18 + (2*14) });
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();
@ -213,6 +233,7 @@ gui_update_player_stats(Gui *gui, Player *player, Map *map, SDL_Renderer *render
static unsigned int dungeon_level = 0;
static int max_health = -1;
static int current_health = -1;
static int current_potion_sips = -1;
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;
}
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) {
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);
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) {
Texture *t;
p.y = 16 + ((LOG_FONT_SIZE+1) * i);
p.y = 16 + ((LOG_FONT_SIZE+5) * i);
t = gui->log_lines[i];
texture_load_from_text(t, log_data.log[i], color, cam->renderer);
texture_render(t, &p, cam);

View File

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

View File

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

View File

@ -155,38 +155,73 @@ move_down(Player *player, RoomMatrix *matrix)
}
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;
bool moved = false;
if (event->type == SDL_KEYDOWN) {
switch (event->key.keysym.sym) {
case SDLK_LEFT:
case SDLK_h:
case SDLK_a:
move_left(player, matrix);
break;
case SDLK_RIGHT:
case SDLK_l:
case SDLK_d:
move_right(player, matrix);
break;
case SDLK_UP:
case SDLK_k:
case SDLK_w:
move_up(player, matrix);
break;
case SDLK_DOWN:
case SDLK_j:
case SDLK_s:
move_down(player, matrix);
break;
}
switch (key) {
case SDLK_LEFT:
case SDLK_h:
case SDLK_a:
move_left(player, matrix);
moved = true;
break;
case SDLK_RIGHT:
case SDLK_l:
case SDLK_d:
move_right(player, matrix);
moved = true;
break;
case SDLK_UP:
case SDLK_k:
case SDLK_w:
move_up(player, matrix);
moved = true;
break;
case SDLK_DOWN:
case SDLK_j:
case SDLK_s:
move_down(player, matrix);
moved = true;
break;
default:
break;
}
if (moved) {
player->sprite->clip.x = 16*step;
if (step == 3)
step = 0;
else
++step;
++step;
step = step % 4;
}
}
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->misses = 0;
player->gold = 0;
player->potion_sips = 0;
char asset[100];
switch (class) {

View File

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

View File

@ -25,6 +25,7 @@ pointer_handle_event(Pointer *p, SDL_Event *event)
if (event->type == SDL_MOUSEMOTION) {
p->sprite->pos.x = event->motion.x;
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
debug(const char *fmt, ...)
{
#ifdef DEBUG
va_list args;
char tstamp[10];
@ -66,6 +67,9 @@ debug(const char *fmt, ...)
vprintf(fmt, args);
va_end(args);
printf("\n");
#else // DEBUG
UNUSED (fmt);
#endif // DEBUG
}
void