// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Spiker 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.x
private alias object.value2 : object.coneState
private alias object.value3 : object.launchTimer // Separate from value0 since the two run alongside each other
private alias object.value4 : object.backupState

private alias 0 : SPIKER_PAUSE
private alias 1 : SPIKER_WANDERING
private alias 2 : SPIKER_LAUNCHING
private alias 3 : SPIKER_CONE

private alias 0 : CONE_NONE
private alias 1 : CONE_ATTACHED
private alias 2 : CONE_LAUNCHING

// Player aliases
private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom


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

reserve function Spiker_Setup
reserve function Spiker_DebugDraw
reserve function Spiker_DebugSpawn


private function Spiker_Setup
	object[arrayPos0].direction = object[arrayPos0].propertyValue
	GetBit(temp0, object[arrayPos0].direction, 0)
	if temp0 == FACING_RIGHT
		object[arrayPos0].xvel = -0x8000
	else
		object[arrayPos0].xvel = 0x8000
	end if
	object[arrayPos0].yvel 				= 0
	object[arrayPos0].timer 			= 64
	object[arrayPos0].frame 			= 0
	object[arrayPos0].animation 		= 0
	object[arrayPos0].animationTimer 	= 0
	object[arrayPos0].coneState 		= CONE_ATTACHED
	object[arrayPos0].priority 			= PRIORITY_BOUNDS
end function


private function Spiker_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(2, FX_FLIP, object.xpos, object.ypos)
end function


private function Spiker_DebugSpawn
	CreateTempObject(TypeName[Spiker], object.direction, object.xpos, object.ypos)
	object[tempObjectPos].startPos.x = object.xpos
	arrayPos0 = object[tempObjectPos].entityPos
	CallFunction(Spiker_Setup)
end function


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

event ObjectUpdate
	switch object.state
	case SPIKER_PAUSE
		object.priority = PRIORITY_ACTIVE
		object.xpos += object.xvel

		object.timer--
		if object.timer == 0
			object.timer = 16
			object.state = SPIKER_WANDERING
		end if

		object.frame = object.animationTimer
		object.frame /= 10
		object.animationTimer++
		if object.animationTimer == 20
			object.animationTimer = 0
		end if
		break

	case SPIKER_WANDERING
		object.timer--
		if object.timer < 0
			object.timer = 64
			object.direction ^= FLIP_X
			FlipSign(object.xvel)
			object.state = SPIKER_PAUSE
		end if
		break

	case SPIKER_LAUNCHING
		object.launchTimer--
		if object.launchTimer < 0
			object.coneState = CONE_NONE
			CreateTempObject(TypeName[Spiker], 0, object.xpos, object.ypos)
			object[tempObjectPos].coneState = CONE_ATTACHED
			object[tempObjectPos].state = SPIKER_CONE
			object[tempObjectPos].direction = object.direction
			GetBit(temp0, object.direction, 1) // see if the Y flip bit is set
			if temp0 == FLIP_NONE
				object[tempObjectPos].yvel = -0x20000
			else
				object[tempObjectPos].yvel = 0x20000
			end if
			
			object.state = object.backupState
		end if
		break

	case SPIKER_CONE
		object.ypos += object.yvel
		object.direction ^= FLIP_X
		break

	end switch

	if object.outOfBounds == true
		temp0 = object.xpos
		object.xpos = object.startPos.x

		if object.outOfBounds == true
			arrayPos0 = object.entityPos
			CallFunction(Spiker_Setup)
		else
			object.xpos = temp0
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.state != SPIKER_CONE
			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -10, 8, 10, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
			if checkResult == true
				CallFunction(Player_BadnikBreak)
			end if

			if object.type != TypeName[Blank Object]
				if object.coneState == CONE_ATTACHED
					BoxCollisionTest(C_TOUCH, object.entityPos, -32, -128, 32, 128, currentPlayer, -1, -1, 1, 1)
					if checkResult == true
						object.coneState 	= CONE_LAUNCHING
						object.launchTimer 	= 8
						object.backupState 	= object.state
						object.state 		= SPIKER_LAUNCHING
					end if
				end if
			end if
		end if

		if object.coneState >= CONE_ATTACHED
			GetBit(temp0, object.direction, 1) // see if the Y flip bit is set
			if temp0 == FLIP_NONE
				BoxCollisionTest(C_TOUCH, object.entityPos, -8, -32, 8, 0, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
			else
				BoxCollisionTest(C_TOUCH, object.entityPos, -8, 0, 8, 32, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
			end if

			if checkResult == true
				CallFunction(Player_Hit)
			end if
		end if
	next
end event


event ObjectDraw
	if object.state != SPIKER_CONE
		// Actual Spiker object, draw the body frame
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
	end if

	if object.coneState >= CONE_ATTACHED
		// Spiker cone projectile
		DrawSpriteFX(2, FX_FLIP, object.xpos, object.ypos)
	end if
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone05")
	if checkResult == true
		LoadSpriteSheet("HTZ/Objects.gif")
		
		// Walking Frames
		SpriteFrame(-12, -8, 23, 24, 66, 206)
		SpriteFrame(-12, -8, 24, 24, 66, 231)
		
		// Cone Frame
		SpriteFrame(-12, -32, 24, 32, 66, 173) 
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		
		// Walking Frames
		SpriteFrame(-12, -8, 23, 24, 67, 781)  
		SpriteFrame(-12, -8, 24, 24, 67, 806)
		
		// Cone Frame
		SpriteFrame(-12, -32, 24, 32, 67, 748)
	end if

	foreach (TypeName[Spiker], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		GetBit(temp0, object[arrayPos0].propertyValue, 1)
		if temp0 == 0
			object[arrayPos0].ypos += 0x80000
		else
			object[arrayPos0].ypos -= 0x80000
		end if
		CallFunction(Spiker_Setup)
	next

	SetTableValue(TypeName[Spiker], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Spiker_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Spiker_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // direction
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // direction
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	object.direction = object.propertyValue

	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(1, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone05")
	if checkResult == true
		LoadSpriteSheet("HTZ/Objects.gif")
		SpriteFrame(-12, -8, 23, 24, 66, 206)  	// #0 - Walking - Frame 1
		SpriteFrame(-12, -32, 24, 32, 66, 173) 	// #1 - Cone
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-12, -8, 23, 24, 67, 781)  	// #0 - Walking - Frame 1
		SpriteFrame(-12, -32, 24, 32, 67, 748) 	// #1 - Cone
	end if

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left",  0)
	AddEnumVariable("Right", 1)
	AddEnumVariable("Left (Roof)",  2)
	AddEnumVariable("Right (Roof)", 3)
end event

