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

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

private alias object.propertyValue : object.hasEggman

private alias object.value0 : object.fzEggmanSlot
private alias object.value3 : object.timer
private alias object.value4 : object.explosionTimer

// Piston States
private alias 0 : FZPISTON_IDLE
private alias 1 : FZPISTON_SETUP_CRUSHING
private alias 2 : FZPISTON_CRUSHING
private alias 3 : FZPISTON_SETUP_RETRACT
private alias 4 : FZPISTON_RETRACTING
private alias 5 : FZPISTON_SETUP_DESTROYED
private alias 6 : FZPISTON_DESTROYED

// Eggman aliases
private alias object.value0 : fzEggman.timer
private alias object.value2 : fzEggman.health // Health counts up instead of down this time...

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


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

event ObjectUpdate
	switch object.state
	case FZPISTON_IDLE
		// Waiting...
		break

	case FZPISTON_SETUP_CRUSHING
		object.timer = 31
		object.yvel = -0x8000
		if object.direction != 0
			FlipSign(object.yvel)
		end if
		object.state++
		break

	case FZPISTON_CRUSHING
		object.timer--
		if object.timer <= 0
			object.state++
		end if
		break

	case FZPISTON_SETUP_RETRACT
		object.timer = 48
		object.yvel = -0x30000
		if object.direction != 0
			FlipSign(object.yvel)
		end if
		object.state++
		break

	case FZPISTON_RETRACTING
		object.timer--
		if object.timer <= 0
			object.timer = 80
			object.yvel = 0x20000
			if object.direction != 0
				FlipSign(object.yvel)
			end if
			object.state++
		end if
		break

	case FZPISTON_SETUP_DESTROYED
	case FZPISTON_DESTROYED
		object.timer--
		if object.timer <= 0
			object.yvel = 0
			object.hasEggman = false
			object.state = FZPISTON_IDLE
		end if
		break

	end switch

	temp0 = object.ypos
	temp0 += object.yvel
	temp0 &= 0xFFFF0000
	temp1 = object.ypos
	temp1 &= 0xFFFF0000
	temp0 -= temp1

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.direction == 0
			BoxCollisionTest(C_SOLID2, object.entityPos, -32, -96, 32, 160, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		else
			BoxCollisionTest(C_SOLID2, object.entityPos, -32, -160, 32, 96, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		end if

		switch checkResult
		case COL_TOP
			player[currentPlayer].ypos += temp0
			break

		case COL_BOTTOM
			if player[currentPlayer].gravity == 0
				if object.yvel > 0
					CallFunction(Player_Kill)
				end if
			end if
			break
			
		end switch
	next

	object.ypos += object.yvel
	arrayPos0 = object.fzEggmanSlot
	if object.hasEggman == true
		object[arrayPos0].xpos = object.xpos
		object[arrayPos0].ypos = object.ypos
		if object.direction == 0
			object[arrayPos0].ypos -= 0xA0000
		else
			object[arrayPos0].ypos += 0xE0000
		end if
	end if

	if object.state > FZPISTON_RETRACTING
		if fzEggman[arrayPos0].health == 8
			if object.state == FZPISTON_SETUP_DESTROYED
				object.state++
				object.timer <<= 1
				object.timer--
				object.yvel >>= 1
				fzEggman[arrayPos0].timer = 160
				arrayPos0 += 6
				if object[arrayPos0].state == 0
					object[arrayPos0].state++
				end if
			end if

			object.explosionTimer++
			temp0 = object.explosionTimer
			temp0 &= 7
			if temp0 == 4
				Rand(temp0, 96)
				temp0 -= 48
				temp0 <<= 16
				temp0 += object.xpos
				Rand(temp1, 48)
				temp1 -= 24
				temp1 <<= 16
				temp1 += object.ypos
				CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
				object[tempObjectPos].drawOrder = 5
				PlaySfx(SfxName[Explosion], false)
			end if
		end if
	end if
end event


event ObjectDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")

	// Piston frame
	SpriteFrame(-32, -96, 64, 216, 124, 66)

	foreach (TypeName[FZ Piston], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 4
	next
end event


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

event RSDKDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-32, -96, 64, 216, 124, 66)
	
	// Used by the boss, but not by the scene file itself
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
