diff --git a/src/kiwano-physics/PhysicBody.cpp b/src/kiwano-physics/PhysicBody.cpp index e74675f0..6652b5d6 100644 --- a/src/kiwano-physics/PhysicBody.cpp +++ b/src/kiwano-physics/PhysicBody.cpp @@ -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{}(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(); diff --git a/src/kiwano-physics/PhysicBody.h b/src/kiwano-physics/PhysicBody.h index 638fd3ad..875e4a8a 100644 --- a/src/kiwano-physics/PhysicBody.h +++ b/src/kiwano-physics/PhysicBody.h @@ -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(); diff --git a/src/kiwano-physics/PhysicWorld.cpp b/src/kiwano-physics/PhysicWorld.cpp index d8e447ab..e97384b7 100644 --- a/src/kiwano-physics/PhysicWorld.cpp +++ b/src/kiwano-physics/PhysicWorld.cpp @@ -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); diff --git a/src/kiwano/2d/Actor.cpp b/src/kiwano/2d/Actor.cpp index 62324ee1..5f6c6c84 100644 --- a/src/kiwano/2d/Actor.cpp +++ b/src/kiwano/2d/Actor.cpp @@ -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) diff --git a/src/kiwano/2d/Actor.h b/src/kiwano/2d/Actor.h index 1af54468..faccb66f 100644 --- a/src/kiwano/2d/Actor.h +++ b/src/kiwano/2d/Actor.h @@ -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