// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Boss Barrier 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.blockPos1.y
private alias object.value2 : object.blockPos2.y
private alias object.value3 : object.blockPos3.y
private alias object.value4 : object.blockPos4.y
private alias object.value5 : object.blockPos5.y
private alias object.value6 : object.blockPos6.y
private alias object.value7 : object.blockPos7.y

private alias 0 : BOSSBARRIER_NOTACTIVE
private alias 1 : BOSSBARRIER_RISE
private alias 2 : BOSSBARRIER_LOWER
private alias 3 : BOSSBARRIER_IDLE

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


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

event ObjectUpdate
	switch object.state
	case BOSSBARRIER_NOTACTIVE
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		next
		break

	case BOSSBARRIER_RISE
		object.frame = object.animationTimer
		object.frame >>= 2
		object.animationTimer++
		object.animationTimer %= 12
		object.timer++
		if object.timer == 64
			object.state = BOSSBARRIER_IDLE
		end if

		object.blockPos1.y = object.timer
		object.blockPos1.y *= -0x8000
		object.blockPos1.y += object.ypos

		object.blockPos2.y = object.timer
		object.blockPos2.y *= -0x10000
		object.blockPos2.y += object.ypos

		object.blockPos3.y = object.timer
		object.blockPos3.y *= -0x18000
		object.blockPos3.y += object.ypos

		object.blockPos4.y = object.timer
		object.blockPos4.y *= -0x20000
		object.blockPos4.y += object.ypos

		object.blockPos5.y = object.timer
		object.blockPos5.y *= -0x28000
		object.blockPos5.y += object.ypos

		object.blockPos6.y = object.timer
		object.blockPos6.y *= -0x30000
		object.blockPos6.y += object.ypos

		object.blockPos7.y = object.timer
		object.blockPos7.y *= -0x38000
		object.blockPos7.y += object.ypos

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

	case BOSSBARRIER_LOWER
		object.frame = object.animationTimer
		object.frame >>= 2
		object.frame += 3
		object.animationTimer++
		object.animationTimer %= 12

		object.timer--
		if object.timer == 0
			object.state = BOSSBARRIER_NOTACTIVE
		end if

		object.blockPos1.y = object.timer
		object.blockPos1.y *= -0x4000
		object.blockPos1.y += object.ypos

		object.blockPos2.y = object.timer
		object.blockPos2.y *= -0x8000
		object.blockPos2.y += object.ypos

		object.blockPos3.y = object.timer
		object.blockPos3.y *= -0xC000
		object.blockPos3.y += object.ypos

		object.blockPos4.y = object.timer
		object.blockPos4.y *= -0x10000
		object.blockPos4.y += object.ypos

		object.blockPos5.y = object.timer
		object.blockPos5.y *= -0x14000
		object.blockPos5.y += object.ypos

		object.blockPos6.y = object.timer
		object.blockPos6.y *= -0x18000
		object.blockPos6.y += object.ypos

		object.blockPos7.y = object.timer
		object.blockPos7.y *= -0x1C000
		object.blockPos7.y += object.ypos

		temp0 = object.timer
		temp0 *= -192
		temp0 >>= 7
		temp0 -= 18
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -16, temp0, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_TOP
				player[currentPlayer].ypos += 0x20000
			end if
		next
		break

	case BOSSBARRIER_IDLE
		object.timer = 128
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -16, -320, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		next
		break
		
	end switch
end event


event ObjectDraw
	switch object.state
	case BOSSBARRIER_NOTACTIVE
		DrawSprite(0)
		break

	case BOSSBARRIER_RISE
	case BOSSBARRIER_LOWER
	case BOSSBARRIER_IDLE
		DrawSpriteXY(6, object.xpos, object.blockPos7.y)
		DrawSpriteXY(6, object.xpos, object.blockPos6.y)
		DrawSpriteXY(6, object.xpos, object.blockPos5.y)
		DrawSpriteXY(6, object.xpos, object.blockPos4.y)
		DrawSpriteXY(6, object.xpos, object.blockPos3.y)
		DrawSpriteXY(6, object.xpos, object.blockPos2.y)
		DrawSpriteXY(6, object.xpos, object.blockPos1.y)
		DrawSprite(object.frame)
		break
		
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("SYZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 110, 223)
	SpriteFrame(-16, -16, 32, 32, 176, 223)
	SpriteFrame(-16, -16, 32, 32, 209, 223)
	SpriteFrame(-16, -16, 32, 32, 209, 223)
	SpriteFrame(-16, -16, 32, 32, 176, 223)
	SpriteFrame(-16, -16, 32, 32, 110, 223)
	SpriteFrame(-16, -16, 32, 32, 110, 190)

	if screen.xsize == 320
		foreach (TypeName[Boss Barrier], arrayPos0, ALL_ENTITIES)
			object[arrayPos0].type = TypeName[Boss Block]
		next
	end if
end event


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

event RSDKDraw
	temp1 = 64
	temp1 *= -0x8000
	temp1 += object.ypos

	temp2 = 64
	temp2 *= -0x10000
	temp2 += object.ypos

	temp3 = 64
	temp3 *= -0x18000
	temp3 += object.ypos

	temp4 = 64
	temp4 *= -0x20000
	temp4 += object.ypos

	temp5 = 64
	temp5 *= -0x28000
	temp5 += object.ypos

	temp6 = 64
	temp6 *= -0x30000
	temp6 += object.ypos

	temp7 = 64
	temp7 *= -0x38000
	temp7 += object.ypos

	DrawSpriteXY(1, object.xpos, temp7)
	DrawSpriteXY(1, object.xpos, temp6)
	DrawSpriteXY(1, object.xpos, temp5)
	DrawSpriteXY(1, object.xpos, temp4)
	DrawSpriteXY(1, object.xpos, temp3)
	DrawSpriteXY(1, object.xpos, temp2)
	DrawSpriteXY(1, object.xpos, temp1)
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("SYZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 110, 223)
	SpriteFrame(-16, -16, 32, 32, 110, 190)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
