This commit is contained in:
h4570
2022-07-30 14:48:59 +02:00
parent 52a19f11a5
commit b7b32fb35a
25 changed files with 792 additions and 21 deletions
+10 -5
View File
@@ -12,19 +12,24 @@
#include <engine.hpp>
#include <game.hpp>
#include "state/state_manager.hpp"
namespace Tyra {
using Tyra::Engine;
using Tyra::Game;
class Demo : public Game {
namespace Demo {
class DemoGame : public Game {
public:
Demo(Engine* engine);
~Demo();
DemoGame(Engine* engine);
~DemoGame();
void init();
void loop();
private:
Engine* engine;
StateManager<GlobalStateType> stateManager;
};
} // namespace Tyra
} // namespace Demo
+17
View File
@@ -0,0 +1,17 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
namespace Demo {
enum GlobalStateType { STATE_INTRO, STATE_GAME, STATE_EXIT };
} // namespace Demo
+40
View File
@@ -0,0 +1,40 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <engine.hpp>
using Tyra::Engine;
namespace Demo {
template <typename StateTypeT>
class State {
public:
State(Engine* t_engine) : engine(t_engine) {}
virtual ~State() {}
virtual const StateTypeT& getState() const = 0;
virtual const bool& wantFinish() const = 0;
virtual void onStart() = 0;
virtual void update() = 0;
/** @return Next game state */
virtual StateTypeT onFinish() = 0;
protected:
Engine* engine;
};
} // namespace Demo
+63
View File
@@ -0,0 +1,63 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <vector>
#include "./state.hpp"
#include "./global_state_type.hpp"
namespace Demo {
template <typename StateTypeT>
class StateManager {
public:
StateManager(const StateTypeT& initialState) {
stateInitialized = false;
currentState = initialState;
}
~StateManager() {
for (auto& state : states) {
delete state;
}
}
const StateTypeT& getCurrentState() const { return currentState; }
void add(State<StateTypeT>* state) { states.push_back(state); }
void update() {
for (auto& state : states) {
if (state->getState() == currentState) {
if (state->wantFinish()) {
currentState = state->onFinish();
stateInitialized = false;
break;
}
if (!stateInitialized) {
state->onStart();
stateInitialized = true;
}
state->update();
break;
}
}
}
private:
bool stateInitialized;
std::vector<State<StateTypeT>*> states;
StateTypeT currentState;
};
} // namespace Demo
+43
View File
@@ -0,0 +1,43 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include "state/state.hpp"
#include "state/global_state_type.hpp"
#include "state/state_manager.hpp"
#include "./intro_state_type.hpp"
namespace Demo {
class IntroState : public State<GlobalStateType> {
public:
IntroState(Engine* t_engine);
~IntroState();
const GlobalStateType& getState() const { return state; }
const bool& wantFinish() const { return _wantFinish; };
void onStart();
void update();
/** @return Next game state */
GlobalStateType onFinish();
private:
GlobalStateType state;
bool _wantFinish;
StateManager<IntroStateType> stateManager;
};
} // namespace Demo
@@ -0,0 +1,23 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
namespace Demo {
enum IntroStateType {
STATE_PS2DEV,
STATE_TYRA,
STATE_WAR_INFO,
STATE_PRESS_KEY,
STATE_INTRO_END
};
} // namespace Demo
@@ -0,0 +1,55 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include "state/state.hpp"
#include "../intro_state_type.hpp"
#include "renderer/core/2d/sprite/sprite.hpp"
#include "time/timer.hpp"
using Tyra::Sprite;
using Tyra::Texture;
using Tyra::Timer;
namespace Demo {
class IntroPs2DevState : public State<IntroStateType> {
public:
IntroPs2DevState(Engine* t_engine);
~IntroPs2DevState();
const IntroStateType& getState() const { return state; }
const bool& wantFinish() const { return _wantFinish; };
void onStart();
void update();
/** @return Next game state */
IntroStateType onFinish();
private:
IntroStateType state;
bool _wantFinish;
Texture* texture;
Timer initialDelayTimer;
bool initialized;
bool initialDelayElapsed;
bool fadeinActivated;
bool fadeoutActivated;
u8 frameSkipper;
Sprite* sprite;
};
} // namespace Demo
@@ -0,0 +1,64 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include "state/state.hpp"
#include "../intro_state_type.hpp"
#include "renderer/core/2d/sprite/sprite.hpp"
#include "time/timer.hpp"
using Tyra::Sprite;
using Tyra::Texture;
using Tyra::Timer;
namespace Demo {
class IntroTyraState : public State<IntroStateType> {
public:
IntroTyraState(Engine* t_engine);
~IntroTyraState();
const IntroStateType& getState() const { return state; }
const bool& wantFinish() const { return _wantFinish; };
void onStart();
void update();
/** @return Next game state */
IntroStateType onFinish();
private:
IntroStateType state;
bool _wantFinish;
Texture* tyraTexture;
Texture* bgTexture;
Texture* bg2Texture;
Texture* fillerTexture;
Timer initialDelayTimer;
bool initialized;
bool initialDelayElapsed;
bool animActivated;
bool fadeoutActivated;
u8 frameSkipper;
Sprite* tyraSprite;
Sprite* bgSprite;
Sprite* bg2Sprite;
Sprite* fillerSprite;
void renderFillers();
};
} // namespace Demo
@@ -0,0 +1,46 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include "state/state.hpp"
#include "../intro_state_type.hpp"
#include "renderer/core/2d/sprite/sprite.hpp"
#include "time/timer.hpp"
using Tyra::Sprite;
using Tyra::Texture;
using Tyra::Timer;
namespace Demo {
class IntroWarInfoState : public State<IntroStateType> {
public:
IntroWarInfoState(Engine* t_engine);
~IntroWarInfoState();
const IntroStateType& getState() const { return state; }
const bool& wantFinish() const { return _wantFinish; };
void onStart();
void update();
/** @return Next game state */
IntroStateType onFinish();
private:
IntroStateType state;
bool _wantFinish;
bool initialized;
};
} // namespace Demo