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

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

private alias object.value1 : object.drawPos.y
private alias object.value2 : object.collisionOffset.y
private alias object.value3 : object.shinePos.y
private alias object.value4 : object.shineAmplitude.y
private alias object.value5 : object.amplitude.y

private alias 0 : GLASSPILLAR_STATIC
private alias 1 : GLASSPILLAR_MOVE_UP_DOWN
private alias 2 : GLASSPILLAR_MOVE_DOWN_UP

// Player Aliases
private alias object.ypos    : player.ypos
private alias object.gravity : player.gravity
private alias object.value18 : player.sortedDrawOrder

private alias object.value17 : debugMode.currentSelection


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

reserve function GlassPillar_DebugDraw
reserve function GlassPillar_DebugSpawn

// ========================
// Static Values
// ========================

private value GlassPillar_startDebugID = 0

private function GlassPillar_DebugDraw
	temp0 = debugMode[0].currentSelection
	temp0 -= GlassPillar_startDebugID
	DrawSprite(temp0)
end function


private function GlassPillar_DebugSpawn
	CreateTempObject(TypeName[Glass Pillar], 0, object.xpos, object.ypos)
	object[tempObjectPos].frame = debugMode[0].currentSelection
	object[tempObjectPos].frame -= GlassPillar_startDebugID

	if object[tempObjectPos].frame == 0
		if object.direction == 1
			object[tempObjectPos].state = GLASSPILLAR_MOVE_DOWN_UP
			object[tempObjectPos].amplitude.y = -0x1000
			object[tempObjectPos].shineAmplitude.y = 0x800
			object[tempObjectPos].ypos -= 0x200000
		else
			object[tempObjectPos].state = GLASSPILLAR_MOVE_UP_DOWN
			object[tempObjectPos].amplitude.y = 0x1000
			object[tempObjectPos].shineAmplitude.y = -0x800
			object[tempObjectPos].ypos -= 0x200000
		end if
	else
		object[tempObjectPos].shineAmplitude.y = 0x1000
	end if

	object[tempObjectPos].drawOrder = 5
end function


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

event ObjectUpdate
	switch object.state
	case GLASSPILLAR_STATIC
		object.angle = oscillation
		object.angle <<= 1

		object.drawPos.y = object.ypos
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID2, object.entityPos, -32, -56, 32, 56, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_BOTTOM
				if player[currentPlayer].gravity == GRAVITY_GROUND
					CallFunction(Player_Kill)
					player[currentPlayer].sortedDrawOrder = 6
				end if
			end if
		next

		object.angle = oscillation
		object.angle <<= 1
		Sin(object.shinePos.y, object.angle)
		object.shinePos.y *= object.shineAmplitude.y
		object.shinePos.y += object.ypos
		object.shinePos.y &= 0xFFFF0000
		break

	case GLASSPILLAR_MOVE_UP_DOWN
	case GLASSPILLAR_MOVE_DOWN_UP
		object.angle = oscillation
		object.angle <<= 1
		Sin(object.collisionOffset.y, object.angle)
		object.collisionOffset.y *= object.amplitude.y
		object.collisionOffset.y += object.ypos
		object.collisionOffset.y &= 0xFFFF0000
		object.collisionOffset.y -= object.drawPos.y

		temp0 = object.ypos
		object.ypos = object.drawPos.y
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID2, object.entityPos, -32, -72, 32, 68, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				if checkResult == true
					player[currentPlayer].ypos += object.collisionOffset.y
				end if
				break

			case COL_BOTTOM
				if player[currentPlayer].gravity == GRAVITY_GROUND
					if object.collisionOffset.y > 0
						CallFunction(Player_Kill)
						player[currentPlayer].sortedDrawOrder = 6
					end if
				end if
				break
			end switch
		next
		
		object.ypos = temp0

		Sin(object.drawPos.y, object.angle)
		object.drawPos.y *= object.amplitude.y
		object.drawPos.y += object.ypos
		object.drawPos.y &= 0xFFFF0000

		Sin(object.shinePos.y, object.angle)
		object.shinePos.y *= object.shineAmplitude.y
		object.shinePos.y += object.ypos
		object.shinePos.y &= 0xFFFF0000

		break
		
	end switch
end event


event ObjectDraw
	DrawSpriteXY(object.frame, object.xpos, object.drawPos.y)
	DrawSpriteXY(2, object.xpos, object.shinePos.y)
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-32, -72, 64, 144, 191, 1)
	SpriteFrame(-32, -56, 64, 112, 126, 1)
	SpriteFrame(-16, -16, 31, 32, 159, 114)

	foreach (TypeName[Glass Pillar], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].frame = object[arrayPos0].propertyValue
		object[arrayPos0].frame >>= 4
		object[arrayPos0].frame %= 3
		object[arrayPos0].state = object[arrayPos0].propertyValue
		object[arrayPos0].state &= 7
		if object[arrayPos0].state > 5
			object[arrayPos0].state = GLASSPILLAR_STATIC
		end if

		switch object[arrayPos0].state
		default
		case GLASSPILLAR_STATIC
			break

		case GLASSPILLAR_MOVE_UP_DOWN
			object[arrayPos0].amplitude.y = 0x1000
			object[arrayPos0].shineAmplitude.y = -0x800
			object[arrayPos0].ypos -= 0x200000
			break

		case GLASSPILLAR_MOVE_DOWN_UP
			object[arrayPos0].amplitude.y = -0x1000
			object[arrayPos0].shineAmplitude.y = 0x800
			object[arrayPos0].ypos -= 0x200000
			break
			
		end switch
		object[arrayPos0].drawOrder = 5
	next

	temp0 = 0
	GlassPillar_startDebugID = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[Glass Pillar], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(GlassPillar_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(GlassPillar_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // frameID
			checkResult = object.propertyValue
			checkResult &= 127
			checkResult >>= 4
			checkResult &= 1
			break
			
		case 1 // moveType
			checkResult = object.propertyValue
			checkResult &= 7
			if checkResult > 5
				checkResult = GLASSPILLAR_STATIC
			end if
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // frameID
			temp1 = 0x70
			Not(temp1)
			object.propertyValue &= temp1

			editor.variableValue &= 1
			editor.variableValue <<= 4
			object.propertyValue |= editor.variableValue
			break
			
		case 1 // moveType
			temp1 = 7
			Not(temp1)
			object.propertyValue &= temp1

			temp0 = editor.variableValue
			temp0 &= 7
			object.propertyValue |= temp0
			break
			
		end switch
	end if
end event


event RSDKDraw
	temp0 = object.propertyValue
	temp0 >>= 4
	temp0 %= 3

	DrawSpriteXY(object.frame, object.xpos, object.ypos)
	DrawSpriteXY(2, object.xpos, object.ypos)
end event


event RSDKLoad
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-32, -72, 64, 144, 191, 1)
	SpriteFrame(-32, -56, 64, 112, 126, 1)
	SpriteFrame(-16, -16, 31, 32, 159, 114)

	AddEditorVariable("frameID")
	SetActiveVariable("frameID")
	AddEnumVariable("Long", 0)
	AddEnumVariable("Medium", 1)

	AddEditorVariable("moveType")
	SetActiveVariable("moveType")
	AddEnumVariable("Static", GLASSPILLAR_STATIC)
	AddEnumVariable("Move Vertically", GLASSPILLAR_MOVE_UP_DOWN)
	AddEnumVariable("Move Vertically (Reversed)", GLASSPILLAR_MOVE_DOWN_UP)
end event
