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

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

private alias object.value0 : object.timer
private alias object.value1 : object.bumpCount

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.value36 : player.flyCarryTimer
private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom


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

reserve function Bumper_DebugDraw
reserve function Bumper_DebugSpawn


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.state > BUMPER_IDLE
		object.frame = object.timer
		object.frame /= 5
		object.frame++
		object.timer++

		if object.timer > 22
			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

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		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 < 5
						CreateTempObject(TypeName[Object Score], 16, object.xpos, object.ypos)
						player.score += 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
				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
			end if
		end if
	next
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	LoadSpriteSheet("SYZ/Objects.gif")
	SpriteFrame(-14, -14, 28, 28, 1, 131)
	SpriteFrame(-12, -12, 24, 24, 30, 131)
	SpriteFrame(-16, -16, 32, 32, 55, 131)
	SpriteFrame(-12, -12, 24, 24, 30, 131)
	SpriteFrame(-16, -16, 32, 32, 55, 131)
	SpriteFrame(-16, -16, 32, 32, 55, 131) // This last frame isn't really used, it seems

	foreach (TypeName[Bumper], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 4
	next

	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
	LoadSpriteSheet("SYZ/Objects.gif")
	SpriteFrame(-14, -14, 28, 28, 1, 131)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
