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

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

private alias object.value0 : object.timer

// States
private alias 0 : VFLIPPER_IDLE
private alias 1 : VFLIPPER_ACTIVATED_L
private alias 2 : VFLIPPER_ACTIVATED_R

// Player Aliases
private alias object.state : player.state
private alias object.iypos : player.iypos
private alias object.xvel : player.xvel
private alias object.speed : player.speed
private alias object.gravity : player.gravity
private alias object.prevAnimation : player.prevAnimation
private alias object.animation : player.animation

private alias object.value30 : player.jumpOffset


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

reserve function VFlipper_DebugDraw
reserve function VFlipper_DebugSpawn


// ========================
// Function Definitions
// ========================

private function VFlipper_DebugDraw
	DrawSprite(0)
end function


private function VFlipper_DebugSpawn
	CreateTempObject(TypeName[V Flipper], 0, object.xpos, object.ypos)
end function


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

event ObjectUpdate
	switch object.state
	default
	case VFLIPPER_IDLE
		break

	case VFLIPPER_ACTIVATED_L
		object.frame = object.timer
		object.frame >>= 1
		object.frame++
		if object.timer < 6
			object.timer++
		else
			object.timer = 0
			object.frame = 0
			object.state = VFLIPPER_IDLE
		end if
		break

	case VFLIPPER_ACTIVATED_R
		object.frame = object.timer
		object.frame >>= 1
		object.frame += 3
		if object.timer < 6
			object.timer++
		else
			object.timer = 0
			object.frame = 0
			object.state = VFLIPPER_IDLE
		end if
		break
		
	end switch

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_SOLID, object.entityPos, -8, -24, 8, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if player[currentPlayer].gravity == GRAVITY_GROUND
			switch checkResult
			default
			case COL_NONE
			case COL_TOP
			case COL_BOTTOM
				break
				
			case COL_LEFT
				player[currentPlayer].speed = -0x100000
				player[currentPlayer].xvel = -0x100000

				if player[currentPlayer].state != Player_State_TubeRoll
					player[currentPlayer].state = Player_State_Roll
					player[currentPlayer].animation = ANI_JUMPING
				end if

				if player[currentPlayer].prevAnimation != ANI_JUMPING
					player[currentPlayer].iypos -= player[currentPlayer].jumpOffset
				end if

				object.state = VFLIPPER_ACTIVATED_L
				PlaySfx(SfxName[Flipper], false)
				break

			case COL_RIGHT
				player[currentPlayer].speed = 0x100000
				player[currentPlayer].xvel = 0x100000

				CheckEqual(player[currentPlayer].state, Player_State_TubeRoll)
				temp0 = checkResult
				CheckEqual(player[currentPlayer].state, Player_State_TubeAirRoll)
				temp0 |= checkResult
				if temp0 == false
					player[currentPlayer].state = Player_State_Roll
					player[currentPlayer].animation = ANI_JUMPING
				end if

				if player[currentPlayer].prevAnimation != ANI_JUMPING
					player[currentPlayer].iypos -= player[currentPlayer].jumpOffset
				end if

				object.state = VFLIPPER_ACTIVATED_R
				PlaySfx(SfxName[Flipper], false)
				break
			end switch
		end if
	next
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone04")
	if checkResult == true
		LoadSpriteSheet("CNZ/Objects.gif")
		SpriteFrame(-8, -24, 16, 48, 101, 157)
		SpriteFrame(-17, -25, 26, 47, 118, 157)
		SpriteFrame(-8, -24, 16, 48, 101, 157)
		SpriteFrame(-9, -25, 26, 47, 74, 157)
		SpriteFrame(-8, -24, 16, 48, 101, 157)
		SpriteFrame(-17, -25, 26, 47, 118, 157)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-8, -24, 16, 48, 321, 338)
		SpriteFrame(-17, -25, 26, 47, 338, 338)
		SpriteFrame(-8, -24, 16, 48, 321, 338)
		SpriteFrame(-9, -25, 26, 47, 394, 338)
		SpriteFrame(-8, -24, 16, 48, 321, 338)
		SpriteFrame(-17, -25, 26, 47, 338, 338)
	end if

	SetTableValue(TypeName[V Flipper], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(VFlipper_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(VFlipper_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone04")
	if checkResult == true
		LoadSpriteSheet("CNZ/Objects.gif")
		SpriteFrame(-8, -24, 16, 48, 101, 157)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-8, -24, 16, 48, 321, 338)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
