Start of interaction script

This commit is contained in:
2026-01-26 08:25:27 -08:00
parent 7bc32985ad
commit c28cfb1f8b
2 changed files with 44 additions and 16 deletions

View File

@ -79,7 +79,6 @@ class Fields {
static event_name : String { "event_name" }
static distance : String { "distance" }
static is_active : String { "is_active" }
static camera : String { "camera" }
}
import "luxe: world/states" for AState, States, Op
@ -102,7 +101,6 @@ class APIGet {
event_name(entity: Entity) : String { Modifier.get(entity).event_name }
distance(entity: Entity) : Num { Modifier.get(entity).distance }
is_active(entity: Entity) : Bool { Modifier.get(entity).is_active }
camera(entity: Entity) : Any { Modifier.get(entity).camera }
}
class APISet {
@ -110,7 +108,6 @@ class APISet {
event_name(entity: Entity, value: String) { Modifier.get(entity).event_name = value }
distance(entity: Entity, value: Num) { Modifier.get(entity).distance = value }
is_active(entity: Entity, value: Bool) { Modifier.get(entity).is_active = value }
camera(entity: Entity, value: Any) { Modifier.get(entity).camera = value }
}
var APIGetter = APIGet.new()

View File

@ -1,21 +1,23 @@
import "fpkit: system/player_interaction.modifier.api" for API, Modifier, APIGet, APISet, Wire, Fields, State, Op
import "luxe: io" for IO
import "luxe: assets" for Assets, Strings
import "luxe: math" for Math
import "luxe: input" for Input
import "luxe: system/camera.modifier" for Camera
import "luxe: world" for Entity, World
import "luxe: system/physics/physics3D.modifier" for Physics3D
import "luxe: system/physics/body3D.modifier" for Body3D
import "luxe: world" for Entity, World, Transform
import "luxe.project/asset" for Asset
import "luxe: draw" for Draw, PathStyle
#block = data
class Data {
var event_name: String = "interact"
var distance: Num = 2
var distance: Num = 1.3
#hidden
var is_active: Bool = true
#hidden
var camera: Link = null
}
#api
@ -35,30 +37,59 @@ class System is Modifier {
init(world: World) {
Log.print("init `%(This)` in world `%(world)`")
_world = world
_drawing = Draw.create(World.render_set(world))
_style = PathStyle.new()
_style.color = [0.5, 0.5, 0.5, 1]
_style.thickness = 0.001
}
#hidden
attach(entity: Entity, data: Data) {
Log.print("attached to `%(Entity.name(entity))` `%(entity)` - `%(This)`")
var camera = Entity.get_with(entity, Camera.id)
Log.print(" Camera Found: %(camera != null)")
attach(player: Entity, data: Data) {
Log.print("attached to `%(Entity.name(player))` `%(player)` - `%(This)`")
// data.camera = camera
_camera = Camera.get_default(_world)
Log.print("Camera Found: %(_camera != null) / %(Entity.name(_camera))")
// if (!Physics3D.has(_world)) {
Physics3D.create_in(_world)
// }
}
#hidden
detach(entity: Entity, data: Data) {
Log.print("detached from `%(Entity.name(entity))` `%(entity)` - `%(This)`")
detach(player: Entity, data: Data) {
Log.print("detached from `%(Entity.name(player))` `%(player)` - `%(This)`")
}
#hidden
tick(delta: Num) {
each {|entity: Entity, data: Data|
each {|player: Entity, data: Data|
if (!data.is_active) return
if (Input.event_began(data.event_name)) {
Log.print("[[ USE ]]")
}
var cam_pos = Transform.get_pos_world(_camera)
var look_dir = Transform.local_dir_to_world(_camera, 0, 0, -1)
var look_pos = Math.add(cam_pos, look_dir)
var did_hit: Bool = false
var hits = Physics3D.cast_ray(_world, cam_pos, look_dir, data.distance)
for (hit: CastRayResult in hits) {
if (hit.body_entity == player) continue
if (Body3D.get.is_sensor(hit.body_entity)) continue
did_hit = true
look_pos = hit.pos
break
}
_style.color = did_hit ? [0.2, 1, 0.2, 1] : [1, 0.2, 0.2, 1]
Draw.sphere3D_slice(_drawing, look_pos, [0.1, 0.1], 0, 360, 8, _style)
Draw.commit(_drawing)
}
}