Best way to handle portals?
#2
When you initialize each portal, instead of storing points, you could use a cBoundingBox. On creating a new portal object, pass two Vector3d objects that define the box's max and min points (MUST be sorted, other words, the min has to contain the lowest coords, the max contains the highest coords). Then use a PlayerOnMoving hook and a for loop to check each portal if the player has walked into it.

-- The Portal meta-object
Portal = {
	Volume = nil,
	Info = ""
}

-- The Portal constructor function
function Portal:new(Min, Max, Info)
	local o = {}
	setmetatable(o, Portal)
	self.__index = self
	o.Volume = cBoundingBox(Min, Max)
	o.Info = Info

	-- Have it insert itself into a global table if desired
	table.insert(g_Portals, o)

	-- And return a reference to the new Portal
	return o
end

Then, to check if the player is inside the portal defined by the bounding box:

function OnPlayerMoving(Player, OldPos, NewPos)
	-- Iterate through each portal
	for _, k in pairs(g_Portals) do
		-- And check if the Players new position is in any portal boxes
		if k.Volume:IsInside(NewPos) then
			-- Do something, grab the k.Info stuff and teleport the player to the desired destination attached to this portal
			-- Also might be a good idea to stop the loop if a match is found to try and reduce overhead
			return true
		end
	end
	return false
end

None of this code is tested, but here's a pretty clean way of going about this.
Reply
Thanks given by:


Messages In This Thread
Best way to handle portals? - by Zee1234 - 12-01-2015, 07:54 AM
RE: Best way to handle portals? - by DiamondToaster - 12-01-2015, 10:56 AM
RE: Best way to handle portals? - by NiLSPACE - 12-01-2015, 05:32 PM



Users browsing this thread: 1 Guest(s)