-- ============================================================================
-- 共通部位
-- ============================================================================
-- 初期化
function Initialize()
	passedTime = 0
	index = 1		-- 配列は1から始まる
	enemyList = {}
	aptimeRelation = true
	
	-- 設置
	InitEnemyList()
	-- 出現順にソート
	SortEnemyListByAppearTime()
end

-- 更新
function Update(elapsedTime)
	-- 出現させる
	if IsStandby() then
		while IsStandby() and (passedTime >= GetNowStandby()["aptime"]) do
			EnemyLaunch()
		end
	else
		cmd.endScript()
	end

	-- 時間経過
	passedTime = passedTime + elapsedTime;
end

function SortEnemyListByAppearTime()
	table.sort(enemyList, function (val1, val2)
			return val1["aptime"] < val2["aptime"]
		end)
end

function SetAptimeRelation(isMode)
	aptimeRelation = isMode
end

function GetNowStandby()
	return enemyList[index]
end

function ToNextStandby()
	index = index + 1
	return IsStandby()
end

function IsStandby()
	return #enemyList >= index
end

function EnemyLaunch()
	local ene = GetNowStandby()

	-- 作成	
	cmd.set(ene["type"])
	-- 位置
	cmd.setPos(ene.pos.x, ene.pos.y, ene.pos.z)
	-- 移動速度
	cmd.setMoveSpeed(ene.speed)
	-- 移動パターン
	local mlist = ene["mlist"]
	for i=1, #mlist do
		cmd.addMovePos(mlist[i]["x"], mlist[i]["y"], mlist[i]["z"])
	end
	-- 攻撃パターン
	cmd.setAttackType(ene.atktype)
	-- ライフ最大値係数
	cmd.setLifeMaxFact(ene.lifefact)
	-- 相対位置
	if (ene.isrefpos ~= nil) and (ene.isrefpos > 0) then
		cmd.setPosRefTarget(ene.isrefpos)
	end
	-- 出撃
	cmd.launch()

	-- 次の敵
	if ToNextStandby() == false then
		cmd.endScript()
	end
end

-- ============================================================================
-- ユーティリティ関数
-- ============================================================================
-- 自機位置を返す
function GetTargetPos()
	return cmd.getTargetPos()
end

-- 乱数を返す
function GetRandReal()
	return cmd.randReal()
end

-- ============================================================================
-- ユーティリティ(敵配置)
-- ============================================================================
-- Fighter2
function SetFighter2(time, posList, moveSpeed, attackType, lifeMaxFact)
	-- 末尾から加算
	if (aptimeRelation) and (#enemyList > 0) then
		local eneTail = enemyList[#enemyList]
		time = time + eneTail.aptime
	end

	local ene = {aptime=time, type="fighter2", pos=posList[1], speed=moveSpeed, mlist=posList, atktype=attackType, lifefact=lifeMaxFact}
	-- enemyList[#enemyList + 1] = ene
	table.insert(enemyList, ene)
end

-- Doll
function SetDoll(time, posList, moveSpeed, attackType, lifeMaxFact, isRefPos)
	-- 末尾から加算
	if (aptimeRelation) and (#enemyList > 0) then
		local eneTail = enemyList[#enemyList]
		time = time + eneTail.aptime
	end

	local ene = {aptime=time, type="doll", pos=posList[1], speed=moveSpeed, mlist=posList, atktype=attackType, lifefact=lifeMaxFact, isrefpos=isRefPos}
	-- enemyList[#enemyList + 1] = ene
	table.insert(enemyList, ene)
end

-- Big
function SetBig(time, posList, moveSpeed, attackType, lifeMaxFact)
	-- 末尾から加算
	if (aptimeRelation) and (#enemyList > 0) then
		local eneTail = enemyList[#enemyList]
		time = time + eneTail.aptime
	end

	local ene = {aptime=time, type="big", pos=posList[1], speed=moveSpeed, mlist=posList, atktype=attackType, lifefact=lifeMaxFact}
	-- enemyList[#enemyList + 1] = ene
	table.insert(enemyList, ene)
end

-- Big やられたら次の敵を出す
function SetBigNext(time, posList, moveSpeed, attackType, lifeMaxFact)
	-- 末尾から加算
	if (aptimeRelation) and (#enemyList > 0) then
		local eneTail = enemyList[#enemyList]
		time = time + eneTail.aptime
	end

	local ene = {aptime=time, type="big_next", pos=posList[1], speed=moveSpeed, mlist=posList, atktype=attackType, lifefact=lifeMaxFact}
	-- enemyList[#enemyList + 1] = ene
	table.insert(enemyList, ene)
end

-- Tama
function SetTama(time, posList, lifeMaxFact)
	-- 末尾から加算
	if (aptimeRelation) and (#enemyList > 0) then
		local eneTail = enemyList[#enemyList]
		time = time + eneTail.aptime
	end

	local ene = {aptime=time, type="tama", pos=posList[1], speed=1, mlist=posList, atktype=1, lifefact=lifeMaxFact}
	-- enemyList[#enemyList + 1] = ene
	table.insert(enemyList, ene)
end

-- Vader
function SetVader(time, posList, moveSpeed, attackType, lifeMaxFact)
	-- 末尾から加算
	if (aptimeRelation) and (#enemyList > 0) then
		local eneTail = enemyList[#enemyList]
		time = time + eneTail.aptime
	end

	local ene = {aptime=time, type="vader", pos=posList[1], speed=moveSpeed, mlist=posList, atktype=attackType, lifefact=lifeMaxFact}
	-- enemyList[#enemyList + 1] = ene
	table.insert(enemyList, ene)
end

-- Ketama
function SetKetama(time, posList, lifeMaxFact)
	-- 末尾から加算
	if (aptimeRelation) and (#enemyList > 0) then
		local eneTail = enemyList[#enemyList]
		time = time + eneTail.aptime
	end

	local ene = {aptime=time, type="ketama", pos=posList[1], speed=1, mlist=posList, atktype=1, lifefact=lifeMaxFact}
	-- enemyList[#enemyList + 1] = ene
	table.insert(enemyList, ene)
end
