Simplify chest animations

This commit is contained in:
Elias Fleckenstein 2021-01-27 15:45:23 +01:00
parent 7210dfcc1c
commit db3a31c2c4

View file

@ -2,25 +2,13 @@ local S = minetest.get_translator("mcl_chests")
local mod_doc = minetest.get_modpath("doc") local mod_doc = minetest.get_modpath("doc")
-- Chest Entity -- Chest Entity
local entity_animations = {}
local entity_animation_speed = 25 local entity_animation_speed = 25
local entity_animations = {
do ["open"] = {x = 0, y = 10},
local names = {"open", "opened", "close", "closed"} ["open_partly"] = {x = 0, y = 7},
local following = {["open"] = "opened", ["close"] = "closed"} ["close"] = {x = 10, y = 20},
local durations = {10, 0, 10, 5} ["close_partly"] = {x = 13, y = 20},
local anim_start = 0
for index, name in ipairs(names) do
local duration = durations[index]
local anim_end = anim_start + duration
entity_animations[name] = {
bounds = {x = anim_start, y = anim_end},
sched_anim = following[name],
sched_time = duration / entity_animation_speed
} }
anim_start = anim_end
end
end
minetest.register_entity("mcl_chests:chest", { minetest.register_entity("mcl_chests:chest", {
initial_properties = { initial_properties = {
@ -33,18 +21,16 @@ minetest.register_entity("mcl_chests:chest", {
set_animation = function(self, animname) set_animation = function(self, animname)
local anim = entity_animations[animname] local anim = entity_animations[animname]
self.object:set_animation(anim.bounds, entity_animation_speed, 0, false) if not anim then return end
if anim.sched_anim then self.object:set_animation(anim, entity_animation_speed, 0, false)
self.sched_anim = anim.sched_anim
self.sched_time = anim.sched_time
end
end, end,
open = function(self, playername) open = function(self, playername)
self.players[playername] = true self.players[playername] = true
if not self.is_open then if not self.is_open then
self.is_open = true self.is_open = true
self:set_animation("open") self.opened_partly = false
self:set_animation(self.opened_partly and "open_partly" or "open")
minetest.sound_play(self.sound_prefix .. "_open", { minetest.sound_play(self.sound_prefix .. "_open", {
pos = self.node_pos, pos = self.node_pos,
}) })
@ -58,11 +44,12 @@ minetest.register_entity("mcl_chests:chest", {
for _ in pairs(playerlist) do for _ in pairs(playerlist) do
return return
end end
self.is_open = false self:set_animation(self.opened_partly and "close_partly" or "close")
self:set_animation("close")
minetest.sound_play(self.sound_prefix .. "_close", { minetest.sound_play(self.sound_prefix .. "_close", {
pos = self.node_pos, pos = self.node_pos,
}) })
self.is_open = false
self.opened_partly = false
end end
end, end,
@ -100,23 +87,12 @@ minetest.register_entity("mcl_chests:chest", {
on_activate = function(self) on_activate = function(self)
self.object:set_armor_groups({immortal = 1}) self.object:set_armor_groups({immortal = 1})
self:set_animation("closed")
self.players = {} self.players = {}
end, end,
on_step = function(self, dtime) on_step = function(self, dtime)
local sched_anim, sched_time = self.sched_anim, self.sched_time
if not self:check() then if not self:check() then
self.object:remove() self.object:remove()
elseif sched_anim and sched_time then
sched_time = sched_time - dtime
if sched_time < 0 then
self:set_animation(sched_anim)
self.sched_time = nil
self.sched_anim = nil
else
self.sched_time = sched_time
end
end end
end end
}) })