player.lua 5.8 KB

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