diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d71df8..ba68acc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ set(PROJECT_SOURCES game.h game.cpp res.qrc bullet.h bullet.cpp + enemy.h enemy.cpp ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) diff --git a/enemy.cpp b/enemy.cpp new file mode 100644 index 0000000..d7e511d --- /dev/null +++ b/enemy.cpp @@ -0,0 +1,51 @@ +#include "enemy.h" +#include + + +Enemy::Enemy(QGraphicsItem *parent) { + //set graphics + setPixmap(QPixmap(":/images/enemy.png")); + + //set points + points << QPointF(200,200) << QPointF(400,200); + point_index = 0; + dest = points[0]; + rotateToPoint(dest); + + QTimer * timer = new QTimer(); + connect(timer, SIGNAL(timeout()), this, SLOT(move_forward())); + timer->start(150); + + +} + +void Enemy::rotateToPoint(QPointF p) { + QLineF ln(pos(), p); + setRotation((-1 * ln.angle())); + +} + +void Enemy::move_forward() { + //if close to dest, rotate to next dest + QLineF ln(pos(), dest); + if (ln.length() < 5) { + point_index++; + if (point_index >= points.size()) { + return; + } + dest = points[point_index]; + rotateToPoint(dest); + } + + int STEP_SIZE = 5; + int theta = rotation(); // degrees + double dy = STEP_SIZE * qSin(qDegreesToRadians(theta)); + double dx = STEP_SIZE * qCos(qDegreesToRadians(theta)); + + setPos(x()+dx, y()+dy); +} + + + + + diff --git a/enemy.h b/enemy.h new file mode 100644 index 0000000..1d1863f --- /dev/null +++ b/enemy.h @@ -0,0 +1,25 @@ +#ifndef ENEMY_H +#define ENEMY_H +#include +#include +#include +#include + + + +class Enemy: public QObject, public QGraphicsPixmapItem { + Q_OBJECT +public: + Enemy(QGraphicsItem *parent=0); + void rotateToPoint(QPointF p); +public slots: + void move_forward(); + +private: + QList points; + QPointF dest; + int point_index; + +}; + +#endif // ENEMY_H diff --git a/enemy.png b/enemy.png new file mode 100644 index 0000000..717e9cc Binary files /dev/null and b/enemy.png differ diff --git a/game.cpp b/game.cpp index d8db7e8..03c6e3a 100644 --- a/game.cpp +++ b/game.cpp @@ -2,6 +2,7 @@ #include #include "tower.h" #include "bullet.h" +#include "enemy.h" Game::Game(): QGraphicsView() { scene = new QGraphicsScene(this); @@ -18,6 +19,10 @@ Game::Game(): QGraphicsView() { setFixedSize(800,600); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + + // create enemy + Enemy *enemy = new Enemy(); + scene->addItem(enemy); } void Game::mousePressEvent(QMouseEvent *event) { diff --git a/res.qrc b/res.qrc index 6ead141..a987762 100644 --- a/res.qrc +++ b/res.qrc @@ -2,6 +2,7 @@ tower.png missile.png + enemy.png diff --git a/tower.cpp b/tower.cpp index d853291..96a394b 100644 --- a/tower.cpp +++ b/tower.cpp @@ -5,6 +5,7 @@ #include "bullet.h" #include #include "game.h" +#include "enemy.h" extern Game *game; @@ -37,13 +38,19 @@ Tower::Tower(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent) { // connect timer to attack target QTimer *timer = new QTimer(); - connect(timer, SIGNAL(timeout()), this,SLOT(attack_target())); + connect(timer, SIGNAL(timeout()), this,SLOT(aquire_target())); timer->start(1000); attack_dest = QPointF(800,0); } -void Tower::attack_target() { +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); @@ -53,3 +60,29 @@ void Tower::attack_target() { bullet->setRotation(angle); game->scene->addItem(bullet); } + +void Tower::aquire_target() { + + QList 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(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(); +} diff --git a/tower.h b/tower.h index 9642e74..649a89b 100644 --- a/tower.h +++ b/tower.h @@ -10,11 +10,15 @@ class Tower : public QObject, public QGraphicsPixmapItem { Q_OBJECT public: Tower(QGraphicsItem *parent=0); + double distanceTo(QGraphicsItem *item); + void fire(); public slots: - void attack_target(); + + void aquire_target(); private: QGraphicsPolygonItem *attack_area; QPointF attack_dest; + bool has_target; }; #endif // TOWER_H