// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Ball Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 75 : TYPE_BALL

// Function declarations
reserve function Ball_DebugDraw
reserve function Ball_DebugSpawn

// Static Values

// Tables

function Ball_DebugDraw
	DrawSprite(0)
end function


function Ball_DebugSpawn
	CreateTempObject(TypeName[Ball], 0, object.xpos, object.ypos)
	object[tempObjectPos].value2 = -1
end function


event ObjectMain
	object.priority = PRIORITY_ACTIVE
	switch object.state
	case 0
		if object.direction == FLIP_X
			object.yvel += 0x800
			if object.yvel >= 0xC000
				object.direction = FLIP_NONE
			end if
		else
			object.yvel -= 0x800
			if object.yvel <= -0xC000
				object.direction = FLIP_X
			end if
		end if
		object.ypos += object.yvel
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.state == 0
				BoxCollisionTest(C_TOUCH, object.entityPos, -256, -192, 256, 192, currentPlayer, 0, 0, 0, 0)
				if checkResult == 1
					object.value2 = currentPlayer
					object.state = 1
				end if
			end if
		next
		break
	case 1
		currentPlayer = object.value2
		if object.xpos > object[currentPlayer].xpos
			object.xvel -= 0x1000
			if object.xvel < -0x20000
				object.xvel = -0x20000
			end if
		else
			object.xvel += 0x1000
			if object.xvel > 0x20000
				object.xvel = 0x20000
			end if
		end if
		temp0 = object[currentPlayer].ypos
		temp0 -= 0x400000
		if object.ypos > temp0
			object.yvel -= 0x1000
			if object.yvel < -0x20000
				object.yvel = -0x20000
			end if
		else
			object.yvel += 0x1000
			if object.yvel > 0x20000
				object.yvel = 0x20000
			end if
		end if
		object.xpos += object.xvel
		object.ypos += object.yvel
		temp0 = object.xpos
		temp0 -= object[currentPlayer].xpos
		Abs(temp0)
		if temp0 <= 0x100000
			temp0 = object[currentPlayer].ypos
			temp0 -= object.ypos
			if temp0 >= 0x400000
				if temp0 <= 0x600000
					if object.outOfBounds == 0
						CreateTempObject(TypeName[Smoke Puff], 0, object.xpos, object.ypos)
						object[tempObjectPos].drawOrder = 4
						CreateTempObject(TypeName[Chemical Drop], 0, object.xpos, object.ypos)
						object[tempObjectPos].drawOrder = 2
						object.type = TypeName[Blank Object]
						PlaySfx(SfxName[Destroy], 0)
					end if
				end if
			end if
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -10, -10, 10, 10, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				CallFunction(PlayerObject_Hit)
				CreateTempObject(TypeName[Smoke Puff], 0, object.xpos, object.ypos)
				object[tempObjectPos].drawOrder = 4
				CreateTempObject(TypeName[Chemical Drop], 0, object.xpos, object.ypos)
				object[tempObjectPos].drawOrder = 2
				object.type = TypeName[Blank Object]
				PlaySfx(SfxName[Destroy], 0)
			end if
		next
		break
	end switch
	if object.outOfBounds == 1
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.value1
		object.ypos = object.value2
		if object.outOfBounds == 1
			object.priority = PRIORITY_ACTIVE_BOUNDS
			object.state = 0
			object.value2 = -1
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if
	object.animationTimer++
	if object.animationTimer >= 3
		object.animationTimer = 0
		object.frame++
		if object.frame >= 3
			object.frame = 0
		end if
	end if
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone02")
	if checkResult == 1
		LoadSpriteSheet("CPZ/Objects.gif")
		SpriteFrame(-12, -12, 24, 24, 166, 1)
		SpriteFrame(-12, -12, 24, 24, 166, 26)
		SpriteFrame(-12, -12, 24, 24, 166, 51)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-12, -12, 24, 24, 166, 1)
		SpriteFrame(-12, -12, 24, 24, 166, 26)
		SpriteFrame(-12, -12, 24, 24, 166, 51)
	end if
	foreach (TypeName[Ball], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value1 = object[arrayPos0].xpos
		object[arrayPos0].value2 = object[arrayPos0].ypos
		object[arrayPos0].value2 = -1
	next
	SetTableValue(TypeName[Ball], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Ball_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Ball_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
