From dce87664d2dfde1fdf0ac54bca638cf06d19f2d1 Mon Sep 17 00:00:00 2001
From: MirceaKitsune <sonichedgehog_hyperblast00@yahoo.com>
Date: Fri, 23 Nov 2012 00:07:35 +0200
Subject: [PATCH] Important improvements to the player script. Instead of
 setting model and texture every X seconds, only do it when the player joins
 (so far)

Add a function which allows registering different frame ranges for different player models

Cosmetic improvements to the player script

More progress on the player animation functions, this commit adds useles code currently
---
 mods/default/player.lua | 95 ++++++++++++++++++++++++-----------------
 1 file changed, 57 insertions(+), 38 deletions(-)

diff --git a/mods/default/player.lua b/mods/default/player.lua
index 8394057a..9d33b781 100644
--- a/mods/default/player.lua
+++ b/mods/default/player.lua
@@ -1,54 +1,73 @@
 -- Minetest 0.4 mod: player
 -- See README.txt for licensing and other information.
 
--- The API documentation in here was moved into doc/lua_api.txt
-
--- Default animation speed. Special animations (such as the walk animation) should be offset from this factor
+-- Animation speed
 animation_speed = 30
-
--- Animation blending / transitioning amount
+-- Animation blending
+-- Note: This is currently broken due to a bug in Irrlicht, leave at 0
 animation_blend = 0
 
--- Animations frame ranges:
--- For player.x:
-animation_player_stand_START = 0
-animation_player_stand_END = 79
-animation_player_walk_forward_START = 81
-animation_player_walk_forward_END = 100
-animation_player_walk_backward_START = 102
-animation_player_walk_backward_END = 121
-animation_player_walk_right_START = 123
-animation_player_walk_right_END = 142
-animation_player_walk_left_START = 144
-animation_player_walk_left_END = 163
-animation_player_mine_START = 165
-animation_player_mine_END = 179
-animation_player_death_START = 181
-animation_player_death_END = 200
-
--- Set mesh for all players
-function switch_player_visual()
+-- Default player appearance
+player_model = "character.x"
+player_texture = "character.png"
+
+-- Frame ranges for each player model
+function player_get_animations(model)
+	if(model == "character.x") then
+		return {
+		stand_START = 0,
+		stand_END = 79,
+		walk_forward_START = 81,
+		walk_forward_END = 100,
+		walk_backward_START = 102,
+		walk_backward_END = 121,
+		walk_right_START = 123,
+		walk_right_END = 142,
+		walk_left_START = 144,
+		walk_left_END = 163,
+		mine_START = 165,
+		mine_END = 179,
+		death_START = 181,
+		death_END = 200
+		}
+	end
+end
+
+-- Called whenever a player's appearance needs to be updated
+function player_update_visuals(player)
 	prop = {
-		mesh = "character.x",
-		textures = {"character.png", },
+		mesh = player_model,
+		textures = {player_texture, },
 		visual = "mesh",
 		visual_size = {x=1, y=1},
 	}
+	player:set_properties(prop)
 
-	for _, obj in pairs(minetest.get_connected_players()) do
-		obj:set_properties(prop)
-		obj:set_animation({x=animation_player_death_START, y=animation_player_death_END}, animation_speed, animation_blend)
-	end
-
-	minetest.after(10.0, switch_player_visual)
+	local anim = player_get_animations(player_model)
+	player:set_animation({x=anim.stand_START, y=anim.stand_END}, animation_speed, animation_blend)
 end
-minetest.after(10.0, switch_player_visual)
 
--- Definitions made by this mod that other mods can use too
-default = {}
+-- Update appearance when the player joins
+minetest.register_on_joinplayer(player_update_visuals)
+
+-- Player states, used to know when to change animations
+local player_anim = {}
+local ANIM_STAND = 1
+local ANIM_WALK_FORWARD = 2
+local ANIM_WALK_BACKWARD = 3
+local ANIM_WALK_LEFT = 4
+local ANIM_WALK_RIGHT = 5
+local ANIM_MINE = 6
+local ANIM_DEATH = 7
 
--- Load other files
-dofile(minetest.get_modpath("default").."/mapgen.lua")
-dofile(minetest.get_modpath("default").."/leafdecay.lua")
+-- Global environment step function
+function on_step(dtime)
+	for _, obj in pairs(minetest.get_connected_players()) do
+		if(player_anim[obj:get_player_name()] == 0) then
+			print("on_step")
+		end
+	end
+end
+minetest.register_globalstep(on_step)
 
 -- END
-- 
GitLab