// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Wall Bumper 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.xvel 			: player.xvel
private alias object.yvel 			: player.yvel
private alias object.direction 		: player.direction
private alias object.state 			: player.state
private alias object.tileCollisions : player.tileCollisions
private alias object.gravity 		: player.gravity
private alias object.speed 			: player.speed
private alias object.collisionMode 	: player.collisionMode
private alias object.pushing 		: player.pushing
private alias object.controlLock 	: player.controlLock
private alias object.animation 		: player.animation
private alias object.prevAnimation 	: player.prevAnimation
private alias object.animationSpeed : player.animationSpeed
private alias object.frame 			: player.frame
private alias object.value1  		: player.timer
private alias object.value25 		: player.gravityStrength


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

event ObjectUpdate
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		// Check if the player's on the ground or in the air
		CheckEqual(player[currentPlayer].gravity, 0)
		temp0 = checkResult
		CheckNotEqual(player[currentPlayer].collisionMode, 0)
		temp0 &= checkResult

		if temp0 == false
			BoxCollisionTest(C_TOUCH, object.entityPos, -10, object.hitboxT, 10, object.hitboxB, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		else
			BoxCollisionTest(C_TOUCH, object.entityPos, -20, object.hitboxT, 20, object.hitboxB, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		end if

		if checkResult == true
			if object.direction == FLIP_NONE
				player[currentPlayer].xvel = 0x80000
			else
				player[currentPlayer].xvel = -0x80000
			end if

			player[currentPlayer].yvel 				= -0x80000
			player[currentPlayer].yvel 			   -= player[currentPlayer].gravityStrength
			player[currentPlayer].direction 		= object.direction
			player[currentPlayer].state 			= Player_State_Air
			player[currentPlayer].tileCollisions 	= true
			player[currentPlayer].gravity 			= 1
			player[currentPlayer].speed 			= player[currentPlayer].xvel
			player[currentPlayer].timer 			= 1
			player[currentPlayer].collisionMode 	= 0
			player[currentPlayer].pushing 			= 0
			player[currentPlayer].controlLock 		= 15
			player[currentPlayer].animation 		= ANI_TWIRL
			player[currentPlayer].prevAnimation 	= ANI_TWIRL
			player[currentPlayer].frame 			= 0
			player[currentPlayer].animationSpeed 	= 90

			PlaySfx(SfxName[Spring], false)
		end if
	next
end event


event ObjectDraw
	if player[0].type == TypeName[Debug Mode]
		temp1 = object.propertyValue
		temp1 &= 0x70
		if temp1 == 0
			temp0 = -0x400000
			temp1 = 8
		else
			temp0 = -0x800000
			temp1 = 16
		end if

		temp0 += object.ypos
		temp0 += 0x80000
		temp4 = 0
		while temp4 < temp1
			DrawSpriteXY(0, object.xpos, temp0)
			temp0 += 0x100000
			temp4++
		loop
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -8, 16, 16, 93, 113) // #0 - Blank control circle

	foreach (TypeName[Wall Bumper], arrayPos0, ALL_ENTITIES)
		temp0 = object[arrayPos0].propertyValue
		temp0 &= 0x70
		if temp0 == 0
			object[arrayPos0].hitboxT = 64
		else
			object[arrayPos0].hitboxT = 128
		end if
		object[arrayPos0].hitboxB = object[arrayPos0].hitboxT
		FlipSign(object[arrayPos0].hitboxT)
		object[arrayPos0].drawOrder = 5
	next
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 // type
			checkResult = object.propertyValue
			checkResult &= 0x70
			checkResult >>= 4
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // type
			editor.variableValue &= 7
			editor.variableValue <<= 4

			temp0 = 0x70
			Not(temp0)
			object.propertyValue &= temp0

			object.propertyValue |= editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	temp1 = object.propertyValue
	temp1 &= 0x70
	if temp1 == 0
		temp0 = -0x400000
		temp1 = 8
	else
		temp0 = -0x800000
		temp1 = 16
	end if

	temp0 += object.ypos
	temp0 += 0x80000
	temp4 = 0
	while temp4 < temp1
		DrawSpriteXY(0, object.xpos, temp0)
		temp0 += 0x100000
		temp4++
	loop
	// TODO: Draw a rectangle of the object's hitbox
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -8, 16, 16, 93, 113) // #0 - Blank control circle
	
	// First bit is unused
	
	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("4 Bumpers", 0)
	AddEnumVariable("8 Bumpers", 1)
	
	// Direction is set via the attribute
end event
