1 #ifndef __BASE_GAME_LOGIC__
2 #define __BASE_GAME_LOGIC__
3 
4 #include "../SharedDefines.h"
5 #include "../Process/ProcessMgr.h"
6 #include "../Actor/Actor.h"
7 #include "CommandHandler.h"
8 
9 typedef std::map<uint32, StrongActorPtr> ActorMap;
10 
11 class GameSaveMgr;
12 class LevelData;
13 class ActorFactory;
14 class BaseGameApp;
15 class BaseGameLogic : public IGameLogic
16 {
17     // This is just to give game app access to game views
18     friend class BaseGameApp;
19 
20     // Command handler should have unlimited access
21     friend class CommandHandler;
22 
23 public:
24     BaseGameLogic();
25     virtual ~BaseGameLogic();
26 
27     bool Initialize();
28 
29     /*// IGameLogic interface
30     virtual WeakActorPtr VGetActorPtr(const uint32 actorId);
31     virtual StrongActorPtr VCreateActor(const std::string& xmlActorResource, TiXmlElement* overrides);
32     virtual void VDestroyActor(const uint32 actorId);
33     virtual bool VLoadGame(const char* xmlLevelResource);
34     virtual void VSetProxy();
35     virtual void VOnUpdate(uint32 msDiff);
36     virtual void VChangeState(enum GameState newState);
37     virtual void VMoveActor(const uint32 actorId, Point newPosition);
38     virtual shared_ptr<IGamePhysics> VGetGamePhysics();
39 
40     // BaseGameLogic*/
41 
42     // View management
43     virtual void VAddView(shared_ptr<IGameView> pView, uint32 actorId = INVALID_ACTOR_ID);
44     virtual void VRemoveView(shared_ptr<IGameView> pView);
45 
46     // Actor management
47     virtual StrongActorPtr VCreateActor(const std::string& xmlActorResource, TiXmlElement* overrides);
48     virtual StrongActorPtr VCreateActor(TiXmlElement* pActorRoot, TiXmlElement* overrides);
49     virtual void VDestroyActor(const uint32 actorId);
50     virtual WeakActorPtr VGetActor(const uint32 actorId);
51     virtual void VModifyActor(const uint32 actorId, TiXmlElement* overrides);
52 
VMoveActor(const uint32_t actorId,Point newPosition)53     virtual void VMoveActor(const uint32_t actorId, Point newPosition) { }
54 
55     std::string GetActorXml(uint32 actorId);
56 
57     // Level management
58 
59     // Subclasses can't override this function, they have to use VLoadGameDelegate() instead
60     virtual bool VLoadGame(const char* xmlLevelResource);
61     virtual bool VLoadScoreScreen(const char* xmlScoreScreenResource);
62     virtual bool VEnterMenu(const char* xmlMenuResource);
63     virtual void VSetProxy();
64 
65     // Logic update
66     virtual void VOnUpdate(uint32 msDiff);
67 
68     // Changing game logic state
69     virtual void VChangeState(GameState newState);
GetGameState()70     const GameState GetGameState() const { return m_GameState; }
71 
72     // ???
73     virtual void VResetLevel();
74 
75     // Render diagnostics
ToggleRenderDiagnostics()76     void ToggleRenderDiagnostics() { m_RenderDiagnostics = !m_RenderDiagnostics; }
77     virtual void VRenderDiagnostics(SDL_Renderer* pRenderer, shared_ptr<CameraNode> pCamera);
VGetGamePhysics()78     virtual shared_ptr<IGamePhysics> VGetGamePhysics() { return m_pPhysics; }
79 
AttachProcess(StrongProcessPtr pProcess)80     void AttachProcess(StrongProcessPtr pProcess) { if (m_pProcessMgr) { m_pProcessMgr->AttachProcess(pProcess); } }
81 
GetCurrentLevelData()82     shared_ptr<LevelData> GetCurrentLevelData() { return m_pCurrentLevel; }
GetGameSaveMgr()83     shared_ptr<GameSaveMgr> GetGameSaveMgr() { return m_pGameSaveMgr; }
84 
85     void UnloadLevel();
SetLevelData(shared_ptr<LevelData> pLevelData)86     void SetLevelData(shared_ptr<LevelData> pLevelData) { m_pCurrentLevel = pLevelData; }
87 
SetRunning(bool running)88     void SetRunning(bool running) { m_bRunning = running; }
IsRunning()89     bool IsRunning() { return m_bRunning; }
90 
91     StrongActorPtr GetClawActor();
92 
93     StrongActorPtr FindActorByName(const std::string& name, bool bIsUnique);
94     ActorList FindActorByName(const std::string& name);
95 
96 protected:
97     virtual ActorFactory* VCreateActorFactory();
98 
VLoadGameDelegate(TiXmlElement * pLevelData)99     virtual bool VLoadGameDelegate(TiXmlElement* pLevelData) { return true; }
100 
101     void MoveActorDelegate(IEventDataPtr pEventData);
102     void RequestNewActorDelegate(IEventDataPtr pEventData);
103     void CollideableTileCreatedDelegate(IEventDataPtr pEventData);
104     void CreateStaticGeometryDelegate(IEventDataPtr pEventData);
105     void RequestDestroyActorDelegate(IEventDataPtr pEventData);
106     void ItemPickedUpDelegate(IEventDataPtr pEventData);
107     void FinishedLevelDelegate(IEventDataPtr pEventData);
108     void ActorEnteredBossAreaDelegate(IEventDataPtr pEventData);
109     void BossFightStartedDelegate(IEventDataPtr pEventData);
110     void IngameMenuEndLifeDelegate(IEventDataPtr pEventData);
111     void WorldFinishedLoadingDelegate(IEventDataPtr pEventData);
112 
113     uint32 m_Lifetime;
114     ProcessMgr* m_pProcessMgr;
115     ActorMap m_ActorMap;
116     uint32 m_LastActorId;
117     GameState m_GameState;
118 
119     int m_HumanPlayersAttached;
120     int m_HumanGamesLoaded;
121     int m_AIPlayersAttached;
122 
123     GameViewList m_GameViews;
124     ActorFactory* m_pActorFactory;
125 
126     bool m_Proxy;
127     bool m_bRunning;
128 
129     bool m_RenderDiagnostics;
130     shared_ptr<IGamePhysics> m_pPhysics;
131     shared_ptr<LevelData> m_pCurrentLevel;
132     shared_ptr<GameSaveMgr> m_pGameSaveMgr;
133 
134     int m_SelectedLevel;
135 
136     Point m_CurrentSpawnPosition;
137 
138 private:
139     void ExecuteStartupCommands(const std::string& startupCommandsFile);
140     void CreateSinglePhysicsTile(int x, int y, const TileCollisionPrototype& proto);
141     //void LoadGameWorkerThread(const char* pXmlLevelPath, float* pProgress, bool* pRet);
142 
143     void RegisterAllDelegates();
144     void RemoveAllDelegates();
145 };
146 
147 //=====================================================================================================================
148 // class LeveData
149 //=====================================================================================================================
150 
151 struct TileCollisionRectangle
152 {
153     CollisionType collisionType;
154     SDL_Rect collisionRect;
155 };
156 
157 struct TileCollisionPrototype
158 {
159     int32 id;
160     uint32 width;
161     uint32 height;
162     std::vector<TileCollisionRectangle> collisionRectangles;
163 };
164 
165 struct TileRect
166 {
167     int32 left;
168     int32 top;
169     int32 right;
170     int32 bottom;
171 };
172 
173 struct TileDescription
174 {
175     int tileId;
176     int type;
177     int width;
178     int height;
179     int insideAttrib;
180     int outsideAttrib;
181     TileRect rect;
182 };
183 
184 typedef std::map<int32, TileDescription> TileDescriptionMap;
185 typedef std::map<int32, TileCollisionPrototype> TileCollisionPrototypeMap;
186 typedef std::map<PickupType, int> PickupMap;
187 
188 // Class containing level (meta)data
189 class LevelData
190 {
191     friend class BaseGameLogic;
192 
193 public:
LevelData(int levelNumber,bool isNewGame,int loadedCheckpoint)194     LevelData(int levelNumber, bool isNewGame, int loadedCheckpoint)
195     {
196         m_bIsNewGame = isNewGame;
197         m_LeveNumber = levelNumber;
198         m_LoadedCheckpoint = loadedCheckpoint;
199     }
200 
LevelData()201     LevelData()
202     {
203         m_LevelName = "Unknown";
204         m_LevelAuthor = "Unknown";
205         m_LevelCreatedDate = "Unknown";
206 
207         m_bIsNewGame = true;
208         m_LeveNumber = -1;
209         m_LoadedCheckpoint = -1;
210     }
211 
GetLevelName()212     std::string GetLevelName() const { return m_LevelName; }
GetLevelAuthor()213     std::string GetLevelAuthor() const { return m_LevelAuthor; }
GetLevelCreatedDate()214     std::string GetLevelCreatedDate() const { return m_LevelCreatedDate; }
GetLevelNumber()215     uint32 GetLevelNumber() const { return m_LeveNumber; }
GetLoadedCheckpointNumber()216     uint32 GetLoadedCheckpointNumber() const { return m_LoadedCheckpoint; }
217 
GetLootedItems()218     const PickupMap* GetLootedItems() { return &m_LootedPickupsMap; }
219 
220 private:
221     std::string m_LevelName;
222     std::string m_LevelAuthor;
223     std::string m_LevelCreatedDate;
224 
225     bool m_bIsNewGame;
226     uint32 m_LeveNumber;
227     uint32 m_LoadedCheckpoint;
228 
229     TileDescriptionMap m_TileDescriptionMap;
230     TileCollisionPrototypeMap m_TileCollisionPrototypeMap;
231 
232     // How many times were certain pickups picked up
233     PickupMap m_LootedPickupsMap;
234     PickupMap m_TotalPickupsMap;
235 };
236 
237 #endif
238