Wrapping up the Game State Manager
This commit is contained in:
parent
df65740a06
commit
c3d915fe9b
|
@ -0,0 +1,15 @@
|
||||||
|
.dub
|
||||||
|
docs.json
|
||||||
|
__dummy.html
|
||||||
|
docs/
|
||||||
|
/ourgame
|
||||||
|
ourgame.so
|
||||||
|
ourgame.dylib
|
||||||
|
ourgame.dll
|
||||||
|
ourgame.a
|
||||||
|
ourgame.lib
|
||||||
|
ourgame-test-*
|
||||||
|
*.exe
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
*.lst
|
|
@ -0,0 +1,52 @@
|
||||||
|
import data;
|
||||||
|
|
||||||
|
class GStateManager {
|
||||||
|
// private class instance
|
||||||
|
private static GStateManager instance;
|
||||||
|
|
||||||
|
// IState interface
|
||||||
|
private IState[GameState] state;
|
||||||
|
|
||||||
|
private GameState currGameState;
|
||||||
|
|
||||||
|
// private constructor
|
||||||
|
private this() { }
|
||||||
|
|
||||||
|
// return the instance; create the instance, if it wasn't created yet
|
||||||
|
static GStateManager getInstance() {
|
||||||
|
if(instance is null) {
|
||||||
|
instance = new GStateManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// add game state
|
||||||
|
void add(IState state, GameState gs) {
|
||||||
|
this.state[gs] = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove game state
|
||||||
|
void remove(GameState gs) {
|
||||||
|
this.state.remove(gs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// set game state
|
||||||
|
void setState(GameState gs) {
|
||||||
|
currGameState = gs;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the current game state
|
||||||
|
GameState getState() {
|
||||||
|
return currGameState;
|
||||||
|
}
|
||||||
|
|
||||||
|
// execute the current game state code
|
||||||
|
void execute() {
|
||||||
|
if(state is null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
state[currGameState].run();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"rillki"
|
||||||
|
],
|
||||||
|
"copyright": "no copyright",
|
||||||
|
"dependencies": {
|
||||||
|
"raylib-d2": "~>3.1.0"
|
||||||
|
},
|
||||||
|
"description": "2D game",
|
||||||
|
"libs": [
|
||||||
|
"raylib"
|
||||||
|
],
|
||||||
|
"license": "no license",
|
||||||
|
"name": "ourgame"
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"fileVersion": 1,
|
||||||
|
"versions": {
|
||||||
|
"ddmp": "0.0.1-0.dev.3",
|
||||||
|
"fluent-asserts": "0.13.3",
|
||||||
|
"libdparse": "0.14.0",
|
||||||
|
"raylib-d2": "3.1.0",
|
||||||
|
"stdx-allocator": "2.77.5"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
import data;
|
||||||
|
import gstatemanager;
|
||||||
|
|
||||||
|
import menu;
|
||||||
|
import play;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
// init
|
||||||
|
InitWindow(windowWidth, windowHeight, "Mission X");
|
||||||
|
scope(exit) { CloseWindow(); }
|
||||||
|
|
||||||
|
// set frames per second
|
||||||
|
SetTargetFPS(60);
|
||||||
|
|
||||||
|
// declaring and initializing menu and play states
|
||||||
|
Menu menu = new Menu();
|
||||||
|
Play play = new Play();
|
||||||
|
|
||||||
|
GStateManager.getInstance.setState(GameState.MainMenu);
|
||||||
|
|
||||||
|
GStateManager.getInstance.add(menu, GameState.MainMenu);
|
||||||
|
GStateManager.getInstance.add(play, GameState.Play);
|
||||||
|
GStateManager.getInstance.add(null, GameState.Exit);
|
||||||
|
|
||||||
|
// outputs "*** menu state ***"
|
||||||
|
//GStateManager.getInstance.execute();
|
||||||
|
|
||||||
|
// change current state to play
|
||||||
|
//GStateManager.getInstance.setState(GameState.Play);
|
||||||
|
|
||||||
|
while(!WindowShouldClose() && GStateManager.getInstance.getState != GameState.Exit) {
|
||||||
|
GStateManager.getInstance.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
// outputs "*** play state ***"
|
||||||
|
//GStateManager.getInstance.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
module data;
|
||||||
|
|
||||||
|
// mostly used libraries
|
||||||
|
public import raylib;
|
||||||
|
public import std.stdio: writeln, write;
|
||||||
|
|
||||||
|
// window dimensions
|
||||||
|
immutable windowWidth = 720;
|
||||||
|
immutable windowHeight = 640;
|
||||||
|
|
||||||
|
// Game states
|
||||||
|
enum GameState {
|
||||||
|
MainMenu,
|
||||||
|
Play,
|
||||||
|
Exit
|
||||||
|
}
|
||||||
|
|
||||||
|
// state interface
|
||||||
|
interface IState {
|
||||||
|
void run();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
import data;
|
||||||
|
|
||||||
|
class GStateManager {
|
||||||
|
// private class instance
|
||||||
|
private static GStateManager instance;
|
||||||
|
|
||||||
|
// IState interface
|
||||||
|
private IState[GameState] state;
|
||||||
|
|
||||||
|
// current game state
|
||||||
|
private GameState currGameState;
|
||||||
|
|
||||||
|
// private constructor
|
||||||
|
private this() { }
|
||||||
|
|
||||||
|
// return the instance; create the instance, if it wasn't created yet
|
||||||
|
static GStateManager getInstance() {
|
||||||
|
if(instance is null) {
|
||||||
|
instance = new GStateManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// add game state
|
||||||
|
void add(IState state, GameState gs) {
|
||||||
|
this.state[gs] = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove game state
|
||||||
|
void remove(GameState gs) {
|
||||||
|
this.state.remove(gs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// set game state
|
||||||
|
void setState(GameState gs) {
|
||||||
|
currGameState = gs;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the current game state
|
||||||
|
GameState getState() {
|
||||||
|
return currGameState;
|
||||||
|
}
|
||||||
|
|
||||||
|
// execute the current game state code
|
||||||
|
void execute() {
|
||||||
|
if(state is null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
state[currGameState].run();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
module menu;
|
||||||
|
|
||||||
|
import data;
|
||||||
|
import gstatemanager;
|
||||||
|
|
||||||
|
class Menu: IState {
|
||||||
|
this() {}
|
||||||
|
|
||||||
|
// inherited from IState interface
|
||||||
|
void run() {
|
||||||
|
update();
|
||||||
|
processEvents();
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
void update() {}
|
||||||
|
|
||||||
|
void processEvents() {
|
||||||
|
if(IsKeyPressed(KeyboardKey.KEY_P)) {
|
||||||
|
GStateManager.getInstance.setState(GameState.Play);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void render() {
|
||||||
|
// enable drawing
|
||||||
|
BeginDrawing(); scope(exit) { EndDrawing(); }
|
||||||
|
|
||||||
|
// clear background
|
||||||
|
ClearBackground(Colors.GREEN);
|
||||||
|
|
||||||
|
// draw
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
module play;
|
||||||
|
|
||||||
|
import data;
|
||||||
|
import gstatemanager;
|
||||||
|
|
||||||
|
class Play: IState {
|
||||||
|
this() {}
|
||||||
|
|
||||||
|
// inherited from IState interface
|
||||||
|
void run() {
|
||||||
|
update();
|
||||||
|
processEvents();
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
void update() {}
|
||||||
|
|
||||||
|
void processEvents() {
|
||||||
|
if(IsKeyPressed(KeyboardKey.KEY_M)) {
|
||||||
|
GStateManager.getInstance.setState(GameState.MainMenu);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void render() {
|
||||||
|
// enable drawing
|
||||||
|
BeginDrawing(); scope(exit) { EndDrawing(); }
|
||||||
|
|
||||||
|
// clear background
|
||||||
|
ClearBackground(Colors.YELLOW);
|
||||||
|
|
||||||
|
// draw
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue