From 0f3b5ca47321d10350c67d86b218c1d39d9a78ba Mon Sep 17 00:00:00 2001 From: Nomango Date: Sun, 21 Jun 2020 18:02:33 +0800 Subject: [PATCH] update ComponentManager & remove Actor::GetPhysicBody --- src/kiwano-physics/PhysicBody.cpp | 25 +++- src/kiwano-physics/PhysicBody.h | 10 ++ src/kiwano-physics/PhysicWorld.cpp | 4 +- src/kiwano/2d/Actor.cpp | 1 - src/kiwano/2d/Actor.h | 25 ---- src/kiwano/base/component/Button.cpp | 2 +- src/kiwano/base/component/Component.h | 3 - .../base/component/ComponentManager.cpp | 113 ++++++++++-------- src/kiwano/base/component/ComponentManager.h | 29 +++-- 9 files changed, 120 insertions(+), 92 deletions(-) diff --git a/src/kiwano-physics/PhysicBody.cpp b/src/kiwano-physics/PhysicBody.cpp index b10f8fd2..e74675f0 100644 --- a/src/kiwano-physics/PhysicBody.cpp +++ b/src/kiwano-physics/PhysicBody.cpp @@ -21,6 +21,8 @@ #include #include +#define KGE_PHYSIC_COMP_NAME "__KGE_PHYSIC_BODY__" + namespace kiwano { namespace physics @@ -48,6 +50,25 @@ 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) @@ -56,7 +77,7 @@ PhysicBody::PhysicBody() , mask_bits_(0xFFFF) , group_index_(0) { - SetName("KGE_PHYSIC_BODY"); + SetName(KGE_PHYSIC_COMP_NAME); } PhysicBody::~PhysicBody() {} @@ -65,8 +86,6 @@ void PhysicBody::InitComponent(Actor* actor) { Component::InitComponent(actor); - actor->SetPhysicBody(this); - UpdateFromActor(actor); } diff --git a/src/kiwano-physics/PhysicBody.h b/src/kiwano-physics/PhysicBody.h index 875e4a8a..638fd3ad 100644 --- a/src/kiwano-physics/PhysicBody.h +++ b/src/kiwano-physics/PhysicBody.h @@ -62,6 +62,16 @@ 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 e97384b7..d8e447ab 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 = child->GetPhysicBody(); + PhysicBody* body = PhysicBody::Get(child); 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 = child->GetPhysicBody(); + PhysicBody* body = PhysicBody::Get(child); 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 5f54647c..f206e223 100644 --- a/src/kiwano/2d/Actor.cpp +++ b/src/kiwano/2d/Actor.cpp @@ -66,7 +66,6 @@ Actor::Actor() , opacity_(1.f) , displayed_opacity_(1.f) , anchor_(default_anchor_x, default_anchor_y) - , physic_body_(nullptr) { } diff --git a/src/kiwano/2d/Actor.h b/src/kiwano/2d/Actor.h index f0e258a0..c9da9662 100644 --- a/src/kiwano/2d/Actor.h +++ b/src/kiwano/2d/Actor.h @@ -33,11 +33,6 @@ class Stage; class Director; class RenderContext; -namespace physics -{ -class PhysicBody; -} - KGE_DECLARE_SMART_PTR(Actor); /// \~chinese @@ -391,14 +386,6 @@ public: /// @brief 获取更新时的回调函数 UpdateCallback GetCallbackOnUpdate() const; - /// \~chinese - /// @brief 获取物理身体,仅当kiwano-physics包启用时生效 - physics::PhysicBody* GetPhysicBody() const; - - /// \~chinese - /// @brief 设置物理身体,仅当kiwano-physics包启用时生效 - void SetPhysicBody(physics::PhysicBody* body); - /// \~chinese /// @brief 判断点是否在角色内 virtual bool ContainsPoint(const Point& point) const; @@ -502,8 +489,6 @@ private: mutable Matrix3x2 transform_matrix_; mutable Matrix3x2 transform_matrix_inverse_; mutable Matrix3x2 transform_matrix_to_parent_; - - physics::PhysicBody* physic_body_; }; /** @} */ @@ -688,16 +673,6 @@ inline Actor::UpdateCallback Actor::GetCallbackOnUpdate() const return cb_update_; } -inline physics::PhysicBody* Actor::GetPhysicBody() const -{ - return physic_body_; -} - -inline void Actor::SetPhysicBody(physics::PhysicBody* body) -{ - physic_body_ = body; -} - inline void Actor::ShowBorder(bool show) { show_border_ = show; diff --git a/src/kiwano/base/component/Button.cpp b/src/kiwano/base/component/Button.cpp index a8150917..c8fbc49b 100644 --- a/src/kiwano/base/component/Button.cpp +++ b/src/kiwano/base/component/Button.cpp @@ -47,7 +47,7 @@ ButtonPtr Button::Create(const Callback& click, const Callback& pressed, const C Button::Button() : status_(Status::Normal) { - SetName("KGE_BUTTON"); + SetName("__KGE_BUTTON__"); } Button::~Button() diff --git a/src/kiwano/base/component/Component.h b/src/kiwano/base/component/Component.h index c48a168a..ccd31895 100644 --- a/src/kiwano/base/component/Component.h +++ b/src/kiwano/base/component/Component.h @@ -21,7 +21,6 @@ #pragma once #include #include -#include #include namespace kiwano @@ -49,10 +48,8 @@ KGE_DECLARE_SMART_PTR(Component); */ class KGE_API Component : public ObjectBase - , protected IntrusiveListValue { friend class ComponentManager; - friend IntrusiveList; public: /// \~chinese diff --git a/src/kiwano/base/component/ComponentManager.cpp b/src/kiwano/base/component/ComponentManager.cpp index 8f99ce75..5a77ec6b 100644 --- a/src/kiwano/base/component/ComponentManager.cpp +++ b/src/kiwano/base/component/ComponentManager.cpp @@ -19,6 +19,7 @@ // THE SOFTWARE. #include +#include namespace kiwano { @@ -35,45 +36,62 @@ Component* ComponentManager::AddComponent(ComponentPtr component) if (component) { component->InitComponent(target_); - components_.PushBack(component); + + size_t hash = std::hash{}(component->GetName()); + components_.insert(std::make_pair(hash, component)); } return component.Get(); } -ComponentList& ComponentManager::GetAllComponents() +Component* ComponentManager::GetComponent(const String& name) +{ + size_t hash = std::hash{}(name); + return GetComponent(hash); +} + +Component* ComponentManager::GetComponent(size_t name_hash) +{ + if (!components_.empty()) + { + auto iter = components_.find(name_hash); + if (iter != components_.end()) + { + return iter->second.Get(); + } + } + return nullptr; +} + +ComponentMap& ComponentManager::GetAllComponents() { return components_; } -const ComponentList& ComponentManager::GetAllComponents() const +const ComponentMap& ComponentManager::GetAllComponents() const { return components_; } void ComponentManager::RemoveComponent(ComponentPtr component) { - auto iter = std::find(components_.begin(), components_.end(), component); - if (iter != components_.end()) - { - component->DestroyComponent(); - components_.Remove(component); - } + RemoveComponent(component->GetName()); } -void ComponentManager::RemoveComponents(const String& name) +void ComponentManager::RemoveComponent(const String& name) { - if (!components_.IsEmpty()) - { - ComponentPtr next; - for (auto component = components_.GetFirst(); component; component = next) - { - next = component->GetNext(); + size_t hash = std::hash{}(name); + RemoveComponent(hash); +} - if (component->IsName(name)) - { - component->DestroyComponent(); - components_.Remove(component); - } +void ComponentManager::RemoveComponent(size_t name_hash) +{ + if (!components_.empty()) + { + auto iter = components_.find(name_hash); + if (iter != components_.end()) + { + iter->second->DestroyComponent(); + components_.erase(iter); } } } @@ -81,31 +99,28 @@ void ComponentManager::RemoveComponents(const String& name) void ComponentManager::RemoveAllComponents() { // Destroy all components - if (!components_.IsEmpty()) + if (!components_.empty()) { - ComponentPtr next; - for (auto component = components_.GetFirst(); component; component = next) + for (auto& p : components_) { - next = component->GetNext(); - - component->DestroyComponent(); + p.second->DestroyComponent(); } } - components_.Clear(); + components_.clear(); } void ComponentManager::Update(Duration dt) { - if (!components_.IsEmpty()) + if (!components_.empty()) { - ComponentPtr next; - for (auto component = components_.GetFirst(); component; component = next) + if (!components_.empty()) { - next = component->GetNext(); - - if (component->IsEnable()) + for (auto& p : components_) { - component->OnUpdate(dt); + if (p.second->IsEnable()) + { + p.second->OnUpdate(dt); + } } } } @@ -113,16 +128,16 @@ void ComponentManager::Update(Duration dt) void ComponentManager::Render(RenderContext& ctx) { - if (!components_.IsEmpty()) + if (!components_.empty()) { - ComponentPtr next; - for (auto component = components_.GetFirst(); component; component = next) + if (!components_.empty()) { - next = component->GetNext(); - - if (component->IsEnable()) + for (auto& p : components_) { - component->OnRender(ctx); + if (p.second->IsEnable()) + { + p.second->OnRender(ctx); + } } } } @@ -130,16 +145,16 @@ void ComponentManager::Render(RenderContext& ctx) void ComponentManager::DispatchToComponents(Event* evt) { - if (!components_.IsEmpty()) + if (!components_.empty()) { - ComponentPtr next; - for (auto component = components_.GetFirst(); component; component = next) + if (!components_.empty()) { - next = component->GetNext(); - - if (component->IsEnable()) + for (auto& p : components_) { - component->HandleEvent(evt); + if (p.second->IsEnable()) + { + p.second->HandleEvent(evt); + } } } } diff --git a/src/kiwano/base/component/ComponentManager.h b/src/kiwano/base/component/ComponentManager.h index 469e7eaa..6f3c6b70 100644 --- a/src/kiwano/base/component/ComponentManager.h +++ b/src/kiwano/base/component/ComponentManager.h @@ -31,8 +31,8 @@ namespace kiwano */ /// \~chinese -/// @brief 组件列表 -typedef IntrusiveList ComponentList; +/// @brief 组件映射 +typedef UnorderedMap ComponentMap; /** * \~chinese @@ -47,12 +47,20 @@ public: Component* AddComponent(ComponentPtr component); /// \~chinese - /// @brief 获取所有组件 - ComponentList& GetAllComponents(); + /// @brief 获取组件 + Component* GetComponent(const String& name); + + /// \~chinese + /// @brief 获取组件 + Component* GetComponent(size_t name_hash); /// \~chinese /// @brief 获取所有组件 - const ComponentList& GetAllComponents() const; + ComponentMap& GetAllComponents(); + + /// \~chinese + /// @brief 获取所有组件 + const ComponentMap& GetAllComponents() const; /// \~chinese /// @brief 移除组件 @@ -61,7 +69,12 @@ public: /// \~chinese /// @brief 移除组件 /// @param name 组件名称 - void RemoveComponents(const String& name); + void RemoveComponent(const String& name); + + /// \~chinese + /// @brief 移除组件 + /// @param name_hash 组件名称hash值 + void RemoveComponent(size_t name_hash); /// \~chinese /// @brief 移除所有组件 @@ -83,8 +96,8 @@ protected: ComponentManager(Actor* target); private: - Actor* target_; - ComponentList components_; + Actor* target_; + ComponentMap components_; }; /** @} */