Skip to content

Instantly share code, notes, and snippets.

@nhatzHK
Last active February 26, 2017 20:20
Show Gist options
  • Save nhatzHK/33a32c0dba01a0e29402db146bb63ad4 to your computer and use it in GitHub Desktop.
Save nhatzHK/33a32c0dba01a0e29402db146bb63ad4 to your computer and use it in GitHub Desktop.
#include "h_ennemy.h"
H_Ennemy::H_Ennemy() {
setRect(0, 0, 50, 10);
collisionList = collidingItems();
QTimer * ennemyTimer = new QTimer();
connect(ennemyTimer, SIGNAL(timeout()), this, SLOT(move()));
ennemyTimer->start(10); //timeout shorter because I update the list in the move slot
}
void H_Ennemy::move() {
setPos(x(), y() + 1); //move less on each call because the method is being called more frequently
if(y() > scene()->height() - rect().height()) { //remove when leaving the view
scene()->removeItem(this);
delete this;
return;
}
collisionList = collidingItems(); //I update the list here
for(qint64 i = 0; i < collisionList.size(); i++) {
if(typeid(*(collisionList[i])) == typeid(H_Bullet)) {//if ennemy touches bullet, remove both bullet and ennemy
emit ((H_Bullet*)collisionList[i])->touched();
scene()->removeItem(collisionList[i]);
scene()->removeItem(this);
delete collisionList[i];
collisionList.clear();
delete this;
} else if(typeid(*(collisionList[i])) == typeid(H_Player)) {//if ennemy touches player, remove ennemy
scene()->removeItem(this);
collisionList.clear();
delete this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment