Jump to content

Recommended Posts

Hello!

I'm having some trouble with my Spyglass component, which forces the user's camera to focus on a target prefrab. This works perfectly fine on caveless worlds, but I can't get the FocalPoint's parent with caves enabled.

I assume this is because TheFocalPoint is entirely client-side? How can I get this to work with caves?

Spyglass

Spoiler
local Spyglass = Class(function(self, inst)
    self.inst = inst
	self.spyglass_looker = nil
	self.target = SpawnPrefab("cutgrass") --Placeholder. Change prefab later.
end)

function Spyglass:OnRemoveFromEntity()
	if TheFocalPoint ~= nil then
		TheFocalPoint.components.focalpoint:StopFocusSource(self.inst)
	end
end



--======================--
--==oO Spying Check Oo==--
--======================--
function Spyglass:IsSpying()
    return self.spyglass_looker ~= nil
        and self.spyglass_looker:IsValid()
        and self.spyglass_looker.sg ~= nil
        and self.spyglass_looker.sg:HasStateTag("spying")
end


--================--
--==oO UPDATE Oo==--
--================--
function Spyglass:OnUpdate()
	print("Are we spying	:	"..self:GetDebugString())

	print(TheFocalPoint.entity:GetParent())
	
    if TheFocalPoint.entity:GetParent() == self.spyglass_looker then
		print("Found FocalPoint Parent")
		
		if not self:IsSpying() then	--Check if we have the right statetag.
			self:StopSpying()
		else
			--==oO Camera Oo==--	Continuously update the location of our focus point. Don't want to abandon our focus by sailing.
			local x, y, z = self.spyglass_looker.Transform:GetWorldPosition()
			local angle = (self.spyglass_looker.Transform:GetRotation() + 90) * DEGREES
		
		
			local dist = 45
			local x1 = x + dist * math.sin(angle)
			local z1 = z + dist * math.cos(angle)
		
			self.target.Transform:SetPosition(x1, 0, z1)
			TheFocalPoint.components.focalpoint:StartFocusSource(self.inst, "Spyglass", self.target, 45, 500, 0)
		end
    end
end


--======================--
--==oO START SPYING Oo==--
--======================--
function Spyglass:StartSpying(doer)
	self.spyglass_looker = doer
	if self.spyglass_looker then
		print("I spy!")
		self.inst:StartUpdatingComponent(self)
		return true	
	end
end

--=====================--
--==oO STOP SPYING Oo==--
--=====================--
function Spyglass:StopSpying()
	print("ATTEMPTING TO STOP SPYING")
	if not self:IsSpying() then
		self.spyglass_looker.sg:GoToState("peer_through_spyglass_pst")
		
		if TheFocalPoint ~= nil then
       	    TheFocalPoint.components.focalpoint:StopFocusSource(self.inst)
        end
    end

    self.spyglass_looker = nil
	self.inst:StopUpdatingComponent(self)
end


--===============--
--==oO DEBUG Oo==--
--===============--
function Spyglass:GetDebugString()
    return self:IsSpying() and "Is Spying" or "Not Spying"
end

return Spyglass

 

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...