-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathOverworld.c
838 lines (677 loc) · 27 KB
/
Overworld.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
// Filename: Overworld.c
// Contains code for the functions that are specific to the overworld game state.
//
// Project Codename: GameB
// TODO: Come up with a better name later.
// 2020 Joseph Ryan Ries <[email protected]>
// My YouTube series where we program an entire video game from scratch in C.
// Watch it on YouTube: https://www.youtube.com/watch?v=3zFFrBSdBvA
// Follow along on GitHub: https://github.com/ryanries/GameB
// Find me on Twitter @JosephRyanRies
// # License
// ----------
// The source code in this project is licensed under the MIT license.
// The media assets such as artwork, custom fonts, music and sound effects are licensed under a separate license.
// A copy of that license can be found in the 'Assets' directory.
// stb_vorbis by Sean Barrett is public domain and a copy of its license can be found in the stb_vorbis.c file.
#include "CommonMain.h"
#include "Platform.h"
#include "Overworld.h"
GAMEAREA gCurrentArea = { 0 };
GAMEAREA gOverworldArea = { 0 };
GAMEAREA gHomeGameArea = { 0 };
PORTAL gPortal001 = { 0 };
PORTAL gPortal002 = { 0 };
PORTAL gPortals[2] = { 0 };
// We use this variable for when the player walks through a portal (or door or entrance) to another area.
// It resets the local counters in DrawOverworld so we have an opportunity to switch music tracks and reset
// the fade-in animation, even though we never actually changed game states.
bool gResetLocalCounters;
bool gShowInventory;
int gInventoryAlphaAdjust = -256;
unsigned int gSelectedInventoryItem = 0;
void DrawOverworld(void)
{
static uint64_t LocalFrameCounter;
static uint64_t LastFrameSeen = 0;
static int AlphaAdjust = -256;
// If global TotalFramesRendered is greater than LastFrameSeen,
// that means we have either just entered this gamestate for the first time,
// or we have left this gamestate previously and have just come back to it.
// For example we may have gone from the title screen, to the options screen,
// and then back to the title screen again. In that case, we want to reset all
// of the "local state," i.e., things that are local to this game state. Such
// as text animation, selected menu item, etc.
if (gResetLocalCounters || gPerformanceData.TotalFramesRendered > (LastFrameSeen + 1))
{
LocalFrameCounter = 0;
AlphaAdjust = -256;
gInventoryAlphaAdjust = -256;
gInputEnabled = false;
gResetLocalCounters = false;
gShowInventory = false;
}
#ifdef SMOOTH_FADES
// Here is a smoother fade in that looks nicer, but the original NES was not capable of such smooth gradients and fade
// effects. We will have to decide which we prefer later - looks better, or is more faithful to the original hardware?
if (AlphaAdjust < 0)
{
AlphaAdjust += 4;
}
#else
// Here is an easy, "chunky" fade-in from black in 4 steps, that sort of has a similar feel
// to the kind of fade-in you might have seen on the classic NES. AlphaAdjust starts at -256 and ends at 0.
switch (LocalFrameCounter)
{
case 15:
case 30:
case 45:
case 60:
{
AlphaAdjust += 64;
}
}
#endif
// It doesn't feel very nice to have to wait the full 60 frames for the fade-in to complete in order for
// input to be enabled again. We should enable it sooner so the kids with fast reflexes can work the menus quickly.
if (LocalFrameCounter == REENABLE_INPUT_AFTER_X_FRAMES_DELAY)
{
gInputEnabled = true;
}
if (LocalFrameCounter == 60)
{
if (MusicIsPlaying() == false)
{
PlayGameMusic(gCurrentArea.Music, true, true);
}
}
BlitBackground(&gOverworld01.GameBitmap, AlphaAdjust);
Blit32BppBitmapEx(
&gPlayer.Sprite[gPlayer.CurrentArmor][gPlayer.SpriteIndex + gPlayer.Direction],
gPlayer.ScreenPos.x,
gPlayer.ScreenPos.y,
0,
0,
0,
AlphaAdjust,
BLIT_FLAG_ALPHABLEND);
DrawPlayerStatsWindow(AlphaAdjust, 0, 0);
// Figure out if any NPCs should be drawn on the screen, and if so, draw them.
//DrawNPCs();
if (gShowInventory)
{
#ifdef SMOOTH_FADES
if (gInventoryAlphaAdjust < 0)
{
gInventoryAlphaAdjust += 4;
}
#else
if (gInventoryAlphaAdjust < 0 && (LocalFrameCounter % 15 == 0))
{
gInventoryAlphaAdjust += 64;
}
#endif
DrawWindow(
0,
32,
300,
190,
&(PIXEL32) { .Colors.Alpha = (uint8_t)(min(255, max((256 + gInventoryAlphaAdjust), 0))), .Colors.Red = 0xFC, .Colors.Green = 0xFC, .Colors.Blue = 0xFC },
&(PIXEL32) { .Colors.Alpha = (uint8_t)(min(255, max((256 + gInventoryAlphaAdjust), 0))), .Colors.Red = 0x00, .Colors.Green = 0x00, .Colors.Blue = 0x00 },
&(PIXEL32) { .Colors.Alpha = (uint8_t)(min(255, max((256 + gInventoryAlphaAdjust), 0))), .Colors.Red = 0x40, .Colors.Green = 0x40, .Colors.Blue = 0x40 },
WINDOW_FLAG_HORIZONTALLY_CENTERED | WINDOW_FLAG_OPAQUE | WINDOW_FLAG_BORDERED | WINDOW_FLAG_SHADOW | WINDOW_FLAG_ROUNDED_CORNERS | WINDOW_FLAG_THICK | WINDOW_FLAG_ALPHABLEND);
BlitStringEx(
"INVENTORY",
&g6x7Font,
(GAME_RES_WIDTH / 2) - (((int)strlen("INVENTORY") * 6) / 2),
36,
255,
255,
255,
gInventoryAlphaAdjust,
BLIT_FLAG_ALPHABLEND | BLIT_FLAG_TEXT_SHADOW);
for (unsigned int item = 0; item < _countof(gPlayer.Inventory); item++)
{
if (gPlayer.Inventory[item].Id == 0 && item > 2)
{
break;
}
if (gPlayer.Inventory[item].Id == 0)
{
switch (item)
{
case EQUIPPED_ARMOR:
{
BlitStringEx(
"(No Armor)",
&g6x7Font,
54,
54 + (item * 10),
96,
96,
96,
gInventoryAlphaAdjust,
BLIT_FLAG_ALPHABLEND | BLIT_FLAG_TEXT_SHADOW);
break;
}
case EQUIPPED_WEAPON:
{
BlitStringEx(
"(No Weapon)",
&g6x7Font,
54,
54 + (item * 10),
96,
96,
96,
gInventoryAlphaAdjust,
BLIT_FLAG_ALPHABLEND | BLIT_FLAG_TEXT_SHADOW);
break;
}
case EQUIPPED_SHIELD:
{
BlitStringEx(
"(No Shield)",
&g6x7Font,
54,
54 + (item * 10),
96,
96,
96,
gInventoryAlphaAdjust,
BLIT_FLAG_ALPHABLEND | BLIT_FLAG_TEXT_SHADOW);
break;
}
}
}
else
{
BlitStringEx(
gPlayer.Inventory[item].Name,
&g6x7Font,
54,
54 + (item * 10),
255,
255,
255,
gInventoryAlphaAdjust,
BLIT_FLAG_ALPHABLEND | BLIT_FLAG_TEXT_SHADOW);
}
if (item == gSelectedInventoryItem)
{
BlitStringEx(
"\xBB",
&g6x7Font,
48,
54 + (item * 10),
255,
255,
255,
gInventoryAlphaAdjust,
BLIT_FLAG_ALPHABLEND | BLIT_FLAG_TEXT_SHADOW);
DrawWindow(
52,
188,
300,
48,
&(PIXEL32) { .Colors.Alpha = (uint8_t)(min(255, max((256 + gInventoryAlphaAdjust), 0))), .Colors.Red = 0xFC, .Colors.Green = 0xFC, .Colors.Blue = 0xFC },
&(PIXEL32) { .Colors.Alpha = (uint8_t)(min(255, max((256 + gInventoryAlphaAdjust), 0))), .Colors.Red = 0x00, .Colors.Green = 0x00, .Colors.Blue = 0x00 },
&(PIXEL32) { .Colors.Alpha = (uint8_t)(min(255, max((256 + gInventoryAlphaAdjust), 0))), .Colors.Red = 0x40, .Colors.Green = 0x40, .Colors.Blue = 0x40 },
WINDOW_FLAG_OPAQUE | WINDOW_FLAG_BORDERED | WINDOW_FLAG_SHADOW | WINDOW_FLAG_ROUNDED_CORNERS | WINDOW_FLAG_THICK | WINDOW_FLAG_ALPHABLEND);
// Word wrap is needed here.
// Break the string up into shorter pieces?
// Can fit 48 characters per line here.
// First calculate how many lines are required to fit this string of text.
int LinesRequired = (int)strlen(gPlayer.Inventory[gSelectedInventoryItem].Description) / 48;
char LineOfText[49] = { 0 };
int CharIndex = 0;
for (int line = 0; line <= LinesRequired; line++)
{
int c = 0;
strncpy_s(LineOfText, sizeof(LineOfText), gPlayer.Inventory[gSelectedInventoryItem].Description + CharIndex, 48);
// don't attempt word-wrapping on the last line.
if (line < LinesRequired)
{
c = (int)strlen(LineOfText) - 1;
while (LineOfText[c] != ' ')
{
LineOfText[c] = '\0';
c--;
}
LineOfText[c] = '\0'; // Remove trailing space
CharIndex += (c + 1);
}
BlitStringEx(
LineOfText,
&g6x7Font,
56,
200 + (line * 10),
255,
255,
255,
gInventoryAlphaAdjust,
BLIT_FLAG_ALPHABLEND | BLIT_FLAG_TEXT_SHADOW);
}
}
}
}
LocalFrameCounter++;
LastFrameSeen = gPerformanceData.TotalFramesRendered;
}
// Process Player Input for the Overworld game state.
// This typically involves the player moving around on the screen.
// TODO: Make a gCurrentMap so that this works for any map we if choose to swap maps.
void PPI_Overworld(void)
{
if (gGameInput.EscapeKeyIsDown && !gGameInput.EscapeKeyWasDown)
{
ASSERT(gCurrentGameState == GAMESTATE_OVERWORLD, "Invalid game state!");
gPreviousGameState = gCurrentGameState;
gCurrentGameState = GAMESTATE_TITLESCREEN;
LogMessageA(LL_INFO, "[%s] Transitioning from game state %d to %d.", __FUNCTION__, gPreviousGameState, gCurrentGameState);
PauseMusic();
return;
}
ASSERT((gCamera.x <= gCurrentArea.Area.right - GAME_RES_WIDTH), "Camera is out of bounds!");
ASSERT((gCamera.y <= gCurrentArea.Area.bottom - GAME_RES_HEIGHT), "Camera is out of bounds!");
// If the player has no movement remaining, it means the player is standing still.
// Also, you cannot move while inventory is showing.
if (!gPlayer.MovementRemaining && !gShowInventory)
{
// If the player wants to move downward, we need to consult the tilemap to see
// if the destination tile can be stepped on - e.g., is it grass or is it water?
// We do this for all four directions - just check the adjacent tile to see if it's passable
// before we allow the player to move there. Careful about index out of bounds errors if the
// player is near the edge of the map.
if (gGameInput.DownKeyIsDown)
{
bool CanMoveToDesiredTile = false;
for (uint8_t Counter = 0; Counter < _countof(gPassableTiles); Counter++)
{
if ((gPlayer.WorldPos.y / 16) + 1 > (gOverworld01.TileMap.Height - 1))
{
break;
}
if (gOverworld01.TileMap.Map[(gPlayer.WorldPos.y / 16) + 1][gPlayer.WorldPos.x / 16] == gPassableTiles[Counter])
{
CanMoveToDesiredTile = true;
break;
}
}
if (CanMoveToDesiredTile)
{
if (gPlayer.ScreenPos.y < GAME_RES_HEIGHT - 16)
{
gPlayer.Direction = DOWN;
gPlayer.MovementRemaining = 16;
}
}
}
else if (gGameInput.LeftKeyIsDown)
{
bool CanMoveToDesiredTile = false;
for (uint8_t Counter = 0; Counter < _countof(gPassableTiles); Counter++)
{
if (gOverworld01.TileMap.Map[gPlayer.WorldPos.y / 16][(gPlayer.WorldPos.x / 16) - 1] == gPassableTiles[Counter])
{
CanMoveToDesiredTile = true;
break;
}
}
if (CanMoveToDesiredTile)
{
if (gPlayer.ScreenPos.x > 0)
{
gPlayer.Direction = LEFT;
gPlayer.MovementRemaining = 16;
}
}
}
else if (gGameInput.RightKeyIsDown)
{
bool CanMoveToDesiredTile = false;
for (uint8_t Counter = 0; Counter < _countof(gPassableTiles); Counter++)
{
if (gOverworld01.TileMap.Map[gPlayer.WorldPos.y / 16][(gPlayer.WorldPos.x / 16) + 1] == gPassableTiles[Counter])
{
CanMoveToDesiredTile = true;
break;
}
}
if (CanMoveToDesiredTile)
{
if (gPlayer.ScreenPos.x < GAME_RES_WIDTH - 16)
{
gPlayer.Direction = RIGHT;
gPlayer.MovementRemaining = 16;
}
}
}
else if (gGameInput.UpKeyIsDown)
{
bool CanMoveToDesiredTile = false;
if (gPlayer.ScreenPos.y > 0)
{
for (uint8_t Counter = 0; Counter < _countof(gPassableTiles); Counter++)
{
if (gOverworld01.TileMap.Map[(gPlayer.WorldPos.y / 16) - 1][gPlayer.WorldPos.x / 16] == gPassableTiles[Counter])
{
CanMoveToDesiredTile = true;
break;
}
}
}
if (CanMoveToDesiredTile)
{
if (gPlayer.ScreenPos.y > 0)
{
gPlayer.Direction = UP;
gPlayer.MovementRemaining = 16;
}
}
}
}
else if (!gShowInventory)
{
// gPlayer.MovementRemaining was greater than 0, which means the player is currently in motion.
// The player must move exactly 16 pixels (1 tile) to complete a full movement. You cannot
// cancel a movement in progress or change directions during the middle of a movement.
// If player is near the center of the screen, then adjust only player's screen position.
// If player is near the edge of the screen, then pan the camera instead of changing player's screen position.
// (Unless player is near the edge of the map, thus the camera cannot pan any further.)
gPlayer.MovementRemaining--;
if (gPlayer.Direction == DOWN)
{
if (gPlayer.ScreenPos.y < GAME_RES_HEIGHT - 64)
{
gPlayer.ScreenPos.y++;
}
else
{
if (gCamera.y < gCurrentArea.Area.bottom - GAME_RES_HEIGHT)
{
gCamera.y++;
}
else
{
gPlayer.ScreenPos.y++;
}
}
gPlayer.WorldPos.y++;
}
else if (gPlayer.Direction == LEFT)
{
if (gPlayer.ScreenPos.x > 64)
{
gPlayer.ScreenPos.x--;
}
else
{
if (gCamera.x > gCurrentArea.Area.left)
{
gCamera.x--;
}
else
{
gPlayer.ScreenPos.x--;
}
}
gPlayer.WorldPos.x--;
}
else if (gPlayer.Direction == RIGHT)
{
if (gPlayer.ScreenPos.x < GAME_RES_WIDTH - 64)
{
gPlayer.ScreenPos.x++;
}
else
{
if (gCamera.x < (gCurrentArea.Area.right - GAME_RES_WIDTH))
{
gCamera.x++;
}
else
{
gPlayer.ScreenPos.x++;
}
}
gPlayer.WorldPos.x++;
}
else if (gPlayer.Direction == UP)
{
if (gPlayer.ScreenPos.y > 64)
{
gPlayer.ScreenPos.y--;
}
else
{
if (gCamera.y > gCurrentArea.Area.top)
{
gCamera.y--;
}
else
{
gPlayer.ScreenPos.y--;
}
}
gPlayer.WorldPos.y--;
}
// During the course of the player's 16 pixel motion, we are changing player's sprite index
// 4 times. One foot forward, neutral, other foot forward, neutral. This achieves a walking
// animation effect as the player is moving. You want to end on neutral/standing still.
switch (gPlayer.MovementRemaining)
{
case 15:
{
gPlayer.HasPlayerMovedSincePortal = true;
gPlayer.SpriteIndex = 0;
break;
}
case 12:
{
gPlayer.SpriteIndex = 1;
break;
}
case 8:
{
gPlayer.SpriteIndex = 0;
break;
}
case 4:
{
gPlayer.SpriteIndex = 2;
break;
}
case 0:
{
// The player must always land on a position that is a multiple of 16.
ASSERT(gPlayer.ScreenPos.x % 16 == 0, "Player did not land on a position that is a multiple of 16!");
ASSERT(gPlayer.ScreenPos.y % 16 == 0, "Player did not land on a position that is a multiple of 16!");
ASSERT(gPlayer.WorldPos.x % 16 == 0, "Player did not land on a position that is a multiple of 16!");
ASSERT(gPlayer.WorldPos.y % 16 == 0, "Player did not land on a position that is a multiple of 16!");
gPlayer.SpriteIndex = 0;
// Is the player standing on a portal? (or door, or staircase, etc.?)
switch (gOverworld01.TileMap.Map[gPlayer.WorldPos.y / 16][gPlayer.WorldPos.x / 16])
{
case TILE_PORTAL_01:
case TILE_HOUSE_01:
{
if (gPlayer.HasPlayerMovedSincePortal == true)
{
PortalHandler();
}
break;
}
default:
{
// This is not a portal tile. Check whether we should encounter a random monster.
if ((gPlayer.StepsTaken - gPlayer.StepsSinceLastRandomMonsterEncounter) >= RANDOM_MONSTER_GRACE_PERIOD_STEPS)
{
uint32_t Random = rand();
Random = Random % 100;
if (Random >= gPlayer.RandomEncounterPercentage)
{
gPlayer.StepsSinceLastRandomMonsterEncounter = gPlayer.StepsTaken;
RandomMonsterEncounter();
}
}
break;
}
}
gPlayer.StepsTaken++;
break;
}
default:
{
}
}
}
// only toggle the inventory when the player is standing still
if ((gGameInput.InvKeyIsDown && !gGameInput.InvKeyWasDown) && (gPlayer.MovementRemaining == 0))
{
gShowInventory = !gShowInventory;
gInventoryAlphaAdjust = -256;
gSelectedInventoryItem = 0;
}
if (gShowInventory)
{
int CountOfItemsInInventory = InventoryItemCount();
if (CountOfItemsInInventory > 0)
{
bool NextItemLocated = false;
if (gGameInput.DownKeyIsDown && !gGameInput.DownKeyWasDown)
{
PlayGameSound(&gSoundMenuNavigate);
gSelectedInventoryItem++;
if (gSelectedInventoryItem >= _countof(gPlayer.Inventory))
{
gSelectedInventoryItem = 0;
}
// Search from current position, looking for the next item. There may be gaps in the inventory.
for (int item = gSelectedInventoryItem; item <= _countof(gPlayer.Inventory) - 1; item++)
{
if (gPlayer.Inventory[item].Id != 0)
{
gSelectedInventoryItem = item;
NextItemLocated = true;
break;
}
}
if (!NextItemLocated)
{
// We've returned to the beginning of the inventory, but there's no guarantee that slot 0 is not empty,
// so re-run the search for the next item.
gSelectedInventoryItem = 0;
for (int item = gSelectedInventoryItem; item <= _countof(gPlayer.Inventory) - 1; item++)
{
if (gPlayer.Inventory[item].Id != 0)
{
gSelectedInventoryItem = item;
NextItemLocated = true;
break;
}
}
if (!NextItemLocated)
{
ASSERT(false, "Bug! Inventory is empty, yet InventoryItemCount() gave us a non-zero value!")
}
}
}
else if (gGameInput.UpKeyIsDown && !gGameInput.UpKeyWasDown)
{
// Do everything we did for the Down key, but backwards.
PlayGameSound(&gSoundMenuNavigate);
if (gSelectedInventoryItem == 0)
{
gSelectedInventoryItem = _countof(gPlayer.Inventory) - 1;
}
else
{
gSelectedInventoryItem--;
}
// Search from current position, backwards, looking for the next item. There may be gaps in the inventory.
for (int item = gSelectedInventoryItem; item >= 0; item--)
{
if (gPlayer.Inventory[item].Id != 0)
{
gSelectedInventoryItem = item;
NextItemLocated = true;
break;
}
}
if (!NextItemLocated)
{
// We may have scanned all the way back to slot 0, but maybe slot 0 is empty, so just in case let's scan again from the end.
gSelectedInventoryItem = _countof(gPlayer.Inventory) - 1;
for (int item = gSelectedInventoryItem; item >= 0; item--)
{
if (gPlayer.Inventory[item].Id != 0)
{
gSelectedInventoryItem = item;
NextItemLocated = true;
break;
}
}
if (!NextItemLocated)
{
ASSERT(false, "Bug! Inventory is empty, yet InventoryItemCount() gave us a non-zero value!")
}
}
}
}
else
{
gSelectedInventoryItem = 0;
}
}
}
// If the player lands on one of the "portal" tiles, this procedure is called.
// A "portal" may be a doorway, or a staircase, or a cave entrance, or whatever.
// Loop through the array of all portals until we find which one the player is standing
// on. Then teleport the player to whatever location that portal dictates.
void PortalHandler(void)
{
gPlayer.HasPlayerMovedSincePortal = false;
bool PortalFound = false;
for (int Counter = 0; Counter < _countof(gPortals); Counter++)
{
if ((gPlayer.WorldPos.x == gPortals[Counter].WorldPos.x) &&
(gPlayer.WorldPos.y == gPortals[Counter].WorldPos.y))
{
PortalFound = true;
StopMusic();
gResetLocalCounters = true;
gPlayer.WorldPos.x = gPortals[Counter].WorldDestination.x;
gPlayer.WorldPos.y = gPortals[Counter].WorldDestination.y;
gPlayer.ScreenPos.x = gPortals[Counter].ScreenDestination.x;
gPlayer.ScreenPos.y = gPortals[Counter].ScreenDestination.y;
gCamera.x = gPortals[Counter].CameraPos.x;
gCamera.y = gPortals[Counter].CameraPos.y;
gCurrentArea = gPortals[Counter].DestinationArea;
break;
}
}
if (PortalFound == false)
{
ASSERT(false, "Player is standing on a portal but we do not have a portal handler for it!");
}
}
void RandomMonsterEncounter(void)
{
ASSERT(gCurrentGameState == GAMESTATE_OVERWORLD, "Invalid game state!");
gPreviousGameState = gCurrentGameState;
gCurrentGameState = GAMESTATE_BATTLE;
LogMessageA(LL_INFO, "[%s] Transitioning from game state %d to %d.", __FUNCTION__, gPreviousGameState, gCurrentGameState);
}
int InventoryItemCount(void)
{
int NumItems = 0;
for (int item = 0; item < _countof(gPlayer.Inventory) - 1; item++)
{
if (gPlayer.Inventory[item].Id != 0)
{
NumItems++;
}
}
return(NumItems);
}