Refactor visor expression/damage code

- Scope entire expression change code
- Use tables for related values
- Make certain variables and functions local
This commit is contained in:
NullBite 2022-03-16 20:23:51 -04:00
parent 9788f8d231
commit 776de24593
Signed by: nullbite
GPG Key ID: 6C4D545385D4925A

View File

@ -3,13 +3,6 @@
-- Texture dimensions -- -- Texture dimensions --
TEXTURE_WIDTH = 128 TEXTURE_WIDTH = 128
TEXTURE_HEIGHT = 128 TEXTURE_HEIGHT = 128
-- Values for UV mappings --
face_damage=0
face_expr=0
step_u_face=32
step_v_face=16
offset_u_face=64
offset_v_face=0
armor_enabled=true armor_enabled=true
vanilla_enabled=true vanilla_enabled=true
@ -67,32 +60,39 @@ VANILLA_INNER={
} }
-- Expression change -- {{{ -- Expression change -- {{{
function getExprUV(damage, expression) do
local u=offset_u_face+(damage*step_u_face) -- Values for UV mappings --
local v=offset_v_face+(expression*step_v_face) expr_current={damage=0, expression=0}
return {u/TEXTURE_WIDTH,v/TEXTURE_HEIGHT} local expr_step={u=32, v=16}
end local expr_offset={u=64, v=0}
function changeExpression(_damage, _expression, ticks)
-- u is damage, v is expression
local damage = _damage
local expression = _expression
if damage == nil then
damage = face_damage
end
if expression == nil then
expression = face_expr
end
HEAD.setUV(getExprUV(damage,expression)) local function getExprUV(damage, expression)
namedWait(ticks, resetExpression, "resetExpression") local u=expr_offset.u+(damage*expr_step.u)
end local v=expr_offset.v+(expression*expr_step.v)
function setExpression(damage, expression) return UV{u, v}
face_damage=damage end
face_expr=expression function changeExpression(_damage, _expression, ticks)
HEAD.setUV(getExprUV(damage, expression)) -- u is damage, v is expression
end local damage = _damage
function resetExpression() local expression = _expression
HEAD.setUV(getExprUV(face_damage,face_expr)) if damage == nil then
damage = expr_current.damage
end
if expression == nil then
expression = expr_current.expression
end
HEAD.setUV(getExprUV(damage,expression))
namedWait(ticks, resetExpression, "resetExpression")
end
function setExpression(damage, expression)
expr_current.damage=damage
expr_current.expression=expression
HEAD.setUV(getExprUV(damage, expression))
end
function resetExpression()
HEAD.setUV(getExprUV(expr_current.damage,expr_current.expression))
end
end end
-- }}} -- }}}
@ -196,7 +196,7 @@ function tick()
-- optimization, only execute these once a second -- -- optimization, only execute these once a second --
if world.getTimeOfDay() % 20 then if world.getTimeOfDay() % 20 then
-- if face is cracked -- if face is cracked
if face_damage==1 and player.getHealth() > 5 then if expr_current.damage==1 and player.getHealth() > 5 then
ping.healed() ping.healed()
end end
end end
@ -236,5 +236,9 @@ function onCommand(input)
v.setEnabled(not v.getEnabled()) v.setEnabled(not v.getEnabled())
end end
end end
if input[1] == chat_prefix .. "test_expression" then
setExpression(input[2], input[3])
print(input[2] .. " " .. input[3])
end
end end