Player must stop sprinting if they hit a block in front of them or if the player's sides are touching a wall

This commit is contained in:
Guy Liner 2022-01-06 12:20:59 -05:00
parent d38662475c
commit fa248bf7da
1 changed files with 69 additions and 3 deletions

View File

@ -28,7 +28,7 @@ mcl_sprint = {}
mcl_sprint.SPEED = 1.3 mcl_sprint.SPEED = 1.3
local players = {} players = {}
-- Returns true if the player with the given name is sprinting, false if not. -- Returns true if the player with the given name is sprinting, false if not.
-- Returns nil if player does not exist. -- Returns nil if player does not exist.
@ -40,8 +40,13 @@ function mcl_sprint.is_sprinting(playername)
end end
end end
-- Determines if a block is in front of a player
--function mcl_sprint.touching_node(pos)
-- local touch
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local playerName = player:get_player_name() playerName = player:get_player_name()
players[playerName] = { players[playerName] = {
sprinting = false, sprinting = false,
@ -141,6 +146,66 @@ local function get_top_node_tile(param2, paramtype2)
end end
end end
--Is player trying to run into a wall
-- Am I trying to run into a wall?
local function wall_collide(pos,player)
local facing_dir = player:get_look_dir()
local detect_walls = {
["left"] = 0,
["right"] = 0
}
local too_close = 1.1
near = {x=pos.x, y=pos.y, z=pos.z}
--
if pos.x > 0 then
detect_walls["left"] = {x=pos.x,y=pos.y,z=pos.z-1}
detect_walls["right"] = {x=pos.x,y=pos.y,z=pos.z+1}
elseif pos.z < 0 then
detect_walls["left"] = {x=pos.x+1,y=pos.y,z=pos.z}
detect_walls["right"] = {x=pos.x-1,y=pos.y,z=pos.z+1}
elseif pos.x < 0 then
detect_walls["left"] = {x=pos.x,y=pos.y,z=pos.z+1}
detect_walls["right"] = {x=pos.x,y=pos.y,z=pos.z-1}
elseif pos.z > 0 then
detect_walls["left"] = {x=pos.x+1,y=pos.y,z=pos.z}
detect_walls["right"] = {x=pos.x-1,y=pos.y,z=pos.z}
end
print(facing_dir.x.." "..facing_dir.z)
--local near = minetest.find_node_near(pos, 1)
--Get the coordinates of the block directly in front fo me
if facing_dir.x < 0 then
near.x = near.x - 1
elseif facing_dir.x > 0 then
near.x = near.x + 1
elseif facing_dir.z < 0 then
near.z = near.z - 1
elseif facing_dir.z > 0 then
near.z = near.z + 1
end
--Identify the block in front of me
local block_front = minetest.get_node(near).name
local block_left = minetest.get_node(detect_walls["left"]).name
local block_right = minetest.get_node(detect_walls["right"]).name
if near then
-- Am I touching a wall? If so, I can't run.
local wall_front = vector.distance(pos, near)
local wall_left = vector.distance(pos,detect_walls["left"])
local wall_right = vector.distance(pos,detect_walls["right"])
--local wall_sides =
if wall_front < too_close and block_front ~= "air" then
return true
elseif wall_left < too_close and block_left ~= "air" and wall_right < too_close and block_right ~= "air" then
return true
else
return false
end
end
return false
end
minetest.register_on_modchannel_message(function(channel_name, sender, message) minetest.register_on_modchannel_message(function(channel_name, sender, message)
if channel_name == "mcl_sprint:" .. sender then if channel_name == "mcl_sprint:" .. sender then
players[sender].clientSprint = minetest.is_yes(message) players[sender].clientSprint = minetest.is_yes(message)
@ -160,8 +225,9 @@ minetest.register_globalstep(function(dtime)
local player = get_player_by_name(playerName) local player = get_player_by_name(playerName)
if player then if player then
local ctrl = player:get_player_control() local ctrl = player:get_player_control()
--Check if the player should be sprinting --Check if the player should be sprinting
if players[playerName]["clientSprint"] or ctrl.aux1 and ctrl.up and not ctrl.sneak then if players[playerName]["clientSprint"] or ctrl.aux1 and ctrl.up and not ctrl.sneak and not wall_collide(player:get_pos(),player) then
players[playerName]["shouldSprint"] = true players[playerName]["shouldSprint"] = true
else else
players[playerName]["shouldSprint"] = false players[playerName]["shouldSprint"] = false