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.

52 lines
1.0 KiB

2 years ago
#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);
}