1 /**
2  * Defines the DGame class, the base class for all game logic.
3  */
4 module core.dgame;
5 import core.prefabs, core.properties;
6 import components.assets;
7 import graphics.graphics;
8 import utility.time, utility.config, utility.output, utility.input;
9 
10 enum GameState { Menu = 0, Game = 1, Reset = 2, Quit = 3 };
11 
12 class DGame
13 {
14 public:
15 	static
16 	{
17 		mixin Property!( "DGame", "instance" );
18 	}
19 
20 	GameState currentState;
21 
22 	/**
23 	 * Overrideable. Returns the name of the window.
24 	 */
25 	@property string title()
26 	{
27 		return "Dash";
28 	}
29 
30 	/**
31 	 * Main Game loop.
32 	 */
33 	final void run()
34 	{
35 		// Init tasks
36 		//TaskManager.initialize();
37 
38         start();
39 
40         // Loop until there is a quit message from the window or the user.
41         while( currentState != GameState.Quit )
42         {
43 			if( currentState == GameState.Reset )
44 				reload();
45 
46 			//////////////////////////////////////////////////////////////////////////
47 			// Update
48 			//////////////////////////////////////////////////////////////////////////
49 
50 			// Platform specific program stuff
51 			Graphics.messageLoop();
52 
53 			// Update time
54 			Time.update();
55 
56 			// Update input
57 			Input.update();
58 
59 			// Update physics
60 			//if( currentState == GameState.Game )
61 			//	PhysicsController.stepPhysics( Time.deltaTime );
62 
63 			// Do the updating of the child class.
64 			onUpdate();
65 
66 			//////////////////////////////////////////////////////////////////////////
67 			// Draw
68 			//////////////////////////////////////////////////////////////////////////
69 
70 			// Begin drawing
71 			Graphics.beginDraw();
72 
73 			// Draw in child class
74 			onDraw();
75 
76 			// End drawing
77 			Graphics.endDraw();
78         }
79 
80         stop();
81 	}
82 
83 	//static Camera camera;
84 
85 protected:
86 	/**
87 	 * To be overridden, logic for when the game is being initalized.
88 	 */
89 	void onInitialize() { }
90 	/**
91 	 * To be overridden, called once per frame during the update cycle.
92 	 */
93 	void onUpdate() { }
94 	/**
95 	 * To be overridden, called once per frame during the draw cycle.
96 	 */
97 	void onDraw() { }
98 	/**
99 	 * To be overridden, called when the came is closing.
100 	 */
101 	void onShutdown() { }
102 	/**
103 	 * To be overridden, called when resetting and the state must be saved.
104 	 */
105 	void onSaveState() { }
106 
107 	//UserInterface ui;
108 
109 private:
110 	/**
111 	 * Function called to initialize controllers.
112 	 */
113 	final void start()
114 	{
115 		currentState = GameState.Game;
116         //camera = null;
117 
118 		Config.initialize();
119 		Input.initialize();
120 		Output.initialize();
121 		Graphics.initialize();
122 		Assets.initialize();
123 		Prefabs.initialize();
124 		//Physics.initialize();
125 
126         //ui = new UserInterface( this );
127 
128         onInitialize();
129 	}
130 
131 	/**
132 	 * Function called to shutdown controllers.
133 	 */
134 	final void stop()
135 	{
136 		onShutdown();
137 		Assets.shutdown();
138 		Graphics.shutdown();
139 	}
140 
141 	/**
142 	 * Reloads content and yaml.
143 	 */
144 	final void reload()
145 	{
146 		stop();
147 
148 		start();
149 	}
150 
151 	/**
152 	 * Called when engine is resetting.
153 	 */
154 	final void saveState()
155 	{
156 		onSaveState();
157 	}
158 }
159 
160 struct Game( T ) if( is( T : DGame ) )
161 {
162 	static this()
163 	{
164 		DGame.instance = new T;
165 	}
166 }