// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Spear 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.startPos.y
private alias object.value31 : object.wrapOffset.y

// States
private alias 0 : SPEAR_DOWN_IDLE
private alias 1 : SPEAR_MOVEUP
private alias 2 : SPEAR_UP_IDLE
private alias 3 : SPEAR_MOVEDOWN

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


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

reserve function Spear_Setup
reserve function Spear_DebugDraw
reserve function Spear_DebugSpawn


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

private value Spear_startDebugID = 0


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

private function Spear_Setup
	object[arrayPos0].ypos = object[arrayPos0].startPos.y
	object[arrayPos0].ypos += object[arrayPos0].wrapOffset.y

	object[arrayPos0].drawOrder = 2
	object[arrayPos0].timer 	= 0
	object[arrayPos0].state 	= 0
	object[arrayPos0].priority 	= PRIORITY_BOUNDS
end function


private function Spear_DebugDraw
	DrawSprite(0)
end function


private function Spear_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= Spear_startDebugID
	temp0 <<= 2

	CreateTempObject(TypeName[Spear], temp0, object.xpos, object.ypos)

	arrayPos0 = object[tempObjectPos].entityPos
	object[arrayPos0].startPos.y = object.ypos
	CallFunction(Spear_Setup)
end function

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

event ObjectUpdate
	switch object.state
	case SPEAR_DOWN_IDLE
		temp0 = oscillation
		temp0 -= object.propertyValue
		temp0 &= 0x7F
		if temp0 == 0
			object.priority = PRIORITY_ACTIVE
			object.timer 	= 8
			object.state 	= SPEAR_MOVEUP
		end if
		break

	case SPEAR_MOVEUP
		object.ypos -= 0x40000

		object.timer--
		if object.timer == 0
			object.timer 	= 3
			object.state 	= SPEAR_UP_IDLE
		end if
		break

	case SPEAR_UP_IDLE
		object.timer--
		if object.timer == 0
			object.timer 	= 8
			object.state 	= SPEAR_MOVEDOWN
		end if
		break

	case SPEAR_MOVEDOWN
		object.ypos += 0x40000

		object.timer--
		if object.timer == 0
			object.state = SPEAR_DOWN_IDLE
		end if
		break
		
	end switch

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -4, -16, 4, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			CallFunction(Player_Hit)
		end if
	next

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


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-4, -16, 8, 32, 34, 102)

	foreach (TypeName[Spear], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		CallFunction(Spear_Setup)
	next

	temp0 = 0
	Spear_startDebugID = DebugMode_ObjCount
	while temp0 < 16
		SetTableValue(TypeName[Spear], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(Spear_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(Spear_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-4, -16, 8, 32, 34, 102)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "intervalOffset")
end event
