-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameWorld.cpp
741 lines (573 loc) · 17.9 KB
/
GameWorld.cpp
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
#include "GameWorld.h"
#include "Vehicle.h"
#include "constants.h"
#include "Obstacle.h"
#include "2d/Geometry.h"
#include "2d/Wall2D.h"
#include "2d/Transformations.h"
#include "SteeringBehaviors.h"
#include "time/PrecisionTimer.h"
#include "misc/Smoother.h"
#include "ParamLoader.h"
#include "misc/WindowUtils.h"
#include "misc/Stream_Utility_Functions.h"
#include "AgentFollower.h"
#include "AgentLeader.h"
#include "resource.h"
#include <list>
using std::list;
#include <string>
#include <iostream>
//------------------------------- ctor -----------------------------------
//------------------------------------------------------------------------
GameWorld::GameWorld(int cx, int cy) :
m_cxClient(cx),
m_cyClient(cy),
m_bPaused(false),
m_vCrosshair(Vector2D(cxClient() / 2.0, cxClient() / 2.0)),
m_bShowWalls(false),
m_bShowObstacles(false),
m_bShowPath(false),
m_bShowWanderCircle(false),
m_bShowSteeringForce(false),
m_bShowFeelers(false),
m_bShowDetectionBox(false),
m_bShowFPS(true),
m_dAvFrameTime(0),
m_pPath(NULL),
m_bRenderNeighbors(false),
m_bViewKeys(false),
m_bShowCellSpaceInfo(false)
{
//setup the spatial subdivision class
m_pCellSpace = new CellSpacePartition<Vehicle*>((double)cx, (double)cy, Prm.NumCellsX, Prm.NumCellsY, Prm.NumAgents);
double border = 30;
m_pPath = new Path(5, border, border, cx - border, cy - border, true);
/*****************************************************************************************************************************************/
// AGENTS debut
/*****************************************************************************************************************************************/
AgentLeader* pLeader;
// Ajout du player au début à la case 0 du tableau. Ne pas déplacer sinon StartLeaderFollowing() sera décalé.
pLeader = new AgentLeader(this,
Vector2D(100,100), //initial position
RandFloat()*TwoPi, //start rotation
Vector2D(0, 0), //velocity
Prm.VehicleMass, //mass
Prm.MaxSteeringForce, //max force
Prm.MaxSpeed, //max velocity
Prm.MaxTurnRatePerSecond, //max turn rate
Prm.VehicleScale * 3.0, //scale
NULL);
pLeader->setManualColor(2);
m_pCellSpace->AddEntity(pLeader);
m_player = pLeader;
//setup the leaders
for (int a = 0; a < Prm.NumLeaders; ++a)
{
//determine a random starting position
Vector2D SpawnPos = Vector2D(cx / 2.0 + RandomClamped()*cx / 2.0,
cy / 2.0 + RandomClamped()*cy / 2.0);
pLeader = new AgentLeader(this,
SpawnPos, //initial position
RandFloat()*TwoPi, //start rotation
Vector2D(0, 0), //velocity
Prm.VehicleMass, //mass
Prm.MaxSteeringForce, //max force
Prm.MaxSpeed * 0.80, //max velocity
Prm.MaxTurnRatePerSecond, //max turn rate
Prm.VehicleScale * 4.0, //scale
NULL); //index
pLeader->SetID(0);
pLeader->setManualColor(1);
pLeader->Steering()->WanderOn();
m_Vehicles.push_back(pLeader);
m_leaders.push_back(pLeader);
//add it to the cell subdivision
m_pCellSpace->AddEntity(pLeader);
AgentFollower* pFollower;
Vehicle* pTarget = pLeader;
int nbAgentPerLeader = Prm.NumAgents / Prm.NumLeaders;
for (int i = 0; i < nbAgentPerLeader; ++i)
{
//determine a random starting position
Vector2D SpawnPos = Vector2D(cx / 2.0 + RandomClamped()*cx / 2.0,
cy / 2.0 + RandomClamped()*cy / 2.0);
pFollower = new AgentFollower(this,
SpawnPos, //initial position
RandFloat()*TwoPi, //start rotation
Vector2D(0, 0), //velocity
Prm.VehicleMass, //mass
Prm.MaxSteeringForce, //max force
Prm.MaxSpeed, //max velocity
Prm.MaxTurnRatePerSecond, //max turn rate
Prm.VehicleScale, //scale
pLeader); //index de l'agent a suivre
pFollower->SetID(i + 1);
pFollower->Steering()->FlockingOn();
m_Vehicles.push_back(pFollower);
m_followers.push_back(pFollower);
//add it to the cell subdivision
m_pCellSpace->AddEntity(pFollower);
}
}
m_Vehicles.push_back(m_player);
StartLeaderFollowing();
/*****************************************************************************************************************************************/
// AGENTS fin
/*****************************************************************************************************************************************/
/*
#define SHOAL
#ifdef SHOAL
m_Vehicles[Prm.NumAgents - 1]->Steering()->FlockingOff();
m_Vehicles[Prm.NumAgents - 1]->Steering()->WanderOn();
m_Vehicles[Prm.NumAgents - 1]->SetMaxSpeed(70);
for (int i = 0; i < Prm.NumAgents - 1; ++i)
{
m_Vehicles[i]->Steering()->EvadeOn(m_Vehicles[Prm.NumAgents - 1]);
}
#endif*/
//create any obstacles or walls
//CreateObstacles();
//CreateWalls();
}
void GameWorld::StartLeaderFollowing()
{
Vehicle* pLeader;
Vehicle* pFollower;
int nbAgentPerLeader = Prm.NumAgents / Prm.NumLeaders;
for (int a = 0; a < Prm.NumLeaders; ++a)
{
pLeader = m_leaders[a];
pLeader->Steering()->WanderOn();
for (int i = 0; i < nbAgentPerLeader; ++i)
{
int id = i + (nbAgentPerLeader * a);
pFollower = m_followers[id];
pFollower->Steering()->FlockingOff();
pFollower->Steering()->OffsetPursuitOn(pLeader, Vector2D(2, 2));
pLeader = pFollower;
}
}
}
//-------------------------------- dtor ----------------------------------
//------------------------------------------------------------------------
GameWorld::~GameWorld()
{
for (unsigned int a = 0; a < m_Vehicles.size(); ++a)
{
delete m_Vehicles[a];
}
for (unsigned int ob = 0; ob < m_Obstacles.size(); ++ob)
{
delete m_Obstacles[ob];
}
delete m_pCellSpace;
delete m_pPath;
}
//----------------------------- Update -----------------------------------
//------------------------------------------------------------------------
void GameWorld::Update(double time_elapsed)
{
if (m_bPaused) return;
//create a smoother to smooth the framerate
const int SampleRate = 10;
static Smoother<double> FrameRateSmoother(SampleRate, 0.0);
m_dAvFrameTime = FrameRateSmoother.Update(time_elapsed);
/*****************************************************************************************************************************************/
// modif fin
/*****************************************************************************************************************************************/
//TODO : ICI J'ESSAYE DE SET MANUELLEMENT LA FORCE AU JOUEUR HUMAIN
/*m_target->SetPos(m_player->Pos() + m_directionPlayer);
m_player->Steering()->PursuitOn(m_target);*/
//m_player->Steering()->SetFixedForce(m_directionPlayer);
Vector2D temp = m_player->Pos() + (m_directionPlayer * 100);
SetCrosshair(temp);
m_player->Steering()->ArriveOn();
/*****************************************************************************************************************************************/
// modif fin
/*****************************************************************************************************************************************/
//update the vehicles
for (unsigned int a = 0; a < m_Vehicles.size(); ++a)
{
m_Vehicles[a]->Update(time_elapsed, m_Vehicles);
}
}
//--------------------------- CreateWalls --------------------------------
//
// creates some walls that form an enclosure for the steering agents.
// used to demonstrate several of the steering behaviors
//------------------------------------------------------------------------
void GameWorld::CreateWalls()
{
//create the walls
double bordersize = 20.0;
double CornerSize = 0.2;
double vDist = m_cyClient - 2 * bordersize;
double hDist = m_cxClient - 2 * bordersize;
const int NumWallVerts = 8;
Vector2D walls[NumWallVerts] = { Vector2D(hDist*CornerSize + bordersize, bordersize),
Vector2D(m_cxClient - bordersize - hDist*CornerSize, bordersize),
Vector2D(m_cxClient - bordersize, bordersize + vDist*CornerSize),
Vector2D(m_cxClient - bordersize, m_cyClient - bordersize - vDist*CornerSize),
Vector2D(m_cxClient - bordersize - hDist*CornerSize, m_cyClient - bordersize),
Vector2D(hDist*CornerSize + bordersize, m_cyClient - bordersize),
Vector2D(bordersize, m_cyClient - bordersize - vDist*CornerSize),
Vector2D(bordersize, bordersize + vDist*CornerSize) };
for (int w = 0; w < NumWallVerts - 1; ++w)
{
m_Walls.push_back(Wall2D(walls[w], walls[w + 1]));
}
m_Walls.push_back(Wall2D(walls[NumWallVerts - 1], walls[0]));
}
//--------------------------- CreateObstacles -----------------------------
//
// Sets up the vector of obstacles with random positions and sizes. Makes
// sure the obstacles do not overlap
//------------------------------------------------------------------------
void GameWorld::CreateObstacles()
{
//create a number of randomly sized tiddlywinks
for (int o = 0; o < Prm.NumObstacles; ++o)
{
bool bOverlapped = true;
//keep creating tiddlywinks until we find one that doesn't overlap
//any others.Sometimes this can get into an endless loop because the
//obstacle has nowhere to fit. We test for this case and exit accordingly
int NumTrys = 0; int NumAllowableTrys = 2000;
while (bOverlapped)
{
NumTrys++;
if (NumTrys > NumAllowableTrys) return;
int radius = RandInt((int)Prm.MinObstacleRadius, (int)Prm.MaxObstacleRadius);
const int border = 10;
const int MinGapBetweenObstacles = 20;
Obstacle* ob = new Obstacle(RandInt(radius + border, m_cxClient - radius - border),
RandInt(radius + border, m_cyClient - radius - 30 - border),
radius);
if (!Overlapped(ob, m_Obstacles, MinGapBetweenObstacles))
{
//its not overlapped so we can add it
m_Obstacles.push_back(ob);
bOverlapped = false;
}
else
{
delete ob;
}
}
}
}
//------------------------- Set Crosshair ------------------------------------
//
// The user can set the position of the crosshair by right clicking the
// mouse. This method makes sure the click is not inside any enabled
// Obstacles and sets the position appropriately
//------------------------------------------------------------------------
void GameWorld::SetCrosshair(POINTS p)
{
Vector2D ProposedPosition((double)p.x, (double)p.y);
//make sure it's not inside an obstacle
for (ObIt curOb = m_Obstacles.begin(); curOb != m_Obstacles.end(); ++curOb)
{
if (PointInCircle((*curOb)->Pos(), (*curOb)->BRadius(), ProposedPosition))
{
return;
}
}
m_vCrosshair.x = (double)p.x;
m_vCrosshair.y = (double)p.y;
}
//------------------------- HandleKeyPresses -----------------------------
void GameWorld::HandleKeyPresses(WPARAM wParam)
{
switch (wParam)
{
case 'U':
{
delete m_pPath;
double border = 60;
m_pPath = new Path(RandInt(3, 7), border, border, cxClient() - border, cyClient() - border, true);
m_bShowPath = true;
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_Vehicles[i]->Steering()->SetPath(m_pPath->GetPath());
}
}
break;
case 'P':
TogglePause(); break;
case 'O':
ToggleRenderNeighbors(); break;
case 'I':
{
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_Vehicles[i]->ToggleSmoothing();
}
}
break;
case 'Y':
m_bShowObstacles = !m_bShowObstacles;
if (!m_bShowObstacles)
{
m_Obstacles.clear();
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_Vehicles[i]->Steering()->ObstacleAvoidanceOff();
}
}
else
{
CreateObstacles();
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_Vehicles[i]->Steering()->ObstacleAvoidanceOn();
}
}
break;
case 'W':
m_directionPlayer.y = -1;
break;
case 'S':
m_directionPlayer.y = 1;
break;
case 'A':
m_directionPlayer.x = -1;
break;
case 'D':
m_directionPlayer.x = 1;
break;
}//end switch
}
//------------------------- HandleKeyReleased ----------------------------- // touche relachees
void GameWorld::HandleKeyReleased(WPARAM wParam)
{
switch (wParam)
{
case 'W':
m_directionPlayer.x -= m_directionPlayer.x;
break;
case 'S':
m_directionPlayer.x -= m_directionPlayer.x;
break;
case 'A':
m_directionPlayer.x -= m_directionPlayer.x;
break;
case 'D':
m_directionPlayer.x -= m_directionPlayer.x;
break;
}//end switch
}
//-------------------------- HandleMenuItems -----------------------------
void GameWorld::HandleMenuItems(WPARAM wParam, HWND hwnd)
{
switch (wParam)
{
case ID_OB_OBSTACLES:
m_bShowObstacles = !m_bShowObstacles;
if (!m_bShowObstacles)
{
m_Obstacles.clear();
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_Vehicles[i]->Steering()->ObstacleAvoidanceOff();
}
//uncheck the menu
ChangeMenuState(hwnd, ID_OB_OBSTACLES, MFS_UNCHECKED);
}
else
{
CreateObstacles();
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_Vehicles[i]->Steering()->ObstacleAvoidanceOn();
}
//check the menu
ChangeMenuState(hwnd, ID_OB_OBSTACLES, MFS_CHECKED);
}
break;
case ID_OB_WALLS:
m_bShowWalls = !m_bShowWalls;
if (m_bShowWalls)
{
CreateWalls();
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_Vehicles[i]->Steering()->WallAvoidanceOn();
}
//check the menu
ChangeMenuState(hwnd, ID_OB_WALLS, MFS_CHECKED);
}
else
{
m_Walls.clear();
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_Vehicles[i]->Steering()->WallAvoidanceOff();
}
//uncheck the menu
ChangeMenuState(hwnd, ID_OB_WALLS, MFS_UNCHECKED);
}
break;
case IDR_PARTITIONING:
{
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_Vehicles[i]->Steering()->ToggleSpacePartitioningOnOff();
}
//if toggled on, empty the cell space and then re-add all the
//vehicles
if (m_Vehicles[0]->Steering()->isSpacePartitioningOn())
{
m_pCellSpace->EmptyCells();
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_pCellSpace->AddEntity(m_Vehicles[i]);
}
ChangeMenuState(hwnd, IDR_PARTITIONING, MFS_CHECKED);
}
else
{
ChangeMenuState(hwnd, IDR_PARTITIONING, MFS_UNCHECKED);
ChangeMenuState(hwnd, IDM_PARTITION_VIEW_NEIGHBORS, MFS_UNCHECKED);
m_bShowCellSpaceInfo = false;
}
}
break;
case IDM_PARTITION_VIEW_NEIGHBORS:
{
m_bShowCellSpaceInfo = !m_bShowCellSpaceInfo;
if (m_bShowCellSpaceInfo)
{
ChangeMenuState(hwnd, IDM_PARTITION_VIEW_NEIGHBORS, MFS_CHECKED);
if (!m_Vehicles[0]->Steering()->isSpacePartitioningOn())
{
SendMessage(hwnd, WM_COMMAND, IDR_PARTITIONING, NULL);
}
}
else
{
ChangeMenuState(hwnd, IDM_PARTITION_VIEW_NEIGHBORS, MFS_UNCHECKED);
}
}
break;
case IDR_WEIGHTED_SUM:
{
ChangeMenuState(hwnd, IDR_WEIGHTED_SUM, MFS_CHECKED);
ChangeMenuState(hwnd, IDR_PRIORITIZED, MFS_UNCHECKED);
ChangeMenuState(hwnd, IDR_DITHERED, MFS_UNCHECKED);
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_Vehicles[i]->Steering()->SetSummingMethod(SteeringBehavior::weighted_average);
}
}
break;
case IDR_PRIORITIZED:
{
ChangeMenuState(hwnd, IDR_WEIGHTED_SUM, MFS_UNCHECKED);
ChangeMenuState(hwnd, IDR_PRIORITIZED, MFS_CHECKED);
ChangeMenuState(hwnd, IDR_DITHERED, MFS_UNCHECKED);
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_Vehicles[i]->Steering()->SetSummingMethod(SteeringBehavior::prioritized);
}
}
break;
case IDR_DITHERED:
{
ChangeMenuState(hwnd, IDR_WEIGHTED_SUM, MFS_UNCHECKED);
ChangeMenuState(hwnd, IDR_PRIORITIZED, MFS_UNCHECKED);
ChangeMenuState(hwnd, IDR_DITHERED, MFS_CHECKED);
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_Vehicles[i]->Steering()->SetSummingMethod(SteeringBehavior::dithered);
}
}
break;
case ID_VIEW_KEYS:
{
ToggleViewKeys();
CheckMenuItemAppropriately(hwnd, ID_VIEW_KEYS, m_bViewKeys);
}
break;
case ID_VIEW_FPS:
{
ToggleShowFPS();
CheckMenuItemAppropriately(hwnd, ID_VIEW_FPS, RenderFPS());
}
break;
case ID_MENU_SMOOTHING:
{
for (unsigned int i = 0; i < m_Vehicles.size(); ++i)
{
m_Vehicles[i]->ToggleSmoothing();
}
CheckMenuItemAppropriately(hwnd, ID_MENU_SMOOTHING, m_Vehicles[0]->isSmoothingOn());
}
break;
}//end switch
}
//------------------------------ Render ----------------------------------
//------------------------------------------------------------------------
void GameWorld::Render()
{
gdi->TransparentText();
//render any walls
gdi->BlackPen();
for (unsigned int w = 0; w < m_Walls.size(); ++w)
{
m_Walls[w].Render(true); //true flag shows normals
}
//render any obstacles
gdi->BlackPen();
for (unsigned int ob = 0; ob < m_Obstacles.size(); ++ob)
{
gdi->Circle(m_Obstacles[ob]->Pos(), m_Obstacles[ob]->BRadius());
}
//render the agents
for (unsigned int a = 0; a < m_Vehicles.size(); ++a)
{
m_Vehicles[a]->Render();
//render cell partitioning stuff
if (m_bShowCellSpaceInfo && a == 0)
{
gdi->HollowBrush();
InvertedAABBox2D box(m_Vehicles[a]->Pos() - Vector2D(Prm.ViewDistance, Prm.ViewDistance),
m_Vehicles[a]->Pos() + Vector2D(Prm.ViewDistance, Prm.ViewDistance));
box.Render();
gdi->RedPen();
CellSpace()->CalculateNeighbors(m_Vehicles[a]->Pos(), Prm.ViewDistance);
for (BaseGameEntity* pV = CellSpace()->begin(); !CellSpace()->end(); pV = CellSpace()->next())
{
gdi->Circle(pV->Pos(), pV->BRadius());
}
gdi->GreenPen();
gdi->Circle(m_Vehicles[a]->Pos(), Prm.ViewDistance);
}
}
//#define CROSSHAIR
#ifdef CROSSHAIR
//and finally the crosshair
gdi->RedPen();
gdi->Circle(m_vCrosshair, 4);
gdi->Line(m_vCrosshair.x - 8, m_vCrosshair.y, m_vCrosshair.x + 8, m_vCrosshair.y);
gdi->Line(m_vCrosshair.x, m_vCrosshair.y - 8, m_vCrosshair.x, m_vCrosshair.y + 8);
gdi->TextAtPos(5, cyClient() - 20, "Click to move crosshair");
#endif
//gdi->TextAtPos(cxClient() -120, cyClient() - 20, "Press R to reset");
gdi->TextColor(Cgdi::grey);
if (RenderPath())
{
gdi->TextAtPos((int)(cxClient() / 2.0f - 80), cyClient() - 20, "Press 'U' for random path");
m_pPath->Render();
}
if (RenderFPS())
{
gdi->TextColor(Cgdi::grey);
gdi->TextAtPos(5, cyClient() - 20, ttos(1.0 / m_dAvFrameTime));
}
if (m_bShowCellSpaceInfo)
{
m_pCellSpace->RenderCells();
}
}