1 #ifndef __INTERFACES_H__
2 #define __INTERFACES_H__
3 
4 #include <SDL2/SDL.h>
5 #include <Box2D/Box2D.h>
6 #include <stdint.h>
7 #include <memory>
8 #include <list>
9 #include <map>
10 #include <tinyxml.h>
11 #include <stdlib.h>
12 #include <float.h>
13 #include <vector>
14 #include <assert.h>
15 #include "Util/EnumString.h"
16 
17 class Actor;
18 typedef std::shared_ptr<Actor> StrongActorPtr;
19 typedef std::weak_ptr<Actor> WeakActorPtr;
20 
21 typedef std::vector<Actor*> ActorList;
22 
23 class ActorComponent;
24 typedef std::shared_ptr<ActorComponent> StrongActorComponentPtr;
25 typedef std::weak_ptr<ActorComponent> WeakActorComponentPtr;
26 
27 typedef std::vector<std::string> SoundList;
28 
29 enum ActionType
30 {
31     ActionType_Fire,
32     ActionType_Attack,
33     ActionType_Change_Ammo_Type
34 };
35 
36 enum Direction
37 {
38     Direction_None,
39     Direction_Up,
40     Direction_Down,
41     Direction_Left,
42     Direction_Right,
43     Direction_Up_Left,
44     Direction_Up_Right,
45     Direction_Down_Right,
46     Direction_Down_Left
47 };
48 
49 enum MovementInfoEvent
50 {
51     MovementEvent_None,
52     MovementEvent_SidehitSolid,
53     MovementEvent_FloorHitSolid,
54     MovementInfo_CeilingHitSolid,
55     MovementInfo_LandedLadder,
56     MovementInfo_HitDeath
57 };
58 
59 enum CollisionType
60 {
61     CollisionType_None,
62     CollisionType_Solid,
63     CollisionType_Ground,
64     CollisionType_Climb,
65     CollisionType_Death
66 };
67 
68 enum CollisionFlag
69 {
70     CollisionFlag_None                  = 0x0,
71     CollisionFlag_All                   = 0x1,
72     CollisionFlag_Controller            = 0x2,
73     CollisionFlag_DynamicActor          = 0x4,
74     CollisionFlag_Bullet                = 0x8,
75     CollisionFlag_Explosion             = 0x10,
76     CollisionFlag_Magic                 = 0x20,
77     CollisionFlag_Crate                 = 0x40,
78     CollisionFlag_Rope                  = 0x80,
79     CollisionFlag_Solid                 = 0x100,
80     CollisionFlag_Ground                = 0x200,
81     CollisionFlag_Death                 = 0x400,
82     CollisionFlag_Ladder                = 0x800,
83     CollisionFlag_PowderKeg             = 0x1000,
84     CollisionFlag_Trigger               = 0x2000,
85     CollisionFlag_Pickup                = 0x4000,
86     CollisionFlag_Checkpoint            = 0x8000,
87     CollisionFlag_ClawAttack            = 0x10000,
88     CollisionFlag_EnemyAIAttack         = 0x20000,
89     CollisionFlag_EnemyAIProjectile     = 0x40000,
90     CollisionFlag_DamageAura            = 0x80000,
91     CollisionFlag_InvisibleController   = 0x100000,
92     CollisionFlag_RopeSensor            = 0x400000,
93 };
94 
95 enum FixtureType
96 {
97     FixtureType_Min = -1,
98     FixtureType_None = 0,
99     // Tiles
100     FixtureType_Solid,
101     FixtureType_Ground,
102     FixtureType_TopLadderGround,
103     FixtureType_Climb,
104     FixtureType_Death,
105     // Sensors
106     FixtureType_FootSensor,
107     FixtureType_Controller,
108     FixtureType_AmbientTrigger,
109     FixtureType_Trigger,
110     FixtureType_Projectile,
111     FixtureType_Crate,
112     FixtureType_PowderKeg,
113     FixtureType_Pickup,
114     FixtureType_Explosion,
115     FixtureType_EnemyAI,
116     FixtureType_EnemyAIMeleeSensor,
117     FixtureType_EnemyAIDuckMeleeSensor,
118     FixtureType_EnemyAIRangedSensor,
119     FixtureType_EnemyAIDuckRangedSensor,
120     FixtureType_EnemyAIDiveAreaSensor,
121     FixtureType_DamageAura,
122     FixtureType_RopeSensor,
123     FixtureType_Trigger_SpawnArea,
124     FixtureType_Trigger_GabrielButton,
125     FixtureType_Trigger_ChaseEnemyAreaSensor,
126     FixtureType_Trigger_RollAreaSensor,
127     FixtureType_Max,
128 };
129 
130 enum PlayerStat
131 {
132     PlayerStat_Score,
133     PlayerStat_Health,
134     PlayerStat_Lives,
135     PlayerStat_Bullets,
136     PlayerStat_Magic,
137     PlayerStat_Dynamite
138 };
139 
140 enum AmmoType
141 {
142     AmmoType_None = -1,
143     AmmoType_Pistol,
144     AmmoType_Magic,
145     AmmoType_Dynamite,
146     //-----
147     AmmoType_Max
148 };
149 
150 enum PowerupType
151 {
152     PowerupType_None = -1,
153     PowerupType_Catnip,
154     PowerupType_Invulnerability,
155     PowerupType_Invisibility,
156     PowerupType_FireSword,
157     PowerupType_FrostSword,
158     PowerupType_LightningSword,
159     PowerupType_Max
160 };
161 
162 enum PickupType
163 {
164     PickupType_None = -1,
165     PickupType_Default = 0,
166     PickupType_Treasure_Goldbars,
167     PickupType_Treasure_Rings_Red,
168     PickupType_Treasure_Rings_Green,
169     PickupType_Treasure_Rings_Blue,
170     PickupType_Treasure_Rings_Purple,
171     PickupType_Treasure_Necklace,
172     PickupType_Treasure_Crosses_Red,
173     PickupType_Treasure_Crosses_Green,
174     PickupType_Treasure_Crosses_Blue,
175     PickupType_Treasure_Crosses_Purple,
176     PickupType_Treasure_Scepters_Red,
177     PickupType_Treasure_Scepters_Green,
178     PickupType_Treasure_Scepters_Blue,
179     PickupType_Treasure_Scepters_Purple,
180     PickupType_Treasure_Geckos_Red,
181     PickupType_Treasure_Geckos_Green,
182     PickupType_Treasure_Geckos_Blue,
183     PickupType_Treasure_Geckos_Purple,
184     PickupType_Ammo_Deathbag,
185     PickupType_Ammo_Shot,
186     PickupType_Ammo_Shotbag,
187     PickupType_Powerup_Catnip_1,
188     PickupType_Powerup_Catnip_2,
189     PickupType_Health_Breadwater,
190     PickupType_Health_25,
191     PickupType_Health_10,
192     PickupType_Health_15,
193     PickupType_Ammo_Magic_5,
194     PickupType_Ammo_Magic_10,
195     PickupType_Ammo_Magic_25,
196     PickupType_Mappiece,
197     PickupType_Warp,
198     PickupType_Treasure_Coins,
199     PickupType_Ammo_Dynamite,
200     PickupType_Curse_Ammo,
201     PickupType_Curse_Magic,
202     PickupType_Curse_Health,
203     PickupType_Curse_Death,
204     PickupType_Curse_Treasure,
205     PickupType_Curse_Freeze,
206     PickupType_Treasure_Chalices_Red,
207     PickupType_Treasure_Chalices_Green,
208     PickupType_Treasure_Chalices_Blue,
209     PickupType_Treasure_Chalices_Purple,
210     PickupType_Treasure_Crowns_Red,
211     PickupType_Treasure_Crowns_Green,
212     PickupType_Treasure_Crowns_Blue,
213     PickupType_Treasure_Crowns_Purple,
214     PickupType_Treasure_Skull_Red,
215     PickupType_Treasure_Skull_Green,
216     PickupType_Treasure_Skull_Blue,
217     PickupType_Treasure_Skull_Purple,
218     PickupType_Powerup_Invisibility,
219     PickupType_Powerup_Invincibility,
220     PickupType_Powerup_Life,
221     PickupType_Powerup_FireSword,
222     PickupType_Powerup_LightningSword,
223     PickupType_Powerup_FrostSword,
224     PickupType_BossWarp,
225     PickupType_Level2_Gem,
226     PickupType_Max
227 };
228 
229 enum DamageType
230 {
231     DamageType_None = -1,
232     DamageType_MeleeAttack,
233     DamageType_Bullet,
234     DamageType_Magic,
235     DamageType_Explosion,
236     DamageType_FireSword,
237     DamageType_LightningSword,
238     DamageType_FrostSword,
239     DamageType_DeathTile,
240     DamageType_EnemyAura,
241     DamageType_GabrielCannonBall,
242     DamageType_Max
243 };
244 
245 enum AnimationType
246 {
247     AnimationType_Explosion,
248     AnimationType_RedHitPoint,
249     AnimationType_BlueHitPoint,
250     AnimationType_TarSplash
251 };
252 
253 /*enum TriggerType
254 {
255     TriggerType_None = -1,
256 
257     TriggerType_Undefined = 0,
258     TriggerType_SpawnArea,
259 
260     TriggerType_Max,
261 };
262 
263 Begin_Enum_String(TriggerType)
264 {
265     Enum_String(TriggerType_None);
266     Enum_String(TriggerType_Undefined);
267     Enum_String(TriggerType_SpawnArea);
268     Enum_String(TriggerType_Max);
269 }
270 End_Enum_String;*/
271 
272 enum ActorPrototype
273 {
274     ActorPrototype_None = -1,
275 
276     ActorPrototype_Start,
277 
278     //=======================================
279     // Level specific
280     //=======================================
281 
282     // Enemy actors
283     ActorPrototype_BaseEnemy,
284 
285     ActorPrototype_Level1_Soldier,
286     ActorPrototype_Level1_Officer,
287     ActorPrototype_Level1_Rat,
288 
289     ActorPrototype_Level2_Soldier,
290     ActorPrototype_Level2_Officer,
291     ActorPrototype_Level2_PunkRat,
292 
293     ActorPrototype_Level3_Rat,
294     ActorPrototype_Level3_CutThroat,
295     ActorPrototype_Level3_RobberThief,
296 
297     ActorPrototype_Level4_Rat,
298     ActorPrototype_Level4_CutThroat,
299     ActorPrototype_Level4_RobberThief,
300 
301     ActorPrototype_Level5_TownGuard1,
302     ActorPrototype_Level5_TownGuard2,
303     ActorPrototype_Level5_Seagull,
304 
305     ActorPrototype_Level6_TownGuard1,
306     ActorPrototype_Level6_TownGuard2,
307     ActorPrototype_Level6_Rat,
308     ActorPrototype_Level6_Seagull,
309 
310     // Elevators
311     ActorPrototype_BaseElevator,
312 
313     ActorPrototype_BasePathElevator,
314 
315     // Toggle pegs
316     ActorPrototype_BaseTogglePeg,
317 
318     ActorPrototype_Level1_TogglePeg,
319     ActorPrototype_Level2_TogglePeg,
320     ActorPrototype_Level5_TogglePeg,
321 
322     // Crumbling pegs
323     ActorPrototype_BaseCrumblingPeg,
324 
325     ActorPrototype_Level1_CrumblingPeg,
326     ActorPrototype_Level3_CrumblingPeg,
327     ActorPrototype_Level4_CrumblingPeg,
328     ActorPrototype_Level5_CrumblingPeg,
329     ActorPrototype_Level6_CrumblingPeg,
330 
331     // Projectile spawners
332     ActorPrototype_Level2_TowerCannonLeft,
333     ActorPrototype_Level2_TowerCannonRight,
334 
335     // Projectiles
336     ActorPrototype_Level2_CannonBall,
337     ActorPrototype_Level2_RatProjectile,
338     ActorPrototype_Level3_RatBomb,
339     ActorPrototype_Level3_ArrowProjectile,
340     ActorPrototype_Level3_KnifeProjectile,
341     ActorPrototype_Level4_RatBomb,
342     ActorPrototype_Level4_ArrowProjectile,
343     ActorPrototype_Level4_KnifeProjectile,
344     ActorPrototype_Level6_WolvingtonMagic,
345 
346     // Boss stagers
347     ActorPrototype_Level2_BossStager,
348     ActorPrototype_Level4_BossStager,
349     ActorPrototype_Level6_BossStager,
350 
351     // Bosses
352     ActorPrototype_Level2_LaRaux,
353     ActorPrototype_Level4_Katherine,
354     ActorPrototype_Level6_Wolvington,
355 
356     // Stepping stones
357     ActorPrototype_Level4_SteppingGround,
358     ActorPrototype_Level6_SteppingGround,
359 
360     // Spring board
361     ActorPrototype_Level4_SpringBoard,
362     ActorPrototype_Level6_GroundBlower,
363 
364     // Loot containers
365     ActorPrototype_BaseLootContainer,
366     ActorPrototype_Level5_LootStatue,
367 
368     // Floor Spikes / Floor goovents etc
369     ActorPrototype_Level6_GooVent,
370 
371     //=======================================
372     // General
373     //=======================================
374 
375     ActorPrototype_BaseProjectile,
376     ActorPrototype_FireSwordProjectile,
377     ActorPrototype_FrostSwordProjectile,
378     ActorPrototype_LightningSwordProjectile,
379 
380     ActorPrototype_BaseProjectileSpawner,
381 
382     // Sound
383     ActorPrototype_LocalAmbientSound,
384 
385     // Stepping Stone
386     ActorPrototype_BaseSteppingGround,
387 
388     // Spring board
389     ActorPrototype_BaseSpringBoard,
390 
391     // Menu
392     ActorPrototype_StaticImage,
393     ActorPrototype_StaticAnimatedImage,
394 
395     ActorPrototype_BaseBossStager,
396 
397     ActorPrototype_BaseFloorSpike,
398 
399     ActorPrototype_BaseRope,
400 
401     ActorPrototype_BaseActorSpawner,
402 
403     // Level 2
404     ActorPrototype_Level2_PunkRatCannon,
405 
406     // Level 7
407     ActorPrototype_Level7_CrumblingPeg,
408     ActorPrototype_Level7_PathElevator,
409     ActorPrototype_Level7_Seagull,
410     ActorPrototype_Level7_SpringBoard,
411     ActorPrototype_Level7_SteppingGround,
412     ActorPrototype_Level7_BearSailor,
413     ActorPrototype_Level7_RedTailPirate,
414     ActorPrototype_Level7_HermitCrab,
415     ActorPrototype_Level7_CrabNest,
416 
417     // Level 8
418     ActorPrototype_Level8_Seagull,
419     ActorPrototype_Level8_BossStager,
420     ActorPrototype_Level8_SteppingGround,
421     ActorPrototype_Level8_BearSailor,
422     ActorPrototype_Level8_RedTailPirate,
423     ActorPrototype_Level8_PunkRat,
424     ActorPrototype_Level8_TogglePeg,
425     ActorPrototype_Level8_TogglePeg_2,
426     ActorPrototype_Level8_CannonBall,
427     ActorPrototype_Level8_PunkRatCannon,
428     ActorPrototype_Level8_GabrielButton,
429     ActorPrototype_Level8_GabrielCannon,
430     ActorPrototype_Level8_Gabriel,
431     ActorPrototype_Level8_GabrielPirate,
432     ActorPrototype_Level8_GabrielBomb,
433     ActorPrototype_Level8_BossGem,
434 
435     // Level 9
436     ActorPrototype_Level9_Seagull,
437     ActorPrototype_Level9_SpringBoard,
438     ActorPrototype_Level9_PuffDartSpawner,
439     ActorPrototype_Level9_DartProjectile_Up,
440     ActorPrototype_Level9_DartProjectile_Down,
441     ActorPrototype_Level9_DartProjectile_Right,
442     ActorPrototype_Level9_DartProjectile_Left,
443     ActorPrototype_Level9_SawBlade,
444     ActorPrototype_Level9_SkullCannon,
445     ActorPrototype_Level9_CannonBall,
446     ActorPrototype_Level9_CrazyHook,
447     ActorPrototype_Level9_PegLeg,
448     ActorPrototype_Level9_BulletProjectile,
449     ActorPrototype_Level9_ConveyorBelt,
450 
451     // Level 9
452     ActorPrototype_Level10_Seagull,
453     ActorPrototype_Level10_PuffDartSpawner,
454     ActorPrototype_Level10_DartProjectile_Up,
455     ActorPrototype_Level10_DartProjectile_Down,
456     ActorPrototype_Level10_DartProjectile_Right,
457     ActorPrototype_Level10_DartProjectile_Left,
458     ActorPrototype_Level10_CrazyHook,
459     ActorPrototype_Level10_PegLeg,
460     ActorPrototype_Level10_CrumblingPeg,
461     ActorPrototype_Level10_TogglePeg,
462     ActorPrototype_Level10_BossStager,
463 
464     ActorPrototype_Max
465 };
466 
467 struct RaycastResult
468 {
RaycastResultRaycastResult469     RaycastResult()
470     {
471         foundIntersection = false;
472         closestPixelDistance = FLT_MAX;
473         deltaX = 0;
474         deltaY = 0;
475     }
476 
477     bool foundIntersection;
478     float closestPixelDistance;
479     float deltaX;
480     float deltaY;
481 };
482 
483 class Point;
484 struct ActorBodyDef;
485 struct ActorFixtureDef;
486 class CameraNode;
487 class IGamePhysics
488 {
489 public:
~IGamePhysics()490     virtual ~IGamePhysics() { }
491 
492     // Initialization and maintanance of the Physics World
493     virtual bool VInitialize() = 0;
494     virtual void VSyncVisibleScene() = 0;
495     virtual void VOnUpdate(const uint32_t msDiff) = 0;
496 
497     // Initialization of Physics Objects
498     virtual void VAddCircle(float radius, uint32_t thickness, WeakActorPtr pTargetActor) = 0;
499     virtual void VAddRect(uint32_t thickness, WeakActorPtr pTargetActor) = 0;
500     virtual void VAddLine(Point from, Point to, uint32_t thickness) = 0;
501     virtual void VAddStaticGeometry(Point position, Point size, CollisionType collisionType, FixtureType fixtureType) = 0;
502     virtual void VAddDynamicActor(WeakActorPtr pActor) = 0;
503     virtual void VAddKinematicBody(WeakActorPtr pActor) = 0;
504     virtual void VAddStaticBody(WeakActorPtr pActor, Point bodySize, CollisionType collisionType) = 0;
505     virtual void VRemoveActor(uint32_t actorId) = 0;
506 
507     virtual void VAddActorBody(const ActorBodyDef* actorBodyDef) = 0;
508     virtual void VAddActorFixtureToBody(uint32_t actorId, const ActorFixtureDef* pFixtureDef) = 0;
509 
510     // Debugging
511     virtual void VRenderDiagnostics(SDL_Renderer* pRenderer, std::shared_ptr<CameraNode> pCamera) = 0;
512 
513     // Physics world modifiers
514     virtual void VCreateTrigger(WeakActorPtr pActor, const Point& pos, Point& size, bool isStatic) = 0;
515     virtual void VApplyForce(uint32_t actorId, const Point& impulse) = 0;
516     virtual void VApplyLinearImpulse(uint32_t actorId, const Point& impulse) = 0;
517     virtual bool VKinematicMove(const Point& pos, uint32_t actorId) = 0;
518 
519     virtual Point GetGravity() const = 0;
520 
521     // Physics actor states
522     virtual void VStopActor(uint32_t actorId) = 0;
523     virtual Point VGetVelocity(uint32_t actorId) = 0;
524     virtual void SetVelocity(uint32_t actorId, const Point& velocity) = 0;
525     virtual void VTranslate(uint32_t actorId, const Point& dir) = 0;
526     virtual void VSetLinearSpeed(uint32_t actorId, const Point& speed) = 0;
527     virtual void VAddLinearSpeed(uint32_t actorId, const Point& speedIncrement) = 0;
528     virtual void VSetGravityScale(uint32_t actorId, const float gravityScale) = 0;
529     virtual void VSetLinearSpeedEx(uint32_t actorId, const Point& speed) = 0;
530     virtual bool VIsAwake(uint32_t actorId) = 0;
531 
532     virtual void VChangeCollisionFlag(uint32_t actorId, uint32_t fromFlag, uint32_t toFlag) = 0;
533 
534     virtual void VActivate(uint32_t actorId) = 0;
535     virtual void VDeactivate(uint32_t actorId) = 0;
536 
537     virtual void VSetPosition(uint32_t actorId, const Point& position) = 0;
538     virtual Point VGetPosition(uint32_t actorId) = 0;
539 
540     virtual SDL_Rect VGetAABB(uint32_t actorId, bool discardSensors) = 0;
541     virtual bool VIsActorOverlap(uint32_t actorId, FixtureType overlapType) = 0;
542 
543     virtual RaycastResult VRayCast(const Point& fromPoint, const Point& toPoint, uint32_t filterMask) = 0;
544 
545     virtual void VScaleActor(uint32_t actorId, double scale) = 0;
546 };
547 
548 enum GameViewType
549 {
550     GameView_Human,
551     GameView_Remote,
552     GameView_AI,
553     GameView_Recorder,
554     GameView_Other
555 };
556 
557 enum GameState
558 {
559     GameState_Invalid,
560     GameState_Initializing,
561     GameState_LoadingMenu,
562     GameState_Menu,
563     GameState_LoadingLevel,
564     GameState_IngameRunning,
565     GameState_IngamePaused,
566     GameState_LoadingScoreScreen,
567     GameState_ScoreScreen,
568     GameState_Cutscene
569 };
570 
571 class IGameLogic
572 {
573 public:
574     virtual WeakActorPtr VGetActor(const uint32_t actorId) = 0;
575     virtual StrongActorPtr VCreateActor(const std::string& actorResource, TiXmlElement* overrides) = 0;
576     virtual StrongActorPtr VCreateActor(TiXmlElement* pActorRoot, TiXmlElement* overrides) = 0;
577     virtual void VDestroyActor(const uint32_t actorId) = 0;
578     virtual bool VLoadGame(const char* xmlLevelResource) = 0;
579     virtual bool VEnterMenu(const char* xmlMenuResource) = 0;
580     virtual void VSetProxy() = 0;
581     virtual void VOnUpdate(uint32_t msDiff) = 0;
582     virtual void VChangeState(enum GameState newState) = 0;
583     virtual void VMoveActor(const uint32_t actorId, Point newPosition) = 0;
584     virtual std::shared_ptr<IGamePhysics> VGetGamePhysics() = 0;
585 };
586 
587 class IGameView
588 {
589 public:
~IGameView()590     virtual ~IGameView() { }
591 
592     virtual void VOnRender(uint32_t msDiff) = 0;
593     virtual void VOnLostDevice() = 0;
594     virtual GameViewType VGetType() = 0;
595     virtual uint32_t VGetId() const = 0;
596     virtual void VOnAttach(uint32_t viewId, uint32_t actorId) = 0;
597 
598     virtual bool VOnEvent(SDL_Event& evt) = 0;
599     virtual void VOnUpdate(uint32_t msDiff) = 0;
600 };
601 
602 class IScreenElement
603 {
604 public:
~IScreenElement()605     virtual ~IScreenElement() { }
606 
607     virtual void VOnLostDevice() = 0;
608     virtual void VOnRender(uint32_t msDiff) = 0;
609     virtual void VOnUpdate(uint32_t msDiff) = 0;
610 
611     virtual int32_t VGetZOrder() const = 0;
612     virtual void VSetZOrder(int32_t const zOrder) = 0;
613     virtual bool VIsVisible() = 0;
614     virtual void VSetVisible(bool visible) = 0;
615 
616     virtual bool VOnEvent(SDL_Event& evt) = 0;
617 
618     virtual bool const operator < (IScreenElement const &other) { return VGetZOrder() < other.VGetZOrder(); }
619 };
620 
621 typedef std::list<std::shared_ptr<IScreenElement>> ScreenElementList;
622 typedef std::list<std::shared_ptr<IGameView>> GameViewList;
623 
624 class IKeyboardHandler
625 {
626 public:
627     virtual bool VOnKeyDown(SDL_Keycode key) = 0;
628     virtual bool VOnKeyUp(SDL_Keycode key) = 0;
629 };
630 
631 class IPointerHandler
632 {
633 public:
634     virtual bool VOnPointerMove(SDL_MouseMotionEvent& mouseEvent) = 0;
635     virtual bool VOnPointerButtonDown(SDL_MouseButtonEvent& mouseEvent) = 0;
636     virtual bool VOnPointerButtonUp(SDL_MouseButtonEvent& mouseEvent) = 0;
637 };
638 
639 //-------------------------------------------------------------------------------------------------
640 // Abstract generic factory
641 //-------------------------------------------------------------------------------------------------
642 
643 template <class BaseType, class SubType>
GenericObjectCreationFunction(void)644 BaseType* GenericObjectCreationFunction(void) { return new SubType; }
645 
646 template <class BaseClass, class IdType>
647 class GenericObjectFactory
648 {
649 public:
650     template <class SubClass>
Register(IdType id)651     bool Register(IdType id)
652     {
653         auto findIter = _creationFunctions.find(id);
654         if (findIter == _creationFunctions.end())
655         {
656             _creationFunctions[id] = &GenericObjectCreationFunction < BaseClass, SubClass >;
657             return true;
658         }
659 
660         return false;
661     }
662 
663     template <class SubClass>
Register()664     bool Register()
665     {
666         IdType id = SubClass::GetIdFromName(SubClass::g_Name);
667 
668         auto findIter = _creationFunctions.find(id);
669         if (findIter == _creationFunctions.end())
670         {
671             _creationFunctions[id] = &GenericObjectCreationFunction < BaseClass, SubClass >;
672             return true;
673         }
674 
675         return false;
676     }
677 
Create(IdType id)678     BaseClass* Create(IdType id)
679     {
680         auto findIter = _creationFunctions.find(id);
681         if (findIter != _creationFunctions.end())
682         {
683             ObjectCreationFunction func = findIter->second;
684             return func();
685         }
686 
687         return NULL;
688     }
689 
690 private:
691     typedef BaseClass* (*ObjectCreationFunction)();
692     std::map<IdType, ObjectCreationFunction> _creationFunctions;
693 };
694 
695 template <typename T> class Singleton
696 {
697 public:
Instance()698     static T* Instance()
699     {
700         static T _singleton;
701         return &_singleton;
702     }
703 
704 protected:
Singleton()705     Singleton() { }
~Singleton()706     ~Singleton() { }
707 };
708 
709 //-------------------------------------------------------------------------------------------------
710 // String-To-Enum
711 // Enum-To-String
712 //-------------------------------------------------------------------------------------------------
713 
714 FixtureType FixtureTypeStringToEnum(std::string fixtureTypeStr);
715 
716 DamageType StringToDamageTypeEnum(const std::string& str);
717 
718 b2BodyType BodyTypeStringToEnum(std::string bodyTypeStr);
719 
720 Direction StringToEnum_Direction(std::string dirStr);
721 std::string EnumToString_Direction(Direction dir);
722 
723 std::string EnumToString_ActorPrototype(ActorPrototype actorProto);
724 ActorPrototype StringToEnum_ActorPrototype(std::string actorProtoStr);
725 
726 #endif