tbftss/src/game/trophies.c

191 lines
4.2 KiB
C
Raw Normal View History

2016-03-03 19:03:07 +01:00
/*
Copyright (C) 2015-2016 Parallel Realities
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 2
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "trophies.h"
static void loadTrophyData(char *filename);
2016-03-09 12:51:26 +01:00
static void resetAlert(void);
static Trophy *alertTrophy;
static SDL_Texture *trophyIcons[TROPHY_MAX];
static SDL_Rect alertRect;
static float alertDX;
static int alertTimer;
2016-03-03 19:03:07 +01:00
void initTrophies(void)
{
loadTrophyData("data/trophies/trophies.json");
2016-03-09 12:51:26 +01:00
trophyIcons[TROPHY_BRONZE] = getTexture("gfx/trophies/bronze.png");
trophyIcons[TROPHY_SILVER] = getTexture("gfx/trophies/silver.png");
trophyIcons[TROPHY_GOLD] = getTexture("gfx/trophies/gold.png");
trophyIcons[TROPHY_PLATINUM] = getTexture("gfx/trophies/platinum.png");
resetAlert();
2016-03-03 19:03:07 +01:00
}
void awardTrophy(char *id)
{
Trophy *t;
for (t = game.trophyHead.next ; t != NULL ; t = t->next)
{
if (!t->awarded && strcmp(t->id, id) == 0)
{
t->awarded = 1;
t->awardDate = time(NULL);
t->notify = 1;
}
}
}
2016-03-09 12:51:26 +01:00
void doTrophies(void)
2016-03-03 19:03:07 +01:00
{
Trophy *t;
2016-03-09 12:51:26 +01:00
for (t = game.trophyHead.next ; t != NULL ; t = t->next)
2016-03-03 19:03:07 +01:00
{
2016-03-09 12:51:26 +01:00
if (t->notify)
2016-03-03 19:03:07 +01:00
{
2016-03-09 12:51:26 +01:00
if (alertTrophy != t)
{
alertTrophy = t;
playSound(SND_TROPHY);
}
alertRect.x += alertDX;
if (alertRect.x > -50)
{
alertDX *= 0.95;
}
if (--alertTimer <= 0)
2016-03-03 19:03:07 +01:00
{
2016-03-09 12:51:26 +01:00
t->notify = 0;
resetAlert();
2016-03-03 19:03:07 +01:00
}
2016-03-09 12:51:26 +01:00
return;
2016-03-03 19:03:07 +01:00
}
}
}
2016-03-09 12:51:26 +01:00
static void resetAlert(void)
{
alertRect.w = 300;
alertRect.h = 100;
alertRect.x = -alertRect.w;
alertRect.y = 10;
alertDX = 3;
alertTimer = FPS * 3;
}
void drawTrophyAlert(void)
{
if (alertTrophy)
{
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(app.renderer, &alertRect);
SDL_SetRenderDrawColor(app.renderer, 64, 64, 64, SDL_ALPHA_OPAQUE);
SDL_RenderDrawRect(app.renderer, &alertRect);
drawText(alertRect.x + 5, alertRect.y + 5, 30, TA_LEFT, colors.white, alertTrophy->title);
drawText(alertRect.x + 5, alertRect.y + 45, 20, TA_LEFT, colors.white, alertTrophy->description);
blit(trophyIcons[alertTrophy->value], alertRect.x + alertRect.w - 64, alertRect.y + 10, 0);
}
}
2016-03-03 19:03:07 +01:00
void drawTrophies(void)
{
}
Trophy *getTrophy(char *id)
{
Trophy *t;
for (t = game.trophyHead.next ; t != NULL ; t = t->next)
{
if (strcmp(t->id, id) == 0)
{
return t;
}
}
return NULL;
}
static void loadTrophyData(char *filename)
{
cJSON *root, *node;
char *text;
Trophy *t, *tail;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
text = readFile(filename);
2016-03-03 19:03:07 +01:00
root = cJSON_Parse(text);
tail = &game.trophyHead;
for (node = root->child ; node != NULL ; node = node->next)
{
t = malloc(sizeof(Trophy));
memset(t, 0, sizeof(Trophy));
STRNCPY(t->id, cJSON_GetObjectItem(node, "id")->valuestring, MAX_NAME_LENGTH);
2016-03-09 12:51:26 +01:00
STRNCPY(t->title, _(cJSON_GetObjectItem(node, "title")->valuestring), MAX_DESCRIPTION_LENGTH);
STRNCPY(t->description, _(cJSON_GetObjectItem(node, "description")->valuestring), MAX_DESCRIPTION_LENGTH);
2016-03-03 19:03:07 +01:00
t->value = lookup(cJSON_GetObjectItem(node, "value")->valuestring);
2016-03-06 18:13:34 +01:00
t->hidden = getJSONValue(node, "hidden", 0);
2016-03-03 19:03:07 +01:00
2016-03-09 13:31:33 +01:00
/* can't use the getJSONValue here, as it could lead to false positives */
if (cJSON_GetObjectItem(node, "stat"))
{
t->stat = lookup(cJSON_GetObjectItem(node, "stat")->valuestring));
t->statValue = cJSON_GetObjectItem(node, "statValue")->valueint;
}
2016-03-03 19:03:07 +01:00
tail->next = t;
tail = t;
}
cJSON_Delete(root);
free(text);
}
2016-03-09 13:31:33 +01:00
void checkStatTrophies(void)
{
Trophy *t;
for (t = game.trophyHead.next ; t != NULL ; t = t->next)
{
if (!t->awarded && game.stats[t->stat] >= t->statValue)
{
t->awarded = 1;
t->awardDate = time(NULL);
t->notify = 1;
}
}
}