-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculatethread.cpp
93 lines (88 loc) · 2.91 KB
/
calculatethread.cpp
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
#include "calculatethread.h"
#include <QMutex>
#include <QTimer>
#include <QDebug>
#include <QTime>
#include "player.h"
#include "header.h"
#include "checkpoint.h"
CalculateThread::CalculateThread()
{
}
void CalculateThread::run()
{
QTimer timer;
timer.setTimerType(Qt::PreciseTimer);
connect(&timer, SIGNAL(timeout()), this, SLOT(doCalculations()));
double frame = 1000/constants::FPS_CALCULATION;
timer.start(frame);
exec();
}
void CalculateThread::doCalculations()
{
QMutex mutex;
if(game->getLvl()->getWin()) {
return;
}
if(!game->getIngame()) {
return;
}
if(game->getLvl()->getTerminate()) {
return;
}
if(game->getLvl()->getPlayer()->getIsDead()){
mutex.try_lock();
game->getLvl()->setTerminate(true);
mutex.unlock();
return;
}
game->getLvl()->verifyTime();
int size = game->getLvl()->getNbEntities();
for(int idEntity = 0; idEntity < size; idEntity++){
if(game->getLvl()->getEntity(idEntity)->getIsDead()){
mutex.try_lock();
delete game->getLvl()->getEntity(idEntity);
game->getLvl()->removeEntity(idEntity);
idEntity--;
size--;
mutex.unlock();
}
}
int xPlayer = game->getLvl()->getPlayer()->getHitbox().left();
for(int idEntity = 0; idEntity < size; idEntity++){
int left = game->getLvl()->getEntity(idEntity)->getHitbox().left();
if(left >= xPlayer-(constants::TILE_WIDTH*15) && left <= xPlayer+(constants::TILE_WIDTH*15)){
mutex.try_lock();
game->getLvl()->getEntity(idEntity)->update(game->getLvl());
mutex.unlock();
}
}
for(int idEntity = 0; idEntity < size; idEntity++){
mutex.try_lock();
QVector<LivingEntity *> colliding = game->getLvl()->getEntity(idEntity)->getCollidingEntities(idEntity, game->getLvl());
for(int i=0; i<colliding.size(); i++) {
game->getLvl()->getEntity(idEntity)->collide(colliding[i], game->getLvl());
}
game->getLvl()->getEntity(idEntity)->deathTimer();
mutex.unlock();
}
for(int idEntity = 0; idEntity < size; idEntity++){
mutex.try_lock();
game->getLvl()->getEntity(idEntity)->endTurn();
mutex.unlock();
}
int halfWidth = game->getWidthOrigin()/2;
int center = game->getLvl()->getXWindow()+halfWidth;
xPlayer = game->getLvl()->getPlayer()->getHitbox().left();
if(xPlayer > game->getLvl()->getCheckpoint()->getXCheckpoint()){
mutex.try_lock();
game->getLvl()->getCheckpoint()->setChecked(true);
game->getLvl()->getCheckpoint()->setAnimPos(1);
mutex.unlock();
}
if(xPlayer > center){
mutex.try_lock();
game->getLvl()->setXWindow(xPlayer-halfWidth);
mutex.unlock();
}
}