You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

88 lines
2.3 KiB

#include "tower.h"
#include <QPixmap>
#include <QVector>
#include <QPointF>
#include "bullet.h"
#include <QTimer>
#include "game.h"
#include "enemy.h"
extern Game *game;
Tower::Tower(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent) {
setPixmap(QPixmap(":/images/tower.png"));
// create points vectors
QVector<QPointF> points;
points << QPoint(1,0) << QPoint(2,0) << QPoint(3,1) << QPoint(3,2) << QPoint(2,3) << QPoint(1,3) << QPoint(0,2) << QPoint(0,1);
// scale points
int SCALE_FACTOR = 70;
for (size_t i = 0, n = points.size(); i <n; i++) {
points[i] *= SCALE_FACTOR;
}
// create a polygon from these points
QPolygonF polygon(points);
// create the QGraphicsPolygonItem
attack_area = new QGraphicsPolygonItem(QPolygonF(points), this);
// move the polygon
QPointF poly_center(1.5, 1.5);
poly_center *= SCALE_FACTOR;
poly_center = mapToScene(poly_center);
QPointF tower_center(x()+34, y()+64);
QLineF ln(poly_center, tower_center);
attack_area->setPos(x()+ln.dx(), y()+ln.dy());
// connect timer to attack target
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this,SLOT(aquire_target()));
timer->start(1000);
attack_dest = QPointF(800,0);
}
double Tower::distanceTo(QGraphicsItem *item) {
QLineF ln(pos(), item->pos());
return ln.length();
}
void Tower::fire(){
Bullet *bullet = new Bullet();
bullet->setPos(x()+34, y()+64);
QLineF ln(QPointF(x()+34, y()+64), attack_dest);
int angle = -1 * ln.angle();
bullet->setRotation(angle);
game->scene->addItem(bullet);
}
void Tower::aquire_target() {
QList<QGraphicsItem *> colliding_items = attack_area->collidingItems();
if (colliding_items.size() == 1) {
has_target = false;
return;
}
double closest_dist = 300;
QPointF closest_pt = QPointF(0,0);
for (size_t i = 0, n = colliding_items.size(); i<n; i++) {
Enemy *enemy = dynamic_cast<Enemy *>(colliding_items[i]);
if (enemy) {
double this_dist = distanceTo(enemy);
if (this_dist < closest_dist) {
closest_dist = this_dist;
closest_pt = colliding_items[i]->pos();
has_target = true;
}
}
}
attack_dest = closest_pt;
fire();
}