From 209c80ccbb3d1201b4b17feaf91c86d87e9c963d Mon Sep 17 00:00:00 2001 From: Georg Spar Date: Sat, 8 Oct 2022 18:46:27 +0200 Subject: [PATCH] Tut 11 --- CMakeLists.txt | 4 ++++ game.cpp | 3 ++- main.cpp | 4 ++-- tower.cpp | 24 +++++++++++++++++++++++- tower.h | 8 +++++++- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b256c7e..6d71df8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) +find_package(Qt6 REQUIRED COMPONENTS Multimedia) +find_package(Qt6 REQUIRED COMPONENTS Gui) set(PROJECT_SOURCES main.cpp @@ -46,6 +48,8 @@ else() endif() target_link_libraries(TowerDefense PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) +target_link_libraries(TowerDefense PRIVATE Qt6::Multimedia) +target_link_libraries(TowerDefense PRIVATE Qt6::Gui) set_target_properties(TowerDefense PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com diff --git a/game.cpp b/game.cpp index d60b66e..d8db7e8 100644 --- a/game.cpp +++ b/game.cpp @@ -3,13 +3,14 @@ #include "tower.h" #include "bullet.h" -Game::Game() { +Game::Game(): QGraphicsView() { scene = new QGraphicsScene(this); scene->setSceneRect(0,0,800,600); setScene(scene); // create a tower Tower *t = new Tower(); + t->setPos(250,250); // add tower scene->addItem(t); diff --git a/main.cpp b/main.cpp index bec84d9..491388b 100644 --- a/main.cpp +++ b/main.cpp @@ -2,13 +2,13 @@ #include - +Game *game; int main(int argc, char *argv[]) { QApplication a(argc, argv); - Game *game = new Game(); + game = new Game(); game->show(); return a.exec(); diff --git a/tower.cpp b/tower.cpp index 25fdcd2..d853291 100644 --- a/tower.cpp +++ b/tower.cpp @@ -2,8 +2,13 @@ #include #include #include +#include "bullet.h" +#include +#include "game.h" -Tower::Tower(QGraphicsItem *parent) { +extern Game *game; + +Tower::Tower(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent) { setPixmap(QPixmap(":/images/tower.png")); // create points vectors @@ -30,4 +35,21 @@ Tower::Tower(QGraphicsItem *parent) { 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(attack_target())); + timer->start(1000); + + attack_dest = QPointF(800,0); +} + +void Tower::attack_target() { + 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); } diff --git a/tower.h b/tower.h index 5e002ce..9642e74 100644 --- a/tower.h +++ b/tower.h @@ -3,12 +3,18 @@ #include #include +#include +#include -class Tower : public QGraphicsPixmapItem { +class Tower : public QObject, public QGraphicsPixmapItem { + Q_OBJECT public: Tower(QGraphicsItem *parent=0); +public slots: + void attack_target(); private: QGraphicsPolygonItem *attack_area; + QPointF attack_dest; }; #endif // TOWER_H