local Player3KeepOnLand = Class(function(self, inst)
    self.inst = inst
	self.time = 0
    self.inst:StartUpdatingComponent(self)
  
end)



function Player3KeepOnLand:OnUpdate(dt)
	if self.inst.boatingwith ~=0 then return end
	self.time = self.time+0.25

    --local onWater = 
    if (not self.inst.components.driver or not self.inst.components.driver:GetIsDriving()) and not self.inst.sg:HasStateTag("busy") and not self.inst.components.health:IsDead() then 
        local pt = self.inst:GetPosition()
        local radius = 1.75 --buffer zone because the walls aren't perfecly along the visual line 
        local result_offset = FindValidPositionByFan(0, radius, 12, function(offset)
            local test_point = pt + offset
            if self.inst:GetIsOnWater(test_point.x, test_point.y, test_point.z) then
                return false
            end
            return true 
        end)

        local onWater = result_offset == nil 

        if onWater then 
            pt = self.inst:GetPosition()
            radius = 5 +self.time*0.75
            result_offset = FindValidPositionByFan(0, radius, 12, function(offset)
                local test_point = pt + offset
                if self.inst:GetIsOnWater(test_point.x, test_point.y, test_point.z) then
                    return false
                end
                return true 
            end)

            if result_offset then 
                local moveto = pt + result_offset
                self.inst.Transform:SetPosition(moveto.x, moveto.y, moveto.z)
				self.time = 0
            elseif self.time>10 then 
                if self.inst.components.health then
					--self.inst.components.health:DoDelta(-1,true)
					if self.time==11 then
						self.inst.components.talker:Say("I am drowning! I need to get out of water!")
					end
					if self.time>15 then
						GetPlayer().components.p3stats_saver:RecordStats(self.inst,"PlayerController_spawner:RespawnPlayer3")			
						GetPlayer().components.p3stats_saver.stat["health"] = 0
						self.inst.components.playercontroller_3:DeleteWithStatsRecord(nil,nil,nil,true)	
					end
					print("behave yourself!")
                else
					print("water walk!!")
                end
            end 
        end 
    end 

end


return Player3KeepOnLand
