State Design Pattern

This commit is contained in:
rillk500 2020-10-31 18:30:51 +06:00
parent 9036f20b5f
commit dada3526de
8 changed files with 176 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,15 @@
{
"authors": [
"rillk500"
],
"copyright": "no copyright",
"dependencies": {
"raylib-d": "~>3.0.3"
},
"description": "2D game",
"libs": [
"raylib"
],
"license": "no license",
"name": "ourgame"
}

View File

@ -0,0 +1,10 @@
{
"fileVersion": 1,
"versions": {
"ddmp": "0.0.1-0.dev.3",
"fluent-asserts": "0.13.3",
"libdparse": "0.14.0",
"raylib-d": "3.0.3",
"stdx-allocator": "2.77.5"
}
}

View File

@ -0,0 +1,61 @@
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);
while(!WindowShouldClose()) {
// process events
// update
// calling the dummy function
GStateManager.getInstance().hello_world();
// render
BeginDrawing();
scope(exit) EndDrawing();
ClearBackground(Colors.WHITE);
// .. draw ..
}*/
// declaring and initializing menu and play states
Menu menu = new Menu();
Play play = new Play();
// calling execute function: nothing happens, since state is null
GStateManager.getInstance.execute();
// change current state to menu
GStateManager.getInstance.setState(menu);
// outputs "*** menu state ***"
GStateManager.getInstance.execute();
// change current state to play
GStateManager.getInstance.setState(play);
// outputs "*** play state ***"
GStateManager.getInstance.execute();
}

View File

@ -0,0 +1,16 @@
module data;
// mostly used libraries
public import raylib;
public import std.stdio: writeln, write;
// window dimensions
immutable windowWidth = 720;
immutable windowHeight = 640;
// state interface
interface IState {
void run();
}

View File

@ -0,0 +1,35 @@
import data;
class GStateManager {
// private class instance
private static GStateManager instance;
// IState interface
private IState state;
// 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;
}
// set game state
void setState(IState state) {
this.state = state;
}
// execute the current game state code
void execute() {
if(state is null) {
return;
}
state.run();
}
}

View File

@ -0,0 +1,12 @@
module menu;
import data;
class Menu: IState {
this() {}
// inherited from IState interface
void run() {
writeln("*** menu state ***");
}
}

View File

@ -0,0 +1,12 @@
module play;
import data;
class Play: IState {
this() {}
// inherited from IState interface
void run() {
writeln("*** play state ***");
}
}