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

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

// Player Aliases
private alias object.ypos	 : player.ypos
private alias object.yvel	 : player.yvel
private alias object.gravity : player.gravity

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function Cork_DebugDraw
reserve function Cork_DebugSpawn


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

private function Cork_DebugDraw
	DrawSprite(0)
end function


private function Cork_DebugSpawn
	CreateTempObject(TypeName[Cork], 0, object.xpos, object.ypos)
	object[tempObjectPos].ypos &= 0xFFFF0000
	GetBit(temp0, object.ypos, 16)
	if temp0 == true
		object[tempObjectPos].ypos++
	end if
	
	// As with the foreach in the ObjectStartup, this line was similarly only added after initial release, in an update
	object[tempObjectPos].drawOrder = 4
end function


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

event ObjectUpdate
	temp4 = stage.waterLevel
	temp4 <<= 16
	temp3 = object.ypos
	if temp3 != temp4
		if temp3 < temp4
			temp3 += 0x10000
			if temp3 < temp4
				temp3 += 0x10000
			end if
		else
			temp3 -= 0x10000
			if temp3 > temp4
				temp3 -= 0x10000
			end if
		end if
	end if

	object.yvel = temp3
	object.yvel -= object.ypos
	if object.yvel > 0
		ObjectTileCollision(CMODE_FLOOR, 0,  16, PATH_A)
	else
		ObjectTileCollision(CMODE_RWALL, 0, -16, PATH_A)
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_SOLID, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)

		if checkResult == COL_TOP
			player[currentPlayer].ypos += object.yvel
		end if

		if checkResult == COL_BOTTOM
			if player[currentPlayer].gravity == GRAVITY_GROUND
				CallFunction(Player_Kill)
			end if
		end if
	next

	object.ypos += object.yvel
end event


event ObjectDraw
	// ...huh???
	// A Player function? And one that's not even a state?
	// And this arrayPos0 is never previously initialised either...
	// I'm not sure if I'd call this a typo since this object already doesn't use any states itself, nor is arrayPos0 used anywhere beyond setup... certainly quite the anomaly...
	if stage.state == Player_HandleSuperPalette_Sonic
		object[arrayPos0].priority = PRIORITY_ACTIVE
	end if
	
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 51)

	// Interesting note - this wasn't in the initial versions of S1, this was only introduced later in an update
	foreach (TypeName[Cork], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 4
	next

	SetTableValue(TypeName[Cork], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Cork_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Cork_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 51)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
