#ifndef WORKER_H
#define WORKER_H
#include <QObject>
#include "gamescene.h"
#include "troop.h"
#include "colldetect.h"
Q_OBJECT
public:
Worker
(GameScene
* sc,
QPoint p,
int x,
int y,
int ut,
int pnum
);
~Worker();
public slots:
void process();
signals:
void finished(Troop* t);
void completed();
private:
GameScene* gs;
CollDetect* cd;
int UserT;
int playernum;
};
#endif // WORKER_H
//Worker.cpp file
#include "worker.h"
#include <QDebug>
Worker
::Worker(GameScene
* sc,
QPoint p,
int x,
int y,
int ut,
int pnum
){
gs = sc;
cd = new CollDetect(p, x, y);
UserT = ut;
playernum = pnum;
}
Worker::~Worker()
{
}
void Worker::process()
{
gs->addItem(cd);
QList<QGraphicsItem*> gi = cd->collidingItems(Qt::IntersectsItemBoundingRect);
//qDebug() << "collisions: " << gi.size();
for (int i = 0; i < gi.size(); i++)
{
if (gi.at(i)->type() == UserT)
{
if (dynamic_cast<Troop*>(gi.at(i))->playernum != playernum)
{
qDebug() << "troop found";
emit finished(dynamic_cast<Troop*>(gi.at(i)));
gs->removeItem(cd);
emit completed();
}
}
}
emit finished(NULL);
gs->removeItem(cd);
emit completed();
}
//In the Troop class advance(step) function
img.load(image);
Worker
* worker
= new Worker
(myscene,
QPoint(this
->scenePos
().
x(),this
->scenePos
().
y()), img.
width(), img.
height(), UserType
+1, playernum
);
worker->moveToThread(thread);
connect(thread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(completed()), thread, SLOT(quit()));
connect(worker, SIGNAL(completed()), worker, SLOT(deleteLater()));
connect(worker, SIGNAL(finished(Troop*)), this, SLOT(settarget(Troop*)));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
#include "gamescene.h"
#include "troop.h"
#include "colldetect.h"
class Worker : public QObject {
Q_OBJECT
public:
Worker(GameScene* sc, QPoint p, int x, int y, int ut, int pnum);
~Worker();
public slots:
void process();
signals:
void finished(Troop* t);
void completed();
private:
GameScene* gs;
CollDetect* cd;
int UserT;
int playernum;
};
#endif // WORKER_H
//Worker.cpp file
#include "worker.h"
#include <QDebug>
Worker::Worker(GameScene* sc, QPoint p, int x, int y, int ut, int pnum)
{
gs = sc;
cd = new CollDetect(p, x, y);
UserT = ut;
playernum = pnum;
}
Worker::~Worker()
{
}
void Worker::process()
{
gs->addItem(cd);
QList<QGraphicsItem*> gi = cd->collidingItems(Qt::IntersectsItemBoundingRect);
//qDebug() << "collisions: " << gi.size();
for (int i = 0; i < gi.size(); i++)
{
if (gi.at(i)->type() == UserT)
{
if (dynamic_cast<Troop*>(gi.at(i))->playernum != playernum)
{
qDebug() << "troop found";
emit finished(dynamic_cast<Troop*>(gi.at(i)));
gs->removeItem(cd);
emit completed();
}
}
}
emit finished(NULL);
gs->removeItem(cd);
emit completed();
}
//In the Troop class advance(step) function
QImage img;
img.load(image);
QThread* thread = new QThread;
Worker* worker = new Worker(myscene, QPoint(this->scenePos().x(),this->scenePos().y()), img.width(), img.height(), UserType+1, playernum);
worker->moveToThread(thread);
connect(thread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(completed()), thread, SLOT(quit()));
connect(worker, SIGNAL(completed()), worker, SLOT(deleteLater()));
connect(worker, SIGNAL(finished(Troop*)), this, SLOT(settarget(Troop*)));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
To copy to clipboard, switch view to plain text mode
Bookmarks