You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.3 KiB
C++
72 lines
2.3 KiB
C++
/*
|
|
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
|
* its licensors.
|
|
*
|
|
* For complete copyright and license terms please see the LICENSE at the root of this
|
|
* distribution (the "License"). All use of this software is governed by the License,
|
|
* or, if provided, by the license below or the license accompanying this file. Do not
|
|
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
*
|
|
*/
|
|
|
|
#include <StdAfx.h>
|
|
|
|
#include <AzCore/Math/Transform.h>
|
|
#include <Family/ActorTracker.h>
|
|
#include <algorithm>
|
|
|
|
namespace Blast
|
|
{
|
|
void ActorTracker::AddActor(BlastActor* actor)
|
|
{
|
|
m_actors.emplace(actor);
|
|
m_entityIdToActor.emplace(actor->GetEntity()->GetId(), actor);
|
|
m_bodyToActor.emplace(actor->GetWorldBody(), actor);
|
|
}
|
|
|
|
void ActorTracker::RemoveActor(BlastActor* actor)
|
|
{
|
|
m_bodyToActor.erase(actor->GetWorldBody());
|
|
m_entityIdToActor.erase(actor->GetEntity()->GetId());
|
|
m_actors.erase(actor);
|
|
}
|
|
|
|
BlastActor* ActorTracker::GetActorById(AZ::EntityId entityId)
|
|
{
|
|
if (const auto it = m_entityIdToActor.find(entityId); it != m_entityIdToActor.end())
|
|
{
|
|
return it->second;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
BlastActor* ActorTracker::GetActorByBody(const AzPhysics::SimulatedBody* body)
|
|
{
|
|
if (const auto it = m_bodyToActor.find(body); it != m_bodyToActor.end())
|
|
{
|
|
return it->second;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
BlastActor* ActorTracker::FindClosestActor(const AZ::Vector3& position)
|
|
{
|
|
AZ_PROFILE_FUNCTION(AZ::Debug::ProfileCategory::Physics);
|
|
|
|
const auto candidate = std::min_element(
|
|
m_actors.begin(), m_actors.end(),
|
|
[&position](BlastActor* a, BlastActor* b)
|
|
{
|
|
return a->GetTransform().GetTranslation().GetDistanceSq(position) <
|
|
b->GetTransform().GetTranslation().GetDistanceSq(position);
|
|
});
|
|
return candidate == m_actors.end() ? nullptr : *candidate;
|
|
}
|
|
|
|
const AZStd::unordered_set<BlastActor*>& ActorTracker::GetActors() const
|
|
{
|
|
return m_actors;
|
|
}
|
|
} // namespace Blast
|