require "UserData/Maps/LuaExamples/jutility"

local tagEmitter			= 1
local tagEmitterDecType		= 11
local tagEmitterIncType		= 12
local tagEmitterDecCount	= 13
local tagEmitterIncCount	= 14
local tagBombusTrap			= 2
local tagPlayerEmitter		= 3
local tagPrevMap			= 1001
local tagNextMap			= 1002

local numProjectileTypes

local emitterType
local emitterCount
local emitterLastTime
local maxProjectiles = 16


-- Functions called by KFY:

function mapOnRoundBegin()
	displayMapName()
	numProjectileTypes = kfyProjectileManager:getNumProjectileTypes()
	emitterType = 2
	emitterCount = 0
	emitterLastTime = 0
	tileString((emitterType < 10 and "0"..emitterType) or emitterType, 11, 60, 2)
	tileString((emitterCount < 10 and "0"..emitterCount) or emitterCount, 16, 60, 2)
end

function mapOnUpdate()
	local roundTime = kfyRound:getTimeMs()

	-- Projectile emitter:
	if emitterCount > 0 and roundTime >= emitterLastTime + ((emitterCount < 6 and 20 * (emitterCount * 0.5 + 0.5)) or 1200) then
		local vel = KfyVec2f(0.0, -100.0)
		if emitterCount < 6 then vel:rotate(kfyRound:getTime() * 80.0) end
		local angleStep = 360 / emitterCount
		for i = 0, emitterCount do
			if i ~= 0 then vel:rotate(angleStep) end
			kfyProjectileManager:createProjectile(emitterType, 232.0, 888.0, vel.x, vel.y)
		end
		kfyMap:tileSetTilesetCoords(tagEmitter, 1, 0)
		kfyMap:tileSleep(tagEmitter, 0.4)
		kfyProjectileManager:addProjectiles()
		emitterLastTime = roundTime
	end
end

function mapOnTileActivate(tag)
	-- Reset emitter face:
	if tag == tagEmitter then
		kfyMap:tileSetTilesetCoords(tagEmitter, 2, 0)
	elseif tag == tagPlayerEmitter then
		kfyMap:tileSetTilesetCoords(tagPlayerEmitter, 2, 0)

	-- Open trap door:
	elseif tag == tagBombusTrap then
		kfyMap:tileSetTilesetCoordsAt(34, 57, 1, 3, 8)
		kfyMap:tileSetTilesetCoordsAt(35, 57, 1, 0, 0)
		kfyMap:tileSetTilesetCoordsAt(36, 57, 1, 0, 0)
		kfyMap:tileSetTilesetCoordsAt(37, 57, 1, 0, 0)
		kfyMap:tileSetTilesetCoordsAt(38, 57, 1, 1, 8)
		kfyMap:tileSetSolidAt(35, 57, false)
		kfyMap:tileSetSolidAt(36, 57, false)
		kfyMap:tileSetSolidAt(37, 57, false)
		kfyMap:tileSetHidden(tagBombusTrap, false)
	end
end

function mapOnTileDeactivate(tag)
end

function mapOnTilePlayerCollision(tag, x, y, player)
	-- Bombus trap:
	if tag == tagBombusTrap then
		-- Close trap door:
		kfyMap:tileSetTilesetCoordsAt(34, 57, 1, 2, 8)
		kfyMap:tileSetTilesetCoordsAt(35, 57, 1, 2, 8)
		kfyMap:tileSetTilesetCoordsAt(36, 57, 1, 2, 8)
		kfyMap:tileSetTilesetCoordsAt(37, 57, 1, 2, 8)
		kfyMap:tileSetTilesetCoordsAt(38, 57, 1, 2, 8)
		kfyMap:tileSetSolidAt(35, 57, true)
		kfyMap:tileSetSolidAt(36, 57, true)
		kfyMap:tileSetSolidAt(37, 57, true)
		kfyMap:tileSetHidden(tagBombusTrap, true)
		kfyMap:tileSleep(tagBombusTrap, 6.0)

		-- Play sound effect and create charged Bombus:
		kfyEffect:playSoundToPlayer("AppData/Sounds/capture_enemy.ogg", player, 1.0)
		bombus = kfyGrenadeManager:createGrenade(5, 584.0, 952.0, 0.0, 0.0)
		bombus:setDeflectCharge(2)
		kfyGrenadeManager:addGrenades()
	end
end

function mapOnTileLmbClick(tag, x, y, player)
	-- Next / Previous map buttons:
	if tag == tagPrevMap then
		kfyRound:prevMap(true)
	elseif tag == tagNextMap then
		kfyRound:nextMap(true)

	-- Increment / Decrement projectile type:
	elseif tag == tagEmitterDecType then
		if emitterType > 1 then emitterType = emitterType - 1
		else emitterType = kfyProjectileManager:getNumProjectileTypes() - 1 end
		tileString((emitterType < 10 and " "..emitterType) or emitterType, 11, 60, 2)
	elseif tag == tagEmitterIncType then
		if emitterType < kfyProjectileManager:getNumProjectileTypes() - 1 then
			emitterType = emitterType + 1
		else emitterType = 1 end
		tileString((emitterType < 10 and " "..emitterType) or emitterType, 11, 60, 2)

	-- Increment / Decrement number of projectiles:
	elseif tag == tagEmitterDecCount then
		emitterCount = (emitterCount > 0 and emitterCount - 1) or maxProjectiles
		tileString((emitterCount < 10 and "0"..emitterCount) or emitterCount, 16, 60, 2)
	elseif tag == tagEmitterIncCount then
		emitterCount = (emitterCount < maxProjectiles and emitterCount + 1) or 0
		tileString((emitterCount < 10 and "0"..emitterCount) or emitterCount, 16, 60, 2)

	-- Create some projectiles and set owner = clicker.
	elseif tag == tagPlayerEmitter and kfyMap:tileIsActive(tagPlayerEmitter) then
		kfyEffect:playSoundAt("AppData/Sounds/PRUTT.ogg", 920.0, 888.0, 0.8 + math.random() * 0.4)
		p1 = kfyProjectileManager:createProjectile(2, 920.0, 888.0, 0.0, -60.0)
		p2 = kfyProjectileManager:createProjectile(2, 920.0, 888.0, 60.0, -60.0)
		p3 = kfyProjectileManager:createProjectile(2, 920.0, 888.0, -60.0, -60.0)
		p1:setOwner(player)
		p2:setOwner(player)
		p3:setOwner(player)
		kfyMap:tileSetTilesetCoords(tagPlayerEmitter, 1, 0)
		kfyMap:tileSleep(tagPlayerEmitter, 0.4)
		kfyProjectileManager:addProjectiles()

	elseif tag == tagEmitter and emitterCount == 0 and kfyMap:tileIsActive(tagPlayerEmitter) then
		kfyEffect:playSoundAt("AppData/Sounds/PRUTT.ogg", 232.0, 888.0, 0.8 + math.random() * 0.4)
		kfyMap:tileSetTilesetCoords(tagEmitter, 1, 0)
		kfyMap:tileSleep(tagEmitter, 0.4)
	end
end
