Refactored player controls into PlayerInput class
This commit is contained in:
2
system/folder.meta.lx
Normal file
2
system/folder.meta.lx
Normal file
@ -0,0 +1,2 @@
|
||||
tags = []
|
||||
uuid = "5b0c88c0-2795-4817-a4da-1830dc78ea4a"
|
||||
135
system/player_input.modifier.api.wren
Normal file
135
system/player_input.modifier.api.wren
Normal file
@ -0,0 +1,135 @@
|
||||
|
||||
class Modifier {
|
||||
static id : String { "system/player_input.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) }
|
||||
each(fn: Fn) {
|
||||
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) {
|
||||
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 get : APIGet { APIGetter }
|
||||
static set : APISet { APISetter }
|
||||
static connect : APIWireConnect { APIWireConnects }
|
||||
static send : APIWireSend { APIWireSends }
|
||||
}
|
||||
|
||||
class Fields {
|
||||
static acceleration : String { "acceleration" }
|
||||
static decelleration : String { "decelleration" }
|
||||
static ground_check_dist : String { "ground_check_dist" }
|
||||
static jump_impulse : String { "jump_impulse" }
|
||||
static jump_force : String { "jump_force" }
|
||||
static jump_force_falloff : String { "jump_force_falloff" }
|
||||
static jump_grace_time : String { "jump_grace_time" }
|
||||
static input_target : String { "input_target" }
|
||||
static time_since_grounded : String { "time_since_grounded" }
|
||||
static is_grounded : String { "is_grounded" }
|
||||
}
|
||||
|
||||
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() {}
|
||||
acceleration(entity: Entity) : Num { Modifier.get(entity).acceleration }
|
||||
decelleration(entity: Entity) : Num { Modifier.get(entity).decelleration }
|
||||
ground_check_dist(entity: Entity) : Num { Modifier.get(entity).ground_check_dist }
|
||||
jump_impulse(entity: Entity) : Num { Modifier.get(entity).jump_impulse }
|
||||
jump_force(entity: Entity) : Num { Modifier.get(entity).jump_force }
|
||||
jump_force_falloff(entity: Entity) : Num { Modifier.get(entity).jump_force_falloff }
|
||||
jump_grace_time(entity: Entity) : Num { Modifier.get(entity).jump_grace_time }
|
||||
input_target(entity: Entity) : Float3 { Modifier.get(entity).input_target }
|
||||
time_since_grounded(entity: Entity) : Num { Modifier.get(entity).time_since_grounded }
|
||||
is_grounded(entity: Entity) : Bool { Modifier.get(entity).is_grounded }
|
||||
}
|
||||
|
||||
class APISet {
|
||||
construct new() {}
|
||||
acceleration(entity: Entity, value: Num) { Modifier.get(entity).acceleration = value }
|
||||
decelleration(entity: Entity, value: Num) { Modifier.get(entity).decelleration = value }
|
||||
ground_check_dist(entity: Entity, value: Num) { Modifier.get(entity).ground_check_dist = value }
|
||||
jump_impulse(entity: Entity, value: Num) { Modifier.get(entity).jump_impulse = value }
|
||||
jump_force(entity: Entity, value: Num) { Modifier.get(entity).jump_force = value }
|
||||
jump_force_falloff(entity: Entity, value: Num) { Modifier.get(entity).jump_force_falloff = value }
|
||||
jump_grace_time(entity: Entity, value: Num) { Modifier.get(entity).jump_grace_time = value }
|
||||
input_target(entity: Entity, value: Float3) { Modifier.get(entity).input_target = value }
|
||||
time_since_grounded(entity: Entity, value: Num) { Modifier.get(entity).time_since_grounded = value }
|
||||
is_grounded(entity: Entity, value: Bool) { Modifier.get(entity).is_grounded = value }
|
||||
}
|
||||
|
||||
var APIGetter = APIGet.new()
|
||||
var APISetter = APISet.new()
|
||||
var APIWireSends = APIWireSend.new()
|
||||
var APIWireConnects = APIWireConnect.new()
|
||||
|
||||
import "system/player_input.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
|
||||
|
||||
|
||||
2
system/player_input.modifier.api.wren.meta.lx
Normal file
2
system/player_input.modifier.api.wren.meta.lx
Normal file
@ -0,0 +1,2 @@
|
||||
tags = []
|
||||
uuid = "83e78c7f-c96e-4f60-9244-fec04c36c231"
|
||||
111
system/player_input.modifier.wren
Normal file
111
system/player_input.modifier.wren
Normal file
@ -0,0 +1,111 @@
|
||||
import "system/player_input.modifier.api" for API, Modifier, APIGet, APISet, Wire, Fields, State, Op
|
||||
import "luxe: io" for IO
|
||||
import "luxe: assets" for Strings
|
||||
import "luxe: input" for Input
|
||||
import "luxe: world" for Entity, World, Transform
|
||||
import "luxe: math" for Math
|
||||
import "luxe.project/asset" for Asset
|
||||
import "luxe: system/physics/character3D.modifier" for Character3D
|
||||
import "luxe: system/physics/physics3D.modifier" for Physics3D
|
||||
|
||||
#block = data
|
||||
class Data {
|
||||
var acceleration: Num = 4.2
|
||||
var decelleration: Num = 3.0
|
||||
var ground_check_dist: Num = 0.05
|
||||
var jump_impulse: Num = 1.25
|
||||
var jump_grace_time: Num = 0.1
|
||||
|
||||
#hidden
|
||||
var input_target: Float3 = [0, 0, 0]
|
||||
|
||||
#hidden
|
||||
var time_since_grounded: Num = 0
|
||||
|
||||
#hidden
|
||||
var is_grounded: Bool = false
|
||||
}
|
||||
|
||||
#api
|
||||
#display = "Player Input"
|
||||
#desc = "**A new modifier**. You should update this description"
|
||||
#icon = "luxe: image/modifier/modifier.svg"
|
||||
class PlayerInput is API {
|
||||
//add public facing API here
|
||||
}
|
||||
|
||||
#system
|
||||
#phase(on, tick)
|
||||
class System is Modifier {
|
||||
EPSILON { 0.001 }
|
||||
|
||||
//required atm
|
||||
construct new(world: World) { super(world) }
|
||||
|
||||
init(world: World) {
|
||||
Log.print("init `%(This)` in world `%(world)`")
|
||||
Physics3D.create_in(world)
|
||||
_camera = Entity.get_named(world, "app.camera")
|
||||
_world = world
|
||||
}
|
||||
|
||||
#hidden
|
||||
attach(player: Entity, data: Data) {
|
||||
Log.print("Attached!")
|
||||
}
|
||||
|
||||
#hidden
|
||||
tick(delta: Num) {
|
||||
each {|player: Entity, data: Data|
|
||||
if (!Character3D.has(player)) return
|
||||
|
||||
// Update grounded state
|
||||
var now_grounded = check_grounded(player, data)
|
||||
|
||||
if (data.is_grounded && !now_grounded && data.time_since_grounded < data.jump_grace_time) {
|
||||
data.time_since_grounded = data.time_since_grounded + delta
|
||||
|
||||
if (data.time_since_grounded >= data.jump_grace_time) {
|
||||
data.is_grounded = false
|
||||
}
|
||||
}
|
||||
|
||||
if (!data.is_grounded && now_grounded && Character3D.get.velocity(player).y <= 0) {
|
||||
data.is_grounded = true
|
||||
}
|
||||
|
||||
// Update movement input
|
||||
var fore = Input.event_active("up", "game") ? -1 : 0
|
||||
var back = Input.event_active("down", "game") ? 1 : 0
|
||||
var left = Input.event_active("left", "game") ? -1 : 0
|
||||
var right = Input.event_active("right", "game") ? 1 : 0
|
||||
data.input_target = Math.normalized(Math.mult(Transform.local_dir_to_world(_camera, right + left, 0, fore + back), [1, 0, 1]))
|
||||
data.input_target = move_toward(Character3D.get.input(player), data.input_target, data, delta)
|
||||
|
||||
if (Input.event_began("jump", "game") && data.is_grounded) {
|
||||
data.time_since_grounded = 0
|
||||
data.input_target = Math.add(data.input_target, [0, data.jump_impulse, 0])
|
||||
data.is_grounded = false
|
||||
|
||||
Transform.translate(player, 0, data.ground_check_dist + this.EPSILON, 0)
|
||||
} else {
|
||||
data.input_target = [data.input_target.x, 0, data.input_target.z]
|
||||
}
|
||||
|
||||
Character3D.set.input(player, data.input_target)
|
||||
}
|
||||
}
|
||||
|
||||
check_grounded(player: Entity, data: Data): Bool {
|
||||
var height = Character3D.get.height(player) * 0.5
|
||||
return Physics3D.cast_ray_closest(_world, Transform.get_pos_world(player), [0,-1,0], height + data.ground_check_dist) != null
|
||||
}
|
||||
|
||||
move_toward(from: Float3, to: Float3, data: Data, delta: Num) {
|
||||
var pos_delta: Float3 = Math.sub(to, from)
|
||||
var pos_len: Num = Math.length(pos_delta)
|
||||
var speed = Math.length(from) < Math.length(to) ? delta * data.acceleration : delta * data.decelleration
|
||||
return pos_len <= delta || pos_len < this.EPSILON ? to : Math.add(from, Math.scale(Math.div(pos_delta, pos_len), speed))
|
||||
}
|
||||
|
||||
}
|
||||
2
system/player_input.modifier.wren.meta.lx
Normal file
2
system/player_input.modifier.wren.meta.lx
Normal file
@ -0,0 +1,2 @@
|
||||
tags = []
|
||||
uuid = "8157474a-e066-4757-b3cf-0f34cc1a3641"
|
||||
Reference in New Issue
Block a user