Update helptext of obsidian
[MineClone/MineClone2.git] / mods / PLAYER / mcl_player / init.lua
blobd2dca49ca316c19517bba61e0f4a5d6cc77978b4
1 -- Minetest 0.4 mod: player
2 -- See README.txt for licensing and other information.
3 mcl_player = {}
5 -- Player animation blending
6 -- Note: This is currently broken due to a bug in Irrlicht, leave at 0
7 local animation_blend = 0
9 mcl_player.registered_player_models = { }
11 -- Local for speed.
12 local models = mcl_player.registered_player_models
14 function mcl_player.player_register_model(name, def)
15 models[name] = def
16 end
18 -- Default player appearance
19 mcl_player.player_register_model("character.b3d", {
20 animation_speed = 30,
21 textures = {"character.png", },
22 animations = {
23 -- Standard animations.
24 stand = { x= 0, y= 79, },
25 lay = { x=162, y=166, },
26 walk = { x=168, y=187, },
27 mine = { x=189, y=198, },
28 walk_mine = { x=200, y=219, },
29 sit = { x= 81, y=160, },
33 -- Player stats and animations
34 local player_model = {}
35 local player_textures = {}
36 local player_anim = {}
37 local player_sneak = {}
38 mcl_player.player_attached = {}
40 function mcl_player.player_get_animation(player)
41 local name = player:get_player_name()
42 return {
43 model = player_model[name],
44 textures = player_textures[name],
45 animation = player_anim[name],
47 end
49 -- Called when a player's appearance needs to be updated
50 function mcl_player.player_set_model(player, model_name)
51 local name = player:get_player_name()
52 local model = models[model_name]
53 if model then
54 if player_model[name] == model_name then
55 return
56 end
57 player:set_properties({
58 mesh = model_name,
59 textures = player_textures[name] or model.textures,
60 visual = "mesh",
61 visual_size = model.visual_size or {x=1, y=1},
63 mcl_player.player_set_animation(player, "stand")
64 else
65 player:set_properties({
66 textures = { "player.png", "player_back.png", },
67 visual = "upright_sprite",
69 end
70 player_model[name] = model_name
71 end
73 function mcl_player.player_set_textures(player, textures, preview)
74 local name = player:get_player_name()
75 player_textures[name] = textures
76 player:set_properties({textures = textures,})
77 if preview then
78 player:get_meta():set_string("mcl_player:preview", preview)
79 end
80 end
82 function mcl_player.player_get_preview(player)
83 local preview = player:get_meta():get_string("mcl_player:preview")
84 if preview == nil or preview == "" then
85 return "player.png"
86 else
87 return preview
88 end
89 end
91 function mcl_player.player_set_animation(player, anim_name, speed)
92 local name = player:get_player_name()
93 if player_anim[name] == anim_name then
94 return
95 end
96 local model = player_model[name] and models[player_model[name]]
97 if not (model and model.animations[anim_name]) then
98 return
99 end
100 local anim = model.animations[anim_name]
101 player_anim[name] = anim_name
102 player:set_animation(anim, speed or model.animation_speed, animation_blend)
105 -- Update appearance when the player joins
106 minetest.register_on_joinplayer(function(player)
107 mcl_player.player_attached[player:get_player_name()] = false
108 mcl_player.player_set_model(player, "character.b3d")
109 player:set_local_animation({x=0, y=79}, {x=168, y=187}, {x=189, y=198}, {x=200, y=219}, 30)
110 player:set_fov(86.1) -- see <https://minecraft.gamepedia.com/Options#Video_settings>>>>
111 end)
113 minetest.register_on_leaveplayer(function(player)
114 local name = player:get_player_name()
115 player_model[name] = nil
116 player_anim[name] = nil
117 player_textures[name] = nil
118 end)
120 -- Localize for better performance.
121 local player_set_animation = mcl_player.player_set_animation
122 local player_attached = mcl_player.player_attached
124 -- Check each player and apply animations
125 minetest.register_globalstep(function(dtime)
126 for _, player in pairs(minetest.get_connected_players()) do
127 local name = player:get_player_name()
128 local model_name = player_model[name]
129 local model = model_name and models[model_name]
130 if model and not player_attached[name] then
131 local controls = player:get_player_control()
132 local walking = false
133 local animation_speed_mod = model.animation_speed or 30
135 -- Determine if the player is walking
136 if controls.up or controls.down or controls.left or controls.right then
137 walking = true
140 -- Determine if the player is sneaking, and reduce animation speed if so
141 if controls.sneak then
142 animation_speed_mod = animation_speed_mod / 2
145 -- Apply animations based on what the player is doing
146 if player:get_hp() == 0 then
147 player_set_animation(player, "lay")
148 elseif walking then
149 if player_sneak[name] ~= controls.sneak then
150 player_anim[name] = nil
151 player_sneak[name] = controls.sneak
153 if controls.LMB then
154 player_set_animation(player, "walk_mine", animation_speed_mod)
155 else
156 player_set_animation(player, "walk", animation_speed_mod)
158 elseif controls.LMB then
159 player_set_animation(player, "mine")
160 else
161 player_set_animation(player, "stand", animation_speed_mod)
165 end)