From 06554f1378995e73394600eb88d478f92ccaee21 Mon Sep 17 00:00:00 2001 From: Nomango Date: Fri, 1 Nov 2019 22:59:50 +0800 Subject: [PATCH] Update ContactListener --- src/kiwano-physics/Body.h | 2 +- src/kiwano-physics/ContactListener.cpp | 25 +++++++++++++++++++++++-- src/kiwano-physics/ContactListener.h | 9 +++++++++ src/kiwano-physics/World.cpp | 9 ++++----- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/kiwano-physics/Body.h b/src/kiwano-physics/Body.h index 3b71e4da..8647eddf 100644 --- a/src/kiwano-physics/Body.h +++ b/src/kiwano-physics/Body.h @@ -63,7 +63,7 @@ namespace kiwano PhysicFixture AddChainShape(Vector const& vertexs, bool loop, float density = 0.f); // 获取夹具 - PhysicFixture GetFixtureList() const { KGE_ASSERT(body_); PhysicFixture(body_->GetFixtureList()); } + PhysicFixture GetFixtureList() const { KGE_ASSERT(body_); return PhysicFixture(body_->GetFixtureList()); } // 移除夹具 void RemoveFixture(PhysicFixture const& fixture); diff --git a/src/kiwano-physics/ContactListener.cpp b/src/kiwano-physics/ContactListener.cpp index cd8974fe..276d5a78 100644 --- a/src/kiwano-physics/ContactListener.cpp +++ b/src/kiwano-physics/ContactListener.cpp @@ -63,7 +63,7 @@ namespace kiwano void PhysicContactDispatcher::StartContactListeners(String const& listener_name) { - for (auto listener = listeners_.first_item(); listener; listener = listener->next_item()) + for (auto listener : listeners_) { if (listener->IsName(listener_name)) { @@ -74,7 +74,7 @@ namespace kiwano void PhysicContactDispatcher::StopContactListeners(String const& listener_name) { - for (auto listener = listeners_.first_item(); listener; listener = listener->next_item()) + for (auto listener : listeners_) { if (listener->IsName(listener_name)) { @@ -97,6 +97,27 @@ namespace kiwano } } + void PhysicContactDispatcher::StartAllContactListeners() + { + for (auto listener : listeners_) + { + listener->Start(); + } + } + + void PhysicContactDispatcher::StopAllContactListeners() + { + for (auto listener : listeners_) + { + listener->Stop(); + } + } + + void PhysicContactDispatcher::RemoveAllContactListeners() + { + listeners_.clear(); + } + void PhysicContactDispatcher::OnContactBegin(b2Contact* b2contact) { if (listeners_.empty()) diff --git a/src/kiwano-physics/ContactListener.h b/src/kiwano-physics/ContactListener.h index 55572af6..9556d633 100644 --- a/src/kiwano-physics/ContactListener.h +++ b/src/kiwano-physics/ContactListener.h @@ -120,6 +120,15 @@ namespace kiwano String const& listener_name ); + // 启动所有监听器 + void StartAllContactListeners(); + + // 停止所有监听器 + void StopAllContactListeners(); + + // 移除所有监听器 + void RemoveAllContactListeners(); + protected: void OnContactBegin(b2Contact* contact); diff --git a/src/kiwano-physics/World.cpp b/src/kiwano-physics/World.cpp index afb6baf5..01ea268d 100644 --- a/src/kiwano-physics/World.cpp +++ b/src/kiwano-physics/World.cpp @@ -102,6 +102,8 @@ namespace kiwano contact_listener_ = nullptr; } + RemoveAllContactListeners(); + // Make sure b2World was destroyed after b2Body RemoveAllChildren(); RemoveAllBodies(); @@ -110,12 +112,9 @@ namespace kiwano void PhysicWorld::RemoveBody(PhysicBody* body) { - if (body) + if (body && body->GetB2Body()) { - if (body->GetB2Body()) - { - world_.DestroyBody(body->GetB2Body()); - } + world_.DestroyBody(body->GetB2Body()); } }