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

// Along with being used in Casino Night Zone, this Object is also used in Mission "Bounce House" - M036 - Mission_Zone09
// Do note, however, that in that stage this Object is given a name of `OBJECT NAME` instead, meaning all the `TypeName[Bumper]` uses
// you see here end up not working there

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

private alias object.value0 : object.timer
private alias object.value1 : object.bumpCount
private alias object.value2 : object.playerTimers
private alias object.value3 : object.DespawnTimer

// States
private alias 0 : BUMPER_IDLE
private alias 1 : BUMPER_BUMPED

// Player Aliases
private alias object.state : player.state
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.xvel : player.xvel
private alias object.yvel : player.yvel
private alias object.speed : player.speed
private alias object.gravity : player.gravity
private alias object.animation : player.animation
private alias object.tileCollisions : player.tileCollisions
private alias object.value1  : player.timer
private alias object.value16 : player.isSidekick			// false if player 1, true if player 2
private alias object.value36 : player.flyCarryTimer		// Tails assist lockout timer
private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom

// Global Variable ID Aliases
private alias 22 : GLOBAL_PLAYERSCORE


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

reserve function Bumper_DebugDraw
reserve function Bumper_DebugSpawn


// ========================
// Function Definitions
// ========================

private function Bumper_DebugDraw
	DrawSprite(0)
end function


private function Bumper_DebugSpawn
	CreateTempObject(TypeName[Bumper], 0, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 4
end function


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

event ObjectUpdate
	if object.DespawnTimer > 0
		object.DespawnTimer--
	end if
	if object.DespawnTimer == 1
		object.type = TypeName[Blank Object]
	end if
	if object.state > BUMPER_IDLE
		object.frame = object.timer
		object.frame /= 5
		object.frame++

		object.timer++
		if object.timer > 15
			object.timer = 0
			object.state = BUMPER_IDLE
			object.frame = 0
		end if
	end if

	if object.outOfBounds == true
		object.bumpCount = 0
		object.priority = PRIORITY_BOUNDS
	end if

	temp7 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp6 = object.playerTimers
		temp6 >>= temp7
		temp6 &= 0xFF
		if temp6 == false
			if player[currentPlayer].animation != ANI_HURT
				BoxCollisionTest(C_TOUCH, object.entityPos, -7, -6, 7, 6, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
				if checkResult == true
					if object.state == BUMPER_IDLE
						PlaySfx(SfxName[Bumper], false)
						if object.bumpCount < 10
							CreateTempObject(TypeName[Object Score], 16, object.xpos, object.ypos)
							
							// global variable "array" (yes, this is actually how its done)
							arrayPos0 = GLOBAL_PLAYERSCORE
#platform: USE_DECOMP
							arrayPos0 = VarName[player.score]
#endplatform
							if player[currentPlayer].isSidekick == false
								arrayPos0 += currentPlayer
							end if
							global[arrayPos0] += 10
							
							object.bumpCount++
						end if
					end if

					if object.timer > 5
						PlaySfx(SfxName[Bumper], false)
					end if

					object.state = BUMPER_BUMPED
					object.priority = PRIORITY_ACTIVE
					temp0 = player[currentPlayer].xpos
					temp0 -= object.xpos
					temp1 = player[currentPlayer].ypos
					temp1 -= object.ypos
					ATan2(temp2, temp0, temp1)
					Cos256(temp0, temp2)
					Sin256(temp1, temp2)
					temp0 *= 0x700
					temp1 *= 0x700
					if player[currentPlayer].state == Player_State_Carried
						player[1].flyCarryTimer = 30
					end if

					CheckEqual(player[currentPlayer].animation, ANI_GLIDING)
					temp2 = checkResult
					CheckEqual(player[currentPlayer].animation, ANI_GLIDING_STOP)
					temp2 |= checkResult
					CheckEqual(player[currentPlayer].animation, ANI_FLYING)
					temp2 |= checkResult
					CheckEqual(player[currentPlayer].state, Player_State_TubeRoll)
					temp2 |= checkResult
					CheckEqual(player[currentPlayer].state, Player_State_TubeAirRoll)
					temp2 |= checkResult
					if temp2 == false
						player[currentPlayer].state = Player_State_Air
						if player[currentPlayer].animation != ANI_JUMPING
							if player[currentPlayer].animation != ANI_RUNNING
								player[currentPlayer].animation = ANI_WALKING
							end if
						end if
					end if

					if player[currentPlayer].animation != ANI_GLIDING
						player[currentPlayer].xvel = temp0
						player[currentPlayer].speed = player[currentPlayer].xvel
						player[currentPlayer].timer = 0
					end if

					player[currentPlayer].yvel = temp1
					player[currentPlayer].gravity = GRAVITY_AIR
					player[currentPlayer].tileCollisions = true

					// 3 frame cooldown
					temp6 = 3
					temp6 <<= temp7
					object.playerTimers |= temp6
				end if
			end if
		else
			// count down by 1 frame on the current player
			temp5 = 1
			temp5 <<= temp7
			object.playerTimers -= temp5
		end if
		temp7 += 8
	next
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone04")
	if checkResult == true
		LoadSpriteSheet("CNZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 32, 148, 100)
		SpriteFrame(-18, -18, 36, 36, 181, 100)
		SpriteFrame(-16, -16, 32, 32, 148, 100)
		SpriteFrame(-18, -18, 36, 36, 181, 100)
	else
#platform: USE_ORIGINS
		if game.playMode == BOOT_PLAYMODE_MISSION // Rather than just a "Zone09" check, check if it's Mission Mode as a whole instead
			LoadSpriteSheet("MPZ/Objects2.gif")
			SpriteFrame(-16, -16, 32, 32, 1, 1)
			SpriteFrame(-18, -18, 36, 36, 34, 1)
			SpriteFrame(-16, -16, 32, 32, 1, 1)
			SpriteFrame(-18, -18, 36, 36, 34, 1)
		else
#endplatform
			LoadSpriteSheet("MBZ/Objects.gif")
			SpriteFrame(-16, -16, 32, 32, 511, 339)
			SpriteFrame(-18, -18, 36, 36, 544, 339)
			SpriteFrame(-16, -16, 32, 32, 511, 339)
			SpriteFrame(-18, -18, 36, 36, 544, 339)
#platform: USE_ORIGINS
		end if
#endplatform
	end if
	
	// Bug Details:
	// -> In Origins's MPZ "Bounce House" mission that uses this object, this foreach does not properly work
	//    This is because Bumpers are given the type name of [OBJECT NAME] instead, in that scene
	// -> However, they did not update this foreach for that new name, which makes this TypeName invalid and resolve as 0 instead
	//    This means that not only do all Bumpers stay on Draw Order 3, but all Blank Objects get bumped up to 4 as well
	foreach (TypeName[Bumper], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 4
	next
	
	// Bug Details:
	// -> This DebugMode_TypesTable entry doesn't work either in that same aforementioned scene,
	//    same reason here where it instead spawns an object with type ID 0 (Blank Object)
	// -> Not too major at all but hey, I'm sure someone somewhere cares
	SetTableValue(TypeName[Bumper], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Bumper_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Bumper_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone04")
	if checkResult == true
		LoadSpriteSheet("CNZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 32, 148, 100)
	else
		CheckCurrentStageFolder("Zone09") // Technically not accurate to how it functions in-game, but we can't really replicate that behaviour either, so...
		if checkResult == true
			LoadSpriteSheet("MPZ/Objects2.gif")
			SpriteFrame(-16, -16, 32, 32, 1, 1)
		else
			LoadSpriteSheet("MBZ/Objects.gif")
			SpriteFrame(-16, -16, 32, 32, 511, 339)
		end if
	end if

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
