package MGRandKill
import Minigame
import EscaperKillers
import Assets
import Fx
import Coin
import Obstacle
import Fx2

constant ARENA_POS = vec2(-4200, 7500)

public class MGRandKill extends Minigame
	CallbackPeriodic chickenTimer = null
	CallbackPeriodic bunnyTimer = null
	CallbackPeriodic coinTimer = null
	vec2 spawn
	Fx2 fx

	override function getGameRect() returns rect
		return gg_rct_coingamearea

	override function getCamBounds() returns rect
		return gg_rct_coingamebounds

	override function onBegin()
		spawn = getGameRect().randomPoint()
		while not spawn.isTerrainWalkable()
			spawn = getGameRect().randomPoint()
		fx = new Fx2(spawn.toVec3(), Doodads.shimmeringPortal)
		fx.eff.setScale(0.5)
		fx.eff.playAnimation(ANIM_TYPE_STAND)
		for i = 0 to GetRandomInt(5, 7)
			new SmallObstacle(getGameRect().randomPoint(), STAFF_PLAYER)
		doAfter(2.5) ->
			if fx != null
				new SmallRandomKiller(spawn, STAFF_PLAYER)
				new SmallRandomKiller(spawn, STAFF_PLAYER)
				new SmallRandomKiller(spawn, STAFF_PLAYER)
		chickenTimer = doPeriodically(5) cb ->
			new SmallRandomKiller(spawn, STAFF_PLAYER)
		bunnyTimer = doPeriodically(13) cb ->
			new BigRandomKiller(spawn, STAFF_PLAYER)
		coinTimer = doPeriodically(23) cb ->
			for i = 0 to ((tim.getElapsed() / 20) + 2).toInt()
				new Coin(getGameRect().randomPoint(), STAFF_PLAYER)

	override function onEnd()
		if fx != null
			destroy chickenTimer
			destroy bunnyTimer
			destroy coinTimer
			GroupEnumUnitsInRect(ENUM_GROUP, gg_rct_coingamearea, Filter(() -> GetFilterUnit().getOwner() == STAFF_PLAYER))
			for obj from ENUM_GROUP
				Log.debug("onEnd Terminate")
				if obj.getEntity() != null
					obj.getEntity().terminate()
			destroy fx
			fx = null

	override function getStartPosition() returns vec2
		return getGameRect().getCenter()

	override function getName() returns string
		return "|cffccaaddRandom Rumble"

init
	minigames.add(new MGRandKill())