api.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. -- Minetest 0.4 mod: player
  2. -- See README.txt for licensing and other information.
  3. player_api = {}
  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. player_api.registered_models = { }
  8. -- Local for speed.
  9. local models = player_api.registered_models
  10. function player_api.register_model(name, def)
  11. models[name] = def
  12. end
  13. -- Player stats and animations
  14. local player_model = {}
  15. local player_textures = {}
  16. local player_anim = {}
  17. local player_sneak = {}
  18. player_api.player_attached = {}
  19. function player_api.get_animation(player)
  20. local name = player:get_player_name()
  21. return {
  22. model = player_model[name],
  23. textures = player_textures[name],
  24. animation = player_anim[name],
  25. }
  26. end
  27. -- Called when a player's appearance needs to be updated
  28. function player_api.set_model(player, model_name)
  29. local name = player:get_player_name()
  30. local model = models[model_name]
  31. if model then
  32. if player_model[name] == model_name then
  33. return
  34. end
  35. player:set_properties({
  36. mesh = model_name,
  37. textures = player_textures[name] or model.textures,
  38. visual = "mesh",
  39. visual_size = model.visual_size or {x=1, y=1},
  40. collisionbox = model.collisionbox or {-0.3, 0.0, -0.3, 0.3, 1.77, 0.3},
  41. })
  42. player_api.set_animation(player, "stand")
  43. else
  44. player:set_properties({
  45. textures = { "player.png", "player_back.png", },
  46. visual = "upright_sprite",
  47. collisionbox = {-0.3, 0.0, -0.3, 0.3, 1.75, 0.3},
  48. })
  49. end
  50. player_model[name] = model_name
  51. end
  52. function player_api.set_textures(player, textures)
  53. local name = player:get_player_name()
  54. local model = models[player_model[name]]
  55. local model_textures = model and model.textures or nil
  56. player_textures[name] = textures or model_textures
  57. player:set_properties({textures = textures or model_textures,})
  58. end
  59. function player_api.set_animation(player, anim_name, speed)
  60. local name = player:get_player_name()
  61. if player_anim[name] == anim_name then
  62. return
  63. end
  64. local model = player_model[name] and models[player_model[name]]
  65. if not (model and model.animations[anim_name]) then
  66. return
  67. end
  68. local anim = model.animations[anim_name]
  69. player_anim[name] = anim_name
  70. player:set_animation(anim, speed or model.animation_speed, animation_blend)
  71. end
  72. minetest.register_on_leaveplayer(function(player)
  73. local name = player:get_player_name()
  74. player_model[name] = nil
  75. player_anim[name] = nil
  76. player_textures[name] = nil
  77. end)
  78. -- Localize for better performance.
  79. local player_set_animation = player_api.set_animation
  80. local player_attached = player_api.player_attached
  81. -- Check each player and apply animations
  82. minetest.register_globalstep(function(dtime)
  83. for _, player in pairs(minetest.get_connected_players()) do
  84. local name = player:get_player_name()
  85. local model_name = player_model[name]
  86. local model = model_name and models[model_name]
  87. if model and not player_attached[name] then
  88. local controls = player:get_player_control()
  89. local walking = false
  90. local animation_speed_mod = model.animation_speed or 30
  91. -- Determine if the player is walking
  92. if controls.up or controls.down or controls.left or controls.right then
  93. walking = true
  94. end
  95. -- Determine if the player is sneaking, and reduce animation speed if so
  96. if controls.sneak then
  97. animation_speed_mod = animation_speed_mod / 2
  98. end
  99. -- Apply animations based on what the player is doing
  100. if player:get_hp() == 0 then
  101. player_set_animation(player, "lay")
  102. elseif walking then
  103. if player_sneak[name] ~= controls.sneak then
  104. player_anim[name] = nil
  105. player_sneak[name] = controls.sneak
  106. end
  107. if controls.LMB then
  108. player_set_animation(player, "walk_mine", animation_speed_mod)
  109. else
  110. player_set_animation(player, "walk", animation_speed_mod)
  111. end
  112. elseif controls.LMB then
  113. player_set_animation(player, "mine")
  114. else
  115. player_set_animation(player, "stand", animation_speed_mod)
  116. end
  117. end
  118. end
  119. end)