wsprint.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. state = 0,
  14. timeOut = 0,
  15. stamina = SPRINT_STAMINA,
  16. moving = 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 they are moving or not
  45. players[playerName]["moving"] = player:get_player_control()["up"]
  46. --If the player has tapped w longer than SPRINT_TIMEOUT ago, set his/her state to 0
  47. if playerInfo["state"] == 2 then
  48. if playerInfo["timeOut"] + SPRINT_TIMEOUT < gameTime then
  49. players[playerName]["timeOut"] = nil
  50. setState(playerName, 0)
  51. end
  52. --If the player is sprinting, create particles behind him/her
  53. elseif playerInfo["state"] == 3 and gameTime % 0.1 == 0 then
  54. local numParticles = math.random(1, 2)
  55. local playerPos = player:getpos()
  56. local playerNode = minetest.get_node({x=playerPos["x"], y=playerPos["y"]-1, z=playerPos["z"]})
  57. if playerNode["name"] ~= "air" then
  58. for i=1, numParticles, 1 do
  59. minetest.add_particle({
  60. 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},
  61. vel = {x=0, y=5, z=0},
  62. acc = {x=0, y=-13, z=0},
  63. expirationtime = math.random(),
  64. size = math.random()+0.5,
  65. collisiondetection = true,
  66. vertical = false,
  67. texture = "sprint_particle.png",
  68. })
  69. end
  70. end
  71. end
  72. --Adjust player states
  73. if players[playerName]["moving"] == false and playerInfo["state"] == 3 then --Stopped
  74. setState(playerName, 0)
  75. elseif players[playerName]["moving"] == true and playerInfo["state"] == 0 then --Moving
  76. setState(playerName, 1)
  77. elseif players[playerName]["moving"] == false and playerInfo["state"] == 1 then --Primed
  78. setState(playerName, 2)
  79. elseif players[playerName]["moving"] == true and playerInfo["state"] == 2 then --Sprinting
  80. setState(playerName, 3)
  81. end
  82. --Lower the player's stamina by dtime if he/she is sprinting and set his/her state to 0 if stamina is zero
  83. if playerInfo["state"] == 3 then
  84. playerInfo["stamina"] = playerInfo["stamina"] - dtime
  85. if playerInfo["stamina"] <= 0 then
  86. playerInfo["stamina"] = 0
  87. setState(playerName, 0)
  88. end
  89. --Increase player's stamina if he/she is not sprinting and his/her stamina is less than SPRINT_STAMINA
  90. elseif playerInfo["state"] ~= 3 and playerInfo["stamina"] < SPRINT_STAMINA then
  91. playerInfo["stamina"] = playerInfo["stamina"] + dtime
  92. end
  93. -- Cap stamina at SPRINT_STAMINA
  94. if playerInfo["stamina"] > SPRINT_STAMINA then
  95. playerInfo["stamina"] = SPRINT_STAMINA
  96. end
  97. --Update the players's hud sprint stamina bar
  98. if SPRINT_HUDBARS_USED then
  99. hb.change_hudbar(player, "sprint", playerInfo["stamina"])
  100. else
  101. local numBars = (playerInfo["stamina"]/SPRINT_STAMINA)*20
  102. player:hud_change(playerInfo["hud"], "number", numBars)
  103. end
  104. end
  105. end
  106. end)
  107. function setState(playerName, state) --Sets the state of a player (0=stopped, 1=moving, 2=primed, 3=sprinting)
  108. local player = minetest.get_player_by_name(playerName)
  109. local gameTime = minetest.get_gametime()
  110. if players[playerName] then
  111. players[playerName]["state"] = state
  112. if state == 0 then--Stopped
  113. player:set_physics_override({speed=1.0,jump=1.0})
  114. elseif state == 2 then --Primed
  115. players[playerName]["timeOut"] = gameTime
  116. elseif state == 3 then --Sprinting
  117. player:set_physics_override({speed=SPRINT_SPEED,jump=SPRINT_JUMP})
  118. end
  119. return true
  120. end
  121. return false
  122. end