Update leenkx/Sources/leenkx/trait/physics/PhysicsWorld.hx
This commit is contained in:
@ -1,7 +1,5 @@
|
||||
package leenkx.trait.physics;
|
||||
|
||||
import iron.object.Object;
|
||||
|
||||
#if (!lnx_physics)
|
||||
|
||||
class Hit { }
|
||||
@ -10,66 +8,11 @@ class PhysicsWorld extends iron.Trait { public function new() { super(); } }
|
||||
#else
|
||||
|
||||
#if lnx_bullet
|
||||
|
||||
typedef PhysicsWorld = leenkx.trait.physics.bullet.PhysicsWorld;
|
||||
typedef Hit = leenkx.trait.physics.bullet.PhysicsWorld.Hit;
|
||||
typedef RigidBody = leenkx.trait.physics.bullet.RigidBody;
|
||||
#else
|
||||
|
||||
typedef PhysicsWorld = leenkx.trait.physics.oimo.PhysicsWorld;
|
||||
typedef Hit = leenkx.trait.physics.oimo.PhysicsWorld.Hit;
|
||||
typedef RigidBody = leenkx.trait.physics.oimo.RigidBody;
|
||||
#end
|
||||
|
||||
typedef RigidBodyCacheEntry = {
|
||||
var rb: RigidBody;
|
||||
var traitCount: Int;
|
||||
}
|
||||
|
||||
class PhysicsCache {
|
||||
|
||||
static var rigidBodyCache: Map<Object, RigidBodyCacheEntry> = new Map();
|
||||
static var contactsCache: Map<RigidBody, Array<RigidBody>> = new Map();
|
||||
static var contactsCacheFrame: Int = 0;
|
||||
|
||||
public static function getCachedRigidBody(object: Object): RigidBody {
|
||||
if (object == null) return null;
|
||||
|
||||
var entry = rigidBodyCache.get(object);
|
||||
|
||||
if (entry == null || entry.traitCount != object.traits.length) {
|
||||
var rb = object.getTrait(RigidBody);
|
||||
rigidBodyCache.set(object, { rb: rb, traitCount: object.traits.length });
|
||||
return rb;
|
||||
}
|
||||
|
||||
return entry.rb;
|
||||
}
|
||||
|
||||
public static function getCachedContacts(rb: RigidBody): Array<RigidBody> {
|
||||
if (rb == null || PhysicsWorld.active == null) return null;
|
||||
|
||||
var cached = contactsCache.get(rb);
|
||||
if (cached != null) return cached;
|
||||
|
||||
var contacts = PhysicsWorld.active.getContacts(rb);
|
||||
contactsCache.set(rb, contacts);
|
||||
return contacts;
|
||||
}
|
||||
|
||||
public static inline function hasContactWith(contacts: Array<RigidBody>, target: RigidBody): Bool {
|
||||
return contacts != null && target != null && contacts.indexOf(target) >= 0;
|
||||
}
|
||||
|
||||
public static function clearCache() {
|
||||
rigidBodyCache.clear();
|
||||
contactsCache.clear();
|
||||
}
|
||||
|
||||
public static function clearContactsCache() {
|
||||
contactsCacheFrame++;
|
||||
contactsCache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
#end
|
||||
|
Reference in New Issue
Block a user