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

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

private alias object.value0 : object.timer

// Player Aliases
private alias object.value17 : debugMode.currentSelection


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

reserve function TurretPlatform_SetupPlatform
reserve function TurretPlatform_DebugDraw
reserve function TurretPlatform_DebugSpawn


// ========================
// Static Values
// ========================

private value TurretPlatform_startDebugID = 0


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

private function TurretPlatform_SetupPlatform
	object[arrayPos0].timer 			= 0
	object[arrayPos0].animationTimer 	= 0
	object[arrayPos0].frame 			= 0
	object[arrayPos0].state 			= 0
	object[arrayPos0].priority 			= PRIORITY_ACTIVE
end function


private function TurretPlatform_DebugDraw
	DrawSprite(0)
end function


private function TurretPlatform_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= TurretPlatform_startDebugID
	temp0 *= 0x30
	temp0 &= 0xF0
	CreateTempObject(TypeName[Turret Platform], temp0, object.xpos, object.ypos)

	arrayPos0 = object[tempObjectPos].entityPos
	CallFunction(TurretPlatform_SetupPlatform)
end function


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

event ObjectUpdate
	// TODO: add cool state names
	switch object.state
	case 0
		temp0 = oscillation
		temp0 &= 0xF0
		if temp0 == object.propertyValue
			object.priority 		= PRIORITY_ACTIVE
			object.timer 			= 160
			object.animationTimer 	= 6
			object.state 			= 1
		end if
		break

	case 1
		object.animationTimer++
		object.frame = object.animationTimer
		object.frame /= 6
		if object.frame == 4
			object.state = 2
		end if
		break

	case 2
		object.timer--
		if object.timer == 0
			object.state = 3
		end if
		break

	case 3
		object.animationTimer--
		object.frame = object.animationTimer
		object.frame /= 6
		if object.animationTimer == 0
			object.state = 0
		end if
		break
		
	end switch

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.frame >= 3
			BoxCollisionTest(C_PLATFORM, object.entityPos, -32, -24, 32, 0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		end if
	next

	if object.outOfBounds == true
		arrayPos0 = object.entityPos
		CallFunction(TurretPlatform_SetupPlatform)
	end if
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	LoadSpriteSheet("SCZ/Objects.gif")
	SpriteFrame(-24, -24, 48, 16, 130, 92)
	SpriteFrame(-24, -24, 48, 16, 130, 109)
	SpriteFrame(-24, -24, 48, 16, 130, 126)
	SpriteFrame(-28, -24, 56, 21, 130, 143)
	SpriteFrame(-32, -24, 64, 24, 130, 165)

	foreach (TypeName[Turret Platform], arrayPos0, ALL_ENTITIES)
		CallFunction(TurretPlatform_SetupPlatform)
	next

	temp0 = 0
	TurretPlatform_startDebugID = DebugMode_ObjCount
	while temp0 < 6
		SetTableValue(TypeName[Turret Platform], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(TurretPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(TurretPlatform_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("SCZ/Objects.gif")
	SpriteFrame(-24, -24, 48, 16, 130, 92)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "intervalOffset")
end event
