// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Debug Mode Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

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

private alias object.value3  : player.drownTimer
private alias object.value4  : player.drownLevel
private alias object.value17 : object.currentSelection
private alias object.value18 : player.currentPlane

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

reserve function DebugMode_SpawnBlank
reserve function DebugMode_DrawBlank

// ========================
// Static Values
// ========================

public value DebugMode_ObjCount = 0;

// ========================
// Tables
// ========================

// Large table to hold the types of every object
public table DebugMode_TypesTable[256]

// Large table to hold the draw functions of every object
public table DebugMode_DrawTable[256]

// Large table to hold the spawn functions of every object
public table DebugMode_SpawnTable[256]


private function DebugMode_SpawnBlank
	// Dummy function, just reset temp0
	temp0 = 0
end function


private function DebugMode_DrawBlank
	// Dummy function, draw the placeholder frame
	DrawSprite(0)
end function


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

event ObjectUpdate
#platform: USE_STANDALONE
	if options.touchControls == true
		CheckTouchRect(0, 96, screen.xcenter, screen.ysize)
		if checkResult > -1
			arrayPos0 = checkResult
			temp0 = touchscreen[arrayPos0].xpos
			temp0 -= saveRAM[39]
			temp1 = touchscreen[arrayPos0].ypos
			temp1 -= saveRAM[40]
			ATan2(temp2, temp0, temp1)
			temp2 += 16
			temp2 &= 255
			temp2 >>= 5

			switch temp2
			case 0
				keyDown[0].right = true
				break

			case 1
				keyDown[0].right = true
				keyDown[0].down = true
				break

			case 2
				keyDown[0].down = true
				break

			case 3
				keyDown[0].down = true
				keyDown[0].left = true
				break

			case 4
				keyDown[0].left = true
				break

			case 5
				keyDown[0].left = true
				keyDown[0].up = true
				break

			case 6
				keyDown[0].up = true
				break

			case 7
				keyDown[0].up = true
				keyDown[0].right = true
				break
			end switch
		end if

		temp0 = screen.xcenter
		temp0 -= 48
		temp1 = temp0
		temp1 += 96
		CheckTouchRect(temp0, 0, temp1, 56)

		if checkResult > -1
			arrayPos0 = checkResult
			if touchscreen[arrayPos0].xpos > screen.xcenter
				if touchJump == false
					object.currentSelection++
					if object.currentSelection >= DebugMode_ObjCount
						object.currentSelection = 0
					end if
				end if

				touchJump = true
			else
				if touchJump == false
					object.currentSelection--
					if object.currentSelection < 0
						object.currentSelection += DebugMode_ObjCount
					end if
				end if

				touchJump = true
			end if
		else

			CheckTouchRect(screen.xcenter, 96, screen.xsize, 240)
			switch checkResult
			case -1
				touchJump = 0
				break

			case 0
				keyDown[0].buttonC = true
				if touchJump == 0
					keyPress[0].buttonC |= keyDown[0].buttonC
				end if
				touchJump = keyDown[0].buttonC
				break
			end switch
		end if

		temp0 = screen.xsize
		temp0 -= 88

		CheckTouchRect(temp0, 0, screen.xsize, 40)
		if checkResult > -1
			PlaySfx(SfxName[Menu Back], false)
			engine.state = 5
		end if

		if keyPress[0].start == true
			PlaySfx(SfxName[Menu Back], false)
			engine.state = 5
		end if

	end if

	CheckTouchRect(0, 0, 112, 56)
	if checkResult > -1
		keyDown[0].buttonB = true
	end if

	if touchDebug == false
		keyPress[0].buttonB |= keyDown[0].buttonB
	end if

	touchDebug = keyDown[0].buttonB

	temp0 = false
	if keyDown[0].left == true
		object.xpos -= object.yvel
		temp0 = true
	end if

	if keyDown[0].right == true
		object.xpos += object.yvel
		temp0 = true
	end if

	if keyDown[0].up == true
		object.ypos -= object.yvel
		temp0 = true
	end if

	if keyDown[0].down == true
		object.ypos += object.yvel
		temp0 = true
	end if

	if temp0 == true
		object.yvel += 0xC00
		if object.yvel > 0x100000
			object.yvel = 0x100000
		end if
	else
		object.yvel = 0
	end if
#endplatform

#platform: USE_ORIGINS
	// They forgot to add the ability to pause in Debug Mode...
	
	temp0 = keyDown[1].left
	temp0 |= keyDown[1].right
	temp0 |= keyDown[1].up
	temp0 |= keyDown[1].down
	if temp0 != false
		object.yvel += 0xC00
		if object.yvel > 0x100000
			object.yvel = 0x100000
		end if
	else
		object.yvel = 0
	end if
	object.xvel = object.yvel
	
	temp0 = false
	if keyDown[1].left == true
		object.xpos -= object.yvel
		temp0 = 1
	end if
	if keyDown[1].right == true
		object.xpos += object.yvel
		temp0 = 1
	end if
	if keyDown[1].up == true
		object.ypos -= object.yvel
		temp0 = 1
	end if
	if keyDown[1].down == true
		object.ypos += object.yvel
		temp0 = 1
	end if
#endplatform

	player.drownTimer = 0
	player.drownLevel = 0

	if keyPress[0].buttonB == true
		// Exit debug mode
		if stage.playerListPos == PLAYER_TAILS_ALONE
			object.type = TypeName[Tails Object]
		else
			object.type = TypeName[Player Object]
		end if
		object.speed = 0
		object.xvel = 0
		object.yvel = 0
		object.state = Player_State_Air
		object.animation = ANI_WALKING
		object.animationSpeed = 0
		object.frame = 0
		object.controlMode = CONTROLMODE_P1
		object.drawOrder = -1
		player.currentPlane = 4
		object.interaction = true
		object.tileCollisions = true
		object.visible = true
	else
		if keyPress[0].buttonA == true
			object.currentSelection++
#platform: USE_ORIGINS
			// Skip the life/coin monitor (no farming coins!)
			if object.currentSelection == 5
				object.currentSelection++
			end if
#endplatform
			if object.currentSelection >= DebugMode_ObjCount
				object.currentSelection = 0
			end if
		end if

		if keyDown[0].buttonA == true
			if keyPress[0].buttonC == true
				// Go back one on the selection list
				object.currentSelection--
#platform: USE_ORIGINS
				// Skip the life/coin monitor (no farming coins!)
				if object.currentSelection == 5
					object.currentSelection--
				end if
#endplatform
				if object.currentSelection < 0
					object.currentSelection += DebugMode_ObjCount
				end if
			end if
		else
			if keyPress[0].buttonC == true
				// Spawn the object
				GetTableValue(temp0, object.currentSelection, DebugMode_SpawnTable)
				CallFunction(temp0)
				object[tempObjectPos].priority = PRIORITY_XBOUNDS_DESTROY
			end if
		end if
	end if
end event


event ObjectDraw
	// Temporarily switch to the selected object in order to draw it
	GetTableValue(object.type, object.currentSelection, DebugMode_TypesTable)
	GetTableValue(temp0, object.currentSelection, DebugMode_DrawTable)
	CallFunction(temp0)

	// Switch back to the Debug Mode object
	object.type = TypeName[Debug Mode]
end event


event ObjectStartup
	LoadSpriteSheet("Global/Display.gif")

	// Placeholder frame, otherwise unused
	// It's the Sonic CD life icon, in case you were wondering
	SpriteFrame(-8, -8, 16, 16, 188, 239)

	DebugMode_ObjCount = 0

	// Fill the table with dummy placeholders
	temp0 = 0
	while temp0 < 128
		SetTableValue(TypeName[Debug Mode], temp0, DebugMode_TypesTable)
		SetTableValue(DebugMode_DrawBlank, temp0, DebugMode_DrawTable)
		SetTableValue(DebugMode_SpawnBlank, temp0, DebugMode_SpawnTable)
		temp0++
	loop
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -8, 16, 16, 188, 239) // Placeholder CD life icon
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
