esprint.lua 3.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
--[[
Sprint mod for Minetest by GunshipPenguin

To the extent possible under law, the author(s)
have dedicated all copyright and related and neighboring rights 
to this software to the public domain worldwide. This software is
distributed without any warranty. 
]]

local players = {}
local staminaHud = {}

minetest.register_on_joinplayer(function(player)
14
	local playerName = player:get_player_name()
15
	players[playerName] = {
crabman77's avatar
crabman77 committed
16 17
		timeOut = false,
		sprinting = false,
18
		stamina = SPRINT_STAMINA, 
19 20
	}
	if SPRINT_HUDBARS_USED then
Wuzzy's avatar
Wuzzy committed
21
		hb.init_hudbar(player, "sprint")
22 23
	else
		players[playerName].hud = player:hud_add({
24
			hud_elem_type = "statbar",
Milan's avatar
Milan committed
25 26
			position = {x=0.503,y=1},
			size = {x=23, y=13.9},
27
			text = "sprint_stamina_icon.png",
28
			number = 20,
Milan's avatar
Milan committed
29 30
			alignment = {x=0,y=0},
            offset = {x=15, y=-142},
31
            }
32 33
		)
	end
34 35
end)
minetest.register_on_leaveplayer(function(player)
36
	local playerName = player:get_player_name()
37 38
	players[playerName] = nil
end)
crabman77's avatar
crabman77 committed
39 40

local gameTime = 0
41 42
minetest.register_globalstep(function(dtime)
	--Get the gametime
crabman77's avatar
crabman77 committed
43
	gameTime = gameTime + dtime
44 45 46 47
	--Loop through all connected players
	for playerName,playerInfo in pairs(players) do
		local player = minetest.get_player_by_name(playerName)
		if player ~= nil then
crabman77's avatar
crabman77 committed
48 49
			local playerPos = player:getpos()
			--Check whether the player wants and can sprint
crabman77's avatar
crabman77 committed
50 51
			if player:get_player_control()["aux1"] and player:get_player_control()["up"] and playerInfo["timeOut"] == false then
				if not playerInfo["sprinting"] then
52 53
					player_monoids.speed:add_change(player, SPRINT_SPEED, "sprint:speed")
					player_monoids.jump:add_change(player, SPRINT_JUMP, "sprint:jump")
crabman77's avatar
crabman77 committed
54 55
					playerInfo["sprinting"] = true
				end
crabman77's avatar
crabman77 committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
				if gameTime > 0.4 then
					local numParticles = math.random(1, 2)
					local playerNode = minetest.get_node({x=playerPos["x"], y=playerPos["y"]-1, z=playerPos["z"]})
					if playerNode["name"] ~= "air" then
						for i=1, numParticles, 1 do
							minetest.add_particle({
								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},
								vel = {x=0, y=5, z=0},
								acc = {x=0, y=-13, z=0},
								expirationtime = math.random(),
								size = math.random()+0.5,
								collisiondetection = true,
								vertical = false,
								texture = "sprint_particle.png",
							})
						end
72 73 74
					end
				end
				playerInfo["stamina"] = playerInfo["stamina"] - dtime
crabman77's avatar
crabman77 committed
75 76
				--if empty stamina
				if playerInfo["stamina"] < 0 then
77
					playerInfo["stamina"] = 0
crabman77's avatar
crabman77 committed
78
					playerInfo["timeOut"] = true
crabman77's avatar
crabman77 committed
79
					playerInfo["sprinting"] = false
80 81
					player_monoids.speed:del_change(player, "sprint:speed")
      				player_monoids.jump:del_change(player, "sprint:jump")
crabman77's avatar
crabman77 committed
82 83 84
					minetest.sound_play("default_breathless",{object=player})
				end
			else
crabman77's avatar
crabman77 committed
85
				if playerInfo["sprinting"] then
86 87
					player_monoids.speed:del_change(player, "sprint:speed")
      				player_monoids.jump:del_change(player, "sprint:jump")
crabman77's avatar
crabman77 committed
88 89
					playerInfo["sprinting"] = false
				end
crabman77's avatar
crabman77 committed
90 91 92 93 94 95 96 97 98 99 100 101
				if playerInfo["stamina"] < SPRINT_STAMINA then
					--if player walk, stamina regen /2
					if player:get_player_control()["up"] then
						playerInfo["stamina"] = playerInfo["stamina"] + dtime/2
					else
						playerInfo["stamina"] = playerInfo["stamina"] + dtime
					end
					-- if stamina regenered
					if playerInfo["stamina"] > SPRINT_STAMINA then
						playerInfo["stamina"] = SPRINT_STAMINA
						playerInfo["timeOut"] = false
					end
102 103
				end
			end
crabman77's avatar
crabman77 committed
104 105 106 107 108 109 110 111
			
			if gameTime > 0.4 then
				-- From playerplus :
				-- am I near a cactus?
				playerPos.y = playerPos.y + 0.1
				if minetest.find_node_near(playerPos, 1, "default:cactus") and player:get_hp() > 0 then
					player:set_hp(player:get_hp()-1)
				end
Wuzzy's avatar
Wuzzy committed
112
			end
113 114
			
			--Update the players's hud sprint stamina bar
115
			if SPRINT_HUDBARS_USED then
Wuzzy's avatar
Wuzzy committed
116
				hb.change_hudbar(player, "sprint", playerInfo["stamina"])
117
			else
118
				local numBars = (playerInfo["stamina"]/SPRINT_STAMINA)*20
119 120
				player:hud_change(playerInfo["hud"], "number", numBars)
			end
crabman77's avatar
crabman77 committed
121
		end	
122
	end
crabman77's avatar
crabman77 committed
123 124 125
	
	if gameTime > 0.4 then
		gameTime = 0
126
	end
crabman77's avatar
crabman77 committed
127
end)