Skip to content

Commit

Permalink
Persist player weapons and ammo between lives
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Jan 14, 2024
1 parent 8efdeed commit e996774
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/cdogs/actors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,9 @@ static void ActorDie(TActor *actor)
GameEvent e = GameEventNew(GAME_EVENT_ACTOR_DIE);
e.u.ActorDie.UID = actor->uid;
GameEventsEnqueue(&gGameEvents, e);

// Persist player ammo for when they respawn
ActorPersistPlayerWeaponsAndAmmo(actor);
}
static bool IsUnarmedBot(const TActor *actor);
static void ActorAddAmmoPickup(const TActor *actor)
Expand Down Expand Up @@ -2338,3 +2341,18 @@ bool ActorIsLocalPlayer(const int uid)

return PlayerIsLocal(a->PlayerUID);
}

void ActorPersistPlayerWeaponsAndAmmo(const TActor *a)
{
// Update the player's weapons and ammo based on their actor
PlayerData *p = PlayerDataGetByUID(a->PlayerUID);
if (p == NULL)
{
return;
}
for (int i = 0; i < MAX_WEAPONS; i++)
{
p->guns[i] = a->guns[i].Gun;
}
CArrayCopy(&p->ammo, &a->ammo);
}
3 changes: 2 additions & 1 deletion src/cdogs/actors.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013-2023 Cong Xu
Copyright (c) 2013-2024 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -251,3 +251,4 @@ bool ActorIsLowHealth(const TActor *a);
bool ActorIsGrimacing(const TActor *a);

bool ActorIsLocalPlayer(const int uid);
void ActorPersistPlayerWeaponsAndAmmo(const TActor *a);
6 changes: 1 addition & 5 deletions src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,11 +616,7 @@ static void PersistPlayerWeaponsAndAmmo(PlayerData *p)
if (!IsPlayerAlive(p))
return;
const TActor *a = ActorGetByUID(p->ActorUID);
for (int i = 0; i < MAX_WEAPONS; i++)
{
p->guns[i] = a->guns[i].Gun;
}
CArrayCopy(&p->ammo, &a->ammo);
ActorPersistPlayerWeaponsAndAmmo(a);
}
static void CheckMissionCompletion(const struct MissionOptions *mo)
{
Expand Down

0 comments on commit e996774

Please sign in to comment.