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

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

private alias object.value1 : object.startPos.x
private alias object.value2 : object.startPos.y

private alias object.value2 : object.targetPlayer // this is probably NOT meant to be value2, but the code says otherwise so it's prolly a bug

private alias 0 : BALL_HOVER
private alias 1 : BALL_ATTACKING

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


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

reserve function Ball_DebugDraw
reserve function Ball_DebugSpawn


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

private function Ball_DebugDraw
	DrawSprite(0)
end function


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


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

event ObjectUpdate

	object.priority = PRIORITY_ACTIVE
	switch object.state
	case BALL_HOVER
		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 == true
					object.targetPlayer = currentPlayer
					object.state = BALL_ATTACKING
				end if
			end if
		next
		break

	case BALL_ATTACKING
		currentPlayer = object.targetPlayer
		if object.xpos > player[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 = player[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 -= player[currentPlayer].xpos
		Abs(temp0)
		if temp0 <= 0x100000
			temp0 = player[currentPlayer].ypos
			temp0 -= object.ypos
			if temp0 >= 0x400000
				if temp0 <= 0x600000
					if object.outOfBounds == false
						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], false)
					end if
				end if
			end if
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -10, -10, 10, 10, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_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], false)
			end if
		next
		break
		
	end switch

	if object.outOfBounds == true
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.startPos.x
		object.ypos = object.startPos.y
		if object.outOfBounds == true
			object.priority 	= PRIORITY_BOUNDS
			object.state 		= BALL_HOVER
			object.targetPlayer = -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
		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)
	
	foreach (TypeName[Ball], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		object[arrayPos0].targetPlayer = -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


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
		LoadSpriteSheet("CPZ/Objects.gif")
		SpriteFrame(-12, -12, 24, 24, 166, 1)
	
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
