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

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

private alias object.value0 : object.hitboxT
private alias object.value1 : object.hitboxB

// Player Aliases
private alias object.type : player.type
private alias object.state : player.state
private alias object.xvel : player.xvel
private alias object.speed : player.speed
private alias object.direction : player.direction
private alias object.gravity : player.gravity
private alias object.animation : player.animation

private alias object.value34 : player.collisionDisabled
private alias object.value35 : player.jumpAbilityState


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

event ObjectUpdate
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if player[currentPlayer].collisionDisabled == true
			player[currentPlayer].collisionDisabled = false
		end if

		CheckEqual(player[currentPlayer].state, Player_State_Death)
		temp0 = checkResult
		CheckEqual(player[currentPlayer].state, Player_State_LedgePullUp)
		temp0 |= checkResult
		if temp0 == false
			BoxCollisionTest(C_TOUCH, object.entityPos, -12, object.hitboxT, 12, object.hitboxB, currentPlayer, 0, 0, 0, 0)
			if checkResult == true
				switch object.direction
				case FLIP_NONE
					if player[currentPlayer].xvel > 0
						if player[currentPlayer].animation != ANI_JUMPING
							if player[currentPlayer].state != CNZSetup_Player_TubeRoll
								PlaySfx(SfxName[Rolling], false)
							end if
						end if

						if player[currentPlayer].state == Player_State_GlideLeft
							FlipSign(player[currentPlayer].speed)
						end if

						player[currentPlayer].direction = FACING_RIGHT
						player[currentPlayer].state 	= CNZSetup_Player_TubeRoll
						player[currentPlayer].animation = ANI_JUMPING
						if player[currentPlayer].collisionDisabled == true
							player[currentPlayer].collisionDisabled = false
						end if

						player[currentPlayer].jumpAbilityState = 0
					else
						if player[currentPlayer].gravity == GRAVITY_GROUND
							if player[currentPlayer].xvel > -0x20000
								player[currentPlayer].speed = -0x20000
							end if
						end if

						CheckEqual(player[currentPlayer].state, CNZSetup_Player_TubeRoll)
						temp0 = checkResult
						CheckEqual(player[currentPlayer].state, CNZSetup_Player_TubeAirRoll)
						temp0 |= checkResult
						if temp0 == true
							player[currentPlayer].state 	= Player_State_Roll
							player[currentPlayer].animation = ANI_JUMPING
						end if
					end if
					break

				case FLIP_X
					if player[currentPlayer].xvel < 0
						if player[currentPlayer].animation != ANI_JUMPING
							if player[currentPlayer].state != CNZSetup_Player_TubeRoll
								PlaySfx(SfxName[Rolling], false)
							end if
						end if

						if player[currentPlayer].state == Player_State_GlideLeft
							FlipSign(player[currentPlayer].speed)
						end if

						player[currentPlayer].direction = FACING_LEFT
						player[currentPlayer].state 	= CNZSetup_Player_TubeRoll
						player[currentPlayer].animation = ANI_JUMPING
						if player[currentPlayer].collisionDisabled == true
							player[currentPlayer].collisionDisabled = false
						end if

						player[currentPlayer].jumpAbilityState = 0
					else
						if player[currentPlayer].gravity == GRAVITY_GROUND
							if player[currentPlayer].xvel < 0x20000
								player[currentPlayer].speed = 0x20000
							end if
						end if

						CheckEqual(player[currentPlayer].state, CNZSetup_Player_TubeRoll)
						temp0 = checkResult
						CheckEqual(player[currentPlayer].state, CNZSetup_Player_TubeAirRoll)
						temp0 |= checkResult
						if temp0 == true
							player[currentPlayer].state 	= Player_State_Roll
							player[currentPlayer].animation = ANI_JUMPING
						end if
					end if
					break
				end switch
			end if
		end if
	next
end event


event ObjectStartup
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -8, 16, 16, 127, 113)
	SpriteFrame(-8, -8, 16, 16, 144, 113)

	foreach (TypeName[Spinner V], arrayPos0, ALL_ENTITIES)
		temp0 = object[arrayPos0].propertyValue
		temp0 &= 3
		temp0 += 2
		object[arrayPos0].hitboxT = 1
		object[arrayPos0].hitboxT <<= temp0
		object[arrayPos0].hitboxT <<= 3
		object[arrayPos0].hitboxB = object[arrayPos0].hitboxT
		FlipSign(object[arrayPos0].hitboxT)

		GetBit(object[arrayPos0].direction, object[arrayPos0].propertyValue, 2)
	next
end event


event ObjectDraw
	if player[0].type == TypeName[Debug Mode]
		temp1 = object.propertyValue
		temp1 &= 3
		temp1 += 2

		temp0 = 1
		temp0 <<= temp1
		temp1 = temp0
		temp0 <<= 19
		FlipSign(temp0)
		temp0 += object.ypos
		temp0 += 0x80000

		GetBit(temp2, object.propertyValue, 2)

		temp3 = 0
		while temp3 < temp1
			DrawSpriteXY(temp2, object.xpos, temp0)
			temp0 += 0x100000
			temp3++
		loop

		if temp1 == 0
			DrawSprite(temp2)
		end if
	end if
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // size
			checkResult = object.propertyValue
			checkResult &= 3
			break
			
		case 1 // type
			GetBit(checkResult, object.propertyValue, 2)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // size
			temp1 = 3
			Not(temp1)
			object.propertyValue &= temp1

			temp0 = editor.variableValue
			temp0 &= 3
			object.propertyValue |= temp0
			break
			
		case 1 // type
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 2, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	temp1 = object.propertyValue
	temp1 &= 3
	temp1 += 2

	temp0 = 1
	temp0 <<= temp1
	temp1 = temp0
	temp0 <<= 19
	FlipSign(temp0)
	temp0 += object.ypos
	temp0 += 0x80000

	GetBit(temp2, object.propertyValue, 2)

	temp3 = 0
	while temp3 < temp1
		DrawSpriteXY(temp2, object.xpos, temp0)
		temp0 += 0x100000
		temp3++
	loop

	if temp1 == 0
		DrawSprite(temp2)
	end if
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -8, 16, 16, 127, 113)
	SpriteFrame(-8, -8, 16, 16, 144, 113)

	AddEditorVariable("size")
	SetActiveVariable("size")
	AddEnumVariable("4 Nodes", 0)
	AddEnumVariable("8 Nodes", 1)
	AddEnumVariable("16 Nodes", 2)
	AddEnumVariable("32 Nodes", 3)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Enter From Left, Exit From Right", 0)
	AddEnumVariable("Enter From Right, Exit From Left", 1)
end event
