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

// States
private alias 0 : SLICER_MOVING
private alias 1 : SLICER_CHECKPLAYERINRANGE
private alias 2 : SLICER_THROWARM
private alias 3 : SLICER_ARMTHROWN

// 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

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function Slicer_Setup
reserve function Slicer_DebugDraw
reserve function Slicer_DebugSpawn


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

private function Slicer_Setup
	object[arrayPos0].direction = object[arrayPos0].propertyValue
	GetBit(temp0, object[arrayPos0].direction, 0)
	if temp0 == FACING_RIGHT
		object[arrayPos0].xvel = 0x4000
	else
		object[arrayPos0].xvel = -0x4000
	end if

	object[arrayPos0].frame 	= 0
	object[arrayPos0].timer 	= 0
	object[arrayPos0].state 	= SLICER_MOVING
	object[arrayPos0].priority 	= PRIORITY_BOUNDS
end function


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


private function Slicer_DebugSpawn
	CreateTempObject(TypeName[Slicer], object.direction, object.xpos, object.ypos)
	arrayPos0 = object[tempObjectPos].entityPos
	object[arrayPos0].startPos.x = object.xpos
	object[arrayPos0].startPos.y = object.ypos
	CallFunction(Slicer_Setup)
end function


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

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

		GetBit(temp0, object.direction, 1) // see if the Y flip bit is set
		if temp0 == false
			ObjectTileGrip(CSIDE_FLOOR, 0, 17, PATH_A)
		else
			ObjectTileGrip(CSIDE_ROOF, 0, -17, PATH_A)
		end if

		if checkResult == false
			object.state = SLICER_CHECKPLAYERINRANGE
			object.timer = 60
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.direction == FLIP_NONE
				BoxCollisionTest(C_TOUCH, object.entityPos, 0, -256, 128, 256, currentPlayer, -1, -1, 1, 1)
			else
				BoxCollisionTest(C_TOUCH, object.entityPos, -128, -256, 0, 256, currentPlayer, -1, -1, 1, 1)
			end if

			if checkResult == true
				object.timer = 9
				object.frame = 3
				object.state = SLICER_THROWARM
			end if
		next

		object.animationTimer++
		if object.animationTimer >= 20
			object.animationTimer = 0
			object.frame ^= 1
		end if
		break

	case SLICER_CHECKPLAYERINRANGE
		object.timer--
		if object.timer == 0
			object.direction ^= FLIP_X
			FlipSign(object.xvel)
			object.state = SLICER_MOVING
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.direction == FLIP_NONE
				BoxCollisionTest(C_TOUCH, object.entityPos, 0, -256, 128, 256, currentPlayer, -1, -1, 1, 1)
			else
				BoxCollisionTest(C_TOUCH, object.entityPos, -128, -256, 0, 256, currentPlayer, -1, -1, 1, 1)
			end if

			if checkResult == true
				object.timer = 9
				object.frame = 3
				object.state = SLICER_THROWARM
			end if
		next
		break

	case SLICER_THROWARM
		object.timer--
		if object.timer == 0
			CreateTempObject(TypeName[Slicer Arm], 0, object.xpos, object.ypos)
			object[tempObjectPos].direction = object.direction
			object[tempObjectPos].startPos.x = object.entityPos
			if object.direction == FLIP_NONE
				object[tempObjectPos].xvel = 0x20000
				object[tempObjectPos].xpos += 0x60000
			else
				object[tempObjectPos].xvel = -0x20000
				object[tempObjectPos].xpos -= 0x60000
			end if

			CreateTempObject(TypeName[Slicer Arm], 0, object.xpos, object.ypos)
			object[tempObjectPos].direction = object.direction
			object[tempObjectPos].startPos.x = object.entityPos
			if object.direction == FLIP_NONE
				object[tempObjectPos].xvel = 0x20000
				object[tempObjectPos].xpos -= 0x100000
			else
				object[tempObjectPos].xvel = -0x20000
				object[tempObjectPos].xpos += 0x100000
			end if

			object.frame = 4
			object.state = SLICER_ARMTHROWN
		end if
		break

	case SLICER_ARMTHROWN
		break
		
	end switch

	if object.outOfBounds == true
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.startPos.x
		object.ypos = object.startPos.y
		object.ypos += object.wrapOffset.y
		if object.outOfBounds == true
			arrayPos0 = object.entityPos
			CallFunction(Slicer_Setup)
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -16, -16, 16, 16, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone09")
	if checkResult == true
		LoadSpriteSheet("MPZ/Objects.gif")
		SpriteFrame(-16, -16, 47, 32, 29, 1)
		SpriteFrame(-16, -16, 47, 32, 77, 1)
		SpriteFrame(-16, -16, 47, 32, 125, 1)
		SpriteFrame(-16, -32, 47, 48, 125, 34)
		SpriteFrame(-17, -16, 31, 32, 92, 34)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -16, 47, 32, 880, 256)
		SpriteFrame(-16, -16, 47, 32, 928, 256)
		SpriteFrame(-16, -16, 47, 32, 976, 256)
		SpriteFrame(-16, -32, 47, 48, 976, 289)
		SpriteFrame(-17, -16, 31, 32, 943, 289)
	end if

	foreach (TypeName[Slicer], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		object[arrayPos0].propertyValue ^= 1
		CallFunction(Slicer_Setup)
	next

	SetTableValue(TypeName[Slicer], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Slicer_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Slicer_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
	object.direction ^= FLIP_X
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone09")
	if checkResult == true
		LoadSpriteSheet("MPZ/Objects.gif")
		SpriteFrame(-16, -16, 47, 32, 29, 1)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -16, 47, 32, 880, 256)
	end if

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