UltraSound = getClass("UltraSound")

UltraSound.minigameID = "ULTRASOUND"
UltraSound.minigameIdx = 10

UltraSound.levels = {}
UltraSound.levels[1] =
{
	duration = 10000,
	heartDuration = 1000,
}
UltraSound.levels[2] =
{
	duration = 12000,
	heartDuration = 1100,
}
UltraSound.levels[3] =
{
	duration = 15000,
	heartDuration = 1200,
}
UltraSound.levels[4] =
{
	duration = 23000,
	heartDuration = 1400,
}
UltraSound.levels[5] =
{
	duration = 28000,
	heartDuration = 1500,
}

UltraSound.inverse = true
UltraSound.view = {x=350, y=475}

local squaredDistance = function(p1, p2)
	local x = p1.x - p2.x
	local y = p1.y - p2.y
	return x*x + y*y
end

UltraSound.newTarget = function(self)
	local border = {x=100, y=100}
	local dx = (UltraSound.view.x - 700/2)
	local dy = (UltraSound.view.y - 524/2)
	self.prevPos = self.targetPos
	local minDist = 180 * (1 + self.level / 3)
	repeat
		self.targetPos = {
			x = math.Random(-(1024/2 + dx - border.x), 1024/2 - dx - border.x),
			y = math.Random(-(768/2 + dy - border.y), 768/2 - dy - border.y),
		}
	until (self.prevPos == nil or squaredDistance(self.targetPos, self.prevPos) >= minDist*minDist)
	self.moveDuration = math.Random(2000, 6000) * (1 - self.level / 20)
	self.moveTimer = 0
end

UltraSound.newDustTarget = function(self)
	local border = {x=-200, y=-200}
	local dx = (UltraSound.view.x - 700/2)
	local dy = (UltraSound.view.y - 524/2)
	self.prevPos = self.targetPos
	self.targetPos = {
		x = math.Random(-(1024/2 + dx - border.x), 1024/2 - dx - border.x),
		y = math.Random(-(768/2 + dy - border.y), 768/2 - dy - border.y),
	}
	self.moveDuration = math.Random(8000, 14000)
	self.moveTimer = 0
end

UltraSound.newRotation = function(self)
	self.rotDuration = math.Random(3000, 8000)
	self.rotTimer = 0
	self.prevRot = self.targetRot or self.rotation
	self.targetRot = self.prevRot + math.Random(-0.5, 0.5)
end

UltraSound.dustTick = function(self, time)
	if (self.moveTimer == nil) then
		UltraSound.newDustTarget(self)
		self.prevPos = self.targetPos
	end

	self.moveTimer = (self.moveTimer or 0) + time
	if (self.moveTimer >= self.moveDuration) then
		UltraSound.newDustTarget(self)
	end

	local moveFrac = math.scurve(self.moveTimer / self.moveDuration)
	self.x = self.prevPos.x + (self.targetPos.x - self.prevPos.x) * moveFrac
	self.y = self.prevPos.y + (self.targetPos.y - self.prevPos.y) * moveFrac
end

UltraSound.babyTick = function(self, time)
	self:tick(time)

	if (self.active) then
		self.moveTimer = (self.moveTimer or 0) + time
		if (self.moveTimer >= self.moveDuration) then
			UltraSound.newTarget(self)
		end

		self.rotTimer = (self.rotTimer or 0) + time
		if (self.rotTimer >= self.rotDuration) then
			UltraSound.newRotation(self)
		end

		local moveFrac = math.scurve(self.moveTimer / self.moveDuration)
		self.x = self.prevPos.x + (self.targetPos.x - self.prevPos.x) * moveFrac
		self.y = self.prevPos.y + (self.targetPos.y - self.prevPos.y) * moveFrac

		local rotFrac = math.scurve(self.rotTimer / self.rotDuration)
		local rot = self.prevRot + (self.targetRot - self.prevRot) * rotFrac
		self.rotation = rot * 360 / math.pi
	end
end

function UltraSound:initGame(baby)
	baby.level = self.level
	UltraSound.newTarget(baby)
	baby.location = baby.targetPos
	UltraSound.newTarget(baby)
	UltraSound.newRotation(baby)
end

UltraSound.layouts = {}
UltraSound.layouts[1] =
{ class="UltraSound", name="UltraSound MiniGame",
	gameSprites =
	{
		{class="Sprite", name="WORLD",
			children={
				{class="Sprite", x=-400, y=-525, image="ULTRASOUND_WOMB1:minigames", anchor="bottomleft"},
				{class="Sprite", x=-400, y=-525, image="ULTRASOUND_WOMB1:minigames", anchor="bottomright"},
				{class="Sprite", x=-400, y=0, image="ULTRASOUND_WOMB1:minigames", anchor="topleft"},
				{class="Sprite", x=-400, y=0, image="ULTRASOUND_WOMB1:minigames", anchor="topright"},
				{class="Sprite", x=-400, y=0, image="ULTRASOUND_WOMB1:minigames", anchor="bottomleft"},
				{class="Sprite", x=-400, y=0, image="ULTRASOUND_WOMB1:minigames", anchor="bottomright"},
				{class="Sprite", x=300, y=-525, image="ULTRASOUND_WOMB1:minigames", anchor="bottomleft"},
				{class="Sprite", x=300, y=0, image="ULTRASOUND_WOMB1:minigames", anchor="topleft"},
				{class="Sprite", x=300, y=0, image="ULTRASOUND_WOMB1:minigames", anchor="bottomleft"},
				{class="Sprite", name="DUST_BACK_1", x=-700, y=-525, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_BACK:minigames", anchor="center"},
				{class="Sprite", name="DUST_BACK_2", x=-700, y=0, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_BACK:minigames", anchor="center"},
				{class="Sprite", name="DUST_BACK_3", x=-700, y=525, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_BACK:minigames", anchor="center"},
				{class="Sprite", name="DUST_BACK_4", x=0, y=-525, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_BACK:minigames", anchor="center"},
				{class="Sprite", name="DUST_BACK_5", x=0, y=0, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_BACK:minigames", anchor="center"},
				{class="Sprite", name="DUST_BACK_6", x=0, y=525, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_BACK:minigames", anchor="center"},
				{class="Sprite", name="DUST_BACK_7", x=700, y=-525, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_BACK:minigames", anchor="center"},
				{class="Sprite", name="DUST_BACK_8", x=700, y=0, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_BACK:minigames", anchor="center"},
				{class="Sprite", name="DUST_BACK_9", x=700, y=525, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_BACK:minigames", anchor="center"},
				{class="Sprite",
					name="BABY", x=-9999,
					image="ULTRASOUND_BABY:minigames",
					onTick=UltraSound.babyTick,
					anchor={254,272},
					children={
						{class="Sprite", name="HEART", image="ULTRASOUND_HEART:minigames", anchor="center", frameTime=120},
					},
				},
				{class="Sprite", name="DUST_FRONT_2_1", x=-700, y=-525, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_FRONT_2:minigames", anchor="topleft"},
				{class="Sprite", name="DUST_FRONT_2_2", x=-700, y=0, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_FRONT_2:minigames", anchor="topleft"},
				{class="Sprite", name="DUST_FRONT_2_3", x=-700, y=525, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_FRONT_2:minigames", anchor="topleft"},
				{class="Sprite", name="DUST_FRONT_2_4", x=0, y=-525, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_FRONT_2:minigames", anchor="topleft"},
				{class="Sprite", name="DUST_FRONT_2_5", x=0, y=0, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_FRONT_2:minigames", anchor="topleft"},
				{class="Sprite", name="DUST_FRONT_2_6", x=0, y=525, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_FRONT_2:minigames", anchor="topleft"},
				{class="Sprite", name="DUST_FRONT_2_7", x=700, y=-525, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_FRONT_2:minigames", anchor="topleft"},
				{class="Sprite", name="DUST_FRONT_2_8", x=700, y=0, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_FRONT_2:minigames", anchor="topleft"},
				{class="Sprite", name="DUST_FRONT_2_9", x=700, y=525, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_FRONT_2:minigames", anchor="topleft"},

				{class="Sprite", name="DUST_FRONT_1", x=0, y=-100, onTick=UltraSound.dustTick, image="ULTRASOUND_DUST_FRONT_1:minigames", anchor="topleft"},
			},
		},
		{class="Sprite", name="COVER", x=0, y=120, image="ULTRASOUND_COVER:minigames"},
		{class="Sprite", name="VIEW", x=UltraSound.view.x, y=UltraSound.view.y, image="ULTRASOUND_VIEW:minigames", anchor="center"},
		{class="Sprite", name="ARROW", x=UltraSound.view.x, y=UltraSound.view.y, image="ULTRASOUND_ARROW:minigames", anchor={15,90}},
		{class="Label", name="X", x=642, y=191, anchor="topright", font="FONT_ULTRASOUND_XY"},
		{class="Label", name="Y", x=642, y=217, anchor="topright", font="FONT_ULTRASOUND_XY"},
		{class="Sprite", x=50, y=190, image="ULTRASOUND_BEAT_FRAME:minigames"},
		{class="Sprite", name="BEAT", x=50, y=190, image="ULTRASOUND_BEAT_STATIC:minigames", frameTime=90},
		{class="Sprite", name="OFF", x=0, y=120, image="ULTRASOUND_OFF:minigames"},
		{class="Sprite", name="FLASH", x=700/2, y=120+525/2, image="ULTRASOUND_FLASH:minigames", anchor="center"},
		{class="Sprite", name="FRONT", x=0, y=120, image="ULTRASOUND_FRONT:minigames"},
		{class="Sprite", name="LIGHT", x=310, y=601, image="ULTRASOUND_LIGHT:minigames"},
	},
}
