Browse Source

Tut 12

master
Georg Spar 2 years ago
parent
commit
6c564841bf
  1. 1
      CMakeLists.txt
  2. 51
      enemy.cpp
  3. 25
      enemy.h
  4. BIN
      enemy.png
  5. 5
      game.cpp
  6. 1
      res.qrc
  7. 37
      tower.cpp
  8. 6
      tower.h

1
CMakeLists.txt

@ -22,6 +22,7 @@ set(PROJECT_SOURCES
game.h game.cpp game.h game.cpp
res.qrc res.qrc
bullet.h bullet.cpp bullet.h bullet.cpp
enemy.h enemy.cpp
) )
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)

51
enemy.cpp

@ -0,0 +1,51 @@
#include "enemy.h"
#include <QTimer>
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);
}

25
enemy.h

@ -0,0 +1,25 @@
#ifndef ENEMY_H
#define ENEMY_H
#include <QGraphicsPixmapItem>
#include <QObject>
#include <QList>
#include <QPointF>
class Enemy: public QObject, public QGraphicsPixmapItem {
Q_OBJECT
public:
Enemy(QGraphicsItem *parent=0);
void rotateToPoint(QPointF p);
public slots:
void move_forward();
private:
QList<QPointF> points;
QPointF dest;
int point_index;
};
#endif // ENEMY_H

BIN
enemy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

5
game.cpp

@ -2,6 +2,7 @@
#include <QGraphicsScene> #include <QGraphicsScene>
#include "tower.h" #include "tower.h"
#include "bullet.h" #include "bullet.h"
#include "enemy.h"
Game::Game(): QGraphicsView() { Game::Game(): QGraphicsView() {
scene = new QGraphicsScene(this); scene = new QGraphicsScene(this);
@ -18,6 +19,10 @@ Game::Game(): QGraphicsView() {
setFixedSize(800,600); setFixedSize(800,600);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// create enemy
Enemy *enemy = new Enemy();
scene->addItem(enemy);
} }
void Game::mousePressEvent(QMouseEvent *event) { void Game::mousePressEvent(QMouseEvent *event) {

1
res.qrc

@ -2,6 +2,7 @@
<qresource prefix="/images"> <qresource prefix="/images">
<file>tower.png</file> <file>tower.png</file>
<file>missile.png</file> <file>missile.png</file>
<file>enemy.png</file>
</qresource> </qresource>
<qresource prefix="/"/> <qresource prefix="/"/>
</RCC> </RCC>

37
tower.cpp

@ -5,6 +5,7 @@
#include "bullet.h" #include "bullet.h"
#include <QTimer> #include <QTimer>
#include "game.h" #include "game.h"
#include "enemy.h"
extern Game *game; extern Game *game;
@ -37,13 +38,19 @@ Tower::Tower(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent) {
// connect timer to attack target // connect timer to attack target
QTimer *timer = new QTimer(); QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this,SLOT(attack_target())); connect(timer, SIGNAL(timeout()), this,SLOT(aquire_target()));
timer->start(1000); timer->start(1000);
attack_dest = QPointF(800,0); 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 *bullet = new Bullet();
bullet->setPos(x()+34, y()+64); bullet->setPos(x()+34, y()+64);
@ -53,3 +60,29 @@ void Tower::attack_target() {
bullet->setRotation(angle); bullet->setRotation(angle);
game->scene->addItem(bullet); 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();
}

6
tower.h

@ -10,11 +10,15 @@ class Tower : public QObject, public QGraphicsPixmapItem {
Q_OBJECT Q_OBJECT
public: public:
Tower(QGraphicsItem *parent=0); Tower(QGraphicsItem *parent=0);
double distanceTo(QGraphicsItem *item);
void fire();
public slots: public slots:
void attack_target();
void aquire_target();
private: private:
QGraphicsPolygonItem *attack_area; QGraphicsPolygonItem *attack_area;
QPointF attack_dest; QPointF attack_dest;
bool has_target;
}; };
#endif // TOWER_H #endif // TOWER_H

Loading…
Cancel
Save