Start of interaction script
This commit is contained in:
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user