-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameLoop.h
222 lines (163 loc) · 4.84 KB
/
GameLoop.h
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#pragma once
// Debug
#include <iostream>
#include <vector>
// Base objects
#include "TickableObject.h"
#include "RenderableObject.h"
#include "RigidBody.h"
#include "Serializable.h"
#include "Particle.h"
#include "Sprites.h"
#include "ObjectsHolder.h"
// Handle objects
#include <Vector>
#include <QTime>
// Threading
#include <thread>
#include <chrono>
// Rendering
#include <QGraphicsScene>
#include <QGraphicsView>
// Handle Keys
#include <QKeyEvent>
#include <QPointF>
/** Classe GameLoop
* Responsabilita':
* loop di gioco, chiamate a tick() per ogni tickableObject registrato, render() per ogni renderableObject registrato
*
* Questa classe deve essere un Singleton
*/
static bool running = false, paused = false;
class GameLoop : public QObject
{
Q_OBJECT
private:
std::string currentlevel = "";
BaseGUI* pauseGUI;
BaseGUI* commandsGUI;
BaseGUI* aboutusGUI;
BaseGUI* pauseSuggestion;
BaseGUI* startGUI;
BaseGUI* endingviewGUI;
BaseGUI** scoredigits;
BaseGUI** KHealth;
BaseGUI** LivesCounter;
BaseGUI* view;
BaseGUI* state;
BaseGUI* Lives;
bool levelblocks[1][32][23] = {
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
}
};
public:
std::thread loopthread;
std::string _music;
std::vector<RenderableObject*> renderableObjects;
std::vector<RenderableObject*> renderableObjectsToBeDeleted;
std::vector<BaseGUI*> GUIItems;
std::vector<GameObject*> deleteLater;
void addElement(GameObject* item);
void removeElement(GameObject* item, bool willBeDeleted = false);
void reload();
int lives = 4;
int health = 6;
long score = 0;
TexID ability = HUD_POWER;
void detachKirby() { KirbyInstance = 0; Camera::getInstance().cancelGoTo(); }
void updateView();
void addScore(long n) {
setScore(getScore() + n);
}
void addLives(int n) {
setLives(getLives() + n);
}
void setScore(int s) {
score = s;
updateView();
}
void setHealth(int h) {
health = h;
updateView();
}
void setLives(int lvs) {
lives = lvs;
std::cout << "New lives: " << lives << "\n";
updateView();
}
void setAbility(TexID ab) {
ability = ab;
updateView();
}
long getScore() { return score; }
int getLives() { return lives; }
int getHealth() { return health; }
// Relativi al singleton
static GameLoop& getInstance() { static GameLoop instance; return instance; }
~GameLoop();
void recalculateTicks(int target_ticks);
void recalculateFps(int target_fps);
BaseGUI& getPauseGUI() { return *pauseGUI; }
BaseGUI& getPauseSuggestion() { return *pauseSuggestion; }
BaseGUI& getStartGUI() { return *startGUI; }
BaseGUI& getEndingView() { return *endingviewGUI; }
GameObject* KirbyInstance = 0;
const char obj_separator = '@';
// Avvia / ferma loop
void start();
void pause(bool pause = true);
void commands(bool pause = true);
void aboutus(bool pause = true);
void endingview(bool pause = true);
void clear();
static void stop();
void saveGame(std::string fileName);
bool loadGame(std::string fileName, bool issave = false, bool savecurrent = false);
void playBackgroundMusicLevelBased(const std::string level);
QPointF getKirbyPos() { return KirbyInstance ? QPointF(KirbyInstance->getX(), KirbyInstance->getY()) : QPointF(0, 0); }
void addToTickable(TickableObject* tco);
void addToRenderable(RenderableObject* rdo);
void addToSerializable(Serializable* s);
void addToCollidable(RigidBody* s);
void addParticle(GameObject *p);
void keyPressEvent(QKeyEvent* e, bool isPressed = true);
std::vector<std::pair<RigidBody*, double>> findCollisions(RigidBody *rb);
std::vector<RigidBody*> getInside(RigidBody* rb, QRectF area = QRectF(0,0,0,0));
std::vector<std::pair<RigidBody*, double>> rayCast(RigidBody* startbody, QPointF ray);
void showStart();
signals:
void pleaseRender(bool clearscene);
public slots:
void renderingCompleted();
protected:
QGraphicsScene* scene = nullptr;
std::atomic_bool thread_working = true;
private:
// Relativi al singleton
GameLoop();
GameLoop(GameLoop const&) = delete;
void operator=(GameLoop const&) = delete;
//
// Internal calls for watchdog & methods
void loop();
void mergeQueues();
void render(bool clear = false);
void tick(double deltatime);
int target_ticks = 20, target_fps = 144;
int min_delta_millis_fps = 1000 / target_fps, min_delta_millis_tick = 1000 / target_ticks;
QTime last_millis_render, last_millis_tick, last_log;
bool waitingForRender = false;
void loadNetworkData();
// Elementi da iterare
std::vector<TickableObject*> tickableObjects;
std::vector<Serializable*> serializableObjects;
std::vector<RigidBody*> collidableObjects;
std::vector<Particle*> particleObjects;
// Elementi in fila
std::vector<TickableObject*> tickableObjectsQueue;
std::vector<RenderableObject*> renderableObjectsQueue;
std::vector<Serializable*> serializableObjectsQueue;
std::vector<RigidBody*> collidableObjectsQueue;
};