local mixtime = 30
local maxtime = 360
local attack = 4800
local taskitemlist = {
[5] = {"buling_zhongziding",1},
[7] = {"buling_planttable_item",1},
[10] = {"buling_zhongjiqi_item",1},
[13] = {"buling_glass",1},
[16] = {"buling_tongxuntai_item",1},
}
local function buling_planet(self)
	local world = GetWorld()
	if world and SaveGameIndex:GetCurrentMode() == "desertplanet"then
		world:DoTaskInTime(1,function()
			GetSeasonManager():StartSummer()
		end)
	end
	if world and SaveGameIndex:GetCurrentMode() == "edenplanet"then
		world:DoTaskInTime(1,function()
			GetSeasonManager():StartHumid()
		end)
	end
	--world:ListenForEvent("daysegschanged",function() self:assault() end)
end
local buling_task = Class(function(self, inst, activcb)
    self.inst = inst
	self.monsterday = 0
    self.tasknum = 1
	buling_planet(self)
	self.buling_time = 0
	self.targettime = 0
	self.attacktime = 0
	--[[self.task = self.inst:DoPeriodicTask(1,function()
		self:robotattack()
	end)]]
end)
function buling_task:Getitem()
	if taskitemlist[self.tasknum] then
		return taskitemlist[self.tasknum][1]
	else
		return taskitemlist[self.tasknum]
	end
end
function buling_task:Getitemnum()
	return taskitemlist[self.tasknum][2]
end
function buling_task:nexttask()
	if self:Getitem() == nil then
		self.tasknum = self.tasknum + 1
	end
end
function buling_task:robotattack()
	self.buling_time = self.buling_time + 1 
	self.targettime = self.targettime + 1
	--print(self.buling_time)
	if self.targettime > attack  then
		if not GetPlayer():HasTag("knowattack") then
			GetPlayer():PushEvent("robotattack")
			GetPlayer():AddTag("knowattack")
		end
		if self.attacktime == 0 then
			self.attacktime = math.random(mixtime,maxtime)
		end
		if not TheCamera.interior and self.targettime > attack + self.attacktime then
			self:dixi()
		end
	end
end
function buling_task:assault()
	local day = GetClock():GetNumCycles()
	local tasktime = math.random(30,360)
	if day%10 == 0 and day ~= 0 and self.targettime == 0 and day > self.monsterday then
	
	self.targettime = self.buling_time + tasktime
		self.inst:DoTaskInTime(tasktime,function()
			if day > self.monsterday then
				self:dixi()
				self.monsterday = day
			end
		end)
	end
end
function buling_task:dixi()
	self.targettime = 0
	self.attacktime = 0
	if GetPlayer():HasTag("knowattack") then
		GetPlayer():RemoveTag("knowattack")
	end
	if  GetClock():GetNumCycles() <= 20 then
		local parachute = SpawnPrefab("buling_parachute")
		local pt = Vector3(self.inst.Transform:GetWorldPosition())
		parachute.Transform:SetPosition(pt.x+math.random(-4,4), pt.y, pt.z+math.random(-4,4))
	else
		local firerain = SpawnPrefab("firerain")
		local pt = Vector3(self.inst.Transform:GetWorldPosition())
		pt.x = pt.x+math.random(-8,8)
		pt.z = pt.z+math.random(-8,8)
		firerain.Transform:SetPosition(pt.x, pt.y, pt.z)
		firerain:StartStep()
		local jidi = SpawnPrefab("buling_jidi")
		jidi.Transform:SetPosition(pt.x, pt.y, pt.z)
		jidi:Hide()
		jidi:DoTaskInTime(1.9,function()
		jidi:Show()
		local pos = Vector3(jidi.Transform:GetWorldPosition())
		local ents = TheSim:FindEntities(pos.x,pos.y,pos.z,15)
			for k,v in pairs(ents) do
				if v and v.prefab == "lavapool"  then
					v:Remove()
				end
			end
		end)
	end
	self.targettime = 0
end

function buling_task:itemnexttask()
	if self:Getitem() ~= nil then
		if self.inst.components.inventory:Has(taskitemlist[self.tasknum][1],taskitemlist[self.tasknum][2]) then
			--self.inst.components.inventory:ConsumeByName(taskitemlist[self.tasknum][1],taskitemlist[self.tasknum][2])
			self.tasknum = self.tasknum + 1
		end
	end
end
function buling_task:zzSave(...)--多谢猪哥
        local data = {}
        for _, v in ipairs(arg) do  --保存字段
            data[v] = self[v]
        end
        return data
end
function buling_task:zzLoad(data)--载入字段
	if not data then
		return
	end
	for k, v in pairs(data) do
		self[k] = v or 0
	end
end
function buling_task:OnSave()
	return self:zzSave('tasknum','monsterday','buling_time','targettime','attacktime')
end   
      
function buling_task:OnLoad(data)
    self:zzLoad(data)
end
return buling_task
