Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Nov 14, 2024
1 parent d503a77 commit f9d98b6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 67 deletions.
96 changes: 39 additions & 57 deletions data/maps/demo/button.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ cubeB = {}
sphereA = {}
sphereB = {}
sound = -1
local ik_target_input = -1
local ik_alpha_input = -1
local ik_co = nil
local cubeA_pos = {}
local cubeB_pos = {}
local sphereA_pos = {}
Expand All @@ -26,6 +23,7 @@ Editor.setPropertyType(this, "sphereB", Editor.ENTITY_PROPERTY)
Editor.setPropertyType(this, "sound", Editor.RESOURCE_PROPERTY, "clip")

function start()
-- remember initial positions so we can reset them when button is pressed
cubeA_pos = cubeA.position
cubeB_pos = cubeB.position
sphereA_pos = sphereA.position
Expand All @@ -43,24 +41,6 @@ function update(time_delta)
interactive = dist_squared < 2
-- animate the label if player is close
label.property_animator.enabled = interactive

if ik_co then
local status, res = coroutine.resume(ik_co, time_delta)
if not status then
LumixAPI.logError("coroutine error: " .. res)
ik_co = nil
end
if not res then
ik_co = nil
end
end
end

function easeOutBack(x: number): number
local c1 = 1.70158;
local c3 = c1 + 1;

return 1 + c3 * math.pow(x - 1, 3) + c1 * math.pow(x - 1, 2);
end

function calcIKTarget()
Expand All @@ -74,41 +54,43 @@ function calcIKTarget()
end

function onInputEvent(event : InputEvent)
-- check if player pressed "F" and is close
if interactive and event.type == "button" and event.device.type == "keyboard" then
if event.key_id == string.byte("F") then
if event.down then
ik_target_input = player.animator:getInputIndex("left_hand_ik_target")
ik_alpha_input = player.animator:getInputIndex("left_hand_ik_alpha")

player.animator:setVec3Input(ik_target_input, calcIKTarget())
co.run(function()
co.parallel(
-- move hand to the button
function() co.lerpAnimatorFloat(player, ik_alpha_input, 0, 1, 0.3) end,
-- wait a bit and then press the button
function()
co.wait(0.2)
co.lerpVec3(this, "local_position", {0, 0, 0}, {0, 0, 0.1}, 0.1)
end
)
-- reset objects' positions
cubeA.position = cubeA_pos
cubeB.position = cubeB_pos
sphereA.position = sphereA_pos
sphereB.position = sphereB_pos
-- play a sound and wait a bit
playSound(sound)
co.wait(0.1)
co.parallel(
-- move hand back
function() co.lerpAnimatorFloat(player, ik_alpha_input, 1, 0, 0.3) end,
-- release the button
function() co.lerpVec3(this, "local_position", {0, 0, 0.1}, {0, 0, 0}, 0.1) end
)
return false
end)
end
end
-- check if player pressed "F" and the button is interactive
if not interactive or event.type ~= "button" or event.device.type ~= "keyboard" then
return
end

if event.key_id ~= string.byte("F") or not event.down then
return
end

local ik_target_input = player.animator:getInputIndex("left_hand_ik_target")
local ik_alpha_input = player.animator:getInputIndex("left_hand_ik_alpha")

player.animator:setVec3Input(ik_target_input, calcIKTarget())
co.run(function()
co.parallel(
-- move hand to the button
function() co.lerpAnimatorFloat(player, ik_alpha_input, 0, 1, 0.3) end,
-- wait a bit and then press the button
function()
co.wait(0.2)
co.lerpVec3(this, "local_position", {0, 0, 0}, {0, 0, 0.1}, 0.1)
end
)
-- reset objects' positions
cubeA.position = cubeA_pos
cubeB.position = cubeB_pos
sphereA.position = sphereA_pos
sphereB.position = sphereB_pos
-- play a sound and wait a bit
playSound(sound)
co.wait(0.1)
co.parallel(
-- move hand back
function() co.lerpAnimatorFloat(player, ik_alpha_input, 1, 0, 0.3) end,
-- release the button
function() co.lerpVec3(this, "local_position", {0, 0, 0.1}, {0, 0, 0}, 0.1) end
)
return false
end)
end
18 changes: 8 additions & 10 deletions data/maps/demo/button_label.anp
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
type = "Scale X",
keyframes = [
0, 15, 30],
values = [
10.000000, 20.000000, 10.000000]
version = 1,
type = "Scale X",
keyframes = [ 0.000000, 0.500000, 1.000000 ],
values = [ 5.000000, 10.000000, 5.000000 ]
},

{
type = "Scale Y",
keyframes = [
0, 15, 30],
values = [
10.000000, 20.000000, 10.000000]
version = 1,
type = "Scale Y",
keyframes = [ 0.000000, 0.500000, 1.000000 ],
values = [ 5.000000, 10.000000, 5.000000 ]
},

Binary file modified data/maps/demo/demo.unv
Binary file not shown.

0 comments on commit f9d98b6

Please sign in to comment.