// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Steam Piston 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.startPos.y
private alias object.value2  : object.stoodPlayers
private alias object.value31 : object.wrapOffset.y

// States
private alias 0 : STEAMPISTON_CLOSED
private alias 1 : STEAMPISTON_OPENING
private alias 2 : STEAMPISTON_OPENED
private alias 3 : STEAMPISTON_CLOSING

// Animations
private alias 0 : STEAMPISTON_ANI_CLOSED
private alias 1 : STEAMPISTON_ANI_ACTIVE

// Player Aliases
private alias object.state : player.state
private alias object.ypos : player.ypos
private alias object.yvel : player.yvel
private alias object.gravity : player.gravity
private alias object.frame : player.frame
private alias object.prevAnimation : player.prevAnimation
private alias object.animationSpeed : player.animationSpeed
private alias object.animation : player.animation
private alias object.pushing : player.pushing
private alias object.collisionMode : player.collisionMode
private alias object.tileCollisions : player.tileCollisions

private alias object.value1  : player.timer
private alias object.value25 : player.gravityStrength 	

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

reserve function SteamPiston_DebugDraw
reserve function SteamPiston_DebugSpawn

private function SteamPiston_DebugDraw
	DrawSprite(8)
end function


private function SteamPiston_DebugSpawn
	CreateTempObject(TypeName[Steam Piston], object.direction, object.xpos, object.ypos)
	object[tempObjectPos].ypos += 0x100000
	object[tempObjectPos].startPos.y = object.ypos
end function


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

event ObjectUpdate
	temp7 = 0
	switch object.state
	case STEAMPISTON_CLOSED
		temp0 = oscillation
		temp0 &= 0x7F
		if temp0 == 0
			object.priority = PRIORITY_ACTIVE
			object.timer 	= 2
			object.state 	= STEAMPISTON_OPENING
		end if
		break

	case STEAMPISTON_OPENING
		object.ypos -= 0x80000
		temp7 = -0x80000

		object.timer--
		if object.timer == 0
			object.state 	 = STEAMPISTON_OPENED
			object.frame 	 = 0
			object.animation = STEAMPISTON_ANI_ACTIVE
		end if
		break

	case STEAMPISTON_OPENED
		temp0 = oscillation
		temp0 &= 0x3F
		if temp0 == 0
			object.priority = PRIORITY_ACTIVE
			object.timer 	= 2
			object.state 	= STEAMPISTON_CLOSING
		end if
		break

	case STEAMPISTON_CLOSING
		object.ypos += 0x80000
		temp7 = 0x80000

		object.timer--
		if object.timer == 0
			object.state = STEAMPISTON_CLOSED
		end if
		break
		
	end switch

	if object.animation == STEAMPISTON_ANI_ACTIVE
		object.frame = object.animationTimer
		object.frame >>= 3

		object.animationTimer++
		if object.frame == 7
			object.animationTimer 	= 0
			object.animation 		= STEAMPISTON_ANI_CLOSED
		end if
	end if

	temp2 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		GetBit(temp0, object.stoodPlayers, temp2)
		if temp0 == true
			player[currentPlayer].ypos += temp7
		end if

		SetBit(object.stoodPlayers, temp2, false)
		BoxCollisionTest(C_SOLID, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == COL_TOP
			SetBit(object.stoodPlayers, temp2, true)
			if object.state == STEAMPISTON_OPENING
				player[currentPlayer].yvel 				= -0xA0000
				player[currentPlayer].yvel 				-= player[currentPlayer].gravityStrength
				player[currentPlayer].state 			= Player_State_Air
				player[currentPlayer].tileCollisions 	= true
				player[currentPlayer].gravity 			= 1
				player[currentPlayer].timer 			= 0
				player[currentPlayer].collisionMode 	= 0
				player[currentPlayer].pushing 			= 0
				player[currentPlayer].animation 		= ANI_TWIRL
				player[currentPlayer].prevAnimation 	= ANI_TWIRL
				player[currentPlayer].frame 			= 0
				player[currentPlayer].animationSpeed 	= 40
				PlaySfx(SfxName[Spring], false)
			end if
		else
			if object.frame == 3
				BoxCollisionTest(C_TOUCH, object.entityPos, -50, -4, -30, 4, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					CallFunction(Player_Hit)
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, 30, -4, 50, 4, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
					if checkResult == true
						CallFunction(Player_Hit)
					end if
				end if
			end if
		end if

		temp2++
	next

	if object.outOfBounds == true
		object.ypos 			= object.startPos.y
		object.ypos 			+= object.wrapOffset.y
		object.timer 			= 0
		object.stoodPlayers	= 0
		object.animation 		= STEAMPISTON_ANI_CLOSED
		object.animationTimer 	= 0
		object.priority 		= PRIORITY_BOUNDS
		object.state 			= STEAMPISTON_CLOSED
	end if
end event


event ObjectDraw
	DrawSprite(8)

	if object.animation == STEAMPISTON_ANI_ACTIVE
		object.direction = FLIP_X
		object.xpos -= 0x280000
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)

		object.direction = FLIP_NONE
		object.xpos += 0x500000
		DrawSprite(object.frame)

		object.xpos -= 0x280000
	end if
end event


event ObjectStartup
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-24, 1, 8, 4, 102, 98)
	SpriteFrame(-24, -1, 14, 8, 102, 103)
	SpriteFrame(-20, -3, 15, 13, 102, 112)
	SpriteFrame(-16, -4, 23, 16, 118, 92)
	SpriteFrame(-8, -4, 23, 16, 119, 109)
	SpriteFrame(-3, -4, 22, 14, 143, 96)
	SpriteFrame(9, -2, 14, 12, 143, 111)
	SpriteFrame(9, -2, 1, 1, 143, 111)
	SpriteFrame(-16, -16, 32, 32, 166, 93)
	
	foreach (TypeName[Steam Piston], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].ypos += 0x100000
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
	next

	SetTableValue(TypeName[Steam Piston], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(SteamPiston_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(SteamPiston_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(8)
end event


event RSDKLoad
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-24, 1, 8, 4, 102, 98)
	SpriteFrame(-24, -1, 14, 8, 102, 103)
	SpriteFrame(-20, -3, 15, 13, 102, 112)
	SpriteFrame(-16, -4, 23, 16, 118, 92)
	SpriteFrame(-8, -4, 23, 16, 119, 109)
	SpriteFrame(-3, -4, 22, 14, 143, 96)
	SpriteFrame(9, -2, 14, 12, 143, 111)
	SpriteFrame(9, -2, 1, 1, 143, 111)
	SpriteFrame(-16, -16, 32, 32, 166, 93)
	
	// Despite them all having Property Values of 1 for some reason, the value isn't actually used
	// (The value *was* used in the original game, though, where it dictated the behaviour of the piston)
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
