2018-02-22 15:42:43 +01:00
|
|
|
/*
|
|
|
|
* BreakHack - A dungeone crawler RPG
|
|
|
|
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-02-24 00:10:49 +01:00
|
|
|
#include <stdlib.h>
|
2018-02-23 11:01:25 +01:00
|
|
|
#include "defines.h"
|
2018-02-22 15:42:43 +01:00
|
|
|
#include "skillbar.h"
|
|
|
|
#include "texture.h"
|
|
|
|
#include "util.h"
|
2018-02-23 11:01:25 +01:00
|
|
|
#include "sprite.h"
|
2018-02-23 13:08:05 +01:00
|
|
|
#include "keyboard.h"
|
2018-02-23 23:53:52 +01:00
|
|
|
#include "texturecache.h"
|
2018-03-23 22:03:34 +01:00
|
|
|
#include "particle_engine.h"
|
2018-02-22 15:42:43 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
load_texture(SkillBar *bar, const char *path, SDL_Renderer *renderer)
|
|
|
|
{
|
2018-02-23 11:01:25 +01:00
|
|
|
static SDL_Color c_yellow = { 255, 255, 0, 255 };
|
|
|
|
|
2018-02-23 23:53:52 +01:00
|
|
|
Texture *t = texturecache_add(path);
|
2018-02-23 11:01:25 +01:00
|
|
|
t->dim.width = 16;
|
|
|
|
t->dim.height = 16;
|
|
|
|
|
2018-05-04 18:14:44 +02:00
|
|
|
for (unsigned int i = 0; i < 10; ++i) {
|
2018-02-23 19:32:01 +01:00
|
|
|
char buffer[4];
|
2018-02-23 11:01:25 +01:00
|
|
|
Sprite *s = sprite_create();
|
|
|
|
s->pos = (Position) { i * 32 + 20, 20 };
|
2018-02-23 23:53:52 +01:00
|
|
|
s->dim = (Dimension) { 8, 8 };
|
2018-02-23 13:08:05 +01:00
|
|
|
s->fixed = true;
|
2018-02-23 11:01:25 +01:00
|
|
|
sprite_load_text_texture(s, "GUI/SDS_8x8.ttf", 0, 8);
|
2018-05-04 18:14:44 +02:00
|
|
|
m_sprintf(buffer, 4, "%u", i+1 < 10 ? i+1 : 0);
|
2018-02-23 11:01:25 +01:00
|
|
|
texture_load_from_text(s->textures[0], buffer, c_yellow, renderer);
|
|
|
|
linkedlist_append(&bar->sprites, s);
|
|
|
|
}
|
2018-02-22 15:42:43 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 17:05:13 +01:00
|
|
|
static void
|
|
|
|
load_countdown_sprites(SkillBar *bar)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < PLAYER_SKILL_COUNT; ++i) {
|
|
|
|
Sprite *s = sprite_create();
|
|
|
|
sprite_load_text_texture(s, "GUI/SDS_8x8.ttf", 0, 16);
|
|
|
|
s->fixed = true;
|
|
|
|
s->pos = (Position) { 8 + (32 * i), 8 };
|
|
|
|
s->dim = (Dimension) { 16, 16 };
|
|
|
|
bar->countdowns[i] = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 15:42:43 +01:00
|
|
|
SkillBar *
|
|
|
|
skillbar_create(SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
SkillBar *bar = ec_malloc(sizeof(SkillBar));
|
2018-02-23 11:01:25 +01:00
|
|
|
bar->sprites = linkedlist_create();
|
2018-02-23 13:08:05 +01:00
|
|
|
bar->activationTimer = timer_create();
|
2018-03-23 22:03:34 +01:00
|
|
|
bar->skillSparkleTimer = timer_create();
|
2018-02-23 13:08:05 +01:00
|
|
|
bar->lastActivation = 0;
|
2018-02-22 15:42:43 +01:00
|
|
|
load_texture(bar, "GUI/GUI0.png", renderer);
|
2018-03-02 17:05:13 +01:00
|
|
|
load_countdown_sprites(bar);
|
2018-02-22 15:42:43 +01:00
|
|
|
return bar;
|
|
|
|
}
|
|
|
|
|
2018-03-23 22:03:34 +01:00
|
|
|
void
|
|
|
|
skillbar_check_skill_activation(SkillBar *bar, Player *player)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < PLAYER_SKILL_COUNT; ++i) {
|
|
|
|
if (!player->skills[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (player->skills[i]->levelcap != player->stats.lvl)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
timer_start(bar->skillSparkleTimer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 11:01:25 +01:00
|
|
|
static void
|
2018-02-24 00:29:25 +01:00
|
|
|
render_frame(Camera *cam)
|
2018-02-23 11:01:25 +01:00
|
|
|
{
|
|
|
|
static SDL_Rect c_top_left = { 1*16, 10*16, 16, 16 };
|
|
|
|
static SDL_Rect c_top_right = { 3*16, 10*16, 16, 16 };
|
|
|
|
static SDL_Rect c_bottom_left = { 1*16, 12*16, 16, 16 };
|
|
|
|
static SDL_Rect c_bottom_right = { 3*16, 12*16, 16, 16 };
|
|
|
|
|
2018-02-23 23:53:52 +01:00
|
|
|
Texture *t = texturecache_get("GUI/GUI0.png");
|
|
|
|
SDL_Rect box = { 0, 0, 16, 16 };
|
2018-02-23 11:01:25 +01:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < MAP_ROOM_WIDTH; ++i) {
|
2018-02-23 23:53:52 +01:00
|
|
|
box.x = i*32;
|
|
|
|
box.y = 0;
|
|
|
|
texture_render_clip(t, &box, &c_top_left, cam);
|
|
|
|
box.y = 16;
|
|
|
|
texture_render_clip(t, &box, &c_bottom_left, cam);
|
|
|
|
|
|
|
|
box.x = i*32 + 16;
|
|
|
|
box.y = 0;
|
|
|
|
texture_render_clip(t, &box, &c_top_right, cam);
|
|
|
|
box.y = 16;
|
|
|
|
texture_render_clip(t, &box, &c_bottom_right, cam);
|
2018-02-23 11:01:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
render_sprites(SkillBar *bar, Camera *cam)
|
|
|
|
{
|
|
|
|
LinkedList *sprites = bar->sprites;
|
|
|
|
|
|
|
|
while (sprites) {
|
|
|
|
sprite_render(sprites->data, cam);
|
|
|
|
sprites = sprites->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:05 +01:00
|
|
|
static void
|
|
|
|
render_activation_indicator(SkillBar *bar, Camera *cam)
|
|
|
|
{
|
|
|
|
if (!timer_started(bar->activationTimer))
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned int ticks = timer_get_ticks(bar->activationTimer);
|
|
|
|
if (ticks > 500) {
|
|
|
|
timer_stop(bar->activationTimer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Rect square = { (bar->lastActivation - 1) * 32, 0, 32, 32 };
|
|
|
|
unsigned int opacity = (unsigned int) ticks/2;
|
|
|
|
SDL_SetRenderDrawColor(cam->renderer, 255, 255, 0, (Uint8)(255 - opacity));
|
|
|
|
SDL_RenderDrawRect(cam->renderer, &square);
|
|
|
|
}
|
|
|
|
|
2018-03-02 17:05:13 +01:00
|
|
|
static void
|
|
|
|
render_skill_countdown(SkillBar *bar, int index, unsigned int count, Camera *cam)
|
|
|
|
{
|
|
|
|
static SDL_Color color = { 255, 255, 255, 255 };
|
|
|
|
char buffer[5];
|
|
|
|
Sprite *s = bar->countdowns[index];
|
|
|
|
|
|
|
|
m_sprintf(buffer, 5, "%u", count);
|
|
|
|
texture_load_from_text(s->textures[0], buffer, color, cam->renderer);
|
|
|
|
sprite_render(s, cam);
|
|
|
|
}
|
|
|
|
|
2018-02-28 22:31:38 +01:00
|
|
|
static void
|
2018-03-01 06:04:12 +01:00
|
|
|
render_skills(Player *player, Camera *cam)
|
2018-02-28 22:31:38 +01:00
|
|
|
{
|
|
|
|
static SDL_Rect activeSkillBox = { 0, 0, 32, 32 };
|
|
|
|
|
2018-03-01 06:04:12 +01:00
|
|
|
for (int i = 0; i < PLAYER_SKILL_COUNT; ++i) {
|
2018-02-28 22:31:38 +01:00
|
|
|
if (!player->skills[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Skill *skill = player->skills[i];
|
2018-03-11 21:06:46 +01:00
|
|
|
if (skill->icon->dim.width >16)
|
|
|
|
skill->icon->pos = (Position) { i * 32, 0 };
|
|
|
|
else
|
|
|
|
skill->icon->pos = (Position) { 8 + i * 32, 8 };
|
2018-02-28 22:31:38 +01:00
|
|
|
sprite_render(skill->icon, cam);
|
|
|
|
|
|
|
|
if (player->skills[i]->active) {
|
|
|
|
activeSkillBox.x = i * 32;
|
|
|
|
SDL_SetRenderDrawColor(cam->renderer, 0, 0, 255, 100);
|
|
|
|
SDL_RenderFillRect(cam->renderer, &activeSkillBox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-03-02 17:05:13 +01:00
|
|
|
render_skill_unavailable(SkillBar *bar, Player *player, Camera *cam)
|
2018-02-28 22:31:38 +01:00
|
|
|
{
|
|
|
|
static SDL_Rect unavailableSkillBox = { 0, 0, 32, 32 };
|
|
|
|
|
2018-03-01 06:04:12 +01:00
|
|
|
for (int i = 0; i < PLAYER_SKILL_COUNT; ++i) {
|
2018-03-23 22:03:34 +01:00
|
|
|
bool unavailable = false;
|
|
|
|
SDL_Color color;
|
|
|
|
|
2018-02-28 22:31:38 +01:00
|
|
|
if (!player->skills[i])
|
|
|
|
continue;
|
|
|
|
|
2018-03-13 16:13:54 +01:00
|
|
|
Skill *skill = player->skills[i];
|
2018-03-23 22:03:34 +01:00
|
|
|
if (skill->levelcap > player->stats.lvl) {
|
|
|
|
unavailable = true;
|
|
|
|
color = (SDL_Color) { 0, 0, 0, 220 };
|
|
|
|
} else if (skill->resetCountdown
|
|
|
|
|| (skill->available && !skill->available(player)))
|
|
|
|
{
|
|
|
|
unavailable = true;
|
|
|
|
color = (SDL_Color) { 255, 0, 0, 70 };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unavailable) {
|
2018-02-28 22:31:38 +01:00
|
|
|
unavailableSkillBox.x = i * 32;
|
2018-03-23 22:03:34 +01:00
|
|
|
SDL_SetRenderDrawColor(cam->renderer, UNPACK_COLOR(color));
|
2018-02-28 22:31:38 +01:00
|
|
|
SDL_RenderFillRect(cam->renderer, &unavailableSkillBox);
|
2018-03-13 16:13:54 +01:00
|
|
|
if (skill->resetCountdown) {
|
2018-03-23 22:03:34 +01:00
|
|
|
render_skill_countdown(bar,
|
|
|
|
i,
|
|
|
|
skill->resetCountdown,
|
|
|
|
cam);
|
2018-03-13 16:13:54 +01:00
|
|
|
}
|
2018-02-28 22:31:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 22:03:34 +01:00
|
|
|
static void
|
|
|
|
render_skill_sparkles(SkillBar *bar, Player *player)
|
|
|
|
{
|
2018-03-24 12:46:23 +01:00
|
|
|
if (timer_get_ticks(bar->skillSparkleTimer) > 1500) {
|
2018-03-23 22:03:34 +01:00
|
|
|
timer_stop(bar->skillSparkleTimer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-24 12:46:23 +01:00
|
|
|
Position pos = { 0, GAME_VIEW_HEIGHT };
|
2018-03-23 22:03:34 +01:00
|
|
|
Dimension dim = { 32, 32 };
|
|
|
|
for (int i = 0; i < PLAYER_SKILL_COUNT; ++i) {
|
|
|
|
if (!player->skills[i])
|
|
|
|
continue;
|
|
|
|
else if (player->skills[i]->levelcap != player->stats.lvl)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pos.x += 32 * i;
|
|
|
|
particle_engine_sparkle(pos, dim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 15:42:43 +01:00
|
|
|
void
|
2018-02-28 22:31:38 +01:00
|
|
|
skillbar_render(SkillBar *bar, Player *player, Camera *cam)
|
2018-02-22 15:42:43 +01:00
|
|
|
{
|
2018-02-24 00:29:25 +01:00
|
|
|
render_frame(cam);
|
2018-03-01 06:04:12 +01:00
|
|
|
render_skills(player, cam);
|
2018-02-23 11:01:25 +01:00
|
|
|
render_sprites(bar, cam);
|
2018-03-02 17:05:13 +01:00
|
|
|
render_skill_unavailable(bar, player, cam);
|
2018-02-23 13:08:05 +01:00
|
|
|
render_activation_indicator(bar, cam);
|
2018-03-23 22:03:34 +01:00
|
|
|
if (timer_started(bar->skillSparkleTimer))
|
|
|
|
render_skill_sparkles(bar, player);
|
2018-02-23 13:08:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
skillbar_handle_event(SkillBar *bar, SDL_Event *event)
|
|
|
|
{
|
|
|
|
if (event->type != SDL_KEYDOWN)
|
|
|
|
return;
|
|
|
|
|
2018-02-23 15:43:54 +01:00
|
|
|
unsigned int key = 0;
|
2018-05-04 18:14:44 +02:00
|
|
|
for (SDL_Keycode keysym = SDLK_0; keysym <= SDLK_9; ++keysym) {
|
|
|
|
if (!keyboard_press(keysym, event))
|
|
|
|
continue;
|
|
|
|
key = (int)(keysym - SDLK_0);
|
|
|
|
if (key == 0) key = 10;
|
|
|
|
break;
|
|
|
|
}
|
2018-02-23 13:08:05 +01:00
|
|
|
|
2018-02-23 15:43:54 +01:00
|
|
|
if (key != 0) {
|
|
|
|
bar->lastActivation = key;
|
2018-02-23 13:08:05 +01:00
|
|
|
timer_start(bar->activationTimer);
|
2018-02-23 15:43:54 +01:00
|
|
|
}
|
2018-02-22 15:42:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
skillbar_destroy(SkillBar *bar)
|
|
|
|
{
|
2018-02-23 11:01:25 +01:00
|
|
|
while (bar->sprites)
|
|
|
|
sprite_destroy(linkedlist_pop(&bar->sprites));
|
2018-03-06 10:27:25 +01:00
|
|
|
for (unsigned int i = 0; i < PLAYER_SKILL_COUNT; ++i)
|
|
|
|
if (bar->countdowns[i])
|
|
|
|
sprite_destroy(bar->countdowns[i]);
|
|
|
|
timer_destroy(bar->activationTimer);
|
2018-05-09 00:21:38 +02:00
|
|
|
timer_destroy(bar->skillSparkleTimer);
|
2018-02-22 15:42:43 +01:00
|
|
|
free(bar);
|
|
|
|
}
|