bf08149b7c9c672c38ac07e5aa5b0bbc50587355
[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 --
5 -- Start of configuration area:
6 --
7
8 -- Player animation speed
9 animation_speed = 30
10
11 -- Player animation blending
12 -- Note: This is currently broken due to a bug in Irrlicht, leave at 0
13 animation_blend = 0
14
15 -- Default player appearance
16 default_model = "character.x"
17 default_textures = {"character.png", }
18
19 -- Frame ranges for each player model
20 function player_get_animations(model)
21         if model == "character.x" then
22                 return {
23                 stand_START = 0,
24                 stand_END = 79,
25                 sit_START = 81,
26                 sit_END = 160,
27                 lay_START = 162,
28                 lay_END = 166,
29                 walk_START = 168,
30                 walk_END = 187,
31                 mine_START = 189,
32                 mine_END = 198,
33                 walk_mine_START = 200,
34                 walk_mine_END = 219
35                 }
36         end
37 end
38
39 --
40 -- End of configuration area.
41 --
42
43 -- Player stats and animations
44 local player_model = {}
45 local player_anim = {}
46 local player_sneak = {}
47 local ANIM_STAND = 1
48 local ANIM_SIT = 2
49 local ANIM_LAY = 3
50 local ANIM_WALK  = 4
51 local ANIM_WALK_MINE = 5
52 local ANIM_MINE = 6
53
54 -- Called when a player's appearance needs to be updated
55 function player_update_visuals(pl)
56         local name = pl:get_player_name()
57
58         player_model[name] = default_model
59         player_anim[name] = 0 -- Animation will be set further below immediately
60         player_sneak[name] = false
61         prop = {
62                 mesh = default_model,
63                 textures = default_textures,
64                 visual = "mesh",
65                 visual_size = {x=1, y=1},
66         }
67         pl:set_properties(prop)
68 end
69
70 -- Update appearance when the player joins
71 minetest.register_on_joinplayer(player_update_visuals)
72
73 -- Check each player and apply animations
74 function player_step(dtime)
75         for _, pl in pairs(minetest.get_connected_players()) do
76                 local name = pl:get_player_name()
77                 local anim = player_get_animations(player_model[name])
78                 local controls = pl:get_player_control()
79                 local walking = false
80                 local animation_speed_mod = animation_speed
81
82                 -- Determine if the player is walking
83                 if controls.up or controls.down or controls.left or controls.right then
84                         walking = true
85                 end
86
87                 -- Determine if the player is sneaking, and reduce animation speed if so
88                 if controls.sneak and pl:get_hp() ~= 0 and (walking or controls.LMB) then
89                         animation_speed_mod = animation_speed_mod / 2
90                         -- Refresh player animation below if sneak state changed
91                         if not player_sneak[name] then
92                                 player_anim[name] = 0
93                                 player_sneak[name] = true
94                         end
95                 else
96                         -- Refresh player animation below if sneak state changed
97                         if player_sneak[name] then
98                                 player_anim[name] = 0
99                                 player_sneak[name] = false
100                         end
101                 end
102
103                 -- Apply animations based on what the player is doing
104                 if pl:get_hp() == 0 then
105                         if player_anim[name] ~= ANIM_LAY then
106                                 pl:set_animation({x=anim.lay_START, y=anim.lay_END}, animation_speed_mod, animation_blend)
107                                 player_anim[name] = ANIM_LAY
108                         end
109                 elseif walking and controls.LMB then
110                         if player_anim[name] ~= ANIM_WALK_MINE then
111                                 pl:set_animation({x=anim.walk_mine_START, y=anim.walk_mine_END}, animation_speed_mod, animation_blend)
112                                 player_anim[name] = ANIM_WALK_MINE
113                         end
114                 elseif walking then
115                         if player_anim[name] ~= ANIM_WALK then
116                                 pl:set_animation({x=anim.walk_START, y=anim.walk_END}, animation_speed_mod, animation_blend)
117                                 player_anim[name] = ANIM_WALK
118                         end
119                 elseif controls.LMB then
120                         if player_anim[name] ~= ANIM_MINE then
121                                 pl:set_animation({x=anim.mine_START, y=anim.mine_END}, animation_speed_mod, animation_blend)
122                                 player_anim[name] = ANIM_MINE
123                         end
124                 elseif player_anim[name] ~= ANIM_STAND then
125                         pl:set_animation({x=anim.stand_START, y=anim.stand_END}, animation_speed_mod, animation_blend)
126                         player_anim[name] = ANIM_STAND
127                 end
128         end
129 end
130 minetest.register_globalstep(player_step)
131
132 -- END