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

//-------Aliases-------//
private alias 68 : TYPE_STEGWAY

// Function declarations
reserve function Stegway_DebugDraw
reserve function Stegway_DebugSpawn

// Static Values

// Tables

function Stegway_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


function Stegway_DebugSpawn
	CreateTempObject(TypeName[Stegway], 0, object.xpos, object.ypos)
	if object.direction == FLIP_NONE
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].xvel = -0x4000
	else
		object[tempObjectPos].direction = FLIP_X
		object[tempObjectPos].xvel = 0x4000
	end if
end function


event ObjectMain
	switch object.state
	case 0
		if object.priority != PRIORITY_XBOUNDS_DESTROY
			object.priority = PRIORITY_ACTIVE
		end if
		object.xpos += object.xvel
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.direction == FLIP_NONE
				BoxCollisionTest(C_TOUCH, object.entityPos, -96, -32, 0, 14, currentPlayer, 0, 0, 0, 0)
			else
				BoxCollisionTest(C_TOUCH, object.entityPos, 0, -32, 96, 14, currentPlayer, 0, 0, 0, 0)
			end if
			if checkResult == 1
				object.state = 2
				object.animation = 3
			end if
		next
		if object.direction == FLIP_NONE
			ObjectTileGrip(CSIDE_FLOOR, -12, 16, 0)
		else
			ObjectTileGrip(CSIDE_FLOOR, 12, 16, 0)
		end if
		if checkResult == 0
			object.state = 1
			object.animation = 3
			object.value3 = 0
			if object.frame == 3
				object.frame = 2
			end if
		end if
		break
	case 1
		if object.value0 < 29
			object.value0++
		else
			object.value0 = 0
			object.state = 0
			object.animation = 0
			object.direction ^= FLIP_X
			if object.direction == FLIP_NONE
				object.xvel = -0x4000
			else
				object.xvel = 0x4000
			end if
		end if
		break
	case 2
		object.value0++
		if object.value0 == 8
			object.value0 = 0
			object.state = 3
			object.animation = 2
			PlaySfx(SfxName[Rev Up], 0)
		end if
		break
	case 3
		object.value0++
		if object.value0 == 32
			object.value0 = 0
			object.state = 4
			object.animation = 1
			object.value3 = 1
			object.xvel *= 12
			PlaySfx(SfxName[Release], 0)
		end if
		break
	case 4
		object.xpos += object.xvel
		if object.direction == FLIP_NONE
			if object.value7 == 0
				temp0 = object.ypos
				ObjectTileGrip(CSIDE_FLOOR, -64, 16, 0)
				if checkResult == 0
					object.value7 = 1
				end if
				object.ypos = temp0
			end if
			ObjectTileGrip(CSIDE_FLOOR, -12, 16, 0)
		else
			if object.value7 == 0
				temp0 = object.ypos
				ObjectTileGrip(CSIDE_FLOOR, 64, 16, 0)
				if checkResult == 0
					object.value7 = 1
				end if
				object.ypos = temp0
			end if
			ObjectTileGrip(CSIDE_FLOOR, 12, 16, 0)
		end if
		if object.value7 == 1
			if ringTimer == 0
				CreateTempObject(TypeName[Dust Puff], 0, object.xpos, object.ypos)
				if object.direction == FLIP_NONE
					object[tempObjectPos].ixpos += 10
				else
					object[tempObjectPos].ixpos -= 10
				end if
				object[tempObjectPos].iypos += 16
			end if
			if object.xvel >= 0x4000
				object.xvel -= 0xA00
				if object.xvel < 0x4000
					object.xvel = 0x4000
					object.state = 0
					object.animation = 0
					object.animationTimer = 0
					object.frame = 1
					object.value3 = 0
					object.value7 = 0
				end if
			end if
			if object.xvel <= -0x4000
				object.xvel += 0xA00
				if object.xvel > -0x4000
					object.xvel = -0x4000
					object.state = 0
					object.animation = 0
					object.animationTimer = 0
					object.frame = 1
					object.value3 = 0
					object.value7 = 0
				end if
			end if
		end if
		if checkResult == 0
			object.state = 1
			object.animation = 3
			object.value3 = 0
			object.value7 = 0
			if object.frame == 3
				object.frame = 2
			end if
		end if
		break
	end switch
	if object.outOfBounds == 1
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.value1
		object.ypos = object.value2
		if object.outOfBounds == 1
			if object.propertyValue == 0
				object.direction = FLIP_NONE
				object.xvel = -0x4000
			else
				object.direction = FLIP_X
				object.xvel = 0x4000
			end if
			object.animation = 0
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_ACTIVE_BOUNDS
			end if
			object.value3 = 0
			object.value5 = 0
			object.value4 = 0
			object.value7 = 0
			object.state = 0
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if
	CallFunction(PlayerObject_EnemyPop)
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -20, -14, 20, 14, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
		if checkResult == 1
			CallFunction(PlayerObject_BadnikBreak)
		end if
	next
	switch object.animation
	case 0
		if object.animationTimer == 0
			object.frame++
			object.frame %= 3
			switch object.frame
			case 0
				object.animationTimer = 14
				break
			case 1
				object.animationTimer = 6
				break
			case 2
				object.animationTimer = 10
				break
			end switch
		else
			object.animationTimer--
		end if
		object.value6++
		object.value6 &= 15
		object.value5 = object.value6
		object.value5 >>= 3
		break
	case 1
		object.frame = 3
		object.value6++
		object.value6 %= 6
		if object.value7 == 1
			object.value5 = 0
		else
			object.value5 = object.value6
			object.value5 /= 3
		end if
		break
	case 2
		object.frame = 2
		object.value6++
		object.value6 &= 3
		object.value5 = object.value6
		object.value5 >>= 1
		break
	end switch
	if object.value3 == 1
		object.value4++
		object.value4 &= 3
	end if
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
	if object.value5 == 1
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
	end if
	if object.value3 == 1
		temp0 = object.value4
		temp0 >>= 1
		temp0 += 5
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-24, -16, 48, 32, 40, 57)
	SpriteFrame(-24, -16, 48, 32, 89, 57)
	SpriteFrame(-24, -16, 48, 32, 82, 90)
	SpriteFrame(-24, -16, 48, 32, 33, 90)
	SpriteFrame(0, 0, 24, 16, 198, 29)
	SpriteFrame(24, 4, 7, 6, 206, 7)
	SpriteFrame(24, 4, 8, 6, 206, 14)
	foreach (TypeName[Stegway], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value1 = object[arrayPos0].xpos
		object[arrayPos0].value2 = object[arrayPos0].ypos
		if object[arrayPos0].propertyValue == 0
			object[arrayPos0].direction = FLIP_NONE
			object[arrayPos0].xvel = -0x4000
		else
			object[arrayPos0].direction = FLIP_X
			object[arrayPos0].xvel = 0x4000
		end if
	next
	SetTableValue(TypeName[Stegway], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Stegway_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Stegway_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

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