1 #ifndef HUMANVIEW_H_
2 #define HUMANVIEW_H_
3 
4 #include <SDL2/SDL.h>
5 #include "../SharedDefines.h"
6 #include "../GameApp/BaseGameApp.h"
7 #include "../Process/ProcessMgr.h"
8 #include "Console.h"
9 #include "GameHUD.h"
10 
11 #include "UserInterface.h"
12 
13 typedef std::list<shared_ptr<IScreenElement>> ScreenElementList;
14 typedef std::list<shared_ptr<IGameView>> GameViewList;
15 
16 class LevelData;
17 class Scene;
18 class CameraNode;
19 class HumanView : public IGameView
20 {
21 public:
22     HumanView(SDL_Renderer* renderer);
23     virtual ~HumanView();
24 
25     // Interface
26     virtual void VOnRender(uint32 msDiff);
27     virtual void VOnLostDevice();
VGetType()28     virtual GameViewType VGetType() { return GameView_Human; }
VGetId()29     virtual uint32 VGetId() const { return m_ViewId; }
VOnAttach(uint32 viewId,uint32 actorId)30     virtual void VOnAttach(uint32 viewId, uint32 actorId) { m_ViewId = viewId; m_ActorId = actorId; }
31 
32     virtual bool VOnEvent(SDL_Event& evt);
33     virtual void VOnUpdate(uint32 msDiff);
34 
35     // Virtual methods to control layering of interface elements
36     virtual void VPushElement(shared_ptr<IScreenElement> element);
37     virtual void VRemoveElement(shared_ptr<IScreenElement> element);
38 
VSetControlledActor(uint32 actorId)39     virtual void VSetControlledActor(uint32 actorId) { m_ActorId = actorId; }
40 
41     virtual void VSetCameraOffset(int32 offsetX, int32 offsetY);
42 
GetCamera()43     shared_ptr<CameraNode> GetCamera() const { return m_pCamera; }
GetScene()44     shared_ptr<Scene> GetScene() const { return m_pScene; }
45 
46     bool EnterMenu(TiXmlElement* pMenuData);
47     void LoadScoreScreen(TiXmlElement* pScoreScreenRootElem);
48     bool LoadGame(TiXmlElement* pLevelXmlElem, LevelData* pLevelData);
49 
50     void RegisterConsoleCommandHandler(void(*handler)(const char*, void*), void* userdata);
51 
GetConsole()52     shared_ptr<Console> GetConsole() const { return m_pConsole; }
53 
SetRendering(bool rendering)54     void SetRendering(bool rendering) { m_bRendering = rendering; }
IsRendering()55     bool IsRendering() { return m_bRendering; }
SetPostponeRenderPresent(bool postpone)56     void SetPostponeRenderPresent(bool postpone) { m_bPostponeRenderPresent = postpone; }
57 
SetCurrentLevelMusic(const std::string & music)58     void SetCurrentLevelMusic(const std::string& music) { m_CurrentLevelMusic = music; }
59 
60 protected:
VLoadGameDelegate(TiXmlElement * pLevelXmlElem,LevelData * pLevelData)61     virtual bool VLoadGameDelegate(TiXmlElement* pLevelXmlElem, LevelData* pLevelData) { VPushElement(m_pScene); return true; }
62 
63     // Delegates
64     void NewHUDElementDelegate(IEventDataPtr pEventData);
65     void ScoreUpdatedDelegate(IEventDataPtr pEventData);
66     void LivesUpdatedDelegate(IEventDataPtr pEventData);
67     void HealthUpdatedDelegate(IEventDataPtr pEventData);
68     void AmmoUpdatedDelegate(IEventDataPtr pEventData);
69     void AmmoTypeUpdatedDelegate(IEventDataPtr pEventData);
70     void PowerupUpdatedTimeDelegate(IEventDataPtr pEventData);
71     void PowerupUpdatedStatusDelegate(IEventDataPtr pEventData);
72     void RequestPlaySoundDelegate(IEventDataPtr pEventData);
73     void RequestResetLevelDelegate(IEventDataPtr pEventData);
74     void LoadGameDelegate(IEventDataPtr pEventData);
75     void SetVolumeDelegate(IEventDataPtr pEventData);
76     void SoundEnabledChangedDelegate(IEventDataPtr pEventData);
77     void ClawDiedDelegate(IEventDataPtr pEventData);
78     void TeleportActorDelegate(IEventDataPtr pEventData);
79     void EnterMenuDelegate(IEventDataPtr pEventData);
80     void FinishedLevelDelegate(IEventDataPtr pEventData);
81     void ActorEnteredBossAreaDelegate(IEventDataPtr pEventData);
82     void BossFightEndedDelegate(IEventDataPtr pEventData);
83     void IngameMenuEndGameDelegate(IEventDataPtr pEventData);
84 
85     uint32 m_ViewId;
86     uint32 m_ActorId;
87 
88     ProcessMgr* m_pProcessMgr;
89 
90     uint64 m_CurrentTick;
91     uint64 m_LastDraw;
92     bool m_RunFullSpeed;
93 
94     shared_ptr<ScreenElementMenu> m_pMenu;
95     shared_ptr<ScreenElementScene> m_pScene;
96     shared_ptr<ScreenElementHUD> m_pHUD;
97     shared_ptr<CameraNode> m_pCamera;
98     shared_ptr<Console> m_pConsole;
99     shared_ptr<ScreenElementMenu> m_pIngameMenu;
100 
101     shared_ptr<IKeyboardHandler> m_pKeyboardHandler;
102     shared_ptr<IPointerHandler> m_pPointerHandler;
103 
104     ScreenElementList m_ScreenElements;
105 
106     bool m_bRendering;
107     bool m_bPostponeRenderPresent;
108 
109     std::string m_CurrentLevelMusic;
110 
111 private:
112     void RegisterAllDelegates();
113     void RemoveAllDelegates();
114 };
115 
116 // TODO: Make generic way of making new special effects
117 // Should be constructed from 1 parameter - struct SpecialEffectDef
118 // This should contain all informations about transitions (could be like 20 transitions) -
119 // - struct SpecialEffectTransitionDef or struct SfxTransitionDef - duration, what to do in this
120 // transition etc.
121 //
122 // Right now I am only interested in death and teleport special effects so this is fine albeit
123 // unflexible
124 
125 class SpecialEffectProcess : public Process
126 {
127 public:
~SpecialEffectProcess()128     virtual ~SpecialEffectProcess() { VRestoreStates(); }
129 
VOnSuccess()130     virtual void VOnSuccess() override { VRestoreStates(); }
VOnFail()131     virtual void VOnFail() override { VRestoreStates(); }
VOnAbort()132     virtual void VOnAbort() override { VRestoreStates(); }
133 
134     virtual void VRestoreStates();
135     virtual void VRender(uint32 msDiff) = 0;
136 };
137 
138 class DeathFadeInOutProcess : public SpecialEffectProcess
139 {
140 public:
141     enum DeathFadeState
142     {
143         DeathFadeState_Started,
144         DeathFadeState_FadingIn,
145         DeathFadeState_FadingOut,
146         DeathFadeState_Ended
147     };
148 
149     DeathFadeInOutProcess(Point epicenter, int fadeInDuration, int fadeOutDuration, int startDelay, int endDelay);
150     virtual ~DeathFadeInOutProcess();
151 
152     virtual void VOnInit() override;
153     virtual void VOnUpdate(uint32 msDiff) override;
154     virtual void VRender(uint32 msDiff) override;
155 
156 private:
157     Point m_Epicenter;
158     int m_StartDelay;
159     int m_FadeInDuration;
160     int m_FadeOutDuration;
161     int m_EndDelay;
162 
163     DeathFadeState m_DeathFadeState;
164     int m_CurrentTime;
165 
166     Point m_FadeInSpeed;
167     Point m_FadeOutSpeed;
168 };
169 
170 class PrimeSearch;
171 class FadingLine
172 {
173 public:
174     FadingLine(int length, Point fragmentSize, int fadeDelay, int fadeDuration, bool isFadingIn);
175     ~FadingLine();
176 
177     void Update(uint32 msDiff);
178     void Reset(int fadeDelay, bool isFadingIn);
179 
Activate()180     void Activate() { m_bIsActive = true; }
IsDone()181     bool IsDone() { return m_bIsDone; }
182 
183     void Render(SDL_Renderer* pRenderer, SDL_Texture* pFragmentTexture, Point& lineOffset, bool asRow);
184 
185 private:
186     int m_Length;
187     Point m_FragmentSize;
188     int m_FadeDuration;
189     int m_FadeDelay;
190     bool m_bIsFadingIn;
191 
192     int m_CurrentTime;
193     int m_SingleFragmentFadeTime;
194 
195     bool m_bIsActive;
196     bool m_bIsDone;
197     int m_FragmentCount;
198     unique_ptr<PrimeSearch> m_pPrimeSearch;
199     std::vector<bool> m_FadedFragments;
200 };
201 
202 class TeleportFadeInOutProcess : public SpecialEffectProcess
203 {
204 public:
205     enum TeleportState
206     {
207         TeleportState_FadingIn,
208         TeleportState_FadingOut,
209     };
210 
211     TeleportFadeInOutProcess(int fadeInDuration, int fadeOutDuration);
212     virtual ~TeleportFadeInOutProcess();
213 
214     virtual void VOnInit() override;
215     virtual void VOnUpdate(uint32 msDiff) override;
216     virtual void VRender(uint32 msDiff) override;
217 
218 private:
219     int m_FadeInDuration;
220     int m_FadeOutDuration;
221     Point m_FadeInSpeed;
222     Point m_FadeOutSpeed;
223 
224     TeleportState m_TeleportState;
225     int m_CurrentTime;
226 
227     Point m_FragmentSize;
228     SDL_Texture* m_pFadingTexture;
229     std::vector<shared_ptr<FadingLine>> m_Lines;
230 };
231 
232 #endif