From dbe6d0829af97bfb4c5d72aa206786f61c4403d9 Mon Sep 17 00:00:00 2001 From: LeenkxTeam Date: Fri, 3 Oct 2025 07:51:07 +0000 Subject: [PATCH] Add leenkx/Sources/leenkx/trait/physics/PhysicsCache.hx --- .../leenkx/trait/physics/PhysicsCache.hx | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 leenkx/Sources/leenkx/trait/physics/PhysicsCache.hx diff --git a/leenkx/Sources/leenkx/trait/physics/PhysicsCache.hx b/leenkx/Sources/leenkx/trait/physics/PhysicsCache.hx new file mode 100644 index 00000000..1d361043 --- /dev/null +++ b/leenkx/Sources/leenkx/trait/physics/PhysicsCache.hx @@ -0,0 +1,98 @@ +package leenkx.trait.physics; + +import iron.object.Object; + +class PhysicsCache { + #if lnx_physics + static var rbCache: Map = new Map(); + static var contactsCache: Map> = new Map(); + static var physicsFrame: Int = 0; + static var lastQueryFrame: Int = -1; + #end + + public static function getCachedRigidBody(object: Object): Dynamic { + #if (!lnx_physics) + return null; + #else + if (object == null) return null; + + var cached = rbCache.get(object.uid); + if (cached != null) return cached; + + #if lnx_bullet + var rb = object.getTrait(leenkx.trait.physics.bullet.RigidBody); + #else + var rb = object.getTrait(leenkx.trait.physics.oimo.RigidBody); + #end + + if (rb != null) rbCache.set(object.uid, rb); + return rb; + #end + } + + public static function getCachedContacts(rb: Dynamic): Array { + #if (!lnx_physics) + return null; + #else + if (rb == null) return null; + + var rbObjectId = (rb.object != null) ? rb.object.uid : -1; + + if (rbObjectId == -1) { + #if lnx_bullet + if (leenkx.trait.physics.bullet.PhysicsWorld.active == null) return null; + return leenkx.trait.physics.bullet.PhysicsWorld.active.getContacts(rb); + #else + if (leenkx.trait.physics.oimo.PhysicsWorld.active == null) return null; + return leenkx.trait.physics.oimo.PhysicsWorld.active.getContacts(rb); + #end + } + + if (lastQueryFrame == physicsFrame) { + var cached = contactsCache.get(rbObjectId); + if (cached != null) return cached; + } + + lastQueryFrame = physicsFrame; + + var cached = contactsCache.get(rbObjectId); + if (cached != null) return cached; + + #if lnx_bullet + if (leenkx.trait.physics.bullet.PhysicsWorld.active == null) return null; + var contacts = leenkx.trait.physics.bullet.PhysicsWorld.active.getContacts(rb); + #else + if (leenkx.trait.physics.oimo.PhysicsWorld.active == null) return null; + var contacts = leenkx.trait.physics.oimo.PhysicsWorld.active.getContacts(rb); + #end + + if (contacts != null) { + contactsCache.set(rbObjectId, contacts); + } + + return contacts; + #end + } + + public static inline function hasContactWith(contacts: Array, target: Dynamic): Bool { + #if (!lnx_physics) + return false; + #else + return contacts != null && target != null && contacts.indexOf(target) >= 0; + #end + } + + public static function clearCache() { + #if lnx_physics + rbCache.clear(); + contactsCache.clear(); + #end + } + + public static function clearContactsCache() { + #if lnx_physics + physicsFrame++; + contactsCache.clear(); + #end + } +}