api.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.7, 0.3},
  41. stepheight = model.stepheight or 0.6,
  42. eye_height = model.eye_height or 1.47,
  43. })
  44. player_api.set_animation(player, "stand")
  45. else
  46. player:set_properties({
  47. textures = {"player.png", "player_back.png"},
  48. visual = "upright_sprite",
  49. collisionbox = {-0.3, 0.0, -0.3, 0.3, 1.75, 0.3},
  50. stepheight = 0.6,
  51. eye_height = 1.625,
  52. })
  53. end
  54. player_model[name] = model_name
  55. end
  56. function player_api.set_textures(player, textures)
  57. local name = player:get_player_name()
  58. local model = models[player_model[name]]
  59. local model_textures = model and model.textures or nil
  60. player_textures[name] = textures or model_textures
  61. player:set_properties({textures = textures or model_textures,})
  62. end
  63. function player_api.set_animation(player, anim_name, speed)
  64. local name = player:get_player_name()
  65. if player_anim[name] == anim_name then
  66. return
  67. end
  68. local model = player_model[name] and models[player_model[name]]
  69. if not (model and model.animations[anim_name]) then
  70. return
  71. end
  72. local anim = model.animations[anim_name]
  73. player_anim[name] = anim_name
  74. player:set_animation(anim, speed or model.animation_speed, animation_blend)
  75. end
  76. minetest.register_on_leaveplayer(function(player)
  77. local name = player:get_player_name()
  78. player_model[name] = nil
  79. player_anim[name] = nil
  80. player_textures[name] = nil
  81. end)
  82. -- Localize for better performance.
  83. local player_set_animation = player_api.set_animation
  84. local player_attached = player_api.player_attached
  85. -- Check each player and apply animations
  86. minetest.register_globalstep(function(dtime)
  87. for _, player in pairs(minetest.get_connected_players()) do
  88. local name = player:get_player_name()
  89. local model_name = player_model[name]
  90. local model = model_name and models[model_name]
  91. if model and not player_attached[name] then
  92. local controls = player:get_player_control()
  93. local walking = false
  94. local animation_speed_mod = model.animation_speed or 30
  95. -- Determine if the player is walking
  96. if controls.up or controls.down or controls.left or controls.right then
  97. walking = true
  98. end
  99. -- Determine if the player is sneaking, and reduce animation speed if so
  100. if controls.sneak then
  101. animation_speed_mod = animation_speed_mod / 2
  102. end
  103. -- Apply animations based on what the player is doing
  104. if player:get_hp() == 0 then
  105. player_set_animation(player, "lay")
  106. elseif walking then
  107. if player_sneak[name] ~= controls.sneak then
  108. player_anim[name] = nil
  109. player_sneak[name] = controls.sneak
  110. end
  111. if controls.LMB then
  112. player_set_animation(player, "walk_mine", animation_speed_mod)
  113. else
  114. player_set_animation(player, "walk", animation_speed_mod)
  115. end
  116. elseif controls.LMB then
  117. player_set_animation(player, "mine")
  118. else
  119. player_set_animation(player, "stand", animation_speed_mod)
  120. end
  121. end
  122. end
  123. end)