Mapgen: Use decoration sidelen 16 for jungletrees and junglegrass
[oweals/minetest_game.git] / mods / default / player.lua
1 -- Minetest 0.4 mod: player
2 -- See README.txt for licensing and other information.
3
4 -- Player animation blending
5 -- Note: This is currently broken due to a bug in Irrlicht, leave at 0
6 local animation_blend = 0
7
8 default.registered_player_models = { }
9
10 -- Local for speed.
11 local models = default.registered_player_models
12
13 function default.player_register_model(name, def)
14         models[name] = def
15 end
16
17 -- Default player appearance
18 default.player_register_model("character.b3d", {
19         animation_speed = 30,
20         textures = {"character.png", },
21         animations = {
22                 -- Standard animations.
23                 stand     = { x=  0, y= 79, },
24                 lay       = { x=162, y=166, },
25                 walk      = { x=168, y=187, },
26                 mine      = { x=189, y=198, },
27                 walk_mine = { x=200, y=219, },
28                 sit       = { x= 81, y=160, },
29         },
30 })
31
32 -- Player stats and animations
33 local player_model = {}
34 local player_textures = {}
35 local player_anim = {}
36 local player_sneak = {}
37 default.player_attached = {}
38
39 function default.player_get_animation(player)
40         local name = player:get_player_name()
41         return {
42                 model = player_model[name],
43                 textures = player_textures[name],
44                 animation = player_anim[name],
45         }
46 end
47
48 -- Called when a player's appearance needs to be updated
49 function default.player_set_model(player, model_name)
50         local name = player:get_player_name()
51         local model = models[model_name]
52         if model then
53                 if player_model[name] == model_name then
54                         return
55                 end
56                 player:set_properties({
57                         mesh = model_name,
58                         textures = player_textures[name] or model.textures,
59                         visual = "mesh",
60                         visual_size = model.visual_size or {x=1, y=1},
61                 })
62                 default.player_set_animation(player, "stand")
63         else
64                 player:set_properties({
65                         textures = { "player.png", "player_back.png", },
66                         visual = "upright_sprite",
67                 })
68         end
69         player_model[name] = model_name
70 end
71
72 function default.player_set_textures(player, textures)
73         local name = player:get_player_name()
74         player_textures[name] = textures
75         player:set_properties({textures = textures,})
76 end
77
78 function default.player_set_animation(player, anim_name, speed)
79         local name = player:get_player_name()
80         if player_anim[name] == anim_name then
81                 return
82         end
83         local model = player_model[name] and models[player_model[name]]
84         if not (model and model.animations[anim_name]) then
85                 return
86         end
87         local anim = model.animations[anim_name]
88         player_anim[name] = anim_name
89         player:set_animation(anim, speed or model.animation_speed, animation_blend)
90 end
91
92 -- Update appearance when the player joins
93 minetest.register_on_joinplayer(function(player)
94         default.player_attached[player:get_player_name()] = false
95         default.player_set_model(player, "character.b3d")
96         player:set_local_animation({x=0, y=79}, {x=168, y=187}, {x=189, y=198}, {x=200, y=219}, 30)
97
98         -- set GUI
99         if not minetest.setting_getbool("creative_mode") then
100                 player:set_inventory_formspec(default.gui_survival_form)
101         end
102         player:hud_set_hotbar_image("gui_hotbar.png")
103         player:hud_set_hotbar_selected_image("gui_hotbar_selected.png")
104 end)
105
106 minetest.register_on_leaveplayer(function(player)
107         local name = player:get_player_name()
108         player_model[name] = nil
109         player_anim[name] = nil
110         player_textures[name] = nil
111 end)
112
113 -- Localize for better performance.
114 local player_set_animation = default.player_set_animation
115 local player_attached = default.player_attached
116
117 -- Check each player and apply animations
118 minetest.register_globalstep(function(dtime)
119         for _, player in pairs(minetest.get_connected_players()) do
120                 local name = player:get_player_name()
121                 local model_name = player_model[name]
122                 local model = model_name and models[model_name]
123                 if model and not player_attached[name] then
124                         local controls = player:get_player_control()
125                         local walking = false
126                         local animation_speed_mod = model.animation_speed or 30
127
128                         -- Determine if the player is walking
129                         if controls.up or controls.down or controls.left or controls.right then
130                                 walking = true
131                         end
132
133                         -- Determine if the player is sneaking, and reduce animation speed if so
134                         if controls.sneak then
135                                 animation_speed_mod = animation_speed_mod / 2
136                         end
137
138                         -- Apply animations based on what the player is doing
139                         if player:get_hp() == 0 then
140                                 player_set_animation(player, "lay")
141                         elseif walking then
142                                 if player_sneak[name] ~= controls.sneak then
143                                         player_anim[name] = nil
144                                         player_sneak[name] = controls.sneak
145                                 end
146                                 if controls.LMB then
147                                         player_set_animation(player, "walk_mine", animation_speed_mod)
148                                 else
149                                         player_set_animation(player, "walk", animation_speed_mod)
150                                 end
151                         elseif controls.LMB then
152                                 player_set_animation(player, "mine")
153                         else
154                                 player_set_animation(player, "stand", animation_speed_mod)
155                         end
156                 end
157         end
158 end)