// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Big Turtloid 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.child
private alias object.value2 : object.handleFrame

// States
private alias 0 : BIGTURTLOID_AWAITPLAYER
private alias 1 : BIGTURTLOID_FIREDSHOT
private alias 2 : BIGTURTLOID_IDLE

// Player Aliases
private alias object.xpos : player.xpos


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

reserve function BigTurtloid_DebugDraw
reserve function BigTurtloid_DebugSpawn

private function BigTurtloid_DebugDraw
	DrawSprite(2)
end function


private function BigTurtloid_DebugSpawn
	CreateTempObject(TypeName[Big Turtloid], 0, object.xpos, object.ypos)
	object[tempObjectPos].xvel = 0x8000
end function


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

event ObjectUpdate
	switch object.state
	case BIGTURTLOID_AWAITPLAYER
		if object.child > 0
			temp0 = object.xpos
			temp0 -= player[0].xpos
			Abs(temp0)
			if temp0 < 0x800000
				object.xvel = 0
				arrayPos0 = object.child
				object[arrayPos0].frame = 1
				object.handleFrame = 3

				CreateTempObject(TypeName[Turtloid Shot], 0, object.xpos, object.ypos)
				object[tempObjectPos].xpos -= 0x120000
				object[tempObjectPos].ypos += 0x90000
				object[tempObjectPos].xvel = -0x8000
				PlaySfx(SfxName[Shot], false)
				object.state++
			end if
		end if
		break

	case BIGTURTLOID_FIREDSHOT
		object.timer++
		if object.timer == 16
			object.timer = 0
			object.xvel = -0x8000
			arrayPos0 = object.child
			object.handleFrame = 2
			object.state++
		end if
		break

	case BIGTURTLOID_IDLE
		break
		
	end switch

	object.xpos += object.xvel
	object.xpos += SCZSetup_scrollVelocity.x
	object.ypos += SCZSetup_scrollVelocity.y
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_PLATFORM, object.entityPos, -24, -15, 24, 0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			player[currentPlayer].xpos += SCZSetup_scrollVelocity.x
			player[currentPlayer].xpos += object.xvel
		end if
	next

	object.frame = object.animationTimer
	object.frame >>= 1
	object.animationTimer++
	object.animationTimer &= 3
end event


event ObjectDraw
	DrawSprite(object.handleFrame)
	DrawSprite(object.frame)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone10")
	if checkResult == true
		LoadSpriteSheet("SCZ/Objects.gif")
		SpriteFrame(28, 6, 16, 8, 186, 90)
		SpriteFrame(28, 6, 7, 8, 186, 99)
		SpriteFrame(-28, -15, 56, 31, 72, 42)
		SpriteFrame(-28, -15, 56, 31, 129, 42)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(28, 6, 16, 8, 186, 90)
		SpriteFrame(28, 6, 7, 8, 186, 99)
		SpriteFrame(-28, -15, 56, 31, 72, 42)
		SpriteFrame(-28, -15, 56, 31, 129, 42)
	end if

	foreach (TypeName[Big Turtloid], arrayPos0, ALL_ENTITIES)
		temp0 = screen.xsize
		temp0 -= 320
		temp0 <<= 16
		object[arrayPos0].handleFrame = 2
		object[arrayPos0].xpos += 0x400000
		object[arrayPos0].xpos -= temp0
		object[arrayPos0].xvel = -0x8000
		arrayPos1 = arrayPos0
		arrayPos1++
		if object[arrayPos1].type == TypeName[SmallTurtloid]
			object[arrayPos0].child = arrayPos1
		else
			object[arrayPos0].child = 0
		end if
	next

	SetTableValue(TypeName[Big Turtloid], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(BigTurtloid_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(BigTurtloid_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(2)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone10")
	if checkResult == true
		LoadSpriteSheet("SCZ/Objects.gif")
		SpriteFrame(28, 6, 16, 8, 186, 90)
		SpriteFrame(28, 6, 7, 8, 186, 99)
		SpriteFrame(-28, -15, 56, 31, 72, 42)
		SpriteFrame(-28, -15, 56, 31, 129, 42)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(28, 6, 16, 8, 186, 90)
		SpriteFrame(28, 6, 7, 8, 186, 99)
		SpriteFrame(-28, -15, 56, 31, 72, 42)
		SpriteFrame(-28, -15, 56, 31, 129, 42)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
