| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include "pickup.h" |
| 29 |
|
| 30 |
#include "json_utils.h" |
| 31 |
#include "map.h" |
| 32 |
#include "net_util.h" |
| 33 |
|
| 34 |
CArray gPickups; |
| 35 |
static unsigned int sPickupUIDs; |
| 36 |
#define PICKUP_SIZE svec2i(8, 8) |
| 37 |
|
| 38 |
void PickupsInit(void) |
| 39 |
{ |
| 40 |
CArrayInit(&gPickups, sizeof(Pickup)); |
| 41 |
CArrayReserve(&gPickups, 128); |
| 42 |
sPickupUIDs = 0; |
| 43 |
} |
| 44 |
void PickupsTerminate(void) |
| 45 |
{ |
| 46 |
CA_FOREACH(const Pickup, p, gPickups) |
| 47 |
if (p->isInUse) |
| 48 |
{ |
| 49 |
PickupDestroy(p->UID); |
| 50 |
} |
| 51 |
CA_FOREACH_END() |
| 52 |
CArrayTerminate(&gPickups); |
| 53 |
} |
| 54 |
int PickupsGetNextUID(void) |
| 55 |
{ |
| 56 |
return sPickupUIDs++; |
| 57 |
} |
| 58 |
static void PickupDraw( |
| 59 |
GraphicsDevice *g, const int id, const struct vec2i pos); |
| 60 |
void PickupAdd(const NAddPickup ap) |
| 61 |
{ |
| 62 |
|
| 63 |
Pickup *p = PickupGetByUID(ap.UID); |
| 64 |
if (p != NULL && p->isInUse) |
| 65 |
{ |
| 66 |
PickupDestroy(ap.UID); |
| 67 |
} |
| 68 |
|
| 69 |
p = NULL; |
| 70 |
int i; |
| 71 |
for (i = 0; i < (int)gPickups.size; i++) |
| 72 |
{ |
| 73 |
Pickup *pu = CArrayGet(&gPickups, i); |
| 74 |
if (!pu->isInUse) |
| 75 |
{ |
| 76 |
p = pu; |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
if (p == NULL) |
| 81 |
{ |
| 82 |
Pickup pu; |
| 83 |
memset(&pu, 0, sizeof pu); |
| 84 |
CArrayPushBack(&gPickups, &pu); |
| 85 |
i = (int)gPickups.size - 1; |
| 86 |
p = CArrayGet(&gPickups, i); |
| 87 |
} |
| 88 |
memset(p, 0, sizeof *p); |
| 89 |
p->UID = ap.UID; |
| 90 |
p->class = StrPickupClass(ap.PickupClass); |
| 91 |
ThingInit(&p->thing, i, KIND_PICKUP, PICKUP_SIZE, ap.ThingFlags); |
| 92 |
p->thing.CPic = p->class->Pic; |
| 93 |
p->thing.CPicFunc = PickupDraw; |
| 94 |
MapTryMoveThing(&gMap, &p->thing, NetToVec2(ap.Pos)); |
| 95 |
p->IsRandomSpawned = ap.IsRandomSpawned; |
| 96 |
p->PickedUp = false; |
| 97 |
p->SpawnerUID = ap.SpawnerUID; |
| 98 |
p->isInUse = true; |
| 99 |
} |
| 100 |
void PickupAddGun(const WeaponClass *w, const struct vec2 pos) |
| 101 |
{ |
| 102 |
if (!w->CanDrop) |
| 103 |
{ |
| 104 |
return; |
| 105 |
} |
| 106 |
GameEvent e = GameEventNew(GAME_EVENT_ADD_PICKUP); |
| 107 |
sprintf(e.u.AddPickup.PickupClass, "gun_%s", w->name); |
| 108 |
e.u.AddPickup.Pos = Vec2ToNet(pos); |
| 109 |
GameEventsEnqueue(&gGameEvents, e); |
| 110 |
} |
| 111 |
void PickupDestroy(const int uid) |
| 112 |
{ |
| 113 |
Pickup *p = PickupGetByUID(uid); |
| 114 |
CASSERT(p->isInUse, "Destroying not-in-use pickup"); |
| 115 |
MapRemoveThing(&gMap, &p->thing); |
| 116 |
p->isInUse = false; |
| 117 |
} |
| 118 |
|
| 119 |
void PickupsUpdate(CArray *pickups, const int ticks) |
| 120 |
{ |
| 121 |
CA_FOREACH(Pickup, p, *pickups) |
| 122 |
if (!p->isInUse) |
| 123 |
{ |
| 124 |
continue; |
| 125 |
} |
| 126 |
ThingUpdate(&p->thing, ticks); |
| 127 |
CA_FOREACH_END() |
| 128 |
} |
| 129 |
|
| 130 |
static void PickupDraw(GraphicsDevice *g, const int id, const struct vec2i pos) |
| 131 |
{ |
| 132 |
const Pickup *p = CArrayGet(&gPickups, id); |
| 133 |
CASSERT(p->isInUse, "Cannot draw non-existent pickup"); |
| 134 |
CPicDrawContext c = CPicDrawContextNew(); |
| 135 |
const Pic *pic = CPicGetPic(&p->thing.CPic, c.Dir); |
| 136 |
if (pic != NULL) |
| 137 |
{ |
| 138 |
c.Offset = svec2i_scale_divide(CPicGetSize(&p->class->Pic), -2); |
| 139 |
} |
| 140 |
CPicDraw(g, &p->thing.CPic, pos, &c); |
| 141 |
} |
| 142 |
|
| 143 |
Pickup *PickupGetByUID(const int uid) |
| 144 |
{ |
| 145 |
CA_FOREACH(Pickup, p, gPickups) |
| 146 |
if (p->UID == uid) |
| 147 |
{ |
| 148 |
return p; |
| 149 |
} |
| 150 |
CA_FOREACH_END() |
| 151 |
return NULL; |
| 152 |
} |