Load hub missions.

This commit is contained in:
Steve 2018-01-28 16:14:17 +00:00
parent fc542ecd82
commit 6b9b3badc8
7 changed files with 67 additions and 35 deletions

View File

@ -199,6 +199,7 @@ enum
enum enum
{ {
MS_LOCKED = -1,
MS_INCOMPLETE, MS_INCOMPLETE,
MS_PARTIAL, MS_PARTIAL,
MS_MISSING_HEART_CELL, MS_MISSING_HEART_CELL,

View File

@ -26,6 +26,8 @@ void initGame(void)
{ {
memset(&game, 0, sizeof(Game)); memset(&game, 0, sizeof(Game));
game.missionStatusTail = &game.missionStatusHead;
game.cells = 5; game.cells = 5;
game.hearts = 10; game.hearts = 10;

View File

@ -76,7 +76,7 @@ void initHub(void)
{ {
mission->status = getMissionStatus(mission->id); mission->status = getMissionStatus(mission->id);
if (mission->unlockCount == 0 && mission->status == -1) if (mission->unlockCount == 0 && mission->status == MS_LOCKED)
{ {
mission->status = MS_INCOMPLETE; mission->status = MS_INCOMPLETE;
unlockMission(mission->id); unlockMission(mission->id);
@ -95,7 +95,7 @@ void initHub(void)
if (mission->status == MS_MISSING_HEART_CELL) if (mission->status == MS_MISSING_HEART_CELL)
{ {
STRNCPY(mission->description, "All objectives for this misson have been completed. However, there is a Cell or a Heart left to find. See if you can locate it.", MAX_DESCRIPTION_LENGTH); STRNCPY(mission->description, _("All objectives for this misson have been completed. However, there is a Cell or a Heart left to find. See if you can locate it."), MAX_DESCRIPTION_LENGTH);
} }
} }
@ -103,7 +103,7 @@ void initHub(void)
for (mission = hubMissionHead.next ; mission != NULL ; mission = mission->next) for (mission = hubMissionHead.next ; mission != NULL ; mission = mission->next)
{ {
if (mission->status == -1 || mission->status == MS_COMPLETE) if (mission->status == MS_LOCKED || mission->status == MS_COMPLETE)
{ {
if (mission == hubMissionTail) if (mission == hubMissionTail)
{ {
@ -131,7 +131,7 @@ static int getMissionStatus(char *id)
} }
} }
return -1; return MS_LOCKED;
} }
static void unlockMission(char *id) static void unlockMission(char *id)
@ -142,10 +142,13 @@ static void unlockMission(char *id)
{ {
if (strcmp(t->key, id) == 0) if (strcmp(t->key, id) == 0)
{ {
if (t->value.i == -1) if (t->value.i == MS_LOCKED)
{ {
t->value.i = MS_INCOMPLETE; t->value.i = MS_INCOMPLETE;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Unlocked mission %s", id);
} }
return; return;
} }
} }
@ -157,6 +160,8 @@ static void unlockMission(char *id)
STRNCPY(t->key, id, MAX_NAME_LENGTH); STRNCPY(t->key, id, MAX_NAME_LENGTH);
t->value.i = MS_INCOMPLETE; t->value.i = MS_INCOMPLETE;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Unlocked mission %s", id);
} }
static void unlockAllLevels(void) static void unlockAllLevels(void)
@ -165,7 +170,7 @@ static void unlockAllLevels(void)
for (mission = hubMissionHead.next ; mission != NULL ; mission = mission->next) for (mission = hubMissionHead.next ; mission != NULL ; mission = mission->next)
{ {
if (mission->status == -1 || mission->status == MS_INCOMPLETE) if (mission->status == MS_LOCKED || mission->status == MS_INCOMPLETE)
{ {
mission->status = MS_INCOMPLETE; mission->status = MS_INCOMPLETE;
unlockMission(mission->id); unlockMission(mission->id);
@ -175,6 +180,33 @@ static void unlockAllLevels(void)
static void loadMissions(void) static void loadMissions(void)
{ {
cJSON *root, *node;
char *text;
HubMission *mission;
text = readFile("data/hub/missions.json");
root = cJSON_Parse(text);
for (node = cJSON_GetObjectItem(root, "missions")->child ; node != NULL ; node = node->next)
{
mission = malloc(sizeof(HubMission));
memset(mission, 0, sizeof(HubMission));
hubMissionTail->next = mission;
hubMissionTail = mission;
STRNCPY(mission->id, cJSON_GetObjectItem(node, "id")->valuestring, MAX_NAME_LENGTH);
STRNCPY(mission->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH);
STRNCPY(mission->description, cJSON_GetObjectItem(node, "description")->valuestring, MAX_DESCRIPTION_LENGTH);
mission->x = cJSON_GetObjectItem(node, "x")->valueint;
mission->y = cJSON_GetObjectItem(node, "y")->valueint;
mission->status = MS_LOCKED;
mission->unlockCount = cJSON_GetObjectItem(node, "unlockCount")->valueint;
}
cJSON_Delete(root);
free(text);
} }
static void unlockNeighbouringMission(HubMission *sourceMission) static void unlockNeighbouringMission(HubMission *sourceMission)
@ -205,7 +237,7 @@ static void unlockNeighbouringMission(HubMission *sourceMission)
if (mission != NULL) if (mission != NULL)
{ {
if (mission->status == -1 || mission->status == MS_INCOMPLETE) if (mission->status == MS_LOCKED || mission->status == MS_INCOMPLETE)
{ {
mission->status = MS_INCOMPLETE; mission->status = MS_INCOMPLETE;
unlockMission(mission->id); unlockMission(mission->id);
@ -215,7 +247,7 @@ static void unlockNeighbouringMission(HubMission *sourceMission)
if (mission != NULL) if (mission != NULL)
{ {
if (mission->status == -1 || mission->status == MS_INCOMPLETE) if (mission->status == MS_LOCKED || mission->status == MS_INCOMPLETE)
{ {
mission->status = MS_INCOMPLETE; mission->status = MS_INCOMPLETE;
unlockMission(mission->id); unlockMission(mission->id);

View File

@ -19,8 +19,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ */
#include "../common.h" #include "../common.h"
#include "../json/cJSON.h"
extern int getDistance(int x1, int y1, int x2, int y2); extern int getDistance(int x1, int y1, int x2, int y2);
extern char *readFile(const char *filename);
extern char *getTranslatedString(char *string);
extern Dev dev; extern Dev dev;
extern Game game; extern Game game;

View File

@ -22,7 +22,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void loadAtlasTexture(void); static void loadAtlasTexture(void);
static void loadAtlasData(void); static void loadAtlasData(void);
static void loadImageData(cJSON *root);
static Atlas atlasHead; static Atlas atlasHead;
static Atlas *atlasTail; static Atlas *atlasTail;
@ -64,8 +63,10 @@ static void loadAtlasTexture(void)
static void loadAtlasData(void) static void loadAtlasData(void)
{ {
Atlas *atlas;
int x, y, w, h;
cJSON *root, *node; cJSON *root, *node;
char *text; char *text, *filename;
text = readFile("data/atlas/atlas.json"); text = readFile("data/atlas/atlas.json");
@ -73,34 +74,25 @@ static void loadAtlasData(void)
for (node = cJSON_GetObjectItem(root, "images")->child ; node != NULL ; node = node->next) for (node = cJSON_GetObjectItem(root, "images")->child ; node != NULL ; node = node->next)
{ {
loadImageData(node); filename = cJSON_GetObjectItem(node, "filename")->valuestring;
x = cJSON_GetObjectItem(node, "x")->valueint;
y = cJSON_GetObjectItem(node, "y")->valueint;
w = cJSON_GetObjectItem(node, "w")->valueint;
h = cJSON_GetObjectItem(node, "h")->valueint;
atlas = malloc(sizeof(Atlas));
memset(atlas, 0, sizeof(Atlas));
atlasTail->next = atlas;
atlasTail = atlas;
STRNCPY(atlas->filename, filename, MAX_FILENAME_LENGTH);
atlas->rect.x = x;
atlas->rect.y = y;
atlas->rect.w = w;
atlas->rect.h = h;
} }
cJSON_Delete(root); cJSON_Delete(root);
free(text); free(text);
} }
static void loadImageData(cJSON *root)
{
Atlas *atlas;
char *filename;
int x, y, w, h;
filename = cJSON_GetObjectItem(root, "filename")->valuestring;
x = cJSON_GetObjectItem(root, "x")->valueint;
y = cJSON_GetObjectItem(root, "y")->valueint;
w = cJSON_GetObjectItem(root, "w")->valueint;
h = cJSON_GetObjectItem(root, "h")->valueint;
atlas = malloc(sizeof(Atlas));
memset(atlas, 0, sizeof(Atlas));
atlasTail->next = atlas;
atlasTail = atlas;
STRNCPY(atlas->filename, filename, MAX_FILENAME_LENGTH);
atlas->rect.x = x;
atlas->rect.y = y;
atlas->rect.w = w;
atlas->rect.h = h;
}

View File

@ -29,6 +29,7 @@ static Texture *atlasTexture;
void initAtlasTest(void) void initAtlasTest(void)
{ {
initGame(); initGame();
initHub();
app.delegate.logic = &logic; app.delegate.logic = &logic;
app.delegate.draw = &draw; app.delegate.draw = &draw;

View File

@ -25,5 +25,6 @@ extern Texture *getTexture(const char *filename);
extern void blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int center); extern void blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int center);
extern void loadMapData(char *filename); extern void loadMapData(char *filename);
extern void initGame(void); extern void initGame(void);
extern void initHub(void);
extern App app; extern App app;