api.lua 4.3 KB

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