diff --git a/assets/system/first_person_player.modifier.wren b/assets/system/first_person_player.modifier.wren index 972cdaf..1920511 100644 --- a/assets/system/first_person_player.modifier.wren +++ b/assets/system/first_person_player.modifier.wren @@ -44,7 +44,7 @@ class Data { #desc = "**Player movement system**. Takes the player's input and moves the player's character." #icon = "luxe: image/modifier/modifier.svg" class FirstPersonPlayer is API { - set_movement_active(entity: Entity, active: Bool) { + static set_movement_active(entity: Entity, active: Bool) { system(entity).movement_active = active } } diff --git a/assets/system/interactive.modifier.api.wren b/assets/system/interactive.modifier.api.wren new file mode 100644 index 0000000..606d458 --- /dev/null +++ b/assets/system/interactive.modifier.api.wren @@ -0,0 +1,116 @@ + +class Modifier { + static id : String { "fpkit: system/interactive.modifier" } + static system(world: World) : System { World.get_system(world, Modifier.id) } + static get(entity: Entity) : Data { Mod.get(entity, Modifier.id) } + construct new(world: World) { + _world = world + _block = World.get_modifier_block(_world, Modifier.id) + } + world : World { _world } + attached_count : Num { Block.count(_block) } + + init(world: World) : None {} + pre(entity: Entity, data) : None {} + attach(entity: Entity, data) : None {} + detach(entity: Entity, data) : None {} + change(entity: Entity, change: ModifierChange) : None {} + tick(delta: Num) : None {} + + internal_tick(delta: Num) : None { + } + + editor_init(world: World) : None {} + editor_pre(entity: Entity, data) : None {} + editor_attach(entity: Entity, data) : None {} + editor_detach(entity: Entity, data) : None {} + editor_change(entity: Entity, change: ModifierChange) : None {} + + editor_tick(delta: Num) : None {} + + get(entity: Entity) : Data { Mod.get(entity, Modifier.id) } + + list() : List { + var list = [] + each {|entity: Entity, unused| + list.add(entity) + } + return list + } + + each(fn: Fn) : None { + var count = attached_count + for(idx in 0 ... count) { + var inst = Block.get_at(_block, idx) + var data = Block.instance(_block, inst) + var entity = Block.get_handle(_block, inst, HandleTag.entity) + fn.call(entity, data) + } + } + + each(start: Num, count: Num, fn: Fn) : None { + for(idx in start ... start + count) { + var inst = Block.get_at(_block, idx) + var data = Block.instance(_block, inst) + var entity = Block.get_handle(_block, inst, HandleTag.entity) + fn.call(entity, data) + } + } +} + +class API { + static id { Modifier.id } + static create(entity: Entity) { Mod.create(Modifier.id, entity) } + static has(entity: Entity) { Mod.has(Modifier.id, entity) } + static destroy(entity: Entity) { Mod.destroy(Modifier.id, entity) } + static get(entity: Entity) : Data { Mod.get(entity, Modifier.id) } + static system_in(world: World) : System { World.get_system(world, Modifier.id) } + static system(entity: Entity) : System { World.get_system(Entity.get_world(entity), Modifier.id) } + static count(world: World) : Num { system_in(world).attached_count } + static each(world: World, fn: Fn) : System { system_in(world).each(fn) } + static with(world: World) : List { system_in(world).list() } + static get : APIGet { APIGetter } + static set : APISet { APISetter } + static connect : APIWireConnect { APIWireConnects } + static send : APIWireSend { APIWireSends } +} + +class Fields { +} + +import "luxe: world/states" for AState, States, Op +#doc="The base class for a state, and our API for accessing it" +class State is AState { + construct create(in_name: String, in_parent: State) { super(in_name, in_parent) } + goto_on(entity: Entity, state: State, wire: Num) { this.goto_on(entity, state, This, wire) } +} + +class APIWireSend { + construct new() {} +} + +class APIWireConnect { + construct new() {} +} + +class APIGet { + construct new() {} +} + +class APISet { + construct new() {} +} + +var APIGetter = APIGet.new() +var APISetter = APISet.new() +var APIWireSends = APIWireSend.new() +var APIWireConnects = APIWireConnect.new() + +import "fpkit: system/interactive.modifier" for Data, System +import "luxe: world/world" for World, Wire +import "luxe: world" for Entity +import "luxe: world/modifier" for Modifier as Mod, ModifierChange +import "luxe: blocks" for Block +import "luxe: io" for HandleTag + + diff --git a/assets/system/interactive.modifier.api.wren.meta.lx b/assets/system/interactive.modifier.api.wren.meta.lx new file mode 100644 index 0000000..129a488 --- /dev/null +++ b/assets/system/interactive.modifier.api.wren.meta.lx @@ -0,0 +1,2 @@ +tags = [] +uuid = "035c6743-2b39-4f0a-97c6-d7cc7c75001e" diff --git a/assets/system/interactive.modifier.wren b/assets/system/interactive.modifier.wren new file mode 100644 index 0000000..d31f3b0 --- /dev/null +++ b/assets/system/interactive.modifier.wren @@ -0,0 +1,58 @@ +import "fpkit: system/interactive.modifier.api" for API, Modifier, APIGet, APISet, Wire, Fields, State, Op +import "luxe: io" for IO +import "luxe: assets" for Assets, Strings +import "luxe: world" for Entity, World +import "luxe.project/asset" for Asset + +#block = data +class Data { + //add data fields here +} + +#api +#display = "FPKit Interactive" +#desc = "**Receives Interaction signals**. Has signals to tap into with your own modifiers when the player looks at or uses this Entity." +#icon = "luxe: image/modifier/modifier.svg" +class Interactive is API { + static begin_look(entity: Entity) { + + } + + static end_look(entity: Entity) {} + + static begin_interact(entity: Entity) { + Log.print("Interact Begin!") + } + static end_interact(entity: Entity) { + Log.print("Interact End!") + } +} + +#system +#phase(on, tick) +class System is Modifier { + + //required atm + construct new(world: World) { super(world) } + + init(world: World) { + Log.print("init `%(This)` in world `%(world)`") + } + + #hidden + attach(entity: Entity, data: Data) { + Log.print("attached to `%(Entity.name(entity))` `%(entity)` - `%(This)`") + } + + #hidden + detach(entity: Entity, data: Data) { + Log.print("detached from `%(Entity.name(entity))` `%(entity)` - `%(This)`") + } + + #hidden + tick(delta: Num) { + // each {|entity: Entity, data: Data| + // } + } + +} \ No newline at end of file diff --git a/assets/system/interactive.modifier.wren.meta.lx b/assets/system/interactive.modifier.wren.meta.lx new file mode 100644 index 0000000..c259aa0 --- /dev/null +++ b/assets/system/interactive.modifier.wren.meta.lx @@ -0,0 +1,2 @@ +tags = [] +uuid = "fce97435-dcc3-4d50-a10f-bef141c22efa" diff --git a/assets/system/player_interaction.modifier.api.wren b/assets/system/player_interaction.modifier.api.wren index 1f306f7..f18785d 100644 --- a/assets/system/player_interaction.modifier.api.wren +++ b/assets/system/player_interaction.modifier.api.wren @@ -76,8 +76,9 @@ class API { } class Fields { - static event_name : String { "event_name" } - static distance : String { "distance" } + static interact_event : String { "interact_event" } + static interact_distance : String { "interact_distance" } + static look_distance : String { "look_distance" } static is_active : String { "is_active" } static camera : String { "camera" } static seen_interaction : String { "seen_interaction" } @@ -100,8 +101,9 @@ class APIWireConnect { class APIGet { construct new() {} - event_name(entity: Entity) : String { Modifier.get(entity).event_name } - distance(entity: Entity) : Num { Modifier.get(entity).distance } + interact_event(entity: Entity) : String { Modifier.get(entity).interact_event } + interact_distance(entity: Entity) : Num { Modifier.get(entity).interact_distance } + look_distance(entity: Entity) : Num { Modifier.get(entity).look_distance } is_active(entity: Entity) : Bool { Modifier.get(entity).is_active } camera(entity: Entity) : Num { Modifier.get(entity).camera } seen_interaction(entity: Entity) : Num { Modifier.get(entity).seen_interaction } @@ -109,8 +111,9 @@ class APIGet { class APISet { construct new() {} - event_name(entity: Entity, value: String) { Modifier.get(entity).event_name = value } - distance(entity: Entity, value: Num) { Modifier.get(entity).distance = value } + interact_event(entity: Entity, value: String) { Modifier.get(entity).interact_event = value } + interact_distance(entity: Entity, value: Num) { Modifier.get(entity).interact_distance = value } + look_distance(entity: Entity, value: Num) { Modifier.get(entity).look_distance = value } is_active(entity: Entity, value: Bool) { Modifier.get(entity).is_active = value } camera(entity: Entity, value: Num) { Modifier.get(entity).camera = value } seen_interaction(entity: Entity, value: Num) { Modifier.get(entity).seen_interaction = value } diff --git a/assets/system/player_interaction.modifier.wren b/assets/system/player_interaction.modifier.wren index 7c802a6..293b527 100644 --- a/assets/system/player_interaction.modifier.wren +++ b/assets/system/player_interaction.modifier.wren @@ -8,13 +8,15 @@ 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 +import "fpkit: system/interactive.modifier" for Interactive + #block = data class Data { - var event_name: String = "interact" - var distance: Num = 1.3 + var interact_event: String = "interact" + var interact_distance: Num = 1.3 + var look_distance: Num = 20.0 #hidden var is_active: Bool = true @@ -23,7 +25,7 @@ class Data { var camera: Handle = 0 #hidden - var seen_interaction: Handle = 0 + var seen_interaction: Handle = -1 } #api @@ -55,12 +57,7 @@ class System is Modifier { Log.print("attached to `%(Entity.name(player))` `%(player)` - `%(This)`") data.camera = Camera.get_default(_world) - Log.print("Camera Found: %(data.camera != null) / %(Entity.name(data.camera))") - - // if (!Physics3D.has(_world)) { - Physics3D.create_in(_world) - // } } #hidden @@ -73,35 +70,40 @@ class System is Modifier { each {|player: Entity, data: Data| if (!data.is_active) return - if (Input.event_began(data.event_name)) { - Log.print("[[ USE ]]") + if (Input.event_began(data.interact_event) && data.seen_interaction >= 0) { + Log.print("[[ USE ]] // Entity: %(Entity.name(data.seen_interaction))") + Interactive.begin_interact(data.seen_interaction) + } else if (Input.event_ended(data.interact_event) && data.seen_interaction >= 0) { + Interactive.end_interact(data.seen_interaction) } var cam_pos = Transform.get_pos_world(data.camera) var look_dir = Transform.local_dir_to_world(data.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) + var use_hits = Physics3D.cast_ray(_world, cam_pos, look_dir, data.interact_distance) var last_seen = data.seen_interaction - data.seen_interaction = 0 + data.seen_interaction = -1 - for (hit: CastRayResult in hits) { + for (hit: CastRayResult in use_hits) { if (hit.body_entity == player) continue if (Body3D.get.is_sensor(hit.body_entity)) continue - did_hit = true - data.seen_interaction = hit.body_entity - if (hit.body_entity != last_seen) { - Log.print("Entity Seen: %(Entity.name(hit.body_entity))") - } - look_pos = hit.pos + + if (Entity.valid(hit.body_entity) && Interactive.has(hit.body_entity)) + { + data.seen_interaction = hit.body_entity + if (hit.body_entity != last_seen) { + Log.print("Entity Seen: %(Entity.name(hit.body_entity))") + Interactive.begin_look(hit.body_entity) + } + } break } - _style.color = did_hit ? [0.2, 1, 0.2, 1] : [1, 0.2, 0.2, 1] + _style.color = data.seen_interaction >= 0 && Entity.valid(data.seen_interaction) ? [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) }