Georg Spar
2 years ago
8 changed files with 123 additions and 3 deletions
@ -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); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -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
|
After Width: | Height: | Size: 3.0 KiB |
Loading…
Reference in new issue