Rework expression handling code

This commit is contained in:
NullBite 2022-03-28 01:23:02 -04:00
parent 28f02d2ce1
commit 10a09c51a5
Signed by: nullbite
GPG Key ID: 6C4D545385D4925A

View File

@ -451,12 +451,14 @@ end
-- UVManager {{{ -- UVManager {{{
do do
local mt={} local mt={}
--- @class UVManager
UVManager = { UVManager = {
step=vectors.of{u=0, v=0}, step=vectors.of{u=0, v=0},
offset=vectors.of{u=0, v=0}, offset=vectors.of{u=0, v=0},
positions={} positions={}
} }
mt.__index=UVManager mt.__index=UVManager
--- @return UVManager
function UVManager.new(self, step, offset, positions) function UVManager.new(self, step, offset, positions)
local t={} local t={}
if step ~= nil then t.step=vectors.of(step) end if step ~= nil then t.step=vectors.of(step) end
@ -484,6 +486,8 @@ end
-- Parts, groups, other constants -- {{{ -- Parts, groups, other constants -- {{{
HEAD=model.Head.Head HEAD=model.Head.Head
FACE=model.Head.Face
SHATTER=model.Head.Shatter
VANILLA_PARTIAL={} VANILLA_PARTIAL={}
VANILLA_GROUPS={ VANILLA_GROUPS={
["HEAD"]={vanilla_model.HEAD, vanilla_model.HAT}, ["HEAD"]={vanilla_model.HEAD, vanilla_model.HAT},
@ -547,10 +551,11 @@ EMISSIVES={
model.Head.Face model.Head.Face
} }
COLORS={} COLORS={}
COLORS.default=vectors.of{127/255,127/255,255/255} COLORS.neutral=vectors.of{127/255,127/255,255/255}
COLORS.hurt= vectors.of{1, 0, 63/255} COLORS.hurt= vectors.of{1, 0, 63/255}
COLORS.lava= vectors.of{1, 168/255, 90/255}
for k, v in pairs(EMISSIVES) do for k, v in pairs(EMISSIVES) do
v.setColor(COLORS.default) v.setColor(COLORS.neutral)
end end
-- }}} -- }}}
@ -662,6 +667,9 @@ do
PM.addPartGroupFunction(vanilla_partial_enabled, function(last) return last or vanillaPartial() end) PM.addPartGroupFunction(vanilla_partial_enabled, function(last) return last or vanillaPartial() end)
PM.addPartGroupFunction(tail_parts, function(last) return last or vanillaPartial() end) PM.addPartGroupFunction(tail_parts, function(last) return last or vanillaPartial() end)
-- Show shattered only at low health
PM.addPartFunction(SHATTER, function(last) return last and local_state.health <= 5 end)
-- Enable tail setting -- Enable tail setting
PM.addPartFunction(model.Body_Tail, function(last) return last and local_state.tail_enabled end) PM.addPartFunction(model.Body_Tail, function(last) return last and local_state.tail_enabled end)
-- no legs, regular tail in water if tail enabled -- no legs, regular tail in water if tail enabled
@ -688,37 +696,46 @@ SNORES={"snore-1", "snore-2", "snore-3"}
-- Expression change -- {{{ -- Expression change -- {{{
do do
-- Values for UV mappings -- function setColor(col)
expr_current={damage=0, expression=0} col=(col~=nil) and col or COLORS.neutral
local expr_step={u=32, v=16} for _, v in pairs(EMISSIVES) do
local expr_offset={u=64, v=0} v.setColor(col)
end
end
local expressions={}
expressions.neutral={0,0}
expressions.hurt={0,1}
local expruvm=UVManager:new({8, 8}, nil, expressions)
local function getExprUV(damage, expression) -- color/expression rules
local u=expr_offset.u+(damage*expr_step.u) function getBestColor()
local v=expr_offset.v+(expression*expr_step.v) if player.isInLava() or player.getWorldName()=="the_nether" then
return UV{u, v} return COLORS.lava
else
return COLORS.neutral
end end
function changeExpression(_damage, _expression, ticks)
-- u is damage, v is expression
local damage = _damage
local expression = _expression
if damage == nil then
damage = expr_current.damage
end end
if expression == nil then function getBestExpression()
expression = expr_current.expression return "neutral"
end end
HEAD.setUV(getExprUV(damage,expression)) -- Expression change code
function setExpression(expression)
FACE.setUV(expruvm:getUV(expression))
setColor(COLORS[expression])
end
function changeExpression(expression, ticks)
setExpression(expression)
namedWait(ticks, resetExpression, "resetExpression") namedWait(ticks, resetExpression, "resetExpression")
end end
function setExpression(damage, expression)
expr_current.damage=damage
expr_current.expression=expression
HEAD.setUV(getExprUV(damage, expression))
end
function resetExpression() function resetExpression()
HEAD.setUV(getExprUV(expr_current.damage,expr_current.expression)) FACE.setUV(expruvm:getUV(getBestExpression()))
setColor(getBestColor())
end
function hurt()
changeExpression("hurt", 10)
PartsManager.refreshPart(SHATTER)
end end
end end
-- }}} -- }}}
@ -727,8 +744,7 @@ end
action_wheel.SLOT_1.setTitle('test expression') action_wheel.SLOT_1.setTitle('test expression')
action_wheel.SLOT_1.setFunction(function() ping.expressionTest() end) action_wheel.SLOT_1.setFunction(function() ping.expressionTest() end)
function ping.expressionTest() function ping.expressionTest()
setExpression(1,0) changeExpression("hurt", 10)
changeExpression(nil, 1, 10)
end end
action_wheel.SLOT_2.setTitle('log health') action_wheel.SLOT_2.setTitle('log health')
action_wheel.SLOT_2.setFunction(function() print(player.getHealth()) end) action_wheel.SLOT_2.setFunction(function() print(player.getHealth()) end)
@ -740,14 +756,7 @@ action_wheel.SLOT_4.setFunction(function() ping.tPose() end)
-- Pings -- -- Pings --
--- Damage function -- --- Damage function --
function ping.oof(health) -- This is a replacement for onDamage, that function doesn't sync for some reason function ping.oof(health) -- This is a replacement for onDamage, that function doesn't sync for some reason
if health <= 5 then hurt()
setExpression(1,0)
end
changeExpression(nil,1,10)
end
--- Heal function (revert expression) --
function ping.healed(health)
setExpression(0,0)
end end
--- Toggle Armor --- --- Toggle Armor ---
@ -983,7 +992,8 @@ end
-- initialize values -- {{{ -- initialize values -- {{{
function player_init() function player_init()
old_state.health=player.getHealth() local_state.health=player.getHealth()
old_state.health=local_state.health
for k, v in pairs(reduce(mergeTable, map(recurseModelGroup, model))) do for k, v in pairs(reduce(mergeTable, map(recurseModelGroup, model))) do
v.setEnabled(true) v.setEnabled(true)
end end
@ -1004,13 +1014,19 @@ anim_tick=0
-- }}} -- }}}
-- Tick function -- {{{ -- Tick function -- {{{
function hostTick()
local_state.health=player.getHealth()
if local_state.health ~= old_state.health then
if local_state.health < old_state.health then
ping.oof(local_state.health)
end
syncState()
end
end
function tick() function tick()
-- optimization, only execute these once a second -- -- optimization, only execute these once a second --
if world.getTimeOfDay() % 20 == 0 then if world.getTimeOfDay() % 20 == 0 then
-- if face is cracked
if expr_current.damage==1 and player.getHealth() > 5 then
ping.healed()
end
if player.getAnimation() == "SLEEPING" then if player.getAnimation() == "SLEEPING" then
if cooldown(20*4, "snore") then if cooldown(20*4, "snore") then
@ -1024,12 +1040,7 @@ function tick()
end end
end end
-- Damage ping (onDamage doesn't work in multiplayer) -- hostTick()
if old_state.health>player.getHealth() then
-- debug
-- print(string.format('old_health=%03.2f, player.getHealth=%03.2f', old_health,player.getHealth()))
ping.oof(player.getHealth())
end
if animation.tpose.isPlaying() and local_state.emote_vector.distanceTo(player.getPos()) >= 0.5 then if animation.tpose.isPlaying() and local_state.emote_vector.distanceTo(player.getPos()) >= 0.5 then
animation.tpose.stop() animation.tpose.stop()