init.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. gamer = {}
  2. minetest.register_item(':', {
  3. type = 'none',
  4. wield_image = 'wieldhand.png',
  5. wield_scale = {x=1,y=1,z=2.5},
  6. range = 10,
  7. tool_capabilities = {
  8. full_punch_interval = 0.9,
  9. max_drop_level = 0,
  10. groupcaps = {
  11. crumbly = {times={[2]=3.00, [3]=0.70}, uses=0, maxlevel=1},
  12. snappy = {times={[3]=0.40}, uses=0, maxlevel=1},
  13. oddly_breakable_by_hand = {times={[1]=3.50,[2]=2.00,[3]=0.10}, uses=0},
  14. cracky = {times={[3]=0.40, [4]=0.30,}, uses=0, maxlevel=2},
  15. spawn = {times={[1]=.1,}, uses=0}
  16. },
  17. }
  18. })
  19. gamer.registered_player_models = { }
  20. -- Local for speed.
  21. local models = gamer.registered_player_models
  22. function gamer.player_register_model(name, def)
  23. models[name] = def
  24. end
  25. -- Default player appearance
  26. gamer.player_register_model('gamer_model.b3d', {
  27. animation_speed = 30,
  28. textures = {'gamer_skin.png', },
  29. animations = {
  30. -- Standard animations.
  31. stand = { x= 0, y= 79, },
  32. lay = { x=162, y=166, },
  33. walk = { x=168, y=187, },
  34. mine = { x=189, y=198, },
  35. walk_mine = { x=200, y=219, },
  36. sit = { x= 81, y=160, },
  37. },
  38. collisionbox = {-0.3, 0.0, -0.3, 0.3, 1.7, 0.3},
  39. stepheight = 0.6,
  40. eye_height = 1.47,
  41. })
  42. -- Player stats and animations
  43. local player_model = {}
  44. local player_textures = {}
  45. local player_anim = {}
  46. local player_sneak = {}
  47. gamer.player_attached = {}
  48. function gamer.player_get_animation(player)
  49. local name = player:get_player_name()
  50. return {
  51. model = player_model[name],
  52. textures = player_textures[name],
  53. animation = player_anim[name],
  54. }
  55. end
  56. -- Called when a player's appearance needs to be updated
  57. function gamer.player_set_model(player, model_name)
  58. local name = player:get_player_name()
  59. local model = models[model_name]
  60. if model then
  61. if player_model[name] == model_name then
  62. return
  63. end
  64. player:set_properties({
  65. mesh = model_name,
  66. textures = player_textures[name] or model.textures,
  67. visual = 'mesh',
  68. visual_size = model.visual_size or {x=1, y=1},
  69. })
  70. gamer.player_set_animation(player, 'stand')
  71. else
  72. player:set_properties({
  73. textures = { 'player.png', 'player_back.png', },
  74. visual = 'upright_sprite',
  75. })
  76. end
  77. player_model[name] = model_name
  78. end
  79. function gamer.player_set_textures(player, textures)
  80. local name = player:get_player_name()
  81. player_textures[name] = textures
  82. player:set_properties({textures = textures,})
  83. end
  84. function gamer.player_set_animation(player, anim_name, speed)
  85. local name = player:get_player_name()
  86. if player_anim[name] == anim_name then
  87. return
  88. end
  89. local model = player_model[name] and models[player_model[name]]
  90. if not (model and model.animations[anim_name]) then
  91. return
  92. end
  93. local anim = model.animations[anim_name]
  94. player_anim[name] = anim_name
  95. player:set_animation(anim, speed or model.animation_speed, animation_blend)
  96. end
  97. -- Update appearance when the player joins
  98. minetest.register_on_joinplayer(function(player)
  99. player:get_inventory():set_size('main', 8*3)
  100. gamer.player_attached[player:get_player_name()] = false
  101. gamer.player_set_model(player, 'gamer_model.b3d')
  102. player:set_local_animation({x=0, y=79}, {x=168, y=187}, {x=189, y=198}, {x=200, y=219}, 30)
  103. player:hud_set_hotbar_image("gui_hotbar.png")
  104. player:hud_set_hotbar_selected_image("gui_hotbar_selected.png")
  105. end)
  106. minetest.register_on_leaveplayer(function(player)
  107. local name = player:get_player_name()
  108. player_model[name] = nil
  109. player_anim[name] = nil
  110. player_textures[name] = nil
  111. end)
  112. -- Localize for better performance.
  113. local player_set_animation = gamer.player_set_animation
  114. local player_attached = gamer.player_attached
  115. -- Check each player and apply animations
  116. minetest.register_globalstep(function(dtime)
  117. for _, player in pairs(minetest.get_connected_players()) do
  118. local name = player:get_player_name()
  119. local model_name = player_model[name]
  120. local model = model_name and models[model_name]
  121. if model and not player_attached[name] then
  122. local controls = player:get_player_control()
  123. local walking = false
  124. local animation_speed_mod = model.animation_speed or 30
  125. -- Determine if the player is walking
  126. if controls.up or controls.down or controls.left or controls.right then
  127. walking = true
  128. end
  129. -- Determine if the player is sneaking, and reduce animation speed if so
  130. if controls.sneak then
  131. animation_speed_mod = animation_speed_mod / 2
  132. end
  133. -- Apply animations based on what the player is doing
  134. if walking then
  135. if player_sneak[name] ~= controls.sneak then
  136. player_anim[name] = nil
  137. player_sneak[name] = controls.sneak
  138. end
  139. if controls.LMB then
  140. player_set_animation(player, 'walk_mine', animation_speed_mod)
  141. else
  142. player_set_animation(player, 'walk', animation_speed_mod)
  143. end
  144. elseif controls.LMB then
  145. player_set_animation(player, 'mine')
  146. else
  147. player_set_animation(player, 'stand', animation_speed_mod)
  148. end
  149. end
  150. end
  151. end)