Skip to content

Commit

Permalink
Define and load pickup menus #712
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Oct 6, 2024
1 parent 43115ab commit 1edbb0b
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 21 deletions.
36 changes: 27 additions & 9 deletions data/.wolf3d/N3Ddata.cdogscpn/pickups.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,33 @@
"Pic": "scroll"
},
"Effects": [{
"Type": "Score",
"Score": 1000
}, {
"Type": "Health",
"Health": 25
}, {
"Type": "Ammo",
"Ammo": "Feed",
"Amount": 10
"Type": "Menu",
"Menu": {
"Text": "What is 1+1?",
"Items": [{
"Text": "2",
"Effects": [{
"Type": "Score",
"Score": 1000
}, {
"Type": "Health",
"Health": 25
}, {
"Type": "Ammo",
"Ammo": "Feed",
"Amount": 10
}]
}, {
"Text": "11",
"Effects": [{
"Type": "Health",
"Health": 4
}, {
"Type": "Health",
"Health": 4
}]
}]
}
}],
"Sound": "pickup"
},
Expand Down
9 changes: 9 additions & 0 deletions src/cdogs/pickup.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ void PickupPickup(TActor *a, Pickup *p, const bool pickupAll)
}
break;

case PICKUP_SOUND:
canPickup = true;
sound = pe->u.Sound;
break;

case PICKUP_MENU:
// TODO: manual activate menu, set player menu
break;

default:
CASSERT(false, "unexpected pickup type");
break;
Expand Down
79 changes: 70 additions & 9 deletions src/cdogs/pickup_class.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2015-2016, 2018, 2020-2023 Cong Xu
Copyright (c) 2015-2016, 2018, 2020-2024 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -45,6 +45,8 @@ PickupType StrPickupType(const char *s)
S2T(PICKUP_GUN, "Gun");
S2T(PICKUP_SHOW_MAP, "ShowMap");
S2T(PICKUP_LIVES, "Lives");
S2T(PICKUP_SOUND, "Sound");
S2T(PICKUP_MENU, "Menu");
return PICKUP_NONE;
}
const char *PickupTypeStr(const PickupType pt)
Expand All @@ -58,6 +60,8 @@ const char *PickupTypeStr(const PickupType pt)
T2S(PICKUP_GUN, "Gun");
T2S(PICKUP_SHOW_MAP, "ShowMap");
T2S(PICKUP_LIVES, "Lives");
T2S(PICKUP_SOUND, "Sound");
T2S(PICKUP_MENU, "Menu");
default:
return "";
}
Expand Down Expand Up @@ -173,8 +177,37 @@ static void PickupClassInit(PickupClass *c)
memset(c, 0, sizeof *c);
CArrayInit(&c->Effects, sizeof(PickupEffect));
}
static void PickupEffectTerminate(PickupEffect *e);
static void PickupMenuItemTerminate(PickupMenuItem *m)
{
CFREE(m->Text);
CA_FOREACH(PickupEffect, e, m->Effects)
PickupEffectTerminate(e);
CA_FOREACH_END()
}
static void PickupEffectTerminate(PickupEffect *e)
{
switch (e->Type)
{
case PICKUP_SOUND:
CFREE(e->u.Sound);
break;
case PICKUP_MENU:
CFREE(e->u.Menu.Text);
CA_FOREACH(PickupMenuItem, m, e->u.Menu.Items)
PickupMenuItemTerminate(m);
CA_FOREACH_END()
break;
default:
// Do nothing
break;
}
}
static void PickupClassTerminate(PickupClass *c)
{
CA_FOREACH(PickupEffect, e, c->Effects)
PickupEffectTerminate(e);
CA_FOREACH_END()
CArrayTerminate(&c->Effects);
CFREE(c->Name);
CFREE(c->Sound);
Expand Down Expand Up @@ -234,24 +267,26 @@ void PickupClassesLoadJSON(CArray *classes, json_t *root)
CArrayPushBack(classes, &c);
}
}
static void LoadPickupEffect(PickupClass *c, json_t *node, const int version);
static PickupEffect LoadPickupEffect(json_t *node, const int version);
static void LoadPickupclass(PickupClass *c, json_t *node, const int version)
{
PickupClassInit(c);

if (version < 3)
{
LoadPickupEffect(c, node, version);
PickupEffect p = LoadPickupEffect(node, version);
CArrayPushBack(&c->Effects, &p);
}
else
{
json_t *effectsNode = json_find_first_label(node, "Effects")->child;
for (json_t *child = effectsNode->child; child; child = child->next)
{
LoadPickupEffect(c, child, version);
PickupEffect p = LoadPickupEffect(child, version);
CArrayPushBack(&c->Effects, &p);
}
}

LoadStr(&c->Sound, node, "Sound");
// Add default pickup sounds
if (c->Sound == NULL)
Expand Down Expand Up @@ -299,7 +334,7 @@ static void LoadPickupclass(PickupClass *c, json_t *node, const int version)
CPicLoadJSON(&c->Pic, picNode);
}
}
static void LoadPickupEffect(PickupClass *c, json_t *node, const int version)
static PickupEffect LoadPickupEffect(json_t *node, const int version)
{
UNUSED(version);
char *tmp;
Expand Down Expand Up @@ -333,7 +368,7 @@ static void LoadPickupEffect(PickupClass *c, json_t *node, const int version)
}
case PICKUP_KEYCARD:
CASSERT(false, "keys now loaded directly from graphics files");
return;
break;
case PICKUP_GUN:
CASSERT(false, "unimplemented");
break;
Expand All @@ -342,11 +377,37 @@ static void LoadPickupEffect(PickupClass *c, json_t *node, const int version)
case PICKUP_LIVES:
LoadInt(&p.u.Lives, node, "Lives");
break;
case PICKUP_SOUND:
LoadStr(&p.u.Sound, node, "Sound");
break;
case PICKUP_MENU: {
json_t *menuNode = json_find_first_label(node, "Menu")->child;
LoadStr(&p.u.Menu.Text, menuNode, "Text");
CArrayInit(&p.u.Menu.Items, sizeof(PickupMenuItem));
json_t *itemsNode = json_find_first_label(menuNode, "Items")->child;
for (json_t *item = itemsNode->child; item; item = item->next)
{
PickupMenuItem m;
memset(&m, 0, sizeof m);
CArrayInit(&m.Effects, sizeof(PickupEffect));
LoadStr(&m.Text, item, "Text");
json_t *effectsNode =
json_find_first_label(item, "Effects")->child;
for (json_t *child = effectsNode->child; child;
child = child->next)
{
PickupEffect mp = LoadPickupEffect(child, version);
CArrayPushBack(&m.Effects, &mp);
}
CArrayPushBack(&p.u.Menu.Items, &m);
}
break;
}
default:
CASSERT(false, "Unknown pickup type");
break;
}
CArrayPushBack(&c->Effects, &p);
return p;
}

// TODO: move ammo pickups to pickups file; remove "ammo_"
Expand Down
21 changes: 18 additions & 3 deletions src/cdogs/pickup_class.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2015-2016, 2018, 2020-2023 Cong Xu
Copyright (c) 2015-2016, 2018, 2020-2024 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -44,30 +44,45 @@ typedef enum
PICKUP_GUN,
PICKUP_SHOW_MAP,
PICKUP_LIVES,
PICKUP_SOUND,
PICKUP_MENU,
} PickupType;
PickupType StrPickupType(const char *s);
const char *PickupTypeStr(const PickupType pt);

typedef struct
{
char *Text;
CArray Effects; // of PickupEffect
} PickupMenuItem;

typedef struct
{
PickupType Type;
union {
int Score;
struct {
struct
{
int Amount;
bool ExceedMax;
} Heal;
NAmmo Ammo;
int Keys; // Refer to flags in mission.h
int GunId;
int Lives;
char *Sound;
struct
{
char *Text;
CArray Items; // of PickupMenuItem
} Menu;
} u;
} PickupEffect;

typedef struct
{
char *Name;
CArray Effects; // of PickupEffect
CArray Effects; // of PickupEffect
CPic Pic;
char *Sound;
} PickupClass;
Expand Down
4 changes: 4 additions & 0 deletions src/cdogs/sounds.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ void SoundInitialize(SoundDevice *device, const char *path)
}
void SoundLoadDir(map_t sounds, const char *path, const char *prefix)
{
if (sounds == NULL)
{
return;
}
tinydir_dir dir;
if (tinydir_open(&dir, path) == -1)
{
Expand Down

0 comments on commit 1edbb0b

Please sign in to comment.