Fix player model for 5.0.0
[minetest_tutorial_subgame.git] / mods / default / player.lua
blob3b07b86fe0e880e0b6431f6f3f3534541dab8657
1 -- Minetest 0.4 mod: player
2 -- See README.txt for licensing and other information.
4 --[[
6 API
7 ---
9 default.player_register_model(name, def)
10 ^ Register a new model to be used by players.
11 ^ <name> is the model filename such as "character.x", "foo.b3d", etc.
12 ^ See Model Definition below for format of <def>.
14 default.registered_player_models[name]
15 ^ See Model Definition below for format.
17 default.player_set_model(player, model_name)
18 ^ <player> is a PlayerRef.
19 ^ <model_name> is a model registered with player_register_model.
21 default.player_set_animation(player, anim_name [, speed])
22 ^ <player> is a PlayerRef.
23 ^ <anim_name> is the name of the animation.
24 ^ <speed> is in frames per second. If nil, default from the model is used
26 default.player_set_textures(player, textures)
27 ^ <player> is a PlayerRef.
28 ^ <textures> is an array of textures
29 ^ If <textures> is nil, the default textures from the model def are used
31 default.player_get_animation(player)
32 ^ <player> is a PlayerRef.
33 ^ Returns a table containing fields "model", "textures" and "animation".
34 ^ Any of the fields of the returned table may be nil.
36 Model Definition
37 ----------------
39 model_def = {
40 animation_speed = 30, -- Default animation speed, in FPS.
41 textures = {"character.png", }, -- Default array of textures.
42 visual_size = {x=1, y=1,}, -- Used to scale the model.
43 animations = {
44 -- <anim_name> = { x=<start_frame>, y=<end_frame>, },
45 foo = { x= 0, y=19, },
46 bar = { x=20, y=39, },
47 -- ...
53 -- Player animation blending
54 -- Note: This is currently broken due to a bug in Irrlicht, leave at 0
55 local animation_blend = 0
57 default.registered_player_models = { }
59 -- Local for speed.
60 local models = default.registered_player_models
62 function default.player_register_model(name, def)
63 models[name] = def
64 end
66 -- Default player appearance
67 default.player_register_model("character.b3d", {
68 animation_speed = 30,
69 textures = {"character.png", },
70 animations = {
71 -- Standard animations.
72 stand = { x= 0, y= 79, },
73 lay = { x=162, y=166, },
74 walk = { x=168, y=187, },
75 mine = { x=189, y=198, },
76 walk_mine = { x=200, y=219, },
77 -- Extra animations (not currently used by the game).
78 sit = { x= 81, y=160, },
82 -- Player stats and animations
83 local player_model = {}
84 local player_textures = {}
85 local player_anim = {}
86 local player_sneak = {}
87 default.player_attached = {}
89 function default.player_get_animation(player)
90 local name = player:get_player_name()
91 return {
92 model = player_model[name],
93 textures = player_textures[name],
94 animation = player_anim[name],
96 end
98 -- Called when a player's appearance needs to be updated
99 function default.player_set_model(player, model_name)
100 local name = player:get_player_name()
101 local model = models[model_name]
102 if model then
103 if player_model[name] == model_name then
104 return
106 player:set_properties({
107 mesh = model_name,
108 textures = player_textures[name] or model.textures,
109 visual = "mesh",
110 visual_size = model.visual_size or {x=1, y=1},
112 default.player_set_animation(player, "stand")
113 else
114 player:set_properties({
115 textures = { "player.png", "player_back.png", },
116 visual = "upright_sprite",
119 player_model[name] = model_name
122 function default.player_set_textures(player, textures)
123 local name = player:get_player_name()
124 player_textures[name] = textures
125 player:set_properties({textures = textures,})
128 function default.player_set_animation(player, anim_name, speed)
129 local name = player:get_player_name()
130 if player_anim[name] == anim_name then
131 return
133 local model = player_model[name] and models[player_model[name]]
134 if not (model and model.animations[anim_name]) then
135 return
137 local anim = model.animations[anim_name]
138 player_anim[name] = anim_name
139 player:set_animation(anim, speed or model.animation_speed, animation_blend)
142 -- Update appearance when the player joins
143 minetest.register_on_joinplayer(function(player)
144 default.player_attached[player:get_player_name()] = false
145 default.player_set_model(player, "character.b3d")
146 player:set_local_animation({x=0, y=79}, {x=168, y=187}, {x=189, y=198}, {x=200, y=219}, 30)
148 -- set GUI
149 if not minetest.setting_getbool("creative_mode") then
150 player:set_inventory_formspec(default.gui_suvival_form)
152 player:hud_set_hotbar_image("gui_hotbar.png")
153 player:hud_set_hotbar_selected_image("gui_hotbar_selected.png")
154 end)
156 minetest.register_on_leaveplayer(function(player)
157 local name = player:get_player_name()
158 player_model[name] = nil
159 player_anim[name] = nil
160 player_textures[name] = nil
161 end)
163 -- Localize for better performance.
164 local player_set_animation = default.player_set_animation
166 -- Check each player and apply animations
167 minetest.register_globalstep(function(dtime)
168 for _, player in pairs(minetest.get_connected_players()) do
169 local name = player:get_player_name()
170 local model_name = player_model[name]
171 local model = model_name and models[model_name]
172 if model and not default.player_attached[name] then
173 local controls = player:get_player_control()
174 local walking = false
175 local animation_speed_mod = model.animation_speed or 30
177 -- Determine if the player is walking
178 if controls.up or controls.down or controls.left or controls.right then
179 walking = true
182 -- Determine if the player is sneaking, and reduce animation speed if so
183 if controls.sneak then
184 animation_speed_mod = animation_speed_mod / 2
187 -- Apply animations based on what the player is doing
188 if player:get_hp() == 0 then
189 player_set_animation(player, "lay")
190 elseif walking then
191 if player_sneak[name] ~= controls.sneak then
192 player_anim[name] = nil
193 player_sneak[name] = controls.sneak
195 if controls.LMB then
196 player_set_animation(player, "walk_mine", animation_speed_mod)
197 else
198 player_set_animation(player, "walk", animation_speed_mod)
200 elseif controls.LMB then
201 player_set_animation(player, "mine")
202 else
203 player_set_animation(player, "stand", animation_speed_mod)
207 end)