Defaults on registration, ID for comparison

This commit is contained in:
Lars Mueller 2020-10-11 14:01:29 +02:00
parent cc57126bce
commit 78d358c6d7
2 changed files with 20 additions and 21 deletions

View file

@ -476,7 +476,7 @@ The player API can register player models and update the player's appearance.
animation_speed = 30, -- Default animation speed, in keyframes per second animation_speed = 30, -- Default animation speed, in keyframes per second
textures = {"character.png"}, -- Default array of textures textures = {"character.png"}, -- Default array of textures
animations = { animations = {
-- [anim_name] = {x = <start_frame>, y = <end_frame>, collisionbox = default collisionbox, eye_height = default eye height}, -- [anim_name] = {x = <start_frame>, y = <end_frame>, collisionbox = model collisionbox, eye_height = model eye height},
stand = ..., lay = ..., walk = ..., mine = ..., walk_mine = ..., -- required animations stand = ..., lay = ..., walk = ..., mine = ..., walk_mine = ..., -- required animations
sit = ... -- used by boats and other MTG mods sit = ... -- used by boats and other MTG mods
}, },

View file

@ -23,21 +23,22 @@ end
function player_api.register_model(name, def) function player_api.register_model(name, def)
models[name] = def models[name] = def
local collisionboxes = {} def.visual_size = def.visual_size or {x = 1, y = 1}
for _, animation in pairs(def.animations) do def.collisionbox = def.collisionbox or {-0.3, 0.0, -0.3, 0.3, 1.7, 0.3}
if animation.eye_height == def.eye_height then def.stepheight = def.stepheight or 0.6
animation.eye_height = nil def.eye_height = def.eye_height or 1.47
end for animation_name, animation in pairs(def.animations) do
if collisionbox_equals(animation.collisionbox, def.collisionbox) then animation.eye_height = animation.eye_height or def.eye_height
animation.collisionbox = nil animation.collisionbox = animation.collisionbox or def.collisionbox
else for _, other_animation in pairs(def.animations) do
for collisionbox in pairs(collisionboxes) do if other_animation._equals then
if collisionbox_equals(collisionbox, def.collisionbox) then if collisionbox_equals(animation.collisionbox, other_animation.collisionbox) and animation.eye_height == other_animation.eye_height then
animation.collisionbox = collisionbox animation._equals = other_animation._equals
break break
end end
end end
end end
animation._equals = animation._equals or animation_name
end end
end end
@ -62,10 +63,8 @@ function player_api.set_model(player, model_name)
mesh = model_name, mesh = model_name,
textures = player_data.textures or model.textures, textures = player_data.textures or model.textures,
visual = "mesh", visual = "mesh",
visual_size = model.visual_size or {x = 1, y = 1}, visual_size = model.visual_size,
collisionbox = model.collisionbox or {-0.3, 0.0, -0.3, 0.3, 1.7, 0.3}, stepheight = model.stepheight
stepheight = model.stepheight or 0.6,
eye_height = model.eye_height or 1.47,
}) })
local animations = model.animations local animations = model.animations
player:set_local_animation( player:set_local_animation(
@ -75,6 +74,7 @@ function player_api.set_model(player, model_name)
animations.walk_mine, animations.walk_mine,
model.animation_speed or 30 model.animation_speed or 30
) )
-- sets collisionbox & eye_height
player_api.set_animation(player, "stand") player_api.set_animation(player, "stand")
else else
player:set_properties({ player:set_properties({
@ -107,16 +107,15 @@ function player_api.set_animation(player, anim_name, speed)
if player_data.animation == anim_name and player_data.animation_speed == speed then if player_data.animation == anim_name and player_data.animation_speed == speed then
return return
end end
local previous_anim = model.animations[player_data.animation] local previous_anim_equals = model.animations[player_data.animation]._equals
local anim = model.animations[anim_name] local anim = model.animations[anim_name]
player_data.animation = anim_name player_data.animation = anim_name
player_data.animation_speed = speed player_data.animation_speed = speed
player:set_animation(anim, speed, animation_blend) player:set_animation(anim, speed, animation_blend)
-- reference comparison for tables works because register_model ensures same references for same collisionboxes if anim._equals == previous_anim_equals then
if previous_anim.eye_height ~= anim.eye_height or previous_anim.collisionbox ~= anim.collisionbox then
player:set_properties({ player:set_properties({
collisionbox = anim.collisionbox or model.collisionbox, collisionbox = anim.collisionbox,
eye_height = anim.eye_height or model.eye_height eye_height = anim.eye_height
}) })
end end
end end