esprint.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. --[[
  2. Sprint mod for Minetest by GunshipPenguin
  3. To the extent possible under law, the author(s)
  4. have dedicated all copyright and related and neighboring rights
  5. to this software to the public domain worldwide. This software is
  6. distributed without any warranty.
  7. ]]
  8. local players = {}
  9. local staminaHud = {}
  10. minetest.register_on_joinplayer(function(player)
  11. local playerName = player:get_player_name()
  12. players[playerName] = {
  13. sprinting = false,
  14. timeOut = 0,
  15. stamina = SPRINT_STAMINA,
  16. shouldSprint = false,
  17. }
  18. if SPRINT_HUDBARS_USED then
  19. hb.init_hudbar(player, "sprint")
  20. else
  21. players[playerName].hud = player:hud_add({
  22. hud_elem_type = "statbar",
  23. position = {x=0.5,y=1},
  24. size = {x=24, y=24},
  25. text = "sprint_stamina_icon.png",
  26. number = 20,
  27. alignment = {x=0,y=1},
  28. offset = {x=-263, y=-110},
  29. }
  30. )
  31. end
  32. end)
  33. minetest.register_on_leaveplayer(function(player)
  34. local playerName = player:get_player_name()
  35. players[playerName] = nil
  36. end)
  37. minetest.register_globalstep(function(dtime)
  38. --Get the gametime
  39. local gameTime = minetest.get_gametime()
  40. --Loop through all connected players
  41. for playerName,playerInfo in pairs(players) do
  42. local player = minetest.get_player_by_name(playerName)
  43. if player ~= nil then
  44. --Check if the player should be sprinting
  45. if player:get_player_control()["aux1"] and player:get_player_control()["up"] then
  46. players[playerName]["shouldSprint"] = true
  47. else
  48. players[playerName]["shouldSprint"] = false
  49. end
  50. --If the player is sprinting, create particles behind him/her
  51. if playerInfo["sprinting"] == true and gameTime % 0.1 == 0 then
  52. local numParticles = math.random(1, 2)
  53. local playerPos = player:getpos()
  54. local playerNode = minetest.get_node({x=playerPos["x"], y=playerPos["y"]-1, z=playerPos["z"]})
  55. if playerNode["name"] ~= "air" then
  56. for i=1, numParticles, 1 do
  57. minetest.add_particle({
  58. pos = {x=playerPos["x"]+math.random(-1,1)*math.random()/2,y=playerPos["y"]+0.1,z=playerPos["z"]+math.random(-1,1)*math.random()/2},
  59. vel = {x=0, y=5, z=0},
  60. acc = {x=0, y=-13, z=0},
  61. expirationtime = math.random(),
  62. size = math.random()+0.5,
  63. collisiondetection = true,
  64. vertical = false,
  65. texture = "sprint_particle.png",
  66. })
  67. end
  68. end
  69. end
  70. --Adjust player states
  71. if players[playerName]["shouldSprint"] == true then --Stopped
  72. setSprinting(playerName, true)
  73. elseif players[playerName]["shouldSprint"] == false then
  74. setSprinting(playerName, false)
  75. end
  76. --Lower the player's stamina by dtime if he/she is sprinting and set his/her state to 0 if stamina is zero
  77. if playerInfo["sprinting"] == true then
  78. playerInfo["stamina"] = playerInfo["stamina"] - dtime
  79. if playerInfo["stamina"] <= 0 then
  80. playerInfo["stamina"] = 0
  81. setSprinting(playerName, false)
  82. end
  83. --Increase player's stamina if he/she is not sprinting and his/her stamina is less than SPRINT_STAMINA
  84. elseif playerInfo["sprinting"] == false and playerInfo["stamina"] < SPRINT_STAMINA then
  85. playerInfo["stamina"] = playerInfo["stamina"] + dtime
  86. end
  87. -- Cap stamina at SPRINT_STAMINA
  88. if playerInfo["stamina"] > SPRINT_STAMINA then
  89. playerInfo["stamina"] = SPRINT_STAMINA
  90. end
  91. --Update the players's hud sprint stamina bar
  92. if SPRINT_HUDBARS_USED then
  93. hb.change_hudbar(player, "sprint", playerInfo["stamina"])
  94. else
  95. local numBars = (playerInfo["stamina"]/SPRINT_STAMINA)*20
  96. player:hud_change(playerInfo["hud"], "number", numBars)
  97. end
  98. end
  99. end
  100. end)
  101. function setSprinting(playerName, sprinting) --Sets the state of a player (0=stopped/moving, 1=sprinting)
  102. local player = minetest.get_player_by_name(playerName)
  103. if players[playerName] then
  104. players[playerName]["sprinting"] = sprinting
  105. if sprinting == true then
  106. player:set_physics_override({speed=SPRINT_SPEED,jump=SPRINT_JUMP})
  107. elseif sprinting == false then
  108. player:set_physics_override({speed=1.0,jump=1.0})
  109. end
  110. return true
  111. end
  112. return false
  113. end