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

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

private alias object.value1 : object.collisionOffset.y

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


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

reserve function SnakePlatform_DebugDraw
reserve function SnakePlatform_DebugSpawn


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

// Unused
// Likely would've been used for collsision in some fashion, though no real guesses as to how...
// This table loosely matches the one the original object used, though it's not a 1:1 copy like some other objects do
private table SnakePlatform_unusedTable
	  0,   0,   0,  0
	-40,  16, -32, 32
	-40,   0, -32, 32
	-40, -16, -32, 32
	-40, -32, -32, 32
end table


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

private function SnakePlatform_DebugDraw
	DrawSprite(16)
end function


private function SnakePlatform_DebugSpawn
	CreateTempObject(TypeName[Snake Platform], 0, object.xpos, object.ypos)
end function


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

event ObjectUpdate
	temp0 = object.frame
	object.frame = object.animationTimer
	object.frame >>= 4
	object.animationTimer++
	object.animationTimer &= 0xFF

	object.collisionOffset.y = 0
	if object.frame > temp0
		switch temp0
		case 1
		case 2
		case 3
			object.collisionOffset.y = -0x100000
			break
			
		case 12
		case 13
		case 14
			object.collisionOffset.y = 0x100000
			break
			
		end switch
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		switch temp0
		case 1
			BoxCollisionTest(C_SOLID, object.entityPos, -40, 16, -24, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 2
			BoxCollisionTest(C_SOLID, object.entityPos, -40, 0, -24, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 3
			BoxCollisionTest(C_SOLID, object.entityPos, -40, -16, -24, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 4
			BoxCollisionTest(C_SOLID, object.entityPos, -40, -32, -24, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 5
			BoxCollisionTest(C_SOLID, object.entityPos, -40, -32, -24, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			BoxCollisionTest(C_SOLID, object.entityPos, -24, -32, -8, -16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 6
			BoxCollisionTest(C_SOLID, object.entityPos, -40, -32, -24, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			BoxCollisionTest(C_SOLID, object.entityPos, -24, -32, 8, -16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 7
			BoxCollisionTest(C_SOLID, object.entityPos, -40, -32, -24, 0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			BoxCollisionTest(C_SOLID, object.entityPos, -24, -32, 24, -16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 8
			BoxCollisionTest(C_SOLID, object.entityPos, -40, -32, 40, -16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 9
			BoxCollisionTest(C_SOLID, object.entityPos, 24, -32, 40, 0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			BoxCollisionTest(C_SOLID, object.entityPos, -24, -32, 24, -16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 10
			BoxCollisionTest(C_SOLID, object.entityPos, 24, -32, 40, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			BoxCollisionTest(C_SOLID, object.entityPos, -8, -32, 24, -16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 11
			BoxCollisionTest(C_SOLID, object.entityPos, 24, -32, 40, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			BoxCollisionTest(C_SOLID, object.entityPos, 8, -32, 24, -16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 12
			BoxCollisionTest(C_SOLID, object.entityPos, 24, -32, 40, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 13
			BoxCollisionTest(C_SOLID, object.entityPos, 24, -16, 40, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 14
			BoxCollisionTest(C_SOLID, object.entityPos, 24, 0, 40, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break

		case 15
			BoxCollisionTest(C_SOLID, object.entityPos, 24, 16, 40, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			break
			
		end switch

		if checkResult == COL_TOP
			player[currentPlayer].ypos += object.collisionOffset.y
		end if
	next
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	LoadSpriteSheet("CNZ/Objects.gif")
	SpriteFrame(0, 0, 0, 0, 61, 66)
	SpriteFrame(-40, 16, 16, 16, 1, 82)
	SpriteFrame(-40, 0, 16, 32, 1, 66)
	SpriteFrame(-40, -16, 16, 48, 1, 50)
	SpriteFrame(-40, -32, 16, 64, 1, 34)
	SpriteFrame(-40, -32, 32, 64, 1, 34)
	SpriteFrame(-40, -32, 48, 48, 1, 34)
	SpriteFrame(-40, -32, 64, 32, 1, 34)
	SpriteFrame(-40, -32, 80, 16, 1, 34)
	SpriteFrame(-24, -32, 64, 32, 17, 34)
	SpriteFrame(-8, -32, 48, 48, 33, 34)
	SpriteFrame(8, -32, 32, 64, 49, 34)
	SpriteFrame(24, -32, 16, 64, 65, 34)
	SpriteFrame(24, -16, 16, 48, 65, 50)
	SpriteFrame(24, 0, 16, 32, 65, 66)
	SpriteFrame(24, 16, 16, 16, 65, 82)
	SpriteFrame(-40, -32, 80, 64, 1, 34)

	SetTableValue(TypeName[Snake Platform], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(SnakePlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(SnakePlatform_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("CNZ/Objects.gif")
	SpriteFrame(-40, -32, 80, 64, 1, 34)
	
	// The CNZ2 layouts set it as was in the original game, but it doesn't mean anything in this remake
	// (In the original game it actually had a use, but this remake makes all platform types behave the same)
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
