ICT290 / src / scene / AIController / EntityBrain / TargetingSystem.cpp
TargetingSystem.cpp
Raw
//
// Created by Micha on 27/09/2021.
//

#include "TargetingSystem.h"
#include "../Vehicle.h"
#include "SensoryMemory.h"

//-------------------------------- ctor ---------------------------------------
//-----------------------------------------------------------------------------
TargetingSystem::TargetingSystem(Vehicle* owner)
    : m_Owner(owner),
      m_CurrentTarget(0) {}

//----------------------------- Update ----------------------------------------

//-----------------------------------------------------------------------------
void TargetingSystem::update() {
    float ClosestDistSoFar = (std::numeric_limits<float>::max)();
    m_CurrentTarget = 0;

    // grab a list of all the opponents the owner can sense
    std::list<Vehicle*> SensedBots;
    // sensing isnt working right now, out for testing
    SensedBots = m_Owner->getSensoryMem()->getListOfRecentlySensedOpponents();
    // SensedBots = m_Owner->getGameWorld()->getAllBots();
    for (auto& curBot : SensedBots) {
        // make sure the bot is alive and that it is not the owner
        // TODO MICHAEL: CHANGED SO THAT IT ONLY ATTACKS THE PLAYER (TEMP
        // SOLUTION)
        if (curBot->isAlive() && curBot != m_Owner && curBot->isPossessed()) {
            float dist = glm::length(curBot->getPosition()
                                     - m_Owner->getPosition());

            if (dist < ClosestDistSoFar) {
                ClosestDistSoFar = dist;
                m_CurrentTarget = curBot;
            }
        }
    }
}

bool TargetingSystem::isTargetWithinFOV() const {
    return m_Owner->getSensoryMem()->isOpponentWithinFOV(m_CurrentTarget);
}

bool TargetingSystem::isTargetShootable() const {
    return m_Owner->getSensoryMem()->isOpponentShootable(m_CurrentTarget);
}

glm::vec2 TargetingSystem::getLastRecordedPosition() const {
    return m_Owner->getSensoryMem()->getLastRecordedPositionOfOpponent(
        m_CurrentTarget);
}

float TargetingSystem::getTimeTargetHasBeenVisible() const {
    return (float)m_Owner->getSensoryMem()->getTimeOpponentHasBeenVisible(
        m_CurrentTarget);
}

float TargetingSystem::getTimeTargetHasBeenOutOfView() const {
    return (float)m_Owner->getSensoryMem()->getTimeOpponentHasBeenOutOfView(
        m_CurrentTarget);
}