Repository navigation
Expand file tree
/
Copy pathSquirrelOnTheRun.cpp
More file actions
1690 lines (1369 loc) · 56.9 KB
/
Copy pathSquirrelOnTheRun.cpp
File metadata and controls
1690 lines (1369 loc) · 56.9 KB
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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Created on in the week 19-23. August 2024
for the OLC Code JAM 2024: https://itch.io/jam/olc-codejam-2024 (Theme: RUN)
Last modified on 23. August 2024
Author: bondoki
Purpose: Rendering of the hexagon tiles in various layout to emphasize pseudo 2.5 view.
This program provides routine for selecting and creation of hex tiles
heavly based on the tutorial http://www.redblobgames.com/grids/hexagons/base
Inspired by the "Coding Quickie: Isometric Tiles" example of
Javidx9 ( https://www.youtube.com/watch?v=ukkbNKTgf5U ) gave the idea for this.
It's just for fun and educational purpose. Feel free to modify and use it :)
Note1: the hex tiles are indexed in the axial coordinate (q,r,s) system with
boundary condition: q+r+s=0 . Solely the third coordinate is given by s=-q-r
and the selected tile has the coordinate (q,r).
___
/ q \
/ \
| |
\s r/
\___/
Note2: The tiles have been original equally size hexagon and scaled in the next
by 50% in height direction.
Note3: For a more isometric 2.5 view you should first rotate your hexagon by 45�
and scale it by 50% in height direction.
Note4: This is way ugly ugly code and drafted together for the
OLC Code JAM 2024: https://itch.io/jam/olc-codejam-2024 (Theme: RUN)
License: This piece of code is licensed to Creative Commons Zero v1.0 Universal according to
https://github.com/Bondoki/SquirrelOnTheRun/blob/main/LICENSE
also attributing Javidx9 for the unique and easy-to-use olc::PixelGameEngine and the underlying OLC-3 license see
https://github.com/OneLoneCoder/olcPixelGameEngine/wiki/Licencing
For more information about Javidx9 and OneLoneCoder please see https://github.com/OneLoneCoder
The assets used in this project are released under public domain; see the ReadMe in assets-folder
*/
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <unordered_set>
#include <set>
#include <list>
#include <unordered_map>
#include <deque> // random pile of tiles
#include <algorithm> // std::shuffle
#include <random> // std::default_random_engine
#include <iterator>
#include <queue> //std::priority_queue
#include <functional> //std::greater
#include <chrono>
#include "Hex.h"
/*
Sample code from https://www.redblobgames.com/pathfinding/a-star/
Copyright 2014 Red Blob Games <redblobgames@gmail.com>
Feel free to use this code in your own projects, including commercial projects
License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
*/
template<typename T, typename priority_t>
struct PriorityQueue {
typedef std::pair<priority_t, T> PQElement;
std::priority_queue<PQElement, std::vector<PQElement>, std::greater<PQElement> > elements;
inline bool empty() const {
return elements.empty();
}
inline void put(T item, priority_t priority) {
elements.emplace(priority, item);
}
T get() {
T best_item = elements.top().second;
elements.pop();
return best_item;
}
};
/*
END Disclaimer
*/
class SquirrelOnTheRun : public olc::PixelGameEngine
{
public:
SquirrelOnTheRun()
{
sAppName = "SquirrelOnTheRun";
}
private:
// Total passed time
float fTotalElapsedTime;
// Number of tiles in world
olc::vi2d vWorldSize = { 2, 2 };
// Size of single tile graphic
olc::vi2d vTileSize = { 72, 46 };
// Size of single tile graphic and offset to align them
olc::vi2d vTileSizeOffset = { 0, 10 };
// Where to place tile (0,0) on screen (in screen pixel steps)
olc::vi2d vOriginScreen = { 72*3, 46*3 };
// Sprite that holds all imagery
olc::Renderable sprIsom;
// List to order for drawing
std::list<Hex> hexlist;
// HexTile to Value
using HexMap=std::unordered_map<Hex, int,decltype(hexhash), decltype(hexequal)>;
HexMap heights{HexMap(10,hexhash,hexequal)};
// Star-Sprite and shadow
//olc::Renderable sprStar;
// Squirrel-Sprite and shadow
olc::Renderable sprSquirrel;
// Acorn-Sprite
olc::Renderable sprAcorn;
// Shadow-Sprite
olc::Renderable sprShadow;
// EnemyBird-Sprite
olc::Renderable sprBird;
// Drey-Sprite
olc::Renderable sprDrey;
olc::Renderable sprDreyAnim;
// ExitCross-Sprite
olc::Renderable sprExitCross;
// Title screen
olc::Renderable sprScreenTitle;
// Help screen
olc::Renderable sprScreenHelp;
// Credits screen
olc::Renderable sprScreenCredits;
// Tag Saved
olc::Renderable sprTagSaved;
// Tag Loosing
olc::Renderable sprTagLoosing;
// Tag Saved
olc::Renderable sprTagRun;
// Position and Value of Acorn
HexMap heightsAcorn{HexMap(10,hexhash,hexequal)};
// Board-style name
std::string sBoardName;
// Animated elements sprite
//olc::Renderable sprWater;
//olc::Renderable sprAir;
olc::Renderable sprFire;
//olc::Renderable sprEarth;
enum class PILETILETYPE
{
SELECTION = 0,
SAND = 1,
MEADOW = 2,
WOOD = 3,
MOUNTAIN = 4,
WARP_IN = 5,
EXIT_DREY = 6,
WARP_IN_BIRD = 7,
BLOCK = 8,
GRID = 9,
// etc
};
enum class STATE
{
STATE_TITLE,
STATE_CREDIT,
STATE_HELP,
STATE_LEVEL_RESET,
STATE_LEVEL_INCEASE,
STATE_GAME_INIT,
STATE_GAME_PILE_DRAW_TILES,
STATE_GAME_PILE_SELECTION,
STATE_GAME_PLAYFIELD_SELECTION,
STATE_GAME_PLAYFIELD_ADDING,
STATE_GAME_BIRD_MOVE_RANDOM,
STATE_GAME_BIRD_MOVE_FOLLOWING,
STATE_GAME_BIRD_MOVE_HIDING,
STATE_GAME_SQUIRREL_CHECK_PATH,
STATE_GAME_SQUIRREL_CHECK_CATCHED_BY_BIRD,
STATE_GAME_SQUIRREL_CHECK_OPEN_DREY,
STATE_GAME_LOOSE_NOPATH,
STATE_GAME_LOOSE_NOMOVE,
STATE_GAME_LOOSE_NOTILES,
STATE_GAME_LOOSE_CATCHED_BY_BIRD,
STATE_GAME_WIN,
} nState, nNextState;
std::deque<PILETILETYPE> deqPileOfTiles;
// Position of Squirrel
Hex posSquirrel;
// Position of Exit
Hex posDrey;
// Position of Exit
std::vector<Hex> posBird;
// Selection of randomly drawn tiles
olc::vi2d vSelectedPileTile;
int vSelectedPileTileType = 0;
olc::vi2d vSelectedPlayfieldTile;
int vSelectedPlayfieldType = 0;
// Position of hovering tile in Playfield
Hex posHoveringTile;
// Hex Nullposition
Hex posNull = Hex(100,100,-200);
// number of collected acorn
int numAcornCollected = 0;
// level
uint level = 1;
bool isDreyOpen = false;
bool isSquirrelAtDrey = false;
bool isSquirrelCatchedByBird = false;
bool isSquirrelNotMovable = false;
std::minstd_rand0 generator; // minstd_rand0 is a standard linear_congruential_engine
public:
bool OnUserCreate() override
{
fTotalElapsedTime=0.0f;
/* initialize random seed: */
srand (time(NULL));
// Load sprites used in demonstration
sprIsom.Load("assets/hextiles5.png"); // ok
// sprStar.Load("assets/star.png");
sprSquirrel.Load("assets/HeroSquirrelTiles.png"); // ok
sprAcorn.Load("assets/Acorn.png"); // ok
sprShadow.Load("assets/Shadow.png"); // ok
sprBird.Load("assets/EnemyBird.png"); // ok
sprDrey.Load("assets/Drey_72x46.png"); // ok
sprDreyAnim.Load("assets/DreyEnd.png"); // ok
sprExitCross.Load("assets/ExitCross.png"); // ok
sprScreenTitle.Load("assets/ScreenTitle.png"); // ok
sprScreenHelp.Load("assets/ScreenHelp.png"); // ok
sprScreenCredits.Load("assets/ScreenCredits.png"); // ok
sprTagSaved.Load("assets/TagSquirrelSaved.png"); // ok
sprTagLoosing.Load("assets/TagLoosing.png"); // ok
sprTagRun.Load("assets/TagSquirrelRun.png");// ok
//sprWater.Load("assets/hex_water_anim.png");
//sprAir.Load("assets/hex_air_anim.png");
sprFire.Load("assets/hex_fire_anim.png"); // ok
//sprEarth.Load("assets/hex_earth_anim.png");
// we start with hexagonal board
sBoardName="";
// for the birds
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
generator.seed(seed); // minstd_rand0 is a standard linear_congruential_engine
nState = STATE::STATE_TITLE;// STATE::STATE_LEVEL_RESET;
return true;
}
// create the board layout and sort for drawing
void createBoard()
{
//clear the list and the map
hexlist.clear();
heights.clear();
int dist = 4;
{
//Hexagon - Playfield
sBoardName="Playfield";
dist = 5;
for (int q = -dist; q <= dist; q++)
{
int r1 = std::max(-dist, -q - dist);
int r2 = std::min(dist, -q + dist);
for (int r = r1; r <= r2; r++)
{
if((hex_distance(Hex(q, r, -q-r),Hex(0,0,0))) == 5 ) // rock boundary
{
hexlist.push_back(Hex(q, r, -q-r));
heights[Hex(q, r, -q-r)] = (int) PILETILETYPE::MOUNTAIN;//(hex_distance(Hex(q, r, -q-r),Hex(0,0,0))+4)%9+1;
}
}
}
hexlist.push_back(Hex(0,0,0));
heights[Hex(0,0,0)] = (int) PILETILETYPE::WARP_IN_BIRD;
// for (int r = -2; r <= 4; r++)
// {
// hexlist.push_back(Hex(-2,r,2-r));
// heights[Hex(-2,r,2-r)] = (int) PILETILETYPE::WOOD;
// }
// remove boundary tile and provide entry and exit point
{
heights.erase(Hex(-5,0,5));
hexlist.remove(Hex(-5,0,5));
heights.erase(Hex(5,0,-5));
hexlist.remove(Hex(5,0,-5));
hexlist.push_back(Hex(-5,0,5));
heights[Hex(-5,0,5)] = (int) PILETILETYPE::WARP_IN; //entry of squirrel
hexlist.push_back(Hex(5,0,-5));
heights[Hex(5,0,-5)] = (int) PILETILETYPE::EXIT_DREY; //exit
// and sort again
hexlist.sort(hexcomp_q);
hexlist.sort(hexcomp_r);
}
}
// center tile 0,0 to screen center
vOriginScreen.x= ScreenWidth()/2-vTileSize.x/2;
vOriginScreen.y= ScreenHeight()/2-vTileSize.y/2;
// sort the list to draw from top to bottom
hexlist.sort(hexcomp_q);
hexlist.sort(hexcomp_r);
}
bool OnUserUpdate(float fElapsedTime) override
{
fTotalElapsedTime += fElapsedTime;
Clear(olc::Pixel(155,180,0));
if (GetKey(olc::Key::R).bPressed)
nNextState = STATE::STATE_LEVEL_RESET;
// Get Mouse in world
olc::vi2d vMouse = { GetMouseX(), GetMouseY() };
// this piece of converts screen pixels to axial tile coordinates
double qfrac = 4.0 / 3.0 * ((1.0*vMouse.x-vOriginScreen.x - 0.5*(vTileSize.x-vTileSizeOffset.x)) / (vTileSize.x-vTileSizeOffset.x));
double rfrac = -2.0 / 3.0 * ((1.0*vMouse.x-vOriginScreen.x - 0.5*(vTileSize.x-vTileSizeOffset.x)) / (vTileSize.x-vTileSizeOffset.x)) + 2.0*1.7320508075688/3.0 * ((1.0*vMouse.y-vOriginScreen.y-0.5*(vTileSize.y-vTileSizeOffset.y)) / (vTileSize.y-vTileSizeOffset.y));
double sfrac = -qfrac-rfrac;
int qi = int(std::round(qfrac));
int ri = int(std::round(rfrac));
int si = int(std::round(sfrac));
double q_diff = std::fabs(qi - qfrac);
double r_diff = std::fabs(ri - rfrac);
double s_diff = std::fabs(si - sfrac);
if (q_diff > r_diff && q_diff > s_diff)
{
qi = -ri - si;
}
else
if (r_diff > s_diff)
{
ri = -qi - si;
}
else
{
si = -qi - ri;
}
olc::vi2d vSelected =
{
qi, ri
};
// Labmda function to convert "world" (offset-array) coordinate into screen space
auto ToScreen = [&](int x, int y)
{
return olc::vi2d{
0.5 * (vTileSize.x - vTileSizeOffset.x) * 3.0 / 2.0 * (x) + vOriginScreen.x,
0.5 * ((vTileSize.y - vTileSizeOffset.y) * 1.7320508075688 * ((y) + 0.5 * ((x) & 1))) + vOriginScreen.y};
};
// Labmda function to convert "world" (axial) coordinate into screen space
auto AxialToScreen = [&](int q, int r)
{
return olc::vi2d{
0.5 * (vTileSize.x - vTileSizeOffset.x) * 3.0 / 2.0 * (q) + vOriginScreen.x,
0.5 * ((vTileSize.y - vTileSizeOffset.y) * (1.7320508075688 / 2.0 * (q) + 1.7320508075688 * r)) + vOriginScreen.y};
};
// Gameplay
switch (nState)
{
case STATE::STATE_TITLE:
nNextState = STATE::STATE_TITLE;
if (GetMouse(0).bPressed)
{
// check mouse position to change to different screens
// Get Mouse in world
olc::vi2d vMouse = { GetMouseX(), GetMouseY() };
// Game screen
if(GetMouseX() >= 440 && GetMouseX() <= 775 && GetMouseY() >= 150 && GetMouseY() <= 245)
{
nNextState = STATE::STATE_LEVEL_RESET;
}
// Help screen
if(GetMouseX() >= 440 && GetMouseX() <= 775 && GetMouseY() >= 290 && GetMouseY() <= 385)
{
nNextState = STATE::STATE_HELP;
}
// Credit screen
if(GetMouseX() >= 440 && GetMouseX() <= 775 && GetMouseY() >= 430 && GetMouseY() <= 520)
{
nNextState = STATE::STATE_CREDIT;
}
// EXIT Cross
if(GetMouseX() >= 735 && GetMouseX() <= 775 && GetMouseY() >= 20 && GetMouseY() <= 60)
{
nNextState = STATE::STATE_TITLE;
return false; // end application
}
}
if(GetKey(olc::Key::ESCAPE).bPressed)
{
return false; // exit application
}
break;
case STATE::STATE_HELP:
nNextState = STATE::STATE_HELP;
if (GetMouse(0).bPressed)
{
// check mouse position to change to different screens
// Get Mouse in world
olc::vi2d vMouse = { GetMouseX(), GetMouseY() };
// EXIT Cross
if(GetMouseX() >= 735 && GetMouseX() <= 775 && GetMouseY() >= 20 && GetMouseY() <= 60)
{
nNextState = STATE::STATE_TITLE;
}
}
if(GetKey(olc::Key::ESCAPE).bPressed)
{
nNextState = STATE::STATE_TITLE;
}
break;
case STATE::STATE_CREDIT:
nNextState = STATE::STATE_CREDIT;
if (GetMouse(0).bPressed)
{
// check mouse position to change to different screens
// Get Mouse in world
olc::vi2d vMouse = { GetMouseX(), GetMouseY() };
// EXIT Cross
if(GetMouseX() >= 735 && GetMouseX() <= 775 && GetMouseY() >= 20 && GetMouseY() <= 60)
{
nNextState = STATE::STATE_TITLE;
}
}
if(GetKey(olc::Key::ESCAPE).bPressed)
{
nNextState = STATE::STATE_TITLE;
}
break;
case STATE::STATE_LEVEL_RESET:
level = 1;
numAcornCollected = 0;
isDreyOpen = false;
isSquirrelAtDrey = false;
isSquirrelCatchedByBird = false;
isSquirrelNotMovable = false;
nNextState = STATE::STATE_GAME_INIT;
break;
case STATE::STATE_LEVEL_INCEASE:
level++;
numAcornCollected = 0;
isDreyOpen = false;
isSquirrelAtDrey = false;
isSquirrelCatchedByBird = false;
isSquirrelNotMovable = false;
nNextState = STATE::STATE_GAME_INIT;
break;
case STATE::STATE_GAME_INIT:
// increase level counter
// create the pile of tiles - in total 3*12
// and shuffle
deqPileOfTiles.clear();
for (int i = 0; i < 12; i++) // there can only be 61 tiles placed on the field
{
deqPileOfTiles.push_back(PILETILETYPE::SAND);
}
for (int i = 0; i < 16; i++) // there can only be 61 tiles placed on the field
{
deqPileOfTiles.push_back(PILETILETYPE::MEADOW);
}
for (int i = 0; i < 20; i++) // there can only be 61 tiles placed on the field
{
deqPileOfTiles.push_back(PILETILETYPE::WOOD);
}
std::shuffle(deqPileOfTiles.begin(), deqPileOfTiles.end(), std::default_random_engine(std::chrono::system_clock::now().time_since_epoch().count()));
/*
std::cout << "shuffled elements:";
for (PILETILETYPE &x : deqPileOfTiles)
std::cout << ' ' << (int)x;
std::cout << '\n';
*/
createBoard();
//createDrawTilesFromPile();
posSquirrel.setAxial(-5, 0, 5);
posDrey.setAxial(5, 0, -5);
posBird.clear();
for(int i = 0; i < level; i++)
posBird.push_back(Hex(0,0,0));
//posBird.setAxial(0,0,0);
posHoveringTile = posNull;
vSelectedPileTileType = 0;
// set acorn
heightsAcorn.clear();
heightsAcorn[Hex(-4,1,3)] = 1;
heightsAcorn[Hex(-4,3,1)] = 1;
heightsAcorn[Hex(4,-1,-3)] = 1;
heightsAcorn[Hex(4,-3,-1)] = 1;
heightsAcorn[Hex(-2,4,-2)] = 2;
heightsAcorn[Hex(-2,-2,4)] = 2;
heightsAcorn[Hex(2,-4,2)] = 2;
heightsAcorn[Hex(2,2,-4)] = 2;
heightsAcorn[Hex(0,4,-4)] = 3;
heightsAcorn[Hex(0,-4,4)] = 3;
// end-set acorn
// reset acorn
numAcornCollected = 0;
nNextState = STATE::STATE_GAME_PILE_DRAW_TILES;//STATE::STATE_GAME_PILE_SELECTION;
break;
case STATE::STATE_GAME_PILE_DRAW_TILES:
if (deqPileOfTiles.size() == 0)
{
// no tiles left
isSquirrelNotMovable = true;
nNextState = STATE::STATE_GAME_LOOSE_NOTILES;
break;
}
// check if Pile has some tiles left
if (deqPileOfTiles.size() > 0)
{
hexlist.push_back(Hex(-2, -6, 8));
heights[Hex(-2, -6, 8)] = (int)deqPileOfTiles.at(0);
}
if (deqPileOfTiles.size() > 1)
{
hexlist.push_back(Hex(0, -7, 7));
heights[Hex(0, -7, 7)] = (int)deqPileOfTiles.at(1);
}
if (deqPileOfTiles.size() > 2)
{
hexlist.push_back(Hex(2, -8, 6));
heights[Hex(2, -8, 6)] = (int)deqPileOfTiles.at(2);
}
/*std::cout << "shuffled elements:";
for (PILETILETYPE &x : deqPileOfTiles)
std::cout << ' ' << (int)x;
std::cout << '\n';
*/
nNextState = STATE::STATE_GAME_PILE_SELECTION;
break;
case STATE::STATE_GAME_PILE_SELECTION:
if(GetKey(olc::Key::ESCAPE).bPressed)
{
nNextState = STATE::STATE_TITLE;
break;
}
// Handle mouse left click to switch tile
if (GetMouse(0).bPressed)
{
// Exit-Cross -Sprite clicked?
{
// check mouse position to change to different screens
// Get Mouse in world
olc::vi2d vMouse = {GetMouseX(), GetMouseY()};
// EXIT Cross
if (GetMouseX() >= 735 && GetMouseX() <= 775 && GetMouseY() >= 20 && GetMouseY() <= 60)
{
nNextState = STATE::STATE_TITLE;
break;
}
}
// Guard array boundary
auto got = heights.find(Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y));
// does it exist?
if (got != heights.end())
{
// check if tile is most left selection
if (Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y) == Hex(-2, -6, 8))
{
int value = heights.at(Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y));
vSelectedPileTileType = value;
vSelectedPileTile = olc::vi2d(vSelected.x, vSelected.y);
//std::cout << (int)vSelectedPileTileType << std::endl;
//nNextState = STATE::STATE_GAME_PLAYFIELD_SELECTION;
}
// check if tile is middle selection
if (Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y) == Hex(0, -7, 7))
{
int value = heights.at(Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y));
vSelectedPileTileType = value;
vSelectedPileTile = olc::vi2d(vSelected.x, vSelected.y);
//std::cout << (int)vSelectedPileTileType << std::endl;
//nNextState = STATE::STATE_GAME_PLAYFIELD_SELECTION;
}
// check if tile is right selection
if (Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y) == Hex(2, -8, 6))
{
int value = heights.at(Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y));
vSelectedPileTileType = value;
vSelectedPileTile = olc::vi2d(vSelected.x, vSelected.y);
//std::cout << (int)vSelectedPileTileType << std::endl;
//nNextState = STATE::STATE_GAME_PLAYFIELD_SELECTION;
}
}
}
// break; no break as player may reselect choice
case STATE::STATE_GAME_PLAYFIELD_SELECTION:
if (vSelectedPileTileType != 0) // hexequal(Hex(vSelected.x,vSelected.y,-vSelected.x-vSelected.y), Hex(Hex(q,r,s))) )
{
// in playfield: max distance < 5
if (hex_distance(Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y), Hex(0, 0, 0)) < 5)
{
olc::vi2d vWorld = AxialToScreen(vSelected.x, vSelected.y);
int moveXSelected = 0.0;
int moveYSelected = 5 * std::sin(fTotalElapsedTime * 5);
// place tile only in vicinity of squirrel
if (hex_distance(posSquirrel, Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y)) == 1)
{
// check if position is empty or already added
if (heights.find(Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y)) == heights.end())
{
if (!hexequal(Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y), posHoveringTile))
{
hexlist.push_back(Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y));
heights[Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y)] = (int)vSelectedPileTileType;
heights.erase(posHoveringTile);
hexlist.remove(posHoveringTile);
posHoveringTile = Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y);
// and sort again
hexlist.sort(hexcomp_q);
hexlist.sort(hexcomp_r);
}
}
}
else
{
if (heights.find(posHoveringTile) != heights.end())
{
heights.erase(posHoveringTile);
hexlist.remove(posHoveringTile);
posHoveringTile = posNull;
// and sort again
hexlist.sort(hexcomp_q);
hexlist.sort(hexcomp_r);
}
}
}
else
{
// check if hovering tile is in default position and delete if necessary
if (!hexequal(posNull, posHoveringTile))
{
// hexlist.push_back(Hex(vSelected.x,vSelected.y,-vSelected.x-vSelected.y));
// heights[Hex(vSelected.x,vSelected.y,-vSelected.x-vSelected.y)] = (int) vSelectedPileTileType;
heights.erase(posHoveringTile);
hexlist.remove(posHoveringTile);
posHoveringTile = posNull;
// and sort again
hexlist.sort(hexcomp_q);
hexlist.sort(hexcomp_r);
}
}
}
// Handle mouse left click to add tile if possible
if (GetMouse(0).bPressed)
{
// Guard array boundary
auto got = heights.find(Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y));
// does it exist? As also rendered possible location aare also in the list, so add the tile to the playfield
if (got != heights.end())
{
// something can be added on the playfield
if (hexequal(Hex(vSelected.x, vSelected.y, -vSelected.x - vSelected.y), posHoveringTile))
{
posSquirrel = posHoveringTile;
posHoveringTile = posNull;
// reset the Pile and delete drawn Tiles
heights.erase(Hex(-2, -6, 8));
hexlist.remove(Hex(-2, -6, 8));
heights.erase(Hex(0, -7, 7));
hexlist.remove(Hex(0, -7, 7));
heights.erase(Hex(2, -8, 6));
hexlist.remove(Hex(2, -8, 6));
// remove left tile
if(vSelectedPileTile == olc::vi2d(-2,-6))
{
deqPileOfTiles.erase (deqPileOfTiles.begin()+0);
}
// remove middle
if(vSelectedPileTile == olc::vi2d(0,-7))
{
deqPileOfTiles.erase (deqPileOfTiles.begin()+1);
}
//remove right
if(vSelectedPileTile == olc::vi2d(2,-8))
{
deqPileOfTiles.erase (deqPileOfTiles.begin()+2);
}
// vSelectedPileTileType = 0;
// check if Hex-Field has an acorn
{
auto gotAcorn = heightsAcorn.find(posSquirrel);
if (gotAcorn != heightsAcorn.end())
{
// increase counter
numAcornCollected += gotAcorn->second;
// delete tile
heightsAcorn.erase(gotAcorn->first);
}
}
if (hexequal(posSquirrel, Hex(4, 0, -4)) && isDreyOpen == true)
{
// Squirrel reached the drey - or more precisely is next to the drey
//std::cout << " Squirrel at the drey! Well done! The rainbow Lizard is happy :)" << std::endl;
isSquirrelAtDrey = true;
vSelectedPileTileType = 0;
nNextState = STATE::STATE_GAME_WIN;
break;
}
// check if at least one tile next to the squirrel is empty
// otherwise lose the game
{
int dist = 1;
bool hasEmptyVicinity = false;
for (int q = -dist; q <= dist; q++)
{
int r1 = std::max(-dist, -q - dist);
int r2 = std::min(dist, -q + dist);
for (int r = r1; r <= r2; r++)
{
if ((hex_distance(hex_add(Hex(q, r, -q - r), posSquirrel), posSquirrel)) == 1) // rock boundary
{
// vicinity tile is not in the list - at least one tile can be added to escape the squirrel
if (heights.find(hex_add(Hex(q, r, -q - r), posSquirrel)) == heights.end())
hasEmptyVicinity = true;
}
}
}
if (hasEmptyVicinity == false)
{
// Exit with "loosing" state
//std::cout << "Ohhh, noooo! The Squirrel can not move anymore! Try again :)" << std::endl;
isSquirrelNotMovable = true;
nNextState = STATE::STATE_GAME_LOOSE_NOMOVE;
break;
}
}
//nNextState = STATE::STATE_GAME_PILE_DRAW_TILES;
if(vSelectedPileTileType == (int) PILETILETYPE::SAND)
{
//FOLLOW
//std::cout << "follow" << std::endl;
vSelectedPileTileType = 0;
nNextState = STATE::STATE_GAME_BIRD_MOVE_FOLLOWING;
}
if(vSelectedPileTileType == (int) PILETILETYPE::MEADOW)
{
//HIDE
//std::cout << "hide" << std::endl;
vSelectedPileTileType = 0;
nNextState = STATE::STATE_GAME_BIRD_MOVE_RANDOM;
}
if(vSelectedPileTileType == (int) PILETILETYPE::WOOD)
{
//BLOCK
//std::cout << "block" << std::endl;
vSelectedPileTileType = 0;
nNextState = STATE::STATE_GAME_BIRD_MOVE_RANDOM;
}
}
}
}
// loop will reenter in STATE::STATE_GAME_PILE_SELECTION if nothing happens
break;
case STATE::STATE_GAME_PLAYFIELD_ADDING:
//
break;
case STATE::STATE_GAME_BIRD_MOVE_RANDOM:
for(int i = 0; i < posBird.size(); i++)
{
// get a (q,r) pair with -1 <= q,r <= 1
//unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
//std::minstd_rand0 generator (seed); // minstd_rand0 is a standard linear_congruential_engine
// create new position within boundary <= 5
Hex tmpBird = posBird.at(i);// posBird;
// this is not working as Hex(1,1,-2) is no neighbour
// do
// {
// // generator() -> between 0, numeric_limits<UIntType>::max()
// int q = generator()%3-1;// gives value between -1,0,1
// int r = generator()%3-1;// gives value between -1,0,1
// std::cout << q << " , " << r << std::endl;
// tmpBird = hex_add(posBird, Hex(q,r,-q-r));
// } while (!(hex_distance(tmpBird, Hex(0, 0, 0)) < 5));
// Six neigbors + stay on position
std::vector<Hex> neighborDirectionHex = {Hex(+1, 0, -1), Hex(+1, -1, 0), Hex(0, -1, +1),
Hex(-1, 0, +1), Hex(-1, +1, 0), Hex(0, +1, -1), Hex(0, 0, 0)};
do
{
// generator() -> between 0, numeric_limits<UIntType>::max()
tmpBird = hex_add(posBird.at(i), neighborDirectionHex.at(generator()%7));
} while ((hex_distance(tmpBird, Hex(0, 0, 0)) >= 5) || (heights.find(tmpBird) != heights.end() ? heights.find(tmpBird)->second == (int) PILETILETYPE::WOOD : false));
//std::cout << "Bird old: ( " << posBird.get_q() << " , " << posBird.get_r() << " ) --> ";
posBird[i] = tmpBird;
//std::cout << "Bird new: ( " << posBird.get_q() << " , " << posBird.get_r() << " ) " << std::endl;
}
nNextState = STATE::STATE_GAME_SQUIRREL_CHECK_PATH;//STATE::STATE_GAME_PILE_DRAW_TILES;
break;
case STATE::STATE_GAME_BIRD_MOVE_HIDING:
nNextState = STATE::STATE_GAME_SQUIRREL_CHECK_PATH;//STATE::STATE_GAME_PILE_DRAW_TILES;
break;
case STATE::STATE_GAME_BIRD_MOVE_FOLLOWING:
for(int i = 0; i < posBird.size(); i++)
{
auto AStarSearch = [&](Hex TileStart, Hex TileEnd)
{
using HexHexUnorderedMap=std::unordered_map<Hex, Hex,decltype(hexhash), decltype(hexequal)>;
HexHexUnorderedMap came_from{HexHexUnorderedMap(10,hexhash,hexequal)};
HexMap cost_so_far{HexMap(10,hexhash,hexequal)};
came_from.clear();
cost_so_far.clear();