-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcEnemy.h
824 lines (625 loc) · 24.7 KB
/
cEnemy.h
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
///////////////////////////////////////////////////////
//*-------------------------------------------------*//
//| Part of Project One (https://www.maus-games.at) |//
//*-------------------------------------------------*//
//| Copyright (c) 2010 Martin Mauersics |//
//| Released under the zlib License |//
//*-------------------------------------------------*//
///////////////////////////////////////////////////////
#pragma once
#ifndef _P1_GUARD_ENEMY_H_
#define _P1_GUARD_ENEMY_H_
// TODO 4: manager: Find, ForEach, ForEachAll -> typed
// TODO 3: implement own enemy-types for custom-enemies which would require instancing
// TODO 4: get rid of ENEMY_SIZE_FACTOR, because lots of places override it directly anyway
// TODO 4: move parent-child system to custom-enemy or boss-enemy, if not elsewhere required
// TODO 3: scripted enemy rotation should use DefaultRotate/DefaultOrientate instead of manually using coreVector2::Direction + SetDirection/SetOrientation, to utilize the rota cache
// TODO 1: completely remove PlayerSide aiming (for coop)
// ****************************************************************
// enemy definitions
#define ENEMY_SET_COUNT (16u) //
#define ENEMY_SIZE_FACTOR (1.05f) //
enum eEnemyStatus : coreUint32
{
ENEMY_STATUS_DEAD = 0x0001u, // completely removed from the game
ENEMY_STATUS_ASSIGNED = 0x0002u, // enemy is currently assigned to something
ENEMY_STATUS_CHILD = 0x0004u, //
ENEMY_STATUS_SHIELDED = 0x0008u, // TODO 1: currently not used, needs to be automatic ?
ENEMY_STATUS_BOSS = 0x0010u, //
ENEMY_STATUS_SINGLE = 0x0020u, //
ENEMY_STATUS_ENERGY = 0x0040u, //
ENEMY_STATUS_BOTTOM = 0x0080u, //
ENEMY_STATUS_TOP = 0x0100u, //
ENEMY_STATUS_INVINCIBLE = 0x0200u, //
ENEMY_STATUS_DAMAGING = 0x0400u, // TODO 1: not required anymore ? + damaging effect maybe keep for marking but remove SECRET
ENEMY_STATUS_IMMORTAL = 0x0800u, //
ENEMY_STATUS_GHOST_PLAYER = 0x1000u, //
ENEMY_STATUS_GHOST_BULLET = 0x2000u, //
ENEMY_STATUS_GHOST = ENEMY_STATUS_GHOST_PLAYER | ENEMY_STATUS_GHOST_BULLET,
ENEMY_STATUS_HIDDEN = 0x4000u, //
ENEMY_STATUS_WORTHLESS = 0x8000u, //
ENEMY_STATUS_LIGHT = 0x00010000u,
ENEMY_STATUS_FLAT = 0x00020000u,
ENEMY_STATUS_SECRET = 0x00040000u,
ENEMY_STATUS_CUSTOM = 0x00080000u,
ENEMY_STATUS_CHAIN = 0x00100000u,
ENEMY_STATUS_KEEPVOLUME = 0x00200000u,
ENEMY_STATUS_SKIPEXPLOSION = 0x00400000u,
ENEMY_STATUS_CRASH = 0x00800000u,
ENEMY_STATUS_DEACTIVATE = 0x01000000u,
ENEMY_STATUS_NODELAY = 0x02000000u
// ENEMY_STATUS_UNDER = 0x20000u
};
//ENEMY_STATUS_INVINCIBLE = 0x0100u, // ### bullets are reflected (bubble)
//ENEMY_STATUS_DAMAGING = 0x0200u, // ### collision causes damage (spikes)
//ENEMY_STATUS_IMMORTAL = 0x0400u, // ### should not die, bullets fly through
//ENEMY_STATUS_GHOST = 0x0800u, // ### no collisions with player or bullets anymore
// ****************************************************************
// enemy entity interface
class INTERFACE cEnemy : public cShip
{
protected:
coreFlow m_fLifeTime; //
coreFloat m_fLifeTimeBefore; //
coreUint8 m_iLastAttacker; //
coreBool m_bWasDamaged; //
coreInt16 m_iExtraDamage;
coreInt32 m_iDamageForwarded; //
coreSet<cEnemy*> m_apMember; //
public:
cEnemy()noexcept;
virtual ~cEnemy()override = default;
FRIEND_CLASS(cEnemyManager)
ENABLE_COPY (cEnemy)
ENABLE_ID
// configure the enemy
void Configure(const coreInt32 iHealth, const coreVector3 vColor, const coreBool bInverted = false, const coreBool bIgnored = false, const coreBool bWhite = false);
// render and move the enemy
void Render()final;
void Move ()final;
// reduce current health
coreInt32 TakeDamage(const coreInt32 iDamage, const coreUint8 iElement, const coreVector2 vImpact, cPlayer* OUTPUT pAttacker, const coreBool bNeutral);
// control life and death
void Resurrect();
void Kill (const coreBool bAnimated);
// reset base properties
void ResetProperties();
//
void ApplyScore(cPlayer* pPlayer);
void ApplyScore();
//
void TrackEnemy();
//
void RefreshColorAll(const coreFloat fFactor);
void RefreshColorAll();
void InvokeBlinkAll ();
inline void SetBaseColor(const coreVector3 vColor, const coreBool bInverted = false, const coreBool bIgnored = false, const coreBool bWhite = false)
{
this->cShip::SetBaseColor(vColor * Core::Rand->Float(0.8f, 1.0f), bInverted, bIgnored, bWhite); // TODO 1: what if function is called repeatedly, maybe base on address, or just check all occasions
}
//
inline const coreBool& WasDamaged()const {return m_bWasDamaged;}
inline const coreInt32& GetDamageForwarded()const {return m_iDamageForwarded;}
inline void ResetDamageForwarded() {m_iDamageForwarded = 0;}
//
inline coreBool IsParent ()const {return !m_apMember.empty() && !HAS_FLAG(m_iStatus, ENEMY_STATUS_CHILD);}
inline coreBool IsChild ()const {return !m_apMember.empty() && HAS_FLAG(m_iStatus, ENEMY_STATUS_CHILD);}
inline cEnemy* GetParent() {ASSERT(this->IsChild()) return m_apMember.front();}
//
void ChangeToBottom();
void ChangeToTop ();
void ChangeToNormal();
inline coreUintW GetOutlineStyle()const {return HAS_FLAG(m_iStatus, ENEMY_STATUS_LIGHT) ? OUTLINE_STYLE_LIGHT : (HAS_FLAG(m_iStatus, ENEMY_STATUS_FLAT) ? OUTLINE_STYLE_FLAT_FULL : OUTLINE_STYLE_FULL);}
//
cPlayer* LastAttacker ()const;
coreUintW LastAttackerIndex ()const;
cPlayer* NearestPlayerSide ()const;
cPlayer* NearestPlayerSideRev()const;
cPlayer* NearestPlayerDual (const coreUintW iIndex)const;
coreVector2 AimAtPlayerSide ()const;
coreVector2 AimAtPlayerSideRev ()const;
coreVector2 AimAtPlayerDual (const coreUintW iIndex)const;
// get object properties
inline const coreFloat& GetLifeTime ()const {return m_fLifeTime;}
inline const coreFloat& GetLifeTimeBefore()const {return m_fLifeTimeBefore;}
virtual eSoundEffect GetExplosionSound()const {return SOUND_ENEMY_EXPLOSION_09;}
virtual coreFloat GetExplosionPitch()const {return 1.0f;}
// enemy configuration values
static constexpr const coreChar* ConfigProgramInstancedName() {return "object_ship_blink_inst_program";}
static constexpr coreUintW ConfigReserve () {return 2u;}
protected:
//
void _SetParent(cEnemy* pParent);
private:
// own routines for derived classes (render functions executed by manager)
virtual void __ResurrectOwn () {}
virtual void __KillOwn (const coreBool bAnimated) {}
virtual void __RenderOwnBottom() {}
virtual void __RenderOwnUnder () {}
virtual void __RenderOwnOver () {}
virtual void __RenderOwnTop () {}
virtual void __MoveOwn () {}
};
// ****************************************************************
// enemy manager class
class cEnemyManager final
{
private:
// enemy set structure
struct INTERFACE sEnemySetGen
{
coreBatchList oEnemyActive; // list with active enemies
coreUintW iTopEnemy; //
sEnemySetGen()noexcept;
virtual ~sEnemySetGen() = default;
};
template <typename T> struct sEnemySet final : public sEnemySetGen
{
coreMemoryPool oMemoryPool; //
coreList<T*> apEnemyPool; // semi-dynamic container with all enemies
sEnemySet()noexcept;
~sEnemySet()final;
};
private:
sEnemySetGen* m_apEnemySet[ENEMY_SET_COUNT]; // enemy sets (each for a different inherited enemy class)
coreSet<cEnemy*> m_apAdditional; // pointers to additional enemies
public:
cEnemyManager()noexcept;
~cEnemyManager();
DISABLE_COPY(cEnemyManager)
// render and move the enemy manager
void Render ();
void RenderBottom();
void RenderUnder ();
void RenderOver ();
void RenderTop ();
void MoveBefore ();
void MoveMiddle ();
void MoveAfter ();
// add and remove enemies
template <typename T> RETURN_RESTRICT T* AllocateEnemy();
void FreeEnemy(cEnemy** OUTPUT ppEnemy);
void ClearEnemies(const coreBool bAnimated);
//
cEnemy* FindEnemy (const coreVector2 vPosition)const;
cEnemy* FindEnemyRev(const coreVector2 vPosition)const;
template <typename F> FORCE_INLINE void ForEachEnemy(F&& nFunction)const; // [](cEnemy* OUTPUT pEnemy) -> void
//
template <typename T> void PrefetchEnemy();
template <typename T> void ReserveEnemy(const coreUintW iNumEnemies);
//
inline void BindEnemy (cEnemy* pEnemy) {ASSERT(!m_apAdditional.count(pEnemy)) m_apAdditional.insert(pEnemy);}
inline void UnbindEnemy(cEnemy* pEnemy) {ASSERT( m_apAdditional.count(pEnemy)) m_apAdditional.erase (pEnemy);}
//
inline coreUintW GetNumEnemiesAlive()const {coreUintW iNum = 0u; this->ForEachEnemy([&](void*) {++iNum;}); return iNum;}
};
// ****************************************************************
// enemy squad class
class cEnemySquad final
{
private:
coreList<cEnemy*> m_apEnemy; //
public:
cEnemySquad() = default;
~cEnemySquad();
FRIEND_CLASS(cEnemyManager)
ENABLE_COPY (cEnemySquad)
//
template <typename T> void AllocateEnemies(const coreUint8 iNumEnemies);
void FreeEnemies();
void ClearEnemies(const coreBool bAnimated)const;
//
cEnemy* FindEnemy (const coreVector2 vPosition)const;
cEnemy* FindEnemyRev(const coreVector2 vPosition)const;
template <typename F> FORCE_INLINE void ForEachEnemy (F&& nFunction)const; // [](cEnemy* OUTPUT pEnemy, const coreUintW i) -> void
template <typename F> FORCE_INLINE void ForEachEnemyAll(F&& nFunction)const; // [](cEnemy* OUTPUT pEnemy, const coreUintW i) -> void
//
inline cEnemy* GetEnemy(const coreUintW iIndex)const {ASSERT(iIndex < m_apEnemy.size()) return m_apEnemy[iIndex];}
inline coreUintW GetIndex(const cEnemy* pEnemy)const {const coreUintW iIndex = m_apEnemy.first_index(c_cast<cEnemy*>(pEnemy)); ASSERT(iIndex < m_apEnemy.size()) return iIndex;}
//
inline coreUintW GetNumEnemies ()const {return m_apEnemy.size();}
inline coreUintW GetNumEnemiesAlive ()const {return std::count_if(m_apEnemy.begin(), m_apEnemy.end(), [](const cEnemy* pEnemy) {return !pEnemy->HasStatus(ENEMY_STATUS_DEAD);});}
inline coreFloat GetNumEnemiesAlivePct()const {return I_TO_F(this->GetNumEnemiesAlive()) * RCP(I_TO_F(this->GetNumEnemies()));}
inline coreBool IsFinished ()const {return std::none_of (m_apEnemy.begin(), m_apEnemy.end(), [](const cEnemy* pEnemy) {return !pEnemy->HasStatus(ENEMY_STATUS_DEAD);});}
private:
//
static cEnemyManager* __GetDefaultEnemyManager();
};
// ****************************************************************
// dummy enemy class
class cDummyEnemy final : public cEnemy
{
public:
cDummyEnemy()noexcept;
ENABLE_COPY(cDummyEnemy)
ASSIGN_ID(0, "Dummy")
};
// ****************************************************************
// scout enemy class
class cScoutEnemy final : public cEnemy
{
public:
cScoutEnemy()noexcept;
ENABLE_COPY(cScoutEnemy)
ASSIGN_ID(1, "Scout")
// get object properties
inline eSoundEffect GetExplosionSound()const final {return SOUND_ENEMY_EXPLOSION_09;}
// enemy configuration values
static constexpr coreUintW ConfigReserve() {return 32u;}
private:
// execute own routines
void __ResurrectOwn()final;
void __KillOwn (const coreBool bAnimated)final;
};
// ****************************************************************
// warrior enemy class
class cWarriorEnemy final : public cEnemy
{
public:
cWarriorEnemy()noexcept;
ENABLE_COPY(cWarriorEnemy)
ASSIGN_ID(2, "Warrior")
// get object properties
inline eSoundEffect GetExplosionSound()const final {return SOUND_ENEMY_EXPLOSION_02;}
// enemy configuration values
static constexpr coreUintW ConfigReserve() {return 16u;}
private:
// execute own routines
void __ResurrectOwn()final;
void __KillOwn (const coreBool bAnimated)final;
};
// ****************************************************************
// star enemy class
class cStarEnemy final : public cEnemy
{
private:
coreFlow m_fAngle; // rotation angle
public:
cStarEnemy()noexcept;
ENABLE_COPY(cStarEnemy)
ASSIGN_ID(3, "Star")
//
void ResetProperties() {cEnemy::ResetProperties(); m_fAngle = 0.0f;}
//
inline void SetAngle(const coreFloat fAngle) {m_fAngle = fAngle;}
// get object properties
inline const coreFloat& GetAngle ()const {return m_fAngle;}
inline eSoundEffect GetExplosionSound()const final {return SOUND_ENEMY_EXPLOSION_10;}
// enemy configuration values
static constexpr coreUintW ConfigReserve() {return 32u;}
private:
// execute own routines
void __ResurrectOwn()final;
void __KillOwn (const coreBool bAnimated)final;
void __MoveOwn ()final;
};
// ****************************************************************
// arrow enemy class
class cArrowEnemy final : public cEnemy
{
private:
coreFlow m_fAngle; // rotation angle
public:
cArrowEnemy()noexcept;
ENABLE_COPY(cArrowEnemy)
ASSIGN_ID(4, "Arrow")
//
void ResetProperties() {cEnemy::ResetProperties(); m_fAngle = 0.0f;}
//
inline void SetAngle(const coreFloat fAngle) {m_fAngle = fAngle;}
// get object properties
inline const coreFloat& GetAngle ()const {return m_fAngle;}
inline eSoundEffect GetExplosionSound()const final {return SOUND_ENEMY_EXPLOSION_04;}
// enemy configuration values
static constexpr coreUintW ConfigReserve() {return 32u;}
private:
// execute own routines
void __ResurrectOwn()final;
void __KillOwn (const coreBool bAnimated)final;
void __MoveOwn ()final;
};
// ****************************************************************
// miner enemy class
class cMinerEnemy final : public cEnemy
{
public:
cMinerEnemy()noexcept;
ENABLE_COPY(cMinerEnemy)
ASSIGN_ID(5, "Miner")
// get object properties
inline eSoundEffect GetExplosionSound()const final {return SOUND_ENEMY_EXPLOSION_04;}
// enemy configuration values
static constexpr coreUintW ConfigReserve() {return 16u;}
private:
// execute own routines
void __ResurrectOwn()final;
void __KillOwn (const coreBool bAnimated)final;
};
// ****************************************************************
// freezer enemy class
class cFreezerEnemy final : public cEnemy
{
private:
coreFlow m_fAngle; // rotation angle
public:
cFreezerEnemy()noexcept;
ENABLE_COPY(cFreezerEnemy)
ASSIGN_ID(6, "Freezer")
//
void ResetProperties() {cEnemy::ResetProperties(); m_fAngle = 0.0f;}
//
inline void SetAngle(const coreFloat fAngle) {m_fAngle = fAngle;}
// get object properties
inline const coreFloat& GetAngle ()const {return m_fAngle;}
inline eSoundEffect GetExplosionSound()const final {return SOUND_ENEMY_EXPLOSION_02;}
// enemy configuration values
static constexpr coreUintW ConfigReserve() {return 16u;}
private:
// execute own routines
void __ResurrectOwn()final;
void __KillOwn (const coreBool bAnimated)final;
void __MoveOwn ()final;
};
// ****************************************************************
// cinder enemy class
class cCinderEnemy final : public cEnemy
{
private:
coreFlow m_fAngle; // rotation angle
public:
cCinderEnemy()noexcept;
ENABLE_COPY(cCinderEnemy)
ASSIGN_ID(7, "Cinder")
//
void ResetProperties() {cEnemy::ResetProperties(); m_fAngle = 0.0f;}
//
inline void SetAngle(const coreFloat fAngle) {m_fAngle = fAngle;}
// get object properties
inline const coreFloat& GetAngle ()const {return m_fAngle;}
inline eSoundEffect GetExplosionSound()const final {return SOUND_ENEMY_EXPLOSION_07;}
// enemy configuration values
static constexpr coreUintW ConfigReserve() {return 32u;}
private:
// execute own routines
void __ResurrectOwn()final;
void __KillOwn (const coreBool bAnimated)final;
void __MoveOwn ()final;
};
// ****************************************************************
// meteor enemy class
class cMeteorEnemy final : public cEnemy
{
public:
cMeteorEnemy()noexcept;
ENABLE_COPY(cMeteorEnemy)
ASSIGN_ID(8, "Meteor")
// get object properties
inline eSoundEffect GetExplosionSound()const final {return SOUND_ENEMY_EXPLOSION_07;}
inline coreFloat GetExplosionPitch()const final {return 0.5f + 0.5f * RCP(this->GetSize().x / 4.5f);}
// enemy configuration values
static constexpr const coreChar* ConfigProgramInstancedName() {return "object_meteor_blink_inst_program";}
static constexpr coreUintW ConfigReserve() {return 32u;}
private:
// execute own routines
void __ResurrectOwn()final;
void __KillOwn (const coreBool bAnimated)final;
};
// ****************************************************************
// UFO enemy class
class cUfoEnemy final : public cEnemy
{
public:
cUfoEnemy()noexcept;
ENABLE_COPY(cUfoEnemy)
ASSIGN_ID(9, "UFO")
// get object properties
inline eSoundEffect GetExplosionSound()const final {return SOUND_ENEMY_EXPLOSION_06;}
private:
// execute own routines
void __ResurrectOwn()final;
void __KillOwn (const coreBool bAnimated)final;
};
// ****************************************************************
// core enemy class
class cCoreEnemy final : public cEnemy
{
public:
cCoreEnemy()noexcept;
ENABLE_COPY(cCoreEnemy)
ASSIGN_ID(10, "Core")
// get object properties
inline eSoundEffect GetExplosionSound()const final {return SOUND_ENEMY_EXPLOSION_06;}
};
// ****************************************************************
// custom enemy class
class cCustomEnemy final : public cEnemy
{
public:
cCustomEnemy()noexcept;
~cCustomEnemy()final;
ENABLE_COPY(cCustomEnemy)
ASSIGN_ID(666, "Custom")
//
inline void SetParent(cEnemy* OUTPUT pParent) {this->_SetParent(pParent);}
};
// ****************************************************************
// repair enemy class
class cRepairEnemy final : public cEnemy
{
private:
cPlayer* m_pPlayer; //
coreVector2 m_vDirection; //
coreFlow m_fAnimation; //
coreObject3D m_Bubble; //
cLodObject m_Ship; //
public:
cRepairEnemy()noexcept;
~cRepairEnemy()final;
ENABLE_COPY(cRepairEnemy)
ASSIGN_ID(1337, "Repair")
//
void AssignPlayer(cPlayer* pPlayer);
//
inline cPlayer* GetPlayer()const {return m_pPlayer;}
private:
// execute own routines
void __ResurrectOwn ()final;
void __KillOwn (const coreBool bAnimated)final;
void __RenderOwnOver()final;
void __MoveOwn ()final;
};
// ****************************************************************
// constructor
template <typename T> cEnemyManager::sEnemySet<T>::sEnemySet()noexcept
{
STATIC_ASSERT(T::ID != cCustomEnemy::ID)
// set shader-program
oEnemyActive.DefineProgram(T::ConfigProgramInstancedName());
//
oEnemyActive.CreateCustom(sizeof(coreFloat), [](coreVertexBuffer* OUTPUT pBuffer)
{
pBuffer->DefineAttribute(SHIP_SHADER_ATTRIBUTE_BLINK_NUM, 1u, GL_FLOAT, sizeof(coreFloat), false, 0u, 0u);
},
[](coreByte* OUTPUT pData, const coreObject3D* pEnemy)
{
(*r_cast<coreFloat*>(pData)) = d_cast<const cEnemy*>(pEnemy)->GetBlink();
},
[](const coreProgramPtr& pProgram, const coreObject3D* pEnemy)
{
d_cast<const cEnemy*>(pEnemy)->_EnableBlink(pProgram);
});
// add enemy set to global shadow and outline
cShadow::GetGlobalContainer()->BindList(&oEnemyActive);
g_pOutline->GetStyle(OUTLINE_STYLE_FULL)->BindList(&oEnemyActive);
//
oMemoryPool.Configure(sizeof(T), T::ConfigReserve());
// set enemy pool to initial size
oEnemyActive.Reallocate(T::ConfigReserve());
apEnemyPool .resize (T::ConfigReserve());
apEnemyPool[0] = POOLED_NEW(oMemoryPool, T); // already request resources
}
// ****************************************************************
// destructor
template <typename T> cEnemyManager::sEnemySet<T>::sEnemySet::~sEnemySet()
{
//
oMemoryPool.Shutdown();
FOR_EACH(it, apEnemyPool) POOLED_DELETE(oMemoryPool, *it)
// remove enemy set from global shadow and outline
cShadow::GetGlobalContainer()->UnbindList(&oEnemyActive);
g_pOutline->GetStyle(OUTLINE_STYLE_FULL)->UnbindList(&oEnemyActive);
// clear memory
apEnemyPool.clear();
}
// ****************************************************************
// add enemy to the game
template <typename T> RETURN_RESTRICT T* cEnemyManager::AllocateEnemy()
{
// get requested enemy set
this->PrefetchEnemy<T>();
sEnemySet<T>* pSet = d_cast<sEnemySet<T>*>(m_apEnemySet[T::ID]);
// save current pool size
const coreUintW iSize = pSet->apEnemyPool.size();
ASSERT(iSize)
// loop through all enemies
while(pSet->iTopEnemy < iSize)
{
// check current enemy status
T*& pEnemy = pSet->apEnemyPool[pSet->iTopEnemy++];
if(!pEnemy) pEnemy = POOLED_NEW(pSet->oMemoryPool, T);
if(!pEnemy->HasStatus(ENEMY_STATUS_ASSIGNED))
{
// prepare enemy and add to active list
pEnemy->ResetProperties();
pEnemy->AddStatus(ENEMY_STATUS_ASSIGNED);
pSet->oEnemyActive.BindObject(pEnemy);
return pEnemy;
}
}
// increase list and pool size by 100%
pSet->oEnemyActive.Reallocate(iSize * 2u);
pSet->apEnemyPool .resize (iSize * 2u);
// execute again with first new enemy
return this->AllocateEnemy<T>();
}
// ****************************************************************
//
template <typename F> FORCE_INLINE void cEnemyManager::ForEachEnemy(F&& nFunction)const
{
//
const coreList<coreObject3D*>& oEnemyList = Core::Manager::Object->GetObjectList(TYPE_ENEMY);
FOR_EACH(it, oEnemyList)
{
cEnemy* pEnemy = d_cast<cEnemy*>(*it);
if(!pEnemy || pEnemy->IsChild()) continue;
//
nFunction(pEnemy);
}
}
// ****************************************************************
//
template <typename T> void cEnemyManager::PrefetchEnemy()
{
STATIC_ASSERT(T::ID < ENEMY_SET_COUNT)
if(!m_apEnemySet[T::ID])
{
// create new enemy set
m_apEnemySet[T::ID] = new sEnemySet<T>();
Core::Log->Info("Enemy Set (%s) created", T::Name);
}
}
// ****************************************************************
//
template <typename T> void cEnemyManager::ReserveEnemy(const coreUintW iNumEnemies)
{
// get requested enemy set
this->PrefetchEnemy<T>();
sEnemySet<T>* pSet = d_cast<sEnemySet<T>*>(m_apEnemySet[T::ID]);
if(pSet->apEnemyPool.size() < iNumEnemies)
{
const coreUintW iNumPot = coreMath::CeilPot(iNumEnemies);
// increase list and pool size by power of two
pSet->oEnemyActive.Reallocate(iNumPot);
pSet->apEnemyPool .resize (iNumPot);
}
}
// ****************************************************************
//
template <typename T> void cEnemySquad::AllocateEnemies(const coreUint8 iNumEnemies)
{
ASSERT(iNumEnemies)
//
ASSERT(m_apEnemy.empty())
m_apEnemy.reserve(iNumEnemies);
//
cEnemySquad::__GetDefaultEnemyManager()->ReserveEnemy<T>(iNumEnemies);
//
for(coreUintW i = iNumEnemies; i--; )
{
T* pEnemy = cEnemySquad::__GetDefaultEnemyManager()->AllocateEnemy<T>();
m_apEnemy.push_back(pEnemy);
}
}
// ****************************************************************
//
template <typename F> FORCE_INLINE void cEnemySquad::ForEachEnemy(F&& nFunction)const
{
//
for(coreUintW i = 0u, ie = m_apEnemy.size(); i < ie; ++i)
{
cEnemy* pEnemy = m_apEnemy[i];
if(pEnemy->HasStatus(ENEMY_STATUS_DEAD) && !pEnemy->ReachedDeath()) continue; // # for scripting
//
nFunction(pEnemy, i);
}
}
template <typename F> FORCE_INLINE void cEnemySquad::ForEachEnemyAll(F&& nFunction)const
{
//
for(coreUintW i = 0u, ie = m_apEnemy.size(); i < ie; ++i)
{
//
nFunction(m_apEnemy[i], i);
}
}
#endif // _P1_GUARD_ENEMY_H_