Various trophy tweaks.

This commit is contained in:
Steve 2016-04-24 13:00:27 +01:00
parent f68e7f9b0d
commit 3ce51feb9c
8 changed files with 90 additions and 26 deletions

View File

@ -14,43 +14,43 @@
{ {
"id" : "CAMPAIGN_10", "id" : "CAMPAIGN_10",
"title" : "1st Lieutenant", "title" : "1st Lieutenant",
"description" : "Complete 10% of all missions", "description" : "Complete 10%% of all missions",
"value" : "TROPHY_BRONZE" "value" : "TROPHY_BRONZE"
}, },
{ {
"id" : "CAMPAIGN_20", "id" : "CAMPAIGN_20",
"title" : "Lieutenant Commander", "title" : "Lieutenant Commander",
"description" : "Complete 20% of all missions", "description" : "Complete 20%% of all missions",
"value" : "TROPHY_BRONZE" "value" : "TROPHY_BRONZE"
}, },
{ {
"id" : "CAMPAIGN_40", "id" : "CAMPAIGN_40",
"title" : "Commander", "title" : "Commander",
"description" : "Complete 40% of all missions", "description" : "Complete 40%% of all missions",
"value" : "TROPHY_BRONZE" "value" : "TROPHY_BRONZE"
}, },
{ {
"id" : "CAMPAIGN_50", "id" : "CAMPAIGN_50",
"title" : "Captain", "title" : "Captain",
"description" : "Complete 50% of all missions", "description" : "Complete 50%% of all missions",
"value" : "TROPHY_SILVER" "value" : "TROPHY_SILVER"
}, },
{ {
"id" : "CAMPAIGN_60", "id" : "CAMPAIGN_60",
"title" : "Commodore", "title" : "Commodore",
"description" : "Complete 60% of all missions", "description" : "Complete 60%% of all missions",
"value" : "TROPHY_SILVER" "value" : "TROPHY_SILVER"
}, },
{ {
"id" : "CAMPAIGN_80", "id" : "CAMPAIGN_80",
"title" : "Rear Admiral", "title" : "Rear Admiral",
"description" : "Complete 80% of all missions", "description" : "Complete 80%% of all missions",
"value" : "TROPHY_SILVER" "value" : "TROPHY_SILVER"
}, },
{ {
"id" : "CAMPAIGN_90", "id" : "CAMPAIGN_90",
"title" : "Admiral", "title" : "Admiral",
"description" : "Complete 90% of all missions", "description" : "Complete 90%% of all missions",
"value" : "TROPHY_GOLD" "value" : "TROPHY_GOLD"
}, },
{ {
@ -147,19 +147,19 @@
{ {
"id" : "CHALLENGE_25", "id" : "CHALLENGE_25",
"title" : "", "title" : "",
"description" : "Complete 25% of all challenges", "description" : "Complete 25%% of all challenges",
"value" : "TROPHY_BRONZE" "value" : "TROPHY_BRONZE"
}, },
{ {
"id" : "CHALLENGE_50", "id" : "CHALLENGE_50",
"title" : "", "title" : "",
"description" : "Complete 50% of all challenges", "description" : "Complete 50%% of all challenges",
"value" : "TROPHY_SILVER" "value" : "TROPHY_SILVER"
}, },
{ {
"id" : "CHALLENGE_100", "id" : "CHALLENGE_100",
"title" : "", "title" : "",
"description" : "Complete 100% of all challenges", "description" : "Complete 100%% of all challenges",
"value" : "TROPHY_GOLD" "value" : "TROPHY_GOLD"
}, },
{ {

View File

@ -174,9 +174,9 @@ static void logic(void)
arrowPulse += 0.1; arrowPulse += 0.1;
doTrophies();
doWidgets(); doWidgets();
doTrophies();
} }
static void doStarSystems(void) static void doStarSystems(void)

View File

@ -73,12 +73,14 @@ void doTrophies(void)
alertRect.x += alertDX; alertRect.x += alertDX;
if (alertRect.x > -50) if (alertRect.x > -150)
{ {
alertDX *= 0.9; alertDX *= 0.9;
alertTimer--;
} }
if (--alertTimer <= 0) if (alertTimer <= 0)
{ {
t->notify = 0; t->notify = 0;
resetAlert(); resetAlert();
@ -91,12 +93,12 @@ void doTrophies(void)
static void resetAlert(void) static void resetAlert(void)
{ {
alertRect.w = 300; alertRect.w = 500;
alertRect.h = 100; alertRect.h = 90;
alertRect.x = -alertRect.w; alertRect.x = -alertRect.w;
alertRect.y = 10; alertRect.y = 10;
alertDX = 3; alertDX = 12;
alertTimer = FPS * 3; alertTimer = FPS * 3;
} }
@ -110,10 +112,10 @@ void drawTrophyAlert(void)
SDL_SetRenderDrawColor(app.renderer, 64, 64, 64, SDL_ALPHA_OPAQUE); SDL_SetRenderDrawColor(app.renderer, 64, 64, 64, SDL_ALPHA_OPAQUE);
SDL_RenderDrawRect(app.renderer, &alertRect); SDL_RenderDrawRect(app.renderer, &alertRect);
drawText(alertRect.x + 5, alertRect.y + 5, 30, TA_LEFT, colors.white, alertTrophy->title); drawText(alertRect.x + 15, alertRect.y + 5, 30, TA_LEFT, colors.white, alertTrophy->title);
drawText(alertRect.x + 5, alertRect.y + 45, 20, TA_LEFT, colors.white, alertTrophy->description); drawText(alertRect.x + 15, alertRect.y + 45, 20, TA_LEFT, colors.white, alertTrophy->shortDescription);
blit(trophyIcons[alertTrophy->value], alertRect.x + alertRect.w - 64, alertRect.y + 10, 0); blitScaled(trophyIcons[alertTrophy->value], alertRect.x + alertRect.w - 64, alertRect.y + 16, 48, 48);
} }
} }
@ -138,7 +140,6 @@ Trophy *getTrophy(char *id)
static void loadTrophyData(char *filename) static void loadTrophyData(char *filename)
{ {
/*
cJSON *root, *node; cJSON *root, *node;
char *text; char *text;
Trophy *t, *tail; Trophy *t, *tail;
@ -158,10 +159,20 @@ static void loadTrophyData(char *filename)
STRNCPY(t->id, cJSON_GetObjectItem(node, "id")->valuestring, MAX_NAME_LENGTH); STRNCPY(t->id, cJSON_GetObjectItem(node, "id")->valuestring, MAX_NAME_LENGTH);
STRNCPY(t->title, _(cJSON_GetObjectItem(node, "title")->valuestring), MAX_DESCRIPTION_LENGTH); STRNCPY(t->title, _(cJSON_GetObjectItem(node, "title")->valuestring), MAX_DESCRIPTION_LENGTH);
STRNCPY(t->description, _(cJSON_GetObjectItem(node, "description")->valuestring), MAX_DESCRIPTION_LENGTH); STRNCPY(t->description, _(cJSON_GetObjectItem(node, "description")->valuestring), MAX_DESCRIPTION_LENGTH);
STRNCPY(t->shortDescription, _(cJSON_GetObjectItem(node, "description")->valuestring), SHORT_DESCRIPTION_LENGTH);
t->value = lookup(cJSON_GetObjectItem(node, "value")->valuestring); t->value = lookup(cJSON_GetObjectItem(node, "value")->valuestring);
t->hidden = getJSONValue(node, "hidden", 0); t->hidden = getJSONValue(node, "hidden", 0);
// can't use the getJSONValue here, as it could lead to false positives if (strlen(t->description) > SHORT_DESCRIPTION_LENGTH)
{
t->shortDescription[SHORT_DESCRIPTION_LENGTH - 1] = '.';
t->shortDescription[SHORT_DESCRIPTION_LENGTH - 2] = '.';
t->shortDescription[SHORT_DESCRIPTION_LENGTH - 3] = '.';
}
t->stat = -1;
/* can't use the getJSONValue here, as it could lead to false positives */
if (cJSON_GetObjectItem(node, "stat")) if (cJSON_GetObjectItem(node, "stat"))
{ {
t->stat = lookup(cJSON_GetObjectItem(node, "stat")->valuestring); t->stat = lookup(cJSON_GetObjectItem(node, "stat")->valuestring);
@ -174,7 +185,6 @@ static void loadTrophyData(char *filename)
cJSON_Delete(root); cJSON_Delete(root);
free(text); free(text);
*/
} }
void awardStatsTrophies(void) void awardStatsTrophies(void)
@ -183,7 +193,7 @@ void awardStatsTrophies(void)
for (t = game.trophyHead.next ; t != NULL ; t = t->next) for (t = game.trophyHead.next ; t != NULL ; t = t->next)
{ {
if (!t->awarded && game.stats[t->stat] >= t->statValue) if (t->stat != -1 && !t->awarded && game.stats[t->stat] >= t->statValue)
{ {
t->awarded = 1; t->awarded = 1;
t->awardDate = time(NULL); t->awardDate = time(NULL);
@ -217,6 +227,7 @@ void awardCampaignTrophies(void)
{ {
name[i] = toupper(starSystem->name[i]); name[i] = toupper(starSystem->name[i]);
} }
sprintf(trophyId, "CAMPAIGN_%s", name); sprintf(trophyId, "CAMPAIGN_%s", name);
awardTrophy(trophyId); awardTrophy(trophyId);
} }

View File

@ -18,6 +18,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ */
#define SHORT_DESCRIPTION_LENGTH 40
#include "../common.h" #include "../common.h"
#include "../json/cJSON.h" #include "../json/cJSON.h"
@ -28,7 +30,7 @@ extern char *readFile(char *filename);
extern int getJSONValue(cJSON *node, char *name, int defValue); extern int getJSONValue(cJSON *node, char *name, int defValue);
extern int getPercent(float current, float total); extern int getPercent(float current, float total);
extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...); extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...);
extern void blit(SDL_Texture *t, int x, int y, int center); extern void blitScaled(SDL_Texture *t, int x, int y, int w, int h);
extern char *getTranslatedString(char *string); extern char *getTranslatedString(char *string);
extern SDL_Texture *getTexture(char *filename); extern SDL_Texture *getTexture(char *filename);
extern void playSound(int id); extern void playSound(int id);

View File

@ -370,6 +370,7 @@ struct Trophy {
char id[MAX_NAME_LENGTH]; char id[MAX_NAME_LENGTH];
char title[MAX_DESCRIPTION_LENGTH]; char title[MAX_DESCRIPTION_LENGTH];
char description[MAX_DESCRIPTION_LENGTH]; char description[MAX_DESCRIPTION_LENGTH];
char shortDescription[MAX_DESCRIPTION_LENGTH];
int value; int value;
int hidden; int hidden;
int stat; int stat;

View File

@ -24,6 +24,7 @@ static void loadStats(cJSON *statsJSON);
static void loadStarSystems(cJSON *starSystemsJSON); static void loadStarSystems(cJSON *starSystemsJSON);
static void loadMissions(cJSON *missionsCJSON); static void loadMissions(cJSON *missionsCJSON);
static void loadChallenges(cJSON *challengesCJSON); static void loadChallenges(cJSON *challengesCJSON);
static void loadTrophies(cJSON *trophiesJSON);
void loadGame(void) void loadGame(void)
{ {
@ -42,6 +43,8 @@ void loadGame(void)
loadChallenges(cJSON_GetObjectItem(gameJSON, "challenges")); loadChallenges(cJSON_GetObjectItem(gameJSON, "challenges"));
loadStats(cJSON_GetObjectItem(gameJSON, "stats")); loadStats(cJSON_GetObjectItem(gameJSON, "stats"));
loadTrophies(cJSON_GetObjectItem(gameJSON, "trophies"));
cJSON_Delete(root); cJSON_Delete(root);
free(text); free(text);
@ -118,3 +121,23 @@ static void loadStats(cJSON *statsJSON)
} }
} }
} }
static void loadTrophies(cJSON *trophiesJSON)
{
Trophy *t;
cJSON *trophyJSON;
if (trophiesJSON)
{
for (trophyJSON = trophiesJSON->child ; trophyJSON != NULL ; trophyJSON = trophyJSON->next)
{
t = getTrophy(cJSON_GetObjectItem(trophyJSON, "id")->valuestring);
if (t)
{
t->awardDate = cJSON_GetObjectItem(trophyJSON, "awardDate")->valueint;
t->awarded = 1;
}
}
}
}

View File

@ -29,5 +29,6 @@ extern int lookup(char *lookup);
extern char *getSaveFilePath(char *filename); extern char *getSaveFilePath(char *filename);
extern char *getLookupName(char *prefix, long num); extern char *getLookupName(char *prefix, long num);
extern StarSystem *getStarSystem(char *name); extern StarSystem *getStarSystem(char *name);
extern Trophy *getTrophy(char *id);
extern Game game; extern Game game;

View File

@ -24,6 +24,7 @@ static void saveStarSystems(cJSON *gameJSON);
static void saveChallenges(cJSON *gameJSON); static void saveChallenges(cJSON *gameJSON);
static cJSON *getMissionsJSON(StarSystem *starSystem); static cJSON *getMissionsJSON(StarSystem *starSystem);
static void saveStats(cJSON *gameJSON); static void saveStats(cJSON *gameJSON);
static void saveTrophies(cJSON *gameJSON);
void saveGame(void) void saveGame(void)
{ {
@ -43,6 +44,8 @@ void saveGame(void)
saveChallenges(gameJSON); saveChallenges(gameJSON);
saveStats(gameJSON); saveStats(gameJSON);
saveTrophies(gameJSON);
out = cJSON_Print(root); out = cJSON_Print(root);
@ -146,3 +149,26 @@ static void saveStats(cJSON *gameJSON)
cJSON_AddItemToObject(gameJSON, "stats", stats); cJSON_AddItemToObject(gameJSON, "stats", stats);
} }
static void saveTrophies(cJSON *gameJSON)
{
Trophy *t;
cJSON *trophiesJSON, *trophyJSON;
trophiesJSON = cJSON_CreateArray();
for (t = game.trophyHead.next ; t != NULL ; t = t->next)
{
if (t->awarded)
{
trophyJSON = cJSON_CreateObject();
cJSON_AddStringToObject(trophyJSON, "id", t->id);
cJSON_AddNumberToObject(trophyJSON, "awardDate", t->awardDate);
cJSON_AddItemToArray(trophiesJSON, trophyJSON);
}
}
cJSON_AddItemToObject(gameJSON, "trophies", trophiesJSON);
}