aboutsummaryrefslogtreecommitdiffstats
path: root/include/engine.hpp
blob: 582495025d8918b19b06f58ccdf310fd56f43ad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
 * @file engine.hpp
 * @brief The main game engine, and functions to assist it.
 */

#ifndef ENGINE_HPP_
#define ENGINE_HPP_

#include <atomic>
#include <chrono>
#include <thread.hpp>

#include <entityx/entityx.h>

#include <components.hpp>
#include <events.hpp>

//game::engine::Systems->add<entityx::deps::Dependency<Visible, Sprite>>();

/**
 * @class Engine
 * The main game engine class. Only one instance of this should be created, it
 * handles everything game-related.
 */
class Engine : public entityx::Receiver<Engine> {
public:
	/**
	 * A flag to indicate if a thread should continue to run.
	 */
	bool shouldRun;

	/**
	 * Handles game systems.
	 */
    entityx::SystemManager systems;

	explicit Engine(void);

	/**
	 * Initializes the game engine, and all systems used within it.
	 */
	void init(void);

	/**
	 * Updates all logic systems.
	 * @param dt the delta time
	 */
	void update(entityx::TimeDelta dt);

	/**
	 * A shortcut to get a system, for calling system-specific functions.
	 * Takes the type of the desired system.
	 */
	template<typename T>
	inline T* getSystem(void) {
		return dynamic_cast<T*>(systems.system<T>().get());
	}

	/**
	 * A handler for the game ending event.
	 * @param gee game end event data
	 */
	inline void receive(const GameEndEvent &gee) {
		shouldRun = !(gee.really);
	}
};

namespace game {
	/**
	 * Handles all game events.
	 */
	extern entityx::EventManager events;

	/**
	 * Handles entity data.
	 */
    extern entityx::EntityManager entities;
	
	/**
	 * An instance of the main game engine.
	 */
    extern Engine engine;

	/**
	 * Ends the game.
	 */
    inline void endGame(void) {
        events.emit<GameEndEvent>();
    }
}



#endif // ENGINE_HPP_