player.lua 4.4 KB

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