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

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

private alias object.frame : object.propellerFrame

private alias object.value0 : object.timer
private alias object.value1 : object.stood
private alias object.value2 : object.gravityForce
private alias object.value3 : object.storedPos.y
private alias object.value4 : object.lastPos.y
private alias object.value5 : object.collisionOffset.y
private alias object.value6 : object.pilotFrame
private alias object.value7 : object.pilotFrameDelay
private alias object.value8 : object.pilotAnimPos

// States
private alias 0 : TORNADO_IDLE
private alias 1 : TORNADO_JUMP_RECOIL
private alias 2 : TORNADO_JUMP_REBOUND
private alias 3 : TORNADO_LAND

// Player Aliases
private alias object.type : player.type
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.yvel : player.yvel
private alias object.jumpPress : player.jumpPress


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

private table Tornado_TailsPilotFrameTable
	5, 32
	6, 8
	7, 8
	8, 8
	7, 8
	6, 16
	9, 32
	6, 16
end table

private table Tornado_SonicPilotFrameTable
	10, 9
	11, 9
	10, 9
	11, 9
	10, 9
	11, 9
	10, 9
	11, 9
end table


event ObjectUpdate
	object.lastPos.y = object.ypos
	object.drawOrder = 4

	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
	end if

	switch object.state
	case TORNADO_IDLE
		if player[0].yvel < 0
			if player[0].jumpPress == true
				object.gravityForce = 0x20000
				object.storedPos.y = object.ypos
				object.state++
			end if
		end if
		break

	case TORNADO_JUMP_RECOIL // jump force pushes tornado downwards
		object.gravityForce -= 0x2000
		object.ypos += object.gravityForce
		if object.gravityForce < 0
			object.state++
		end if
		break

	case TORNADO_JUMP_REBOUND // tornado returns to its original position
		object.gravityForce -= 0x1000
		object.ypos += object.gravityForce
		if object.ypos <= object.storedPos.y
			object.ypos = object.storedPos.y
			object.gravityForce = 0
			object.state = TORNADO_IDLE
		end if
		break

	case TORNADO_LAND
		object.gravityForce -= 0x2000
		object.ypos += object.gravityForce
		object.timer++
		if object.timer == 24
			object.gravityForce = 0
			object.state = TORNADO_IDLE
		end if
		break
		
	end switch

	if object.stood == true
		object.xpos += SCZSetup_scrollVelocity.x
		object.ypos += SCZSetup_scrollVelocity.y
		if keyDown[0].up == true
			object.yvel = -0x10000
		else
			object.yvel = 0
		end if

		if keyDown[0].down == true
			object.yvel = 0x10000
		end if
	else
		object.yvel = 0
	end if

	object.ypos += object.yvel
	temp0 = camera[0].ypos
	temp0 -= 52
	temp0 <<= 16
	if object.ypos < temp0
		object.ypos = temp0
	end if

	temp0 = camera[0].ypos
	temp0 += 76
	temp0 <<= 16
	if object.ypos > temp0
		object.ypos = temp0
	end if

	temp0 = object.ypos
	object.lastPos.y &= 0xFFFF0000
	object.collisionOffset.y = object.ypos
	object.collisionOffset.y &= 0xFFFF0000
	object.collisionOffset.y -= object.lastPos.y
	object.ypos = object.lastPos.y

	object.stood = false
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp2 = player[currentPlayer].yvel
		BoxCollisionTest(C_PLATFORM, object.entityPos, -20, -8, 20, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			player[currentPlayer].xpos += SCZSetup_scrollVelocity.x
			player[currentPlayer].ypos += object.collisionOffset.y
			if temp2 > 0x10000
				object.timer = 0
				object.gravityForce = 0x20000
				object.state = TORNADO_LAND
			end if
			object.stood |= true
		end if
	next

	object.ypos = temp0

	temp0 = player[0].xpos
	temp0 -= object.xpos
	Abs(temp0)
	if temp0 > 0x100000
		if player[0].xpos > object.xpos
			temp0 = player[0].xpos
			temp0 -= object.xpos
			temp0 -= 0x100000

			object.xpos += temp0
		else
			temp0 = player[0].xpos
			temp0 -= object.xpos
			temp0 += 0x100000
	
			object.xpos += temp0
		end if
	end if

	temp0 = -0x100000
	temp0 += object.xpos
	temp1 = stage.curXBoundary1
	temp1 <<= 16
	if temp0 < temp1
		object.xpos = temp1
		object.xpos += 0x100000
	end if

	object.propellerFrame++
	object.propellerFrame &= 3

	if object.pilotFrameDelay <= 1
		if stage.playerListPos == PLAYER_TAILS_ALONE
			GetTableValue(object.pilotFrame, object.pilotAnimPos, Tornado_SonicPilotFrameTable)
			object.pilotAnimPos++

			GetTableValue(object.pilotFrameDelay, object.pilotAnimPos, Tornado_SonicPilotFrameTable)
			object.pilotAnimPos++
		else
			GetTableValue(object.pilotFrame, object.pilotAnimPos, Tornado_TailsPilotFrameTable)
			object.pilotAnimPos++

			GetTableValue(object.pilotFrameDelay, object.pilotAnimPos, Tornado_TailsPilotFrameTable)
			object.pilotAnimPos++
		end if
		object.pilotAnimPos &= 15
	else
		object.pilotFrameDelay--
	end if
end event


event ObjectDraw
	DrawSprite(object.propellerFrame) 		// propeller
	DrawSprite(4)					// tornado
	DrawSprite(object.pilotFrame) 	// pilot
end event


event ObjectStartup
	LoadSpriteSheet("SCZ/Objects.gif")
	SpriteFrame(21, 8, 4, 24, 378, 215)			// Propeller - Frame 1 		- #0
	SpriteFrame(21, 11, 4, 18, 383, 218)		// Propeller - Frame 2 		- #1
	SpriteFrame(21, 16, 4, 8, 388, 223)			// Propeller - Frame 3 		- #2
	SpriteFrame(21, 11, 4, 18, 393, 218)		// Propeller - Frame 4 		- #3
	SpriteFrame(-95, -16, 116, 64, 261, 191) 	// Tornado 					- #4
	SpriteFrame(-45, -8, 21, 16, 261, 174)		// Pilot (Tails) - Frame 1 	- #5
	SpriteFrame(-45, -8, 21, 16, 283, 174)		// Pilot (Tails) - Frame 2 	- #6
	SpriteFrame(-45, -8, 21, 16, 305, 174)		// Pilot (Tails) - Frame 3 	- #7
	SpriteFrame(-45, -8, 21, 16, 327, 174)		// Pilot (Tails) - Frame 4 	- #8
	SpriteFrame(-47, -8, 23, 16, 349, 174)		// Pilot (Tails) - Frame 5 	- #9
	SpriteFrame(-50, -8, 24, 16, 294, 157)		// Pilot (Sonic) - Frame 6 	- #10
	SpriteFrame(-50, -8, 24, 16, 319, 157)		// Pilot (Sonic) - Frame 2 	- #11

	// Bye bye player 2
	player[1].type = TypeName[Blank Object]
	playerCount = 1

	if stage.playerListPos == PLAYER_TAILS_ALONE
		player[0].ypos += 0x40000
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("SCZ/Objects.gif")
	SpriteFrame(-95, -16, 116, 64, 261, 191)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
