CreateTempObject// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Asteron Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0  : object.timer
private alias object.value1  : object.startPos.x
private alias object.value2  : object.startPos.y
private alias object.value31 : object.wrapOffset.y

// States
private alias 0 : ASTERON_IDLE
private alias 1 : ASTERON_CHECKPLAYERINRANGE
private alias 2 : ASTERON_EXPLODE

// Player Aliases
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos

private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom


// ========================
// Function Declarations
// ========================

reserve function Asteron_Setup
reserve function Asteron_DebugDraw
reserve function Asteron_DebugSpawn


// ========================
// Function Definitions
// ========================

private function Asteron_Setup
	object[arrayPos0].frame 	= 0
	object[arrayPos0].timer 	= 0
	object[arrayPos0].state 	= ASTERON_IDLE
	object[arrayPos0].priority 	= 0
end function


private function Asteron_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


private function Asteron_DebugSpawn
	CreateTempObject(TypeName[Asteron], 0, object.xpos, object.ypos)
	arrayPos0 = object[tempObjectPos].entityPos
	object[arrayPos0].startPos.x = object.xpos
	object[arrayPos0].startPos.y = object.ypos
	object[arrayPos0].drawOrder = 5
	CallFunction(Asteron_Setup)
end function


// ========================
// Events
// ========================

event ObjectUpdate


	switch object.state
	case ASTERON_IDLE
		temp0 = 0

		temp1 = 0
		

		if temp0 == temp1
			object.priority = PRIORITY_ACTIVE
			object.state 	= ASTERON_CHECKPLAYERINRANGE
		end if
		break

	case ASTERON_CHECKPLAYERINRANGE
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.state == ASTERON_CHECKPLAYERINRANGE
				BoxCollisionTest(C_TOUCH, object.entityPos, -96, -96, 96, 96, currentPlayer, -1, -1, 1, 1)
				if checkResult == true
					temp1 = false
					temp0 = object.xpos
					temp0 -= player[currentPlayer].xpos
					Abs(temp0)
					if temp0 >= 0x100000
						temp1 = true
						if object.xpos > player[currentPlayer].xpos
							object.xvel = -0x4000
						else
							object.xvel = 0x4000
						end if
					end if

					temp0 = object.ypos
					temp0 -= player[currentPlayer].ypos
					Abs(temp0)
					if temp0 >= 0x100000
						temp1 = true
						if object.ypos > player[currentPlayer].ypos
							object.yvel = -0x4000
						else
							object.yvel = 0x4000
						end if
					end if

					if temp1 == true
						object.priority = PRIORITY_ACTIVE
						object.timer 	= 65
						object.state 	= ASTERON_EXPLODE
					end if
				end if
			end if
		next
		break

	case ASTERON_EXPLODE
		object.xpos += object.xvel
		object.ypos += object.yvel

		object.frame = object.animationTimer
		object.frame >>= 1
		object.animationTimer++
		object.animationTimer &= 3

		object.timer--
		if object.timer == 0
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, object.xpos, object.ypos)

			CreateTempObject(TypeName[Asteron Spike], 0, object.xpos, object.ypos)
			object[tempObjectPos].ypos -= 0x80000
			object[tempObjectPos].yvel = -0x40000
			object[tempObjectPos].drawOrder = 5

			CreateTempObject(TypeName[Asteron Spike], 1, object.xpos, object.ypos)
			object[tempObjectPos].xpos += 0x80000
			object[tempObjectPos].ypos -= 0x40000
			object[tempObjectPos].xvel = 0x30000
			object[tempObjectPos].yvel = -0x10000
			object[tempObjectPos].drawOrder = 5

			CreateTempObject(TypeName[Asteron Spike], 2, object.xpos, object.ypos)
			object[tempObjectPos].xpos += 0x80000
			object[tempObjectPos].ypos += 0x80000
			object[tempObjectPos].xvel = 0x30000
			object[tempObjectPos].yvel = 0x30000
			object[tempObjectPos].drawOrder = 5

			CreateTempObject(TypeName[Asteron Spike], 2, object.xpos, object.ypos)
			object[tempObjectPos].xpos -= 0x80000
			object[tempObjectPos].ypos += 0x80000
			object[tempObjectPos].xvel = -0x30000
			object[tempObjectPos].yvel = 0x30000
			object[tempObjectPos].direction = FLIP_X
			object[tempObjectPos].drawOrder = 5

			CreateTempObject(TypeName[Asteron Spike], 1, object.xpos, object.ypos)
			object[tempObjectPos].xpos -= 0x80000
			object[tempObjectPos].ypos -= 0x40000
			object[tempObjectPos].xvel = -0x30000
			object[tempObjectPos].yvel = -0x10000
			object[tempObjectPos].direction = FLIP_X
			object[tempObjectPos].drawOrder = 5

			CreateTempObject(TypeName[Smoke Puff], 0, object.xpos, object.ypos)
			object[tempObjectPos].ypos += 0x20000
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Destroy], false)
		end if
		break
		
	end switch

	if object.outOfBounds == true
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.startPos.x
		object.ypos = object.startPos.y
		object.ypos += object.wrapOffset.y
		if object.outOfBounds == true
			arrayPos0 = object.entityPos
			CallFunction(Asteron_Setup)
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next



			
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone09")
	if checkResult == true
		LoadSpriteSheet("MPZ/Objects.gif")
		SpriteFrame(-16, -14, 32, 28, 223, 1)
		SpriteFrame(-16, -14, 32, 28, 190, 1)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -14, 32, 28, 843, 289)
		SpriteFrame(-16, -14, 32, 28, 876, 289)
	end if

	foreach (TypeName[Asteron], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x 	= object[arrayPos0].xpos
		object[arrayPos0].startPos.y 	= object[arrayPos0].ypos
		object[arrayPos0].drawOrder 	= 6
		CallFunction(Asteron_Setup)
	next

	SetTableValue(TypeName[Asteron], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Asteron_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Asteron_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone09")
	if checkResult == true
		LoadSpriteSheet("MPZ/Objects.gif")
		SpriteFrame(-16, -14, 32, 28, 223, 1)
		SpriteFrame(-16, -14, 32, 28, 190, 1)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -14, 32, 28, 843, 289)
		SpriteFrame(-16, -14, 32, 28, 876, 289)
	end if
	
	// Asterons in MPZ have it set to 46, though this value isn't used
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
