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+dt

    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 
        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
            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
				self.inst.boatingwith = 0
            elseif self.time>2 then 
					--self.inst.components.health:DoDelta(-1,true)
					if self.time==3 then
						self.inst.components.talker:Say("I am drowning! I need to get out of water!")
					end
					if self.time>4 then
						local pt = GetPlayer():GetPosition()
						self.inst.Transform:SetPosition(pt.x, 0, pt.z)
						self.inst.components.playercontroller_3:MountBoat()
						self.time = 0
						---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!")
					print("water walk!!")
            end 
        end 
    end 

end


return Player3KeepOnLand
