-
Notifications
You must be signed in to change notification settings - Fork 0
/
Modes.hpp
673 lines (526 loc) · 18 KB
/
Modes.hpp
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
/*
* Modes.hpp
*
* Copyright 2015 Joaquín Monteagudo Gómez <kindos7@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
/*
C++ library (header file)
Modes
Gamemodes
*/
/*
Version 0.1 (dd/mm/yy, 22/11/2015 ->)
*/
/*
Preprocessor
*/
/*Header guard*/
#ifndef _FDX_MODES_H_
#define _FDX_MODES_H_
/* Includes */
//Spaceships
#include "Ships.hpp"
//Lasers
#include "Laser.hpp"
//Graphics
#include "SFML/Graphics.hpp"
//Linked lists
#include <list>
//Double ending queue
#include <deque>
//Strings
#include <string>
//Dictionary
#include "ClassMenu.hpp"
/* Defines */
/*Constants*/
/*Macros*/
namespace fdx { namespace fighter
{
/*
Class definitions
*/
//Game mode
class Mode;
/*
Function prototypes
*/
/*
Data types
*/
/* Typedefs */
/* Classes */
class Mode : public sf::Drawable
{
/*Interface*/
public:
virtual void put_player(Spaceship* ispshp) = 0;//Put the main player
virtual const Spaceship* get_player() const = 0;//Get the player
virtual bool has_time() const = 0;//Has time
virtual int get_minutes() const = 0;//Get the minutes
virtual int get_seconds() const = 0;//Get the seconds
virtual bool has_friendly() const = 0;//Has number of ally ships
virtual int get_current_friend() const = 0;//Get current allies
virtual int get_max_friend() const = 0;//Get max allies
virtual bool has_enemies() const = 0;//Has number of enemy ships
virtual int get_current_enemy() const = 0;//Get current enemies
virtual int get_max_enemy() const= 0;//Get max enemies
virtual bool has_text() const = 0;//Displays text
virtual sf::Text get_text() = 0;//Text to be displayed
virtual bool has_radar() const = 0;//Radar of enemies
virtual std::list<const Target *> get_targets() const= 0;//Get radar targets
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;//Draw the ships
virtual void update(unsigned int ticks) = 0;//Move the ships
virtual bool end_game() const = 0;//End of the game?
virtual Team get_winner() const = 0;//Winner of the game
virtual bool has_score() const = 0;//Has score?
virtual int get_score() const = 0;//Get the score
};
class Offline_Survival : public Mode
{
/*Config*/
private:
static constexpr unsigned int DEF_SAME_ENEM=4u;//Max number of concurrennt enemies
static constexpr unsigned int DEF_TOTAL_ENEM=20u;//Total number of enemies
static constexpr unsigned int DEF_LIFES=10u;//Number of lifes
static constexpr unsigned int DEF_RADIUS=500u;//Radius when creating an enemy
/*RNG*/
private:
static std::uniform_real_distribution<double> RNG_D;//Generates random doubles
static std::default_random_engine RE;//Engine for the random numbers
/*Attributes*/
private:
//Vars
std::list<AI_spaceship> ais;//AIs of the ship
std::deque<sf::Text> texts;//Text to output
Spaceship* player;//Player spaceship
sf::Clock ck;//Clock for the time
//Spaceship
const sf::Texture& txture;
const Spshp_config& cfg;
const Cannon& can;
Array_Laser al;
//Counters
int now_cur,max_cur;//Numbers of enemy ships at the moment
int now_tot,max_tot;//Numbers of enemy ships for the whole level
int now_lif,max_lif;//Lifes of the player's ship
/*Constructors, copy control*/
public:
//Default constructor
Offline_Survival(const sf::Texture& itxture,const Spshp_config &icfg,const Cannon& ican,unsigned int icur=DEF_SAME_ENEM, unsigned int itot=DEF_TOTAL_ENEM, unsigned int ilifes=DEF_LIFES)
:ais(),texts(),player(nullptr),ck(),txture(itxture),cfg(icfg),can(ican),now_cur(0),max_cur(icur),now_tot(0),max_tot(itot),now_lif(0),max_lif(ilifes)
{}
//Destructor
virtual ~Offline_Survival() {}
/*Population control*/
private:
//Objectives
bool need_add()//Need to add enemies?
{
return ((now_cur<max_cur)&&(now_tot<max_tot));
}
bool need_rev()//Need to revive?
{
return (player&&(!player->alive())&&((now_lif)<max_lif));
}
arrow::Vct get_rand_cnt()//Get a random new center
{
return arrow::Vct::mk_ang_mod(RNG_D(RE)*6.28,RNG_D(RE)*DEF_RADIUS)+(player?player->get_center():arrow::Vct(0,0));
}
void add_enemy();//Add enemy
void clean();//Delete dead ships
/*Implementation*/
public:
void put_player(Spaceship* ispshp)//Put the main player
{
player=ispshp;
if (player)
{
player->put_lasers(&al);
player->put_cannon_lasers(&al);
}
for (auto it=ais.begin();it!=ais.end();++it)
it->set_enemy(ispshp);
}
const Spaceship* get_player() const//Get the player
{
return player;
}
bool has_time() const//Has time
{
return true;
}
int get_minutes() const //Get the minutes
{
return static_cast<int>(ck.getElapsedTime().asSeconds())/60;
}
int get_seconds() const//Get the seconds
{
return static_cast<int>(ck.getElapsedTime().asSeconds())%60;
}
bool has_friendly() const//Has number of ally ships
{
return true;
}
int get_current_friend() const//Get current allies
{
return max_lif-now_lif;
}
int get_max_friend() const//Get max allies
{
return max_lif;
}
bool has_enemies() const//Has number of enemy ships
{
return true;
}
int get_current_enemy() const//Get current enemies
{
return now_cur+(max_tot-now_tot);
}
int get_max_enemy() const//Get max enemies
{
return max_tot;
}
bool has_text() const//Displays text
{
return !texts.empty();
}
sf::Text get_text()//Text to be displayed
{
if (!has_text())
return sf::Text();
sf::Text that=texts.front();
texts.pop_front();
return that;
}
bool has_radar() const//Radar of enemies
{
return true;
}
std::list<const Target *> get_targets() const//Get radar targets
{
std::list<const Target *> rv;
for (const Target& it : ais)
rv.push_front(&it);
return rv;
}
void draw(sf::RenderTarget& target, sf::RenderStates states) const//Draw the ships
{
for (const Spaceship& that : ais)
target.draw(that,states);
target.draw(al,states);
}
void update(unsigned int ticks);//Move the ships
bool end_game() const//End of the game?
{
return (now_lif);
}
Team get_winner() const//Winner of the game
{
Team rv(player?player->team():Team());
if (end_game())
{
if ((now_cur+now_tot)==0)//All enemies died
return rv;
else//Player died
return rv.get_swapped();
}
return rv;
}
virtual bool has_score() const
{
return false;
}
virtual int get_score() const
{
return 0;
}
};
class Multi_DM : public Mode
{
/*Config*/
public:
//Enums
enum Players : int
{
PLAYER_1=1,
PLAYER_2,
MAX_PLAYERS
};
static constexpr int DEF_MAX_LIF=5;
/*RNG*/
private:
static std::uniform_real_distribution<double> RNG_D;//Generates random doubles
static std::default_random_engine RE;//Engine for the random numbers
static constexpr int PUNT_DEATH=-100,PUNT_KILL=200;
static constexpr int DEF_DEL=1000000;
/*Attributes*/
private:
//Vars
std::deque<sf::Text> texts,texts2;//Text to output
Spaceship *player,*player2;//Player spaceship
sf::Clock ck;//Clock for the time
Array_Laser al;
sf::Sound snd;
sf::Sprite ded;
sf::Sprite ded2;
int max_player_lif;
int cur_player,cur_player2;
int sel_play;
bool ended=false;
int delay1=0,delay2=0;
int score1=0,score2=0;
/*Constructors, copy control*/
public:
//Default constructor
Multi_DM(unsigned int itot=DEF_MAX_LIF)
:texts(),texts2(),player(nullptr),player2(nullptr),ck(),al(),max_player_lif(itot),cur_player(0),cur_player2(0)
{}
//Destructor
virtual ~Multi_DM() {}
/*Population control*/
private:
arrow::Vct get_rand_cnt(arrow::Vct r)//Get a random new center
{
return arrow::Vct::mk_ang_mod(RNG_D(RE)*6.28,RNG_D(RE)*300+200)+(r);
}
void rev_ships()
{
if (player&&(!player->alive())&&cur_player<max_player_lif)
{
if (!delay1) {delay1=DEF_DEL;
ded.setPosition(player->get_center().x, player->get_center().y);}
cur_player++;
score2+=PUNT_KILL;
score1+=PUNT_DEATH;
snd.play();
if (cur_player!=max_player_lif)
{
player->revive_at(get_rand_cnt(player->get_center()));
}
}
if (player2&&(!player2->alive())&&cur_player2<max_player_lif)
{
if (!delay2){delay2=DEF_DEL;
ded2.setPosition(player2->get_center().x, player2->get_center().y);}
score1+=PUNT_KILL;
score2+=PUNT_DEATH;
cur_player2++;
snd.play();
if (cur_player2!=max_player_lif)
{
player2->revive_at(get_rand_cnt(player2->get_center()));
}
}
}
/*Implementation*/
public:
void sel_player(int iplayer)
{
sel_play=iplayer;
}
void put_player(Spaceship* ispshp)//Put the main player
{
switch(sel_play)
{
case PLAYER_1:
{
player=ispshp;
if (player)
{
player->put_lasers(&al);
player->put_cannon_lasers(&al);
}
break;
}
case PLAYER_2:
{
player2=ispshp;
if (player2)
{
player2->put_lasers(&al);
player2->put_cannon_lasers(&al);
}
break;
}
}
}
const Spaceship* get_player() const//Get the player
{
switch (sel_play)
{
case PLAYER_1:
return player;
case PLAYER_2:
return player2;
}
}
bool has_time() const//Has time
{
return true;
}
int get_minutes() const //Get the minutes
{
return static_cast<int>(ck.getElapsedTime().asSeconds())/60;
}
int get_seconds() const//Get the seconds
{
return static_cast<int>(ck.getElapsedTime().asSeconds())%60;
}
bool has_friendly() const//Has number of ally ships
{
return true;
}
int get_current_friend() const//Get current allies
{
switch (sel_play)
{
case PLAYER_1:
return max_player_lif-cur_player;
case PLAYER_2:
return max_player_lif-cur_player2;
}
}
int get_max_friend() const//Get max allies
{
return max_player_lif;
}
bool has_enemies() const//Has number of enemy ships
{
return true;
}
int get_current_enemy() const//Get current enemies
{
switch (sel_play)
{
case PLAYER_1:
return max_player_lif-cur_player2;
case PLAYER_2:
return max_player_lif-cur_player;
}
}
int get_max_enemy() const//Get max enemies
{
return max_player_lif;
}
bool has_text() const//Displays text
{
switch (sel_play)
{
case PLAYER_1:
return !texts.empty();
case PLAYER_2:
return !texts2.empty();
default:
return false;
}
}
sf::Text get_text()//Text to be displayed
{
if (!has_text())
return sf::Text();
sf::Text that;
switch (sel_play)
{
case PLAYER_1:
that=texts.front();
texts.pop_front();
break;
case PLAYER_2:
that=texts2.front();
texts2.pop_front();
}
return that;
}
bool has_radar() const//Radar of enemies
{
return true;
}
std::list<const Target *> get_targets() const//Get radar targets
{
std::list<const Target *> rv;
switch (sel_play)
{
case PLAYER_1:
rv.push_back(player2);
break;
case PLAYER_2:
rv.push_back(player);
break;
}
return rv;
}
void draw(sf::RenderTarget& target, sf::RenderStates states) const//Draw the ships
{
target.draw(al,states);
if (delay1)
target.draw(ded);
if (delay2)
target.draw(ded2);
}
void put_sound (sf::SoundBuffer *nsnd)
{
snd.setBuffer(*nsnd);
}
void put_image(sf::Texture *img)
{
ded.setTexture(*img);
ded2.setTexture(*img);
ded.setOrigin(25.f, 23.f);
ded2.setOrigin(25.f, 23.f);
}
void update(unsigned int ticks);//Move the ships
bool end_game() const//End of the game?
{
return cur_player==max_player_lif||cur_player2==max_player_lif;
}
Team get_winner() const//Winner of the game
{
Team rv(player?player->team():Team());
if (end_game())
{
if (score1>score2)//All enemies died
return rv;
else//Player died
return rv.get_swapped();
}
return rv;
}
virtual bool has_score() const
{
return true;
}
virtual int get_score() const
{
switch(sel_play)
{
case PLAYER_1:
return score1;
case PLAYER_2:
return score2;
default:
return 0;
}
}
};
}}//End of namespace
//End of library
#endif