| 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 |
|
| 29 |
#include "hud_num_popup.h" |
| 30 |
|
| 31 |
#include "actors.h" |
| 32 |
#include "font.h" |
| 33 |
#include "gamedata.h" |
| 34 |
#include "hud_defs.h" |
| 35 |
#include "player_hud.h" |
| 36 |
|
| 37 |
|
| 38 |
#define TIMER_MS 1000 |
| 39 |
#define TIMER_OBJECTIVE_MS 2000 |
| 40 |
|
| 41 |
void HUDNumPopupsInit( |
| 42 |
HUDNumPopups *popups, const struct MissionOptions *mission) |
| 43 |
{ |
| 44 |
memset(popups, 0, sizeof *popups); |
| 45 |
CArrayInitFillZero( |
| 46 |
&popups->objective, sizeof(HUDNumPopup), |
| 47 |
mission->missionData->Objectives.size); |
| 48 |
} |
| 49 |
|
| 50 |
void HUDNumPopupsTerminate(HUDNumPopups *popups) |
| 51 |
{ |
| 52 |
CArrayTerminate(&popups->objective); |
| 53 |
} |
| 54 |
|
| 55 |
static void MergePopups(HUDNumPopup *dst, const HUDNumPopup src); |
| 56 |
void HUDNumPopupsAdd( |
| 57 |
HUDNumPopups *popups, const HUDNumPopupType type, const int idxOrUID, |
| 58 |
const int amount) |
| 59 |
{ |
| 60 |
HUDNumPopup s; |
| 61 |
memset(&s, 0, sizeof s); |
| 62 |
|
| 63 |
|
| 64 |
int localPlayerIdx = -1; |
| 65 |
switch (type) |
| 66 |
{ |
| 67 |
case NUMBER_POPUP_SCORE: |
| 68 |
localPlayerIdx = FindLocalPlayerIndex(idxOrUID); |
| 69 |
if (localPlayerIdx < 0) |
| 70 |
{ |
| 71 |
|
| 72 |
return; |
| 73 |
} |
| 74 |
s.u.PlayerUID = idxOrUID; |
| 75 |
break; |
| 76 |
case NUMBER_POPUP_OBJECTIVE: |
| 77 |
s.u.ObjectiveIndex = idxOrUID; |
| 78 |
break; |
| 79 |
default: |
| 80 |
CASSERT(false, "unknown HUD popup type"); |
| 81 |
break; |
| 82 |
} |
| 83 |
|
| 84 |
s.Amount = amount; |
| 85 |
|
| 86 |
|
| 87 |
switch (type) |
| 88 |
{ |
| 89 |
case NUMBER_POPUP_SCORE: |
| 90 |
s.Timer = TIMER_MS; |
| 91 |
s.TimerMax = TIMER_MS; |
| 92 |
break; |
| 93 |
case NUMBER_POPUP_OBJECTIVE: |
| 94 |
s.Timer = TIMER_OBJECTIVE_MS; |
| 95 |
s.TimerMax = TIMER_OBJECTIVE_MS; |
| 96 |
break; |
| 97 |
default: |
| 98 |
CASSERT(false, "unknown HUD popup type"); |
| 99 |
break; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
switch (type) |
| 104 |
{ |
| 105 |
case NUMBER_POPUP_SCORE: |
| 106 |
MergePopups(&popups->score[localPlayerIdx], s); |
| 107 |
break; |
| 108 |
case NUMBER_POPUP_OBJECTIVE: |
| 109 |
MergePopups(CArrayGet(&popups->objective, s.u.ObjectiveIndex), s); |
| 110 |
break; |
| 111 |
default: |
| 112 |
CASSERT(false, "unknown HUD popup type"); |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
static void MergePopups(HUDNumPopup *dst, const HUDNumPopup src) |
| 117 |
{ |
| 118 |
|
| 119 |
if (dst->Timer <= 0) |
| 120 |
{ |
| 121 |
|
| 122 |
dst->Amount = src.Amount; |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
|
| 127 |
dst->Amount += src.Amount; |
| 128 |
} |
| 129 |
dst->Timer = src.Timer; |
| 130 |
dst->TimerMax = src.TimerMax; |
| 131 |
dst->u.PlayerUID = src.u.PlayerUID; |
| 132 |
} |
| 133 |
|
| 134 |
static void NumPopupUpdate(HUDNumPopup *p, const int ms); |
| 135 |
void HUDPopupsUpdate(HUDNumPopups *popups, const int ms) |
| 136 |
{ |
| 137 |
for (int i = 0; i < MAX_LOCAL_PLAYERS; i++) |
| 138 |
{ |
| 139 |
NumPopupUpdate(&popups->score[i], ms); |
| 140 |
} |
| 141 |
CA_FOREACH(HUDNumPopup, p, popups->objective) |
| 142 |
NumPopupUpdate(p, ms); |
| 143 |
CA_FOREACH_END() |
| 144 |
} |
| 145 |
static void NumPopupUpdate(HUDNumPopup *p, const int ms) |
| 146 |
{ |
| 147 |
if (p->Timer > 0) |
| 148 |
{ |
| 149 |
p->Timer -= ms; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
static void DrawNumUpdate(const HUDNumPopup *p, FontOpts opts); |
| 154 |
|
| 155 |
void HUDNumPopupsDrawPlayer( |
| 156 |
const HUDNumPopups *popups, const int idx, const int drawFlags, |
| 157 |
const Rect2i r) |
| 158 |
{ |
| 159 |
const HUDNumPopup *u = &popups->score[idx]; |
| 160 |
if (!IsScoreNeeded(gCampaign.Entry.Mode)) |
| 161 |
{ |
| 162 |
return; |
| 163 |
} |
| 164 |
if (u->Amount == 0) |
| 165 |
{ |
| 166 |
return; |
| 167 |
} |
| 168 |
const PlayerData *p = PlayerDataGetByUID(u->u.PlayerUID); |
| 169 |
if (!IsPlayerAlive(p)) |
| 170 |
return; |
| 171 |
const FontOpts opts = PlayerHUDGetScorePos(drawFlags, r); |
| 172 |
DrawNumUpdate(u, opts); |
| 173 |
} |
| 174 |
|
| 175 |
void HUDNumPopupsDrawObjective( |
| 176 |
const HUDNumPopups *popups, const int idx, const struct vec2i pos) |
| 177 |
{ |
| 178 |
const HUDNumPopup *p = CArrayGet(&popups->objective, idx); |
| 179 |
FontOpts opts = FontOptsNew(); |
| 180 |
opts.Area = gGraphicsDevice.cachedConfig.Res; |
| 181 |
opts.Pad = pos; |
| 182 |
DrawNumUpdate(p, opts); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
#define NUM_UPDATE_POP_UP_DURATION_MS 100 |
| 191 |
#define NUM_UPDATE_FALL_DOWN_DURATION_MS 100 |
| 192 |
#define NUM_UPDATE_POP_UP_HEIGHT 5 |
| 193 |
static void DrawNumUpdate(const HUDNumPopup *p, FontOpts opts) |
| 194 |
{ |
| 195 |
if (p->Timer <= 0 || p->Amount == 0) |
| 196 |
{ |
| 197 |
return; |
| 198 |
} |
| 199 |
color_t color = p->Amount > 0 ? colorGreen : colorRed; |
| 200 |
|
| 201 |
struct vec2i pos = svec2i_zero(); |
| 202 |
char s[50]; |
| 203 |
sprintf(s, "%s%d", p->Amount > 0 ? "+" : "", p->Amount); |
| 204 |
|
| 205 |
|
| 206 |
int timer = p->TimerMax - p->Timer; |
| 207 |
if (timer < NUM_UPDATE_POP_UP_DURATION_MS) |
| 208 |
{ |
| 209 |
|
| 210 |
|
| 211 |
int popupHeight = |
| 212 |
timer * NUM_UPDATE_POP_UP_HEIGHT / NUM_UPDATE_POP_UP_DURATION_MS; |
| 213 |
opts.Pad.y -= popupHeight; |
| 214 |
} |
| 215 |
else if ( |
| 216 |
timer < |
| 217 |
NUM_UPDATE_POP_UP_DURATION_MS + NUM_UPDATE_FALL_DOWN_DURATION_MS) |
| 218 |
{ |
| 219 |
|
| 220 |
|
| 221 |
timer -= NUM_UPDATE_POP_UP_DURATION_MS; |
| 222 |
timer = NUM_UPDATE_FALL_DOWN_DURATION_MS - timer; |
| 223 |
int popupHeight = timer * NUM_UPDATE_POP_UP_HEIGHT / |
| 224 |
NUM_UPDATE_FALL_DOWN_DURATION_MS; |
| 225 |
opts.Pad.y -= popupHeight; |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
|
| 230 |
color.a = (uint8_t)CLAMP(p->Timer * 255 * 2 / p->TimerMax, 0, 255); |
| 231 |
} |
| 232 |
|
| 233 |
opts.Mask = color; |
| 234 |
FontStrOpt(s, pos, opts); |
| 235 |
} |