Activate waving=1 for more plants
[minetest_hades/hades_revisited.git] / mods / hades_player / init.lua
blobbf61dbfee4b638194ed0efe4c155443024c40a97
1 -- See README.txt for licensing and other information.
3 -- Player animation blending
4 -- Note: This is currently broken due to a bug in Irrlicht, leave at 0
5 local animation_blend = 0
7 hades_player = {}
9 hades_player.registered_player_models = { }
11 -- Local for speed.
12 local models = hades_player.registered_player_models
14 function hades_player.player_register_model(name, def)
15 models[name] = def
16 end
18 -- Default player appearance
19 hades_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 -- Extra animations (not currently used by the game).
30 sit = { x= 81, y=160, },
34 -- Player stats and animations
35 local player_model = {}
36 local player_textures = {}
37 local player_anim = {}
38 local player_sneak = {}
39 hades_player.player_attached = {}
41 function hades_player.player_get_animation(player)
42 local name = player:get_player_name()
43 return {
44 model = player_model[name],
45 textures = player_textures[name],
46 animation = player_anim[name],
48 end
50 -- Called when a player's appearance needs to be updated
51 function hades_player.player_set_model(player, model_name)
52 local name = player:get_player_name()
53 local model = models[model_name]
54 if model then
55 if player_model[name] == model_name then
56 return
57 end
58 player:set_properties({
59 mesh = model_name,
60 textures = player_textures[name] or model.textures,
61 visual = "mesh",
62 visual_size = model.visual_size or {x=1, y=1},
64 hades_player.player_set_animation(player, "stand")
65 else
66 player:set_properties({
67 textures = { "player.png", "player_back.png", },
68 visual = "upright_sprite",
70 end
71 player_model[name] = model_name
72 end
74 function hades_player.player_set_textures(player, textures)
75 local name = player:get_player_name()
76 player_textures[name] = textures
77 player:set_properties({textures = textures,})
78 end
80 function hades_player.player_set_animation(player, anim_name, speed)
81 local name = player:get_player_name()
82 if player_anim[name] == anim_name then
83 return
84 end
85 local model = player_model[name] and models[player_model[name]]
86 if not (model and model.animations[anim_name]) then
87 return
88 end
89 local anim = model.animations[anim_name]
90 player_anim[name] = anim_name
91 player:set_animation(anim, speed or model.animation_speed, animation_blend)
92 end
94 -- Update appearance when the player joins
95 minetest.register_on_joinplayer(function(player)
96 hades_player.player_attached[player:get_player_name()] = false
97 hades_player.player_set_model(player, "character.b3d")
98 player:set_local_animation({x=0, y=79}, {x=168, y=187}, {x=189, y=198}, {x=200, y=219}, 30)
100 -- set GUI
101 if not minetest.is_creative_enabled(player:get_player_name()) then
102 player:set_inventory_formspec(hades_gui.gui_survival_form)
104 player:hud_set_hotbar_itemcount(10)
105 player:hud_set_hotbar_image("hades_gui_hotbar.png")
106 player:hud_set_hotbar_selected_image("hades_gui_hotbar_selected.png")
107 player:set_formspec_prepend("listcolors[#000000;#C68D3C;#ffffff;#C68D3C;#000000]")
108 end)
110 minetest.register_on_leaveplayer(function(player)
111 local name = player:get_player_name()
112 player_model[name] = nil
113 player_anim[name] = nil
114 player_textures[name] = nil
115 end)
117 -- Localize for better performance.
118 local player_set_animation = hades_player.player_set_animation
119 local player_attached = hades_player.player_attached
121 -- Check each player and apply animations
122 minetest.register_globalstep(function(dtime)
123 for _, player in pairs(minetest.get_connected_players()) do
124 local name = player:get_player_name()
125 local model_name = player_model[name]
126 local model = model_name and models[model_name]
128 if model and not player_attached[name] then
129 local controls = player:get_player_control()
130 local walking = false
131 local animation_speed_mod = model.animation_speed or 30
133 -- Determine if the player is walking
134 if controls.up or controls.down or controls.left or controls.right then
135 walking = true
138 -- Determine if the player is sneaking, and reduce animation speed if so
139 if controls.sneak then
140 animation_speed_mod = animation_speed_mod / 2
143 -- Apply animations based on what the player is doing
144 if player:get_hp() == 0 then
145 player_set_animation(player, "lay")
146 elseif walking then
147 if player_sneak[name] ~= controls.sneak then
148 player_anim[name] = nil
149 player_sneak[name] = controls.sneak
151 if controls.LMB then
152 player_set_animation(player, "walk_mine", animation_speed_mod)
153 else
154 player_set_animation(player, "walk", animation_speed_mod)
156 elseif controls.LMB then
157 player_set_animation(player, "mine")
158 else
159 player_set_animation(player, "stand", animation_speed_mod)
163 end)
165 minetest.register_on_dieplayer(function(player)
166 local name = player:get_player_name()
167 minetest.sound_play({name="hades_player_death"}, {to_player=name}, true)
168 end)
170 minetest.register_on_respawnplayer(function(player)
171 local name = player:get_player_name()
172 minetest.sound_play({name="hades_player_respawn"}, {to_player=name}, true)
173 end)