#include "proyectil.h"
#include "zombiesimple.h"

proyectil::proyectil(QPixmap pixmap, int x, int y, bool direction)
{
    setPixmap(pixmap);
    int dx = (direction)?0:boundingRect().width();
    setPos(QPointF(0,y));
    xAnimation = new QPropertyAnimation(this,"x",this);
    xAnimation->setStartValue(x-dx);
    if(direction)xAnimation->setEndValue(x+dx+250);
    else xAnimation->setEndValue(x-dx-250);
    xAnimation->setEasingCurve(QEasingCurve::Linear);
    xAnimation->setDuration(700);
    xAnimation->start();
    connect(xAnimation,&QPropertyAnimation::finished,this,[=](){
        emit xAnimationFinished();
    });
}

proyectil::~proyectil()
{
    delete xAnimation;
}

qreal proyectil::x() const
{
    return m_x;
}

void proyectil::setX(qreal newX)
{
    moveBy(newX-m_x,0);
    m_x = newX;
    checkZombieColision();
}

void proyectil::checkZombieColision()
{
    QList<QGraphicsItem*> collidingItems = this->collidingItems();
    foreach(QGraphicsItem *item, collidingItems){
        zombieSimple * zombie = dynamic_cast<zombieSimple*>(item);
        if(zombie){
            emit detectedZombieColision();
        }
    }
}
