I've tried to add animations' support to your API, eventually I've managed to make a working function. I've tried it with the wolves and it seems to work acceptably. \o/
First of all, to use an animation you must know which are its first and last frames. Quick method: fetch them from an existing mob's definition; Other method: open (import) the mob's model using Blender, at the window's bottom will be displayed a meter showing the animation frames, with markers named "walk start", "walk end", and alike - those markers will tell you the frame's number.
Secondarily, the animation function itself isn't very useful if the API does not specify when the function has to be called; e.g. you can call the function to load the "moving fast" animation when the mob is moving toward its targed, but it should also be called when the mob is standing still, or simply walking, or digging, etc.
Usage example: apply_animation(self, "moving")
Last but not least: this is my very first attempt to do such a thing, thus don't expect this code to be "bug-proof", elegant or performant; it's just a first step. :)
Now, the code.
The function must be added to your api.lua, while the animations definition must be added to the mob that you want to use it.
I'm releasing this code under the GPLv3 license, which basically means "credit me please, share alike (freely you have been given, freely you shall give), don't use it for closed source software (else your soul will be doomed to <insert unpleasant things here>)".
api.lua
Code: Select all
function apply_animation(self, action)
if (self.animations ~= nil) then
-- For backward compatibility: if the existing entities have been
-- spawned without the "animations" specification in the entity
-- definition, the game will not crash.
-- If (self.animations == nil) then it will trigger line #78
local animations_table = self.animations
-- fetch the "animations" specification from the entity's definition
local standing = animations_table["standing"]
-- fetch the values for the "standing" animation
-- i.e. the mob stands still
local moving = animations_table["moving"]
-- fetch the values for the "moving" animation
-- i.e. the mob walks, swims or flies
local moving_fast = animations_table["moving_fast"]
-- fetch the values for the "moving_fast" animation
-- same as the previous, but for running or similar
local attacking = animations_table["attacking"]
-- fetch the values for the "attacking" animation
-- i.e. the mob bites, punches, digs, etc.
if (action ~= nil) then
-- When calling the function you must specify which action
-- is being done, else the function will abort.
-- It triggers line #80
if (action == "standing") then
self.object:set_animation(
standing[1],
-- i.e. {x = N1, y = N2}
-- N1 = first animation's frame
-- N2 = last animation's frame
standing["frame_speed"],
-- usually a number like 30, or 60, etc.
standing["frame_blend"],
-- a number, 0 to disable, I guess it makes animations smoother
standing["frame_loop"]
-- whether if the animation must be repeated when the last frame
-- has been reached
)
elseif (action == "moving") then
self.object:set_animation(
moving[1],
moving["frame_speed"],
moving["frame_blend"],
moving["frame_loop"]
)
elseif (action == "moving_fast") then
self.object:set_animation(
moving_fast[1],
moving_fast["frame_speed"],
moving_fast["frame_blend"],
moving_fast["frame_loop"]
)
elseif (action == "attacking") then
self.object:set_animation(
attacking[1],
attacking["frame_speed"],
attacking["frame_blend"],
attacking["frame_loop"]
)
else
print("M.I.L.A " ..mila.version..": action \"" .. action ..
"\" not supported or typed wrong.")
end
else
print("M.I.L.A " ..mila.version..": action's value is nil.")
end
end
end
mob definition (frames' values are from the "Mobs Wolf" mob)
Code: Select all
animations = { -- ../minetest/doc/lua_api.txt at line 3278
standing = { -- the mob stands still
{x = 1, y = 60}, -- x: animation start, y: animation end
frame_speed = 30, -- animation's speed
frame_blend = 0, -- should make the animation smoother
frame_loop = true -- if false then the animation runs just once
},
moving = { -- the mob walks, swims or flies
{x = 61, y = 120}, -- x: animation start, y: animation end
frame_speed = 65, -- animation's speed
frame_blend = 0, -- should make the animation smoother
frame_loop = true -- if false then the animation runs just once
},
moving_fast = { -- the mob runs, swims or flies faster
{x = 61, y = 120}, -- x: animation start, y: animation end
frame_speed = 70, -- animation's speed
frame_blend = 0, -- should make the animation smoother
frame_loop = true -- if false then the animation runs just once
},
attacking = { -- the mob bites, punches, etc.
{x = 1, y = 60}, -- x: animation start, y: animation end
frame_speed = 60, -- animation's speed
frame_blend = 0, -- should make the animation smoother
frame_loop = true -- if false then the animation runs just once
},
}
EDIT--
Happy 14 July,
froggy. ;)