ICT290 / src / scene / AIController / Goals / GoalAttackTarget.cpp
GoalAttackTarget.cpp
Raw
#include "GoalAttackTarget.h"
#include "GoalDodgeSideToSide.h"
#include "GoalHuntTarget.h"
#include "GoalSeekToPosition.h"

//------------------------------- Activate ------------------------------------
//-----------------------------------------------------------------------------
void GoalAttackTarget::Activate() {
    m_Status = active;

    // if this goal is reactivated then there may be some existing subgoals that
    // must be removed
    removeAllSubgoals();

    // it is possible for a bot's target to die whilst this goal is active so we
    // must test to make sure the bot always has an active target
    if (!m_Owner->getTargetSys()->isTargetPresent()) {
        m_Status = completed;

        return;
    }

    // if the bot is able to shoot the target (there is LOS between bot and
    // target), then select a tactic to follow while shooting
    if (m_Owner->getTargetSys()->isTargetShootable()) {
        // if the bot has space to strafe then do so
        glm::vec2 dummy;
        if (m_Owner->canStepLeft(dummy) || m_Owner->canStepRight(dummy)) {
            addSubgoal(new GoalDodgeSideToSide(m_Owner));
            // std::cout << "Bot " << m_Owner->getID() << " can dodge" <<
            // std::endl;
        }

        // if not able to strafe, head directly at the target's position
        else {
            addSubgoal(
                new GoalSeekToPosition(m_Owner,
                                       m_Owner->getTargetBot()->getPosition()));
        }
    }

    // if the target is not visible, go hunt it.
    else {
        addSubgoal(new GoalHuntTarget(m_Owner));
    }
}

//-------------------------- Process ------------------------------------------
//-----------------------------------------------------------------------------
int GoalAttackTarget::Process() {
    // if status is inactive, call Activate()
    activateIfInactive();

    // process the subgoals
    m_Status = processSubgoals();

    reactivateIfFailed();

    return m_Status;
}