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

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

private alias object.value0 : object.timer

// States
private alias 0 : PIPEVALVE_INACTIVE
private alias 1 : PIPEVALVE_ACTIVE

// Player Aliases
private alias object.state : player.state
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.xvel : player.xvel
private alias object.yvel : player.yvel
private alias object.speed : player.speed
private alias object.gravity : player.gravity
private alias object.animation : player.animation

private alias object.value1 : player.timer

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

reserve function PipeValve_DebugDraw
reserve function PipeValve_DebugSpawn

private function PipeValve_DebugDraw
	DrawSprite(2)
	DrawSprite(0)
end function


private function PipeValve_DebugSpawn
	CreateTempObject(TypeName[Pipe Valve], 0, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 5
end function


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

event ObjectUpdate
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -16, -8, 16, 8, currentPlayer, 0, 0, 0, 0)
		if checkResult == true
			player[currentPlayer].state = Player_State_Air
			player[currentPlayer].gravity = GRAVITY_AIR
			player[currentPlayer].animation = ANI_WATERSLIDE
			player[currentPlayer].yvel = -0x100000
			player[currentPlayer].xpos = object.xpos
			player[currentPlayer].ypos = object.ypos
			player[currentPlayer].speed = 0
			player[currentPlayer].xvel = 0
			player[currentPlayer].timer = 0

			if object.state == PIPEVALVE_INACTIVE
				object.state = PIPEVALVE_ACTIVE
				CreateTempObject(TypeName[Water Geyser], 0, object.xpos, object.ypos)
				PlaySfx(SfxName[Water Geyser], false)
			end if
		end if
	next

	if object.state == PIPEVALVE_ACTIVE
		object.timer++
		if object.timer == 64
			object.timer = 0
			object.state = PIPEVALVE_INACTIVE
		end if
		StopSfx(SfxName[Water Splash])
	end if

	object.frame = object.timer
	object.frame >>= 2
	object.frame &= 1
end event


event ObjectDraw
	DrawSprite(2)
	DrawSprite(object.frame)
end event


event ObjectStartup
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-12, -12, 24, 24, 418, 1)
	SpriteFrame(-12, -12, 24, 24, 418, 26)
	SpriteFrame(-16, -16, 32, 32, 443, 1)

	foreach (TypeName[Pipe Valve], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 5
	next

	SetTableValue(TypeName[Pipe Valve], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(PipeValve_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(PipeValve_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-12, -12, 24, 24, 418, 1)
	SpriteFrame(-12, -12, 24, 24, 418, 26)
	SpriteFrame(-16, -16, 32, 32, 443, 1)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
