//
// Created by jovian on 18/07/17.
//

#include <iostream>
#include "Entity.h"


Entity::Entity(Faction faction, unsigned int imgId, b2World *physics)
        : m_faction(faction), m_exist(true), m_physics(physics), m_body(nullptr), m_imgId(imgId) {

}

Entity::~Entity() {
    if (m_body != nullptr)
        m_physics->DestroyBody(m_body);
}

Visual *Entity::makeVisual() {
    return new Visual(m_imgId, m_body->GetPosition(), m_body->GetAngle());
}

bool Entity::isExist() const {
    return m_exist;
}

bool Entity::isTouching() const {
    bool touch(false);

    for (b2ContactEdge *ce(m_body->GetContactList()); ce && !touch; ce = ce->next)
        touch = ce->contact->IsTouching();

    return touch;
}

b2Vec2 Entity::getPos() const {
    if (m_body == nullptr)
        return b2Vec2(0.0f, 0.0f);
    else
        return m_body->GetPosition();
}

void Entity::establishPhysicalLink() {
    if (m_body == nullptr) {
        std::cout << "Entity::establishPhysicalLink() > Body is invalid for a " << m_faction << " object." << std::endl;
        return;
    } else {
        m_body->GetUserData().pointer = (uintptr_t) this;
    }
}

Faction Entity::getFaction() const {
    return m_faction;
}

void Entity::setExistence(bool exist) {
    Entity::m_exist = exist;
}