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

//-------Aliases-------//
private alias 45 : TYPE_TARGETBUMPER

// Function declarations
reserve function TargetBumper_DebugDraw
reserve function TargetBumper_DebugSpawn
reserve function TargetBumper_Function113

// Static Values
private value TargetBumper_value33 = 0;

// Tables

function TargetBumper_DebugDraw
	temp0 = object[0].value17
	temp0 -= TargetBumper_value33
	temp1 = object.direction
	switch temp0
	case 0
		object.direction = FLIP_NONE
		temp0 = 0
		break
	case 1
		object.direction = FLIP_NONE
		temp0 = 3
		break
	case 2
		object.direction = FLIP_X
		temp0 = 3
		break
	case 3
		object.direction = FLIP_NONE
		temp0 = 6
		break
	case 4
		object.direction = FLIP_X
		temp0 = 6
		break
	end switch
	DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
	object.direction = temp1
end function


function TargetBumper_DebugSpawn
	temp0 = object[0].value17
	temp0 -= TargetBumper_value33
	CreateTempObject(TypeName[Target Bumper], temp0, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 4
	switch object[tempObjectPos].propertyValue
	case 0
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].value4 = 0
		object[tempObjectPos].value1 = 0
		object[tempObjectPos].value2 = 0x20000
		break
	case 1
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].value4 = 3
		object[tempObjectPos].value1 = -0x20000
		object[tempObjectPos].value2 = 0x20000
		break
	case 2
		object[tempObjectPos].direction = FLIP_X
		object[tempObjectPos].value4 = 3
		object[tempObjectPos].value1 = 0x20000
		object[tempObjectPos].value2 = 0x20000
		break
	case 3
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].value4 = 6
		object[tempObjectPos].value1 = -0x20000
		object[tempObjectPos].value2 = 0
		break
	case 4
		object[tempObjectPos].direction = FLIP_X
		object[tempObjectPos].value4 = 6
		object[tempObjectPos].value1 = 0x20000
		object[tempObjectPos].value2 = 0
		break
	end switch
end function


function TargetBumper_Function113
	switch object.propertyValue
	case 0
		object.xvel = object[currentPlayer].xvel
		if object[currentPlayer].ypos > object.ypos
			object.yvel = 0x70000
		else
			object.yvel = -0x70000
		end if
		break
	case 1
		ATan2(temp0, object[currentPlayer].xvel, object[currentPlayer].yvel)
		temp0 -= 96
		temp1 = temp0
		Abs(temp1)
		FlipSign(temp0)
		temp0 += 96
		temp2 = temp1
		temp2 &= 255
		if temp2 >= 64
			temp1 -= 128
			FlipSign(temp1)
			temp2 = temp1
			temp2 &= 255
			if temp2 >= 56
				temp0 = 96
				temp0 += 128
			end if
		else
			if temp2 >= 56
				temp0 = 96
			end if
		end if
		temp0 &= 255
		Cos256(object.xvel, temp0)
		Sin256(object.yvel, temp0)
		object.xvel *= -0x700
		object.yvel *= -0x700
		break
	case 2
		ATan2(temp0, object[currentPlayer].xvel, object[currentPlayer].yvel)
		temp0 -= 32
		temp1 = temp0
		Abs(temp1)
		FlipSign(temp0)
		temp0 += 32
		temp2 = temp1
		temp2 &= 255
		if temp2 >= 64
			temp1 -= 128
			FlipSign(temp1)
			temp2 = temp1
			temp2 &= 255
			if temp2 >= 56
				temp0 = 32
				temp0 += 128
			end if
		else
			if temp2 >= 56
				temp0 = 32
			end if
		end if
		temp0 &= 255
		Cos256(object.xvel, temp0)
		Sin256(object.yvel, temp0)
		object.xvel *= -0x700
		object.yvel *= -0x700
		break
	case 3
		object.yvel = object[currentPlayer].yvel
		if object[currentPlayer].xpos > object.xpos
			object.xvel = 0x70000
		else
			object.xvel = -0x70000
		end if
		break
	case 4
		object.yvel = object[currentPlayer].yvel
		if object[currentPlayer].xpos > object.xpos
			object.xvel = 0x70000
		else
			object.xvel = -0x70000
		end if
		break
	end switch
	if object[currentPlayer].state == PlayerObject_HandleFlyCarry
		object[1].value36 = 30
	end if
	CheckEqual(object[currentPlayer].animation, ANI_GLIDING)
	temp2 = checkResult
	CheckEqual(object[currentPlayer].animation, ANI_GLIDING_STOP)
	temp2 |= checkResult
	CheckEqual(object[currentPlayer].animation, ANI_FLYING)
	temp2 |= checkResult
	CheckEqual(object[currentPlayer].state, PlayerObject_HandleRollDecel)
	temp2 |= checkResult
	CheckEqual(object[currentPlayer].state, PlayerObject_Unknown)
	temp2 |= checkResult
	if temp2 == 0
		object[currentPlayer].state = PlayerObject_HandleAir
		if object[currentPlayer].animation != ANI_JUMPING
			if object[currentPlayer].animation != ANI_RUNNING
				object[currentPlayer].animation = ANI_WALKING
			end if
		end if
	end if
	if object[currentPlayer].animation == ANI_GLIDING
		if object.xvel > 0
			if object[currentPlayer].xvel < 0
				object[currentPlayer].animation = ANI_GLIDING_DROP
				object[currentPlayer].state = PlayerObject_KnuxGlideDrop
			else
				object[currentPlayer].state = PlayerObject_KnuxGlideRight
			end if
		else
			if object[currentPlayer].xvel > 0
				object[currentPlayer].animation = ANI_GLIDING_DROP
				object[currentPlayer].state = PlayerObject_KnuxGlideDrop
			else
				object[currentPlayer].state = PlayerObject_KnuxGlideLeft
			end if
		end if
	end if
	object[currentPlayer].xvel = object.xvel
	object[currentPlayer].speed = object[currentPlayer].xvel
	if object[currentPlayer].animation == ANI_GLIDING
		if object.xvel < 0
			FlipSign(object[currentPlayer].speed)
		end if
	end if
	object[currentPlayer].value1 = 0
	object[currentPlayer].yvel = object.yvel
	object[currentPlayer].gravity = GRAVITY_AIR
	object[currentPlayer].tileCollisions = 1
end function


event ObjectMain
	if object.state > 0
		object.animationTimer = object.value0
		object.animationTimer >>= 2
		object.animationTimer++
		object.value0++
		if object.value0 > 12
			object.value0 = 0
			object.state = 0
			object.animationTimer = 0
			if object.value3 > 2
				object.type = TypeName[Blank Object]
			end if
		end if
	end if
	if object.visible == 1
		temp7 = 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp6 = object.value6
			temp6 >>= temp7
			temp6 &= 255
			if temp6 == 0
				switch object.propertyValue
				case 0
					BoxCollisionTest(C_TOUCH, object.entityPos, -14, -4, 14, 4, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					break
				case 1
					BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					break
				case 2
					BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					break
				case 3
					BoxCollisionTest(C_TOUCH, object.entityPos, -4, -14, 4, 14, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					break
				case 4
					BoxCollisionTest(C_TOUCH, object.entityPos, -4, -14, 4, 14, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					break
				end switch
				if checkResult == 1
					if object.state == 0
						PlaySfx(SfxName[Target Bumper], 0)
						if object.value3 < 3
							switch object.value5
							case 0
								CheckNotEqual(object[+1].type, TypeName[Target Bumper])
								temp0 = checkResult
								CheckNotEqual(object[+2].type, TypeName[Target Bumper])
								temp0 &= checkResult
								break
							case 1
								CheckNotEqual(object[-1].type, TypeName[Target Bumper])
								temp0 = checkResult
								CheckNotEqual(object[+1].type, TypeName[Target Bumper])
								temp0 &= checkResult
								break
							case 2
								CheckNotEqual(object[-1].type, TypeName[Target Bumper])
								temp0 = checkResult
								CheckNotEqual(object[-2].type, TypeName[Target Bumper])
								temp0 &= checkResult
								break
							end switch
							CheckEqual(object.value3, 2)
							temp0 &= checkResult
							arrayPos0 = 22
							if object[currentPlayer].value16 == 0
								arrayPos0 += currentPlayer
							end if
							if temp0 == 0
								CreateTempObject(TypeName[Object Score], 16, object.xpos, object.ypos)
								player.score += 10
							else
								CreateTempObject(TypeName[Object Score], 2, object.xpos, object.ypos)
								player.score += 500
							end if
							object.value3++
						end if
						CallFunction(TargetBumper_Function113)
					else
						if object.value0 > 5
							PlaySfx(SfxName[Target Bumper], 0)
							CallFunction(TargetBumper_Function113)
						end if
					end if
					object.state = 1
					object.priority = PRIORITY_ACTIVE
					temp6 = 3
					temp6 <<= temp7
					object.value6 |= temp6
				end if
			else
				temp5 = 1
				temp5 <<= temp7
				object.value6 -= temp5
			end if
			temp7 += 8
		next
	end if
end event


event ObjectDraw
	if object.visible == 1
		temp0 = object.animationTimer
		temp0 &= 1
		object.frame = object.value4
		if object.value3 < 2
			object.frame += object.value3
		else
			object.frame += 2
		end if
		if temp0 == 0
			DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
		else
			temp1 = object.xpos
			temp1 += object.value1
			temp2 = object.ypos
			temp2 += object.value2
			DrawSpriteFX(object.frame, FX_FLIP, temp1, temp2)
		end if
	end if
end event


event ObjectStartup
	LoadSpriteSheet("CNZ/Objects.gif")
	SpriteFrame(-16, -6, 32, 12, 1, 166)
	SpriteFrame(-16, -6, 32, 12, 1, 153)
	SpriteFrame(-16, -6, 32, 12, 1, 140)
	SpriteFrame(-12, -12, 24, 24, 1, 229)
	SpriteFrame(-12, -12, 24, 24, 1, 204)
	SpriteFrame(-12, -12, 24, 24, 1, 179)
	SpriteFrame(-6, -16, 12, 32, 60, 140)
	SpriteFrame(-6, -16, 12, 32, 47, 140)
	SpriteFrame(-6, -16, 12, 32, 34, 140)
	foreach (TypeName[Target Bumper], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value5 = object[arrayPos0].propertyValue
		object[arrayPos0].value5 >>= 6
		object[arrayPos0].propertyValue &= 63
		switch object[arrayPos0].propertyValue
		case 0
			object[arrayPos0].direction = FLIP_NONE
			object[arrayPos0].value4 = 0
			object[arrayPos0].value1 = 0
			object[arrayPos0].value2 = 0x20000
			break
		case 1
			object[arrayPos0].direction = FLIP_NONE
			object[arrayPos0].value4 = 3
			object[arrayPos0].value1 = -0x20000
			object[arrayPos0].value2 = 0x20000
			break
		case 2
			object[arrayPos0].direction = FLIP_X
			object[arrayPos0].value4 = 3
			object[arrayPos0].value1 = 0x20000
			object[arrayPos0].value2 = 0x20000
			break
		case 3
			object[arrayPos0].direction = FLIP_NONE
			object[arrayPos0].value4 = 6
			object[arrayPos0].value1 = -0x20000
			object[arrayPos0].value2 = 0
			break
		case 4
			object[arrayPos0].direction = FLIP_X
			object[arrayPos0].value4 = 6
			object[arrayPos0].value1 = 0x20000
			object[arrayPos0].value2 = 0
			break
		end switch
	next
	temp0 = 0
	TargetBumper_value33 = DebugMode_ObjCount
	while temp0 < 5
		SetTableValue(TypeName[Target Bumper], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(TargetBumper_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(TargetBumper_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

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