ICT290 / src / scene / AIController / Attacks / TestProjectile.cpp
TestProjectile.cpp
Raw
//
// Created by Micha on 20/09/2021.
//

#include "TestProjectile.h"
#include "../Messaging/ArcanistMessaging.h"
#include "../Messaging/MessageDispatcher.h"
#include "../Utils/WallIntersectionTests.h"
#include "../Vehicle.h"
#include "Projectile.h"
//-------------------------- ctor ---------------------------------------------
//-----------------------------------------------------------------------------

TestProjectile::TestProjectile(Vehicle* shooter, glm::vec2 target)
    : Projectile(target,
                 shooter->getGameWorld(),
                 shooter->getID(),
                 shooter->getPosition(),
                 shooter->getHeadingVec(),
                 25,     // damage
                 0.15f,  // scale
                 5.f,    // maxspeed
                 1.1f,   // mass
                 10.0f,  // maxforce
                 0.0f)   // maxturn
/*
                 script->GetInt("Slug_Damage"),
                 script->GetDouble("Slug_Scale"),
                 script->GetDouble("Slug_MaxSpeed"),
                 script->GetDouble("Slug_Mass"),
                 script->GetDouble("Slug_MaxForce"));
*/
// m_TimeShotIsVisible = 10000;    //Uint32
// m_dTimeShotIsVisible(script->GetDouble("Slug_Persistance"))
{}

void TestProjectile::update(float deltaTime) {
    if (!hasImpacted() && isVisibleToPlayer()) {
        // calculate the steering force
        glm::vec2 DesiredVelocity = glm::normalize(m_Target - m_Origin)
                                    * getMaxSpeed();

        glm::vec2 sf = DesiredVelocity - m_VelocityVec;

        // update the position
        glm::vec2 accel = sf / m_mass;

        m_VelocityVec += accel;

        // make sure the slug does not exceed maximum velocity
        // get length difference and scale accordingly
        // float length = glm::length(m_VelocityVec);
        if (glm::length(m_VelocityVec) > m_MaxSpeed) {
            float speedScalar = m_MaxSpeed / glm::length(m_VelocityVec);
            m_VelocityVec = m_VelocityVec * speedScalar;
        }

        // update the position
        m_Position += m_VelocityVec * deltaTime;

        testForImpact();
    } else {
        m_Dead = true;
    }
}

//----------------------------------- TestForImpact ---------------------------
void TestProjectile::testForImpact() {
    Vehicle* hitBot = getHitBot();

    if (hitBot) {
        m_Impacted = true;

        Dispatcher->DispatchMsg(SEND_MSG_IMMEDIATELY,
                                m_ShooterID,
                                hitBot->getID(),
                                Msg_YouGotHit,
                                (void*)&m_DamageInflicted);
    }
    // float distance;
    //  if (FindClosestPointOfIntersectionWithWalls(m_Position - m_VelocityVec,
    //                                              m_Position,
    //                                              distance,
    //                                              m_ImpactPoint,
    //                                              m_World->currentLevel
    //                                                  .getWalls())) {
    //      m_Impacted = true;
    //      m_Position = m_ImpactPoint;
    //  }

    glm::vec2 intersectPoint;
    float distToThisIP{0};

    // TODO MOVE TO FUNCTIONS AND PERFORM CHECKS FOR ENTITIES AND WALLS RELATIVE
    // TO THE ROOM ITS IN
    //  Test for entity collision
    auto entities = m_World->currentLevel.getAllObstacles();
    for (auto& entity : entities) {
        if (glm::abs(m_Position.x - entity->getPosition().x)
                < entity->getBoundingRadius()
            && glm::abs(m_Position.y - entity->getPosition().y)
                   < entity->getBoundingRadius()) {
            m_Impacted = true;
            break;
        }
    }

    // Test for wall collision
    auto walls = m_World->currentLevel.getAllWalls();
    for (auto& wall : walls) {
        if (LineSegmentIntersections(m_Position,             // line origin
                                     m_Position + 0.1f,      // line end
                                     wall.getOriginPoint(),  // wall origin
                                     wall.getEndPoint(),
                                     intersectPoint,
                                     distToThisIP)) {
            if (wall.getOriginPoint().x == wall.getEndPoint().x) {
                if (intersectPoint.y <= wall.getEndPoint().y
                    && intersectPoint.y >= wall.getOriginPoint().y) {
                    m_Impacted = true;
                    break;
                }
            } else {
                if (intersectPoint.x <= wall.getEndPoint().x
                    && intersectPoint.x >= wall.getOriginPoint().x) {
                    m_Impacted = true;
                    break;
                }
            }
        }
    }
}

/*
 *    glm::vec2 positionToTarget = m_Target - m_Position;
    float facing = dot(glm::normalize(positionToTarget),
 m_OriginToTargetNormal); if(glm::length((positionToTarget)) <
 glm::length(m_VelocityVec) || facing < 0){ m_Impacted = true; m_Dead =
 true;
    }



    //test to see if the ray between the current position of the slug and
    //the start position intersects with any bots.
    std::list<Vehicle*> hits = getListOfIntersectingBots(m_Origin,
 m_Position);

    */