From fa248bf7da4c1fda7f4f0cf61b53e482eaba68fd Mon Sep 17 00:00:00 2001 From: Guy Liner Date: Thu, 6 Jan 2022 12:20:59 -0500 Subject: [PATCH] Player must stop sprinting if they hit a block in front of them or if the player's sides are touching a wall --- mods/PLAYER/mcl_sprint/init.lua | 72 +++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/mods/PLAYER/mcl_sprint/init.lua b/mods/PLAYER/mcl_sprint/init.lua index 4c0d609c9..805c370cf 100644 --- a/mods/PLAYER/mcl_sprint/init.lua +++ b/mods/PLAYER/mcl_sprint/init.lua @@ -28,7 +28,7 @@ mcl_sprint = {} mcl_sprint.SPEED = 1.3 -local players = {} +players = {} -- Returns true if the player with the given name is sprinting, false if not. -- Returns nil if player does not exist. @@ -40,8 +40,13 @@ function mcl_sprint.is_sprinting(playername) 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) - local playerName = player:get_player_name() + playerName = player:get_player_name() players[playerName] = { sprinting = false, @@ -141,6 +146,66 @@ local function get_top_node_tile(param2, paramtype2) 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) if channel_name == "mcl_sprint:" .. sender then players[sender].clientSprint = minetest.is_yes(message) @@ -160,8 +225,9 @@ minetest.register_globalstep(function(dtime) local player = get_player_by_name(playerName) if player then local ctrl = player:get_player_control() + --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 else players[playerName]["shouldSprint"] = false