forked from thunderdog1138/star_wars
Improve and optimize the player script and add better comments
This commit is contained in:
parent
c7a4a68d28
commit
7a01de2f36
|
@ -1,9 +1,14 @@
|
||||||
-- Minetest 0.4 mod: player
|
-- Minetest 0.4 mod: player
|
||||||
-- See README.txt for licensing and other information.
|
-- See README.txt for licensing and other information.
|
||||||
|
|
||||||
-- Animation speed
|
--
|
||||||
|
-- Start of configuration area:
|
||||||
|
--
|
||||||
|
|
||||||
|
-- Player animation speed
|
||||||
animation_speed = 30
|
animation_speed = 30
|
||||||
-- Animation blending
|
|
||||||
|
-- Player animation blending
|
||||||
-- Note: This is currently broken due to a bug in Irrlicht, leave at 0
|
-- Note: This is currently broken due to a bug in Irrlicht, leave at 0
|
||||||
animation_blend = 0
|
animation_blend = 0
|
||||||
|
|
||||||
|
@ -11,16 +16,6 @@ animation_blend = 0
|
||||||
default_model = "character.x"
|
default_model = "character.x"
|
||||||
default_texture = "character.png"
|
default_texture = "character.png"
|
||||||
|
|
||||||
-- Player states
|
|
||||||
local player_model = {}
|
|
||||||
local player_anim = {}
|
|
||||||
local player_sneak = {}
|
|
||||||
local ANIM_STAND = 0
|
|
||||||
local ANIM_WALK = 1
|
|
||||||
local ANIM_WALK_MINE = 2
|
|
||||||
local ANIM_MINE = 3
|
|
||||||
local ANIM_DEATH = 4
|
|
||||||
|
|
||||||
-- Frame ranges for each player model
|
-- Frame ranges for each player model
|
||||||
function player_get_animations(model)
|
function player_get_animations(model)
|
||||||
if model == "character.x" then
|
if model == "character.x" then
|
||||||
|
@ -39,54 +34,70 @@ function player_get_animations(model)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Called whenever a player's appearance needs to be updated
|
--
|
||||||
function player_update_visuals(player)
|
-- End of configuration area.
|
||||||
player_model[player:get_player_name()] = default_model
|
--
|
||||||
player_anim[player:get_player_name()] = ANIM_STAND
|
|
||||||
|
|
||||||
local name = player:get_player_name()
|
-- Player stats and animations
|
||||||
local anim = player_get_animations(player_model[name])
|
local player_model = {}
|
||||||
|
local player_anim = {}
|
||||||
|
local player_sneak = {}
|
||||||
|
local ANIM_STAND = 1
|
||||||
|
local ANIM_WALK = 2
|
||||||
|
local ANIM_WALK_MINE = 3
|
||||||
|
local ANIM_MINE = 4
|
||||||
|
local ANIM_DEATH = 5
|
||||||
|
|
||||||
|
-- Called when a player's appearance needs to be updated
|
||||||
|
function player_update_visuals(pl)
|
||||||
|
local name = pl:get_player_name()
|
||||||
|
|
||||||
|
player_model[name] = default_model
|
||||||
|
player_anim[name] = 0 -- Animation will be set further below immediately
|
||||||
|
player_sneak[name] = false
|
||||||
prop = {
|
prop = {
|
||||||
mesh = default_model,
|
mesh = default_model,
|
||||||
textures = {default_texture, },
|
textures = {default_texture, },
|
||||||
visual = "mesh",
|
visual = "mesh",
|
||||||
visual_size = {x=1, y=1},
|
visual_size = {x=1, y=1},
|
||||||
}
|
}
|
||||||
player:set_properties(prop)
|
pl:set_properties(prop)
|
||||||
player:set_animation({x=anim.stand_START, y=anim.stand_END}, animation_speed, animation_blend) -- initial animation
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Update appearance when the player joins
|
-- Update appearance when the player joins
|
||||||
minetest.register_on_joinplayer(player_update_visuals)
|
minetest.register_on_joinplayer(player_update_visuals)
|
||||||
|
|
||||||
-- Global environment step function
|
-- Check each player and apply animations
|
||||||
function on_step(dtime)
|
function player_step(dtime)
|
||||||
for _, pl in pairs(minetest.get_connected_players()) do
|
for _, pl in pairs(minetest.get_connected_players()) do
|
||||||
local name = pl:get_player_name()
|
local name = pl:get_player_name()
|
||||||
local anim = player_get_animations(player_model[name])
|
local anim = player_get_animations(player_model[name])
|
||||||
local controls = pl:get_player_control()
|
local controls = pl:get_player_control()
|
||||||
|
|
||||||
local walking = false
|
local walking = false
|
||||||
|
local animation_speed_modified = animation_speed
|
||||||
|
|
||||||
|
-- Determine if the player is walking
|
||||||
if controls.up or controls.down or controls.left or controls.right then
|
if controls.up or controls.down or controls.left or controls.right then
|
||||||
walking = true
|
walking = true
|
||||||
end
|
end
|
||||||
|
|
||||||
local animation_speed_modified = animation_speed
|
-- Determine if the player is sneaking, and reduce animation speed if so
|
||||||
if controls.sneak and pl:get_hp() ~= 0 and (walking or controls.LMB) then
|
if controls.sneak and pl:get_hp() ~= 0 and (walking or controls.LMB) then
|
||||||
animation_speed_modified = animation_speed_modified / 2
|
animation_speed_modified = animation_speed_modified / 2
|
||||||
-- Refresh player animation below
|
-- Refresh player animation below if sneak state changed
|
||||||
if not player_sneak[name] then
|
if not player_sneak[name] then
|
||||||
player_anim[name] = -1
|
player_anim[name] = 0
|
||||||
player_sneak[name] = true
|
player_sneak[name] = true
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
-- Refresh player animation below
|
-- Refresh player animation below if sneak state changed
|
||||||
if player_sneak[name] then
|
if player_sneak[name] then
|
||||||
player_anim[name] = -1
|
player_anim[name] = 0
|
||||||
player_sneak[name] = false
|
player_sneak[name] = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Apply animations based on what the player is doing
|
||||||
if pl:get_hp() == 0 then
|
if pl:get_hp() == 0 then
|
||||||
if player_anim[name] ~= ANIM_DEATH then
|
if player_anim[name] ~= ANIM_DEATH then
|
||||||
-- TODO: The death animation currently loops, we must make it play only once then stay at the last frame somehow
|
-- TODO: The death animation currently loops, we must make it play only once then stay at the last frame somehow
|
||||||
|
@ -114,6 +125,6 @@ function on_step(dtime)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
minetest.register_globalstep(on_step)
|
minetest.register_globalstep(player_step)
|
||||||
|
|
||||||
-- END
|
-- END
|
||||||
|
|
Loading…
Reference in New Issue