// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Coconuts 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.targetDelay
private alias object.value2 : object.throwID
private alias object.value3 : object.startPos.x
private alias object.value4 : object.startPos.y
private alias object.value5 : object.targetPlayer
private alias object.value6 : object.unused
private alias object.value7 : object.targetDistance

// States
private alias 0 : COCONUTS_AWAITPLAYER
private alias 1 : COCONUTS_MOVING
private alias 2 : COCONUTS_THROW
private alias 3 : COCONUTS_HASTHROWN

// Player Aliases
private alias object.xpos    : player.xpos
private alias object.value38 : player.hitboxLeft
private alias object.value39 : player.hitboxRight
private alias object.value40 : player.hitboxTop
private alias object.value41 : player.hitboxBottom


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

reserve function Coconuts_DebugDraw
reserve function Coconuts_DebugSpawn


// ========================
// Tables
// ========================

private table Coconuts_throwDelays
	32, 24, 16, 40, 32, 16
end table


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

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


private function Coconuts_DebugSpawn
	CreateTempObject(TypeName[Coconuts], 0, object.xpos, object.ypos)
end function


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

event ObjectUpdate
	switch object.state
	case COCONUTS_AWAITPLAYER
		object.priority = PRIORITY_ACTIVE
		object.targetDistance = 0x7FFFFFFF
		object.targetPlayer = 0
		object.unused = 0xFFFF
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = player[currentPlayer].xpos
			temp0 -= object.xpos
			if temp0 < 0
				temp1 = FLIP_NONE
				FlipSign(temp0)
			else
				temp1 = FLIP_X
			end if

			if temp0 < object.targetDistance
				object.targetDistance = temp0
				object.targetPlayer = currentPlayer
				object.direction = temp1
			end if
		next

		temp0 = false
		if object.targetDelay == 0
			if object.targetDistance > -0x600000
				if object.targetDistance < 0x600000
					object.timer 		= 8
					object.targetDelay 	= 32
					object.frame 		= 1
					object.state 		= COCONUTS_THROW
					temp0 				= true
				end if
			end if
		else
			object.targetDelay--
		end if

		if temp0 == false
			object.timer--
			if object.timer < 0
				GetBit(temp0, object.throwID, 0)
				if temp0 == false
					object.yvel = -0x10000
				else
					object.yvel = 0x10000
				end if

				GetTableValue(object.timer, object.throwID, Coconuts_throwDelays)

				object.throwID++
				if object.throwID > 5
					object.throwID = 0
				end if

				object.state = COCONUTS_MOVING
			end if
		end if
		break

	case COCONUTS_MOVING
		object.animationTimer++
		if object.animationTimer == 6
			object.frame = 1
		end if

		if object.animationTimer == 12
			object.frame = 0
			object.animationTimer = 0
		end if

		object.ypos += object.yvel

		object.timer--
		if object.timer < 0
			object.timer = 16
			object.state = COCONUTS_AWAITPLAYER
		end if
		break

	case COCONUTS_THROW
		object.timer--
		if object.timer < 0
			CreateTempObject(TypeName[Coconut], 0, object.xpos, object.ypos)
			if object.direction == FLIP_NONE
				object[tempObjectPos].xpos += 0xB0000
				object[tempObjectPos].xvel = -0x10000
			else
				object[tempObjectPos].xpos -= 0xB0000
				object[tempObjectPos].xvel = 0x10000
			end if
			object[tempObjectPos].ypos -= 0xD0000
			object[tempObjectPos].yvel = -0x10000
			object[tempObjectPos].direction = object.direction
			object[tempObjectPos].priority = PRIORITY_ACTIVE
			object.timer = 8
			object.frame = 2
			object.state = COCONUTS_HASTHROWN
		end if
		break

	case COCONUTS_HASTHROWN
		object.timer--
		if object.timer < 0
			GetBit(temp0, object.throwID, 0)
			if temp0 == false
				object.yvel = -0x10000
			else
				object.yvel = 0x10000
			end if

			GetTableValue(object.timer, object.throwID, Coconuts_throwDelays)
			object.throwID++
			if object.throwID > 5
				object.throwID = 0
			end if

			object.frame = 0
			object.state = COCONUTS_MOVING
		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

		if object.outOfBounds == true
			object.targetDelay = 0
			object.timer = 16
			object.state = COCONUTS_AWAITPLAYER
			object.priority = PRIORITY_BOUNDS
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -12, -16, 12, 16, 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("Zone01")
	if checkResult == true
		LoadSpriteSheet("EHZ/Objects.gif")
		SpriteFrame(-8, -14, 26, 45, 1, 63)
		SpriteFrame(-8, -14, 26, 45, 28, 63)
		SpriteFrame(-8, -14, 26, 45, 55, 63)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-8, -14, 26, 45, 50, 256)
		SpriteFrame(-8, -14, 26, 45, 77, 256)
		SpriteFrame(-8, -14, 26, 45, 104, 256)
	end if

	foreach (TypeName[Coconuts], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		object[arrayPos0].timer = 16
		
		// this is a lie, its later set based on what dir the player is in anyways so this doesn't matter much
		// that is also a lie sorta, the propertyValues in the scene are like "30" so it was prolly smth related to movement anyways LOL
		if object[arrayPos0].propertyValue == 0
			object[arrayPos0].direction = FLIP_NONE
		else
			object[arrayPos0].direction = FLIP_X
		end if
	next

	SetTableValue(TypeName[Coconuts], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Coconuts_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Coconuts_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("EHZ/Objects.gif")
		SpriteFrame(-8, -14, 26, 45, 1, 63)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-8, -14, 26, 45, 50, 256)
	end if
	
	// Not "technically" unused, but it doesn't do anything meaningful
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
