Terminus
game.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 
5 #include <QObject>
6 #include <QOpenGLFunctions>
7 #include <QQuickItem>
8 
9 #include <eventhandler.h>
10 #include <network/networkmanager.h>
11 #include <util/actionscheduler.h>
12 #include <util/timer.h>
13 
14 class QTimer;
15 class QTime;
16 class QVariant;
17 
18 namespace terminus
19 {
20 
21 class World;
22 
23 const unsigned short defaultPort = 7331;
24 
36 class Game : public QQuickItem
37 {
38  Q_OBJECT
39  Q_PROPERTY(QVariant qmlData READ qmlData NOTIFY qmlDataChanged())
40 
41 public:
45  Game();
46 
50  Game(const Game & other) = delete;
51 
55  Game & operator=(const Game & other) = delete;
56 
61  ~Game();
62 
67  Q_INVOKABLE void startLocalGame();
68 
72  Q_INVOKABLE void hostNetworkGame();
73 
78  Q_INVOKABLE void joinNetworkGame(QString host);
79 
87  void createWorld(bool isNetworkGame, bool isPlayerOne, int terrainSeed);
88 
89  void endGame(bool localPlayerWins, bool showMessage);
90 
94  void toggleUI();
95  void showUI();
96  void hideUI();
97 
98  World & world() const;
99  QVariant & qmlData();
101 
103 
104  Timer & timer();
105 
109  Q_INVOKABLE void buttonInput(int type);
110 
114  Q_INVOKABLE void keyInput(Qt::Key key);
115 
119  Q_INVOKABLE void moveInput(int type, qreal x, qreal y);
120  Q_INVOKABLE void touchInput(qreal oldx, qreal oldy, qreal x, qreal y);
121 
122 public slots:
130  void sync();
131 
140  void render();
141 
142  void cleanup();
143 
154  void handleWindowChanged(QQuickWindow * win);
155 
160  void setPaused(bool paused);
161  void togglePaused();
162  bool isPaused() { return m_timer.isPaused(); }
163 
164 signals:
165  void qmlDataChanged();
166 
167 protected:
168  void connectSignals(QQuickWindow * win);
169  void disconnectSignals();
173  void updateQMLData();
174 
175 protected:
180 
181 
182  QVariant m_qmlData;
183 
184  QQuickWindow * m_window;
185  std::unique_ptr<QTimer> m_renderTrigger;
187  QOpenGLFunctions m_gl;
188 
191 
192  std::unique_ptr<World> m_world;
193 };
194 
195 }
QVariant & qmlData()
void connectSignals(QQuickWindow *win)
Definition: game.cpp:244
Timer & timer()
Definition: game.cpp:133
void toggleUI()
Hide or show the UI.
Definition: game.cpp:90
NetworkManager m_networkManager
Definition: game.h:179
QOpenGLFunctions m_gl
Definition: game.h:187
Q_INVOKABLE void joinNetworkGame(QString host)
Try to connect to the given host and receive game information.
Definition: game.cpp:55
Q_INVOKABLE void buttonInput(int type)
Handle a pressed button event from the UI.
Definition: game.cpp:138
The Timer class implements a set of pauseable duration counters (subsequently called timers) and mech...
Definition: timer.h:18
const unsigned short defaultPort
Definition: game.h:23
The Game class is the main entry point. It subclasses QQuickItem and handles all communication betwee...
Definition: game.h:36
bool isPaused()
Definition: game.h:162
Q_INVOKABLE void startLocalGame()
Sets up a local game against an AI player with the local plaer being the one on the right...
Definition: game.cpp:44
Q_INVOKABLE void moveInput(int type, qreal x, qreal y)
Handle a mouse move event.
Definition: game.cpp:154
void sync()
Update game world, taking elapsed time into account.
Definition: game.cpp:170
ActionScheduler & scheduler()
Definition: game.cpp:123
void setPaused(bool paused)
Pause or continue ingame time.
Definition: game.cpp:233
void disconnectSignals()
Definition: game.cpp:255
The EventHandler class receives events (touch, key, mouse, gyroscope, etc.) and triggers actions...
Definition: eventhandler.h:14
void showUI()
Definition: game.cpp:102
bool isPaused()
Definition: timer.cpp:51
bool m_isGLInitialized
The timer that triggers a redraw.
Definition: game.h:186
Q_INVOKABLE void hostNetworkGame()
Start a paused game and wait for a connecting player.
Definition: game.cpp:49
The NetworkManager implements the basic functionality for the multiplayer mode of this game...
Definition: networkmanager.h:28
std::unique_ptr< World > m_world
Definition: game.h:192
Timer m_timer
Definition: game.h:176
void togglePaused()
Definition: game.cpp:238
EventHandler m_eventHandler
Definition: game.h:177
void createWorld(bool isNetworkGame, bool isPlayerOne, int terrainSeed)
Generic method to create a World.
Definition: game.cpp:60
NetworkManager & networkManager()
Definition: game.cpp:128
The World class represents a running game instance and contains all graphical (3D) elements of the sc...
Definition: world.h:46
std::unique_ptr< QTimer > m_renderTrigger
Definition: game.h:185
void handleWindowChanged(QQuickWindow *win)
Handles a change of the QQuickWindow.
Definition: game.cpp:220
void updateQMLData()
Update the data storage used by the UI to display game info.
Definition: game.cpp:268
bool m_isUIActive
Definition: game.h:190
void qmlDataChanged()
Definition: eventhandler.cpp:18
World & world() const
Definition: game.cpp:117
void hideUI()
Definition: game.cpp:110
Q_INVOKABLE void touchInput(qreal oldx, qreal oldy, qreal x, qreal y)
Definition: game.cpp:162
void endGame(bool localPlayerWins, bool showMessage)
Definition: game.cpp:69
ActionScheduler m_scheduler
Definition: game.h:178
bool m_isPlayerOne
Definition: game.h:189
void render()
Render game world.
Definition: game.cpp:201
Q_INVOKABLE void keyInput(Qt::Key key)
Handle a key event.
Definition: game.cpp:146
QQuickWindow * m_window
Definition: game.h:184
The ActionScheduler class maintains a list of actions, that should be executed later in a specific co...
Definition: actionscheduler.h:16
void cleanup()
Definition: game.cpp:215
QVariant m_qmlData
Definition: game.h:182