forked from LeenkxTeam/LNXSDK
added checkbox to enable on called on any object on has contact and on on contact nodes so users has less work to do to make to trigger on any object
This commit is contained in:
@ -5,21 +5,41 @@ import leenkx.trait.physics.RigidBody;
|
||||
|
||||
class HasContactNode extends LogicNode {
|
||||
|
||||
public var property0: Bool; // "any object" checkbox
|
||||
|
||||
public function new(tree: LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
override function get(from: Int): Dynamic {
|
||||
var object1: Object = inputs[0].get();
|
||||
var object2: Object = inputs[1].get();
|
||||
|
||||
if (object1 == null || object2 == null) return false;
|
||||
if (object1 == null) return false;
|
||||
|
||||
#if lnx_physics
|
||||
var physics = leenkx.trait.physics.PhysicsWorld.active;
|
||||
var rb2 = object2.getTrait(RigidBody);
|
||||
var rbs = physics.getContacts(object1.getTrait(RigidBody));
|
||||
if (rbs != null) for (rb in rbs) if (rb == rb2) return true;
|
||||
var rb1 = object1.getTrait(RigidBody);
|
||||
if (rb1 != null) {
|
||||
var rbs = physics.getContacts(rb1);
|
||||
if (rbs != null) {
|
||||
// If "any object" is enabled, check for any contact
|
||||
if (property0) {
|
||||
return rbs.length > 0;
|
||||
} else {
|
||||
// Original logic: check for specific object contact
|
||||
// Only try to get object2 if we have a second input
|
||||
if (inputs.length > 1) {
|
||||
var object2: Object = inputs[1].get();
|
||||
if (object2 == null) return false;
|
||||
|
||||
var rb2 = object2.getTrait(RigidBody);
|
||||
for (rb in rbs) {
|
||||
if (rb == rb2) return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#end
|
||||
return false;
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import leenkx.trait.physics.RigidBody;
|
||||
class OnContactNode extends LogicNode {
|
||||
|
||||
public var property0: String;
|
||||
public var property1: Bool; // "on any object" checkbox
|
||||
var lastContact = false;
|
||||
|
||||
public function new(tree: LogicTree) {
|
||||
@ -16,10 +17,8 @@ class OnContactNode extends LogicNode {
|
||||
|
||||
function update() {
|
||||
var object1: Object = inputs[0].get();
|
||||
var object2: Object = inputs[1].get();
|
||||
|
||||
if (object1 == null) object1 = tree.object;
|
||||
if (object2 == null) object2 = tree.object;
|
||||
|
||||
var contact = false;
|
||||
|
||||
@ -29,11 +28,23 @@ class OnContactNode extends LogicNode {
|
||||
if (rb1 != null) {
|
||||
var rbs = physics.getContacts(rb1);
|
||||
if (rbs != null) {
|
||||
var rb2 = object2.getTrait(RigidBody);
|
||||
for (rb in rbs) {
|
||||
if (rb == rb2) {
|
||||
contact = true;
|
||||
break;
|
||||
// If "on any object" is enabled, check for any contact
|
||||
if (property1) {
|
||||
contact = rbs.length > 0;
|
||||
} else {
|
||||
// Original logic: check for specific object contact
|
||||
// Only try to get object2 if we have a second input
|
||||
if (inputs.length > 1) {
|
||||
var object2: Object = inputs[1].get();
|
||||
if (object2 == null) object2 = tree.object;
|
||||
|
||||
var rb2 = object2.getTrait(RigidBody);
|
||||
for (rb in rbs) {
|
||||
if (rb == rb2) {
|
||||
contact = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,16 @@
|
||||
from lnx.logicnode.lnx_nodes import *
|
||||
|
||||
def toggle_second_input(self, context):
|
||||
"""Show/hide the second input socket based on the 'On Any Object' checkbox"""
|
||||
if self.property0: # "On Any Object" is enabled
|
||||
# Hide the second input socket
|
||||
if len(self.inputs) > 1:
|
||||
self.inputs.remove(self.inputs.values()[-1])
|
||||
else: # "On Any Object" is disabled
|
||||
# Show the second input socket if it doesn't exist
|
||||
if len(self.inputs) == 1:
|
||||
self.add_input('LnxNodeSocketObject', 'RB 2')
|
||||
|
||||
class HasContactNode(LnxLogicTreeNode):
|
||||
"""Returns whether the given rigid body has contact with another given rigid body."""
|
||||
bl_idname = 'LNHasContactNode'
|
||||
@ -7,8 +18,18 @@ class HasContactNode(LnxLogicTreeNode):
|
||||
lnx_section = 'contact'
|
||||
lnx_version = 1
|
||||
|
||||
property0: HaxeBoolProperty(
|
||||
'property0',
|
||||
name='On Any Object',
|
||||
description='If enabled, returns true if the rigid body has contact with any object',
|
||||
default=False,
|
||||
update=toggle_second_input)
|
||||
|
||||
def lnx_init(self, context):
|
||||
self.add_input('LnxNodeSocketObject', 'RB 1')
|
||||
self.add_input('LnxNodeSocketObject', 'RB 2')
|
||||
|
||||
self.add_output('LnxBoolSocket', 'Has Contact')
|
||||
|
||||
def draw_buttons(self, context, layout):
|
||||
layout.prop(self, 'property0') # "On Any Object" checkbox
|
||||
|
@ -1,5 +1,16 @@
|
||||
from lnx.logicnode.lnx_nodes import *
|
||||
|
||||
def toggle_second_input(self, context):
|
||||
"""Show/hide the second input socket based on the 'On Any Object' checkbox"""
|
||||
if self.property1: # "On Any Object" is enabled
|
||||
# Hide the second input socket
|
||||
if len(self.inputs) > 1:
|
||||
self.inputs.remove(self.inputs.values()[-1])
|
||||
else: # "On Any Object" is disabled
|
||||
# Show the second input socket if it doesn't exist
|
||||
if len(self.inputs) == 1:
|
||||
self.add_input('LnxNodeSocketObject', 'RB 2')
|
||||
|
||||
class OnContactNode(LnxLogicTreeNode):
|
||||
"""Activates the output when the rigid body make contact with
|
||||
another rigid body.
|
||||
@ -23,6 +34,13 @@ class OnContactNode(LnxLogicTreeNode):
|
||||
('end', 'End', 'The contact between the rigid bodies ends')],
|
||||
name='', default='begin')
|
||||
|
||||
property1: HaxeBoolProperty(
|
||||
'property1',
|
||||
name='On Any Object',
|
||||
description='If enabled, triggers on contact with any object instead of a specific object',
|
||||
default=False,
|
||||
update=toggle_second_input)
|
||||
|
||||
def lnx_init(self, context):
|
||||
self.add_input('LnxNodeSocketObject', 'RB 1')
|
||||
self.add_input('LnxNodeSocketObject', 'RB 2')
|
||||
@ -31,3 +49,4 @@ class OnContactNode(LnxLogicTreeNode):
|
||||
|
||||
def draw_buttons(self, context, layout):
|
||||
layout.prop(self, 'property0')
|
||||
layout.prop(self, 'property1') # "On Any Object" checkbox
|
||||
|
Reference in New Issue
Block a user