[deploy] add Actor::GetPhysicBody

This commit is contained in:
Nomango 2020-06-22 00:57:43 +08:00
parent 2efd06ade3
commit 0651cd1f74
5 changed files with 34 additions and 31 deletions

View File

@ -50,25 +50,6 @@ PhysicBodyPtr PhysicBody::Create(PhysicWorld* world, Type type)
return nullptr;
}
PhysicBody* PhysicBody::Get(Actor* actor)
{
if (actor)
{
static size_t physic_comp_name_hash = 0;
if (physic_comp_name_hash == 0)
{
physic_comp_name_hash = std::hash<String>{}(KGE_PHYSIC_COMP_NAME);
}
return (PhysicBody*)actor->GetComponent(physic_comp_name_hash);
}
return nullptr;
}
PhysicBody* PhysicBody::Get(ActorPtr actor)
{
return PhysicBody::Get(actor.Get());
}
PhysicBody::PhysicBody()
: body_(nullptr)
, world_(nullptr)
@ -86,11 +67,15 @@ void PhysicBody::InitComponent(Actor* actor)
{
Component::InitComponent(actor);
actor->SetPhysicBody(this);
UpdateFromActor(actor);
}
void PhysicBody::DestroyComponent()
{
GetBoundActor()->SetPhysicBody(nullptr);
// Detach from actor first
Component::DestroyComponent();

View File

@ -62,16 +62,6 @@ public:
/// @param type ÎïÌåÀàÐÍ
static PhysicBodyPtr Create(PhysicWorld* world, Type type);
/// \~chinese
/// @brief 获取角色的物理身体
/// @param actor 角色
static PhysicBody* Get(Actor* actor);
/// \~chinese
/// @brief 获取角色的物理身体
/// @param actor 角色
static PhysicBody* Get(ActorPtr actor);
PhysicBody();
virtual ~PhysicBody();

View File

@ -413,7 +413,7 @@ void PhysicWorld::BeforeSimulation(Actor* parent, const Matrix3x2& parent_to_wor
{
Matrix3x2 child_to_world = child->GetTransformMatrixToParent() * parent_to_world;
PhysicBody* body = PhysicBody::Get(child);
PhysicBody* body = child->GetPhysicBody();
if (body)
{
body->BeforeSimulation(child.Get(), parent_to_world, child_to_world, parent_rotation);
@ -428,7 +428,7 @@ void PhysicWorld::AfterSimulation(Actor* parent, const Matrix3x2& parent_to_worl
{
for (auto child : parent->GetAllChildren())
{
PhysicBody* body = PhysicBody::Get(child);
PhysicBody* body = child->GetPhysicBody();
if (body)
{
body->AfterSimulation(child.Get(), parent_to_world, parent_rotation);

View File

@ -62,6 +62,7 @@ Actor::Actor()
, evt_dispatch_enabled_(true)
, parent_(nullptr)
, stage_(nullptr)
, physic_body_(nullptr)
, hash_name_(0)
, z_order_(0)
, opacity_(1.f)

View File

@ -33,6 +33,11 @@ class Stage;
class Director;
class RenderContext;
namespace physics
{
class PhysicBody;
}
KGE_DECLARE_SMART_PTR(Actor);
/// \~chinese
@ -417,6 +422,10 @@ public:
/// @param enabled 是否开启
void SetEventDispatchEnabled(bool enabled);
/// \~chinese
/// @brief 获取物理身体
physics::PhysicBody* GetPhysicBody() const;
/// \~chinese
/// @brief 序列化
void DoSerialize(Serializer* serializer) const override;
@ -470,6 +479,12 @@ protected:
/// @brief 处理事件
bool HandleEvent(Event* evt);
/// \~chinese
/// @brief 设置物理身体
void SetPhysicBody(physics::PhysicBody* body);
friend physics::PhysicBody;
private:
bool visible_;
bool update_pausing_;
@ -491,6 +506,8 @@ private:
UpdateCallback cb_update_;
Transform transform_;
physics::PhysicBody* physic_body_;
bool is_fast_transform_;
mutable bool visible_in_rt_;
mutable bool dirty_visibility_;
@ -748,4 +765,14 @@ inline void Actor::SetSkew(float skewx, float skewy)
this->SetSkew(Vec2(skewx, skewy));
}
inline physics::PhysicBody* Actor::GetPhysicBody() const
{
return physic_body_;
}
inline void Actor::SetPhysicBody(physics::PhysicBody* body)
{
physic_body_ = body;
}
} // namespace kiwano