// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Push Button Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 56 : TYPE_PUSHBUTTON

// Function declarations
reserve function PushButton_DebugDraw
reserve function PushButton_DebugSpawn
reserve function PushButton_PowerCycle

// Static Values

// Tables

function PushButton_DebugDraw
	DrawSprite(1)
end function


function PushButton_DebugSpawn
	CreateTempObject(TypeName[Push Button], 0, object.xpos, object.ypos)
end function

function PushButton_PowerCycle
	switch object.value2
		case 1
		case 21
			Slots.Power ^= 2
			object.value2++
			break
		case 11
			Slots.Power ^= 1
			object.value2++
			break
		case 22
			object.value2 = 0
			break
		default
			object.value2++
			break
	end switch
end function

event ObjectMain
	object.value0 = 0
	switch options.gameMode
		case 5
			if Slots.Power == 1
				object.value1 = 0
			else
				object.value1 = 1
			end if
			break
		default
			object.value1 = 0
			break
	end switch
	switch object.propertyValue
	case 0 //up
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.frame == 0
				BoxCollisionTest(C_PLATFORM, object.entityPos, -14, -6, 14, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					object.value1 = 1
					object.value0 = 1
					object[currentPlayer].ypos += 0x9C40
					PlaySfx(SfxName[Explosion], 0)
					if object.value2 == 0
						object.value2 = 1
					end if
				end if
			else
				if object[currentPlayer].yvel >= 0
					BoxCollisionTest(C_PLATFORM, object.entityPos, -14, -6, 14, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						object.value1 = 1
						object[currentPlayer].ypos += 0x20000
					else
						BoxCollisionTest(C_TOUCH, object.entityPos, -20, -12, 20, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
						if checkResult == 1
							object[currentPlayer].ypos = object[currentPlayer].collisionBottom
							FlipSign(object[currentPlayer].ypos)
							object[currentPlayer].ypos <<= 16
							object[currentPlayer].ypos += object.ypos
							object[currentPlayer].ypos -= 0x20000
							object[currentPlayer].gravity = GRAVITY_AIR
						end if
					end if
				end if
			end if
		next
		break
	case 1 //right
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].gravity == GRAVITY_AIR
				if object[currentPlayer].xvel <= 0
					if object.frame == 0
						BoxCollisionTest(C_TOUCH, object.entityPos, -6, -14, 12, 14, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
						if checkResult == 1
							object.value1 = 1
							object.value0 = 1
							PlaySfx(SfxName[Explosion], 0)
							if object.value2 == 0
								object.value2 = 1
							end if
							object[currentPlayer].xpos = object[currentPlayer].collisionLeft
							FlipSign(object[currentPlayer].xpos)
							object[currentPlayer].xpos <<= 16
							object[currentPlayer].xpos += object.xpos
							object[currentPlayer].xpos += 0x20000
							object[currentPlayer].xvel = 0
							object[currentPlayer].speed = 0
						end if
					else
						BoxCollisionTest(C_TOUCH, object.entityPos, -8, -20, 8, 20, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
						if checkResult == 1
							object.value1 = 1
							object[currentPlayer].xpos = object[currentPlayer].collisionLeft
							FlipSign(object[currentPlayer].xpos)
							object[currentPlayer].xpos <<= 16
							object[currentPlayer].xpos += object.xpos
							object[currentPlayer].xpos += 0x40000
							object[currentPlayer].xvel = 0
							object[currentPlayer].speed = 0
						end if
					end if
				end if
			end if
		next
		break
	case 2 //down
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			//if object[currentPlayer].gravity == GRAVITY_AIR
				if object[currentPlayer].yvel <= 0
					if object.frame == 0
						BoxCollisionTest(C_TOUCH, object.entityPos, -14, -6, 14, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
						if checkResult == 1
							object.value1 = 1
							object.value0 = 1
							PlaySfx(SfxName[Explosion], 0)
							if object.value2 == 0
								object.value2 = 1
							end if
							object[currentPlayer].ypos = object[currentPlayer].collisionTop
							FlipSign(object[currentPlayer].ypos)
							object[currentPlayer].ypos <<= 16
							object[currentPlayer].ypos += object.ypos
							object[currentPlayer].ypos += 0x20000
							object[currentPlayer].yvel = 0
						end if
					else
						BoxCollisionTest(C_TOUCH, object.entityPos, -20, -8, 20, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
						if checkResult == 1
							object.value1 = 1
							object[currentPlayer].ypos = object[currentPlayer].collisionTop
							FlipSign(object[currentPlayer].ypos)
							object[currentPlayer].ypos <<= 16
							object[currentPlayer].ypos += object.ypos
							object[currentPlayer].ypos += 0x40000
							object[currentPlayer].yvel = 0
						end if
					end if
				end if
			//end if
		next
		break
	case 3 //left
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].gravity == GRAVITY_AIR
				if object[currentPlayer].xvel >= 0
					if object.frame == 0
						BoxCollisionTest(C_TOUCH, object.entityPos, -12, -14, 6, 14, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
						if checkResult == 1
							object.value1 = 1
							object.value0 = 1
							PlaySfx(SfxName[Explosion], 0)
							if object.value2 == 0
								object.value2 = 1
							end if
							object[currentPlayer].xpos = object[currentPlayer].collisionRight
							FlipSign(object[currentPlayer].xpos)
							object[currentPlayer].xpos <<= 16
							object[currentPlayer].xpos += object.xpos
							object[currentPlayer].xpos += 0x20000
							object[currentPlayer].xvel = 0
							object[currentPlayer].speed = 0
						end if
					else
						BoxCollisionTest(C_TOUCH, object.entityPos, -8, -20, 8, 20, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
						if checkResult == 1
							object.value1 = 1
							object[currentPlayer].xpos = object[currentPlayer].collisionRight
							FlipSign(object[currentPlayer].xpos)
							object[currentPlayer].xpos <<= 16
							object[currentPlayer].xpos += object.xpos
							object[currentPlayer].xpos -= 0x40000
							object[currentPlayer].xvel = 0
							object[currentPlayer].speed = 0
						end if
					end if
				end if
			end if
		next
		break
	end switch
	
	if object.value2 > 0
		CallFunction(PushButton_PowerCycle)
	end if
end event


event ObjectDraw
	object.frame = object.value1
	DrawSpriteFX(object.frame, FX_ROTATE, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("CNZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 219, 120)
	SpriteFrame(-16, -4, 32, 8, 219, 120)
	foreach (TypeName[Push Button], arrayPos0, ALL_ENTITIES)
		if options.gameMode == 5
			object[arrayPos0].rotation = object[arrayPos0].propertyValue
			object[arrayPos0].rotation <<= 7
		else
			ResetObjectEntity(arrayPos0, TypeName[Blank Object], 0, 0, 0)
		end if
	next
	SetTableValue(TypeName[Push Button], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(PushButton_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(PushButton_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
