// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Chop Chop 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.bubbleTimer
private alias object.value2 : object.startPos.x
private alias object.value3 : object.startPos.y

// States
private alias 0 : CHOPCHOP_SWIMMING
private alias 1 : CHOPCHOP_ATTACKDELAY
private alias 2 : CHOPCHOP_ATTACK

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

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

// Air Bubble Aliases
private alias object.value1 : object.originPos.x
private alias object.value2 : object.targetPlayer


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

reserve function ChopChop_Setup
reserve function ChopChop_DebugDraw
reserve function ChopChop_DebugSpawn


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

private function ChopChop_Setup
	object[arrayPos0].direction = object[arrayPos0].propertyValue
	if object[arrayPos0].direction == FLIP_NONE
		object[arrayPos0].xvel = -0x4000
	else
		object[arrayPos0].xvel = 0x4000
	end if

	object[arrayPos0].yvel = 0
	object[arrayPos0].timer = 512
	object[arrayPos0].bubbleTimer = 0
	object[arrayPos0].frame = 0
	object[arrayPos0].animation = 0
	object[arrayPos0].animationTimer = 0
	object[arrayPos0].priority = PRIORITY_BOUNDS
end function


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


private function ChopChop_DebugSpawn
	CreateTempObject(TypeName[Chop Chop], object.direction, object.xpos, object.ypos)
	object[tempObjectPos].startPos.x = object.xpos
	object[tempObjectPos].startPos.y = object.ypos
	arrayPos0 = object[tempObjectPos].entityPos
	CallFunction(ChopChop_Setup)
end function


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

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

		object.timer--
		if object.timer == 0
			object.direction ^= FLIP_X
			FlipSign(object.xvel)
			object.timer = 512
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.state == CHOPCHOP_SWIMMING
				if object.direction == FLIP_NONE
					BoxCollisionTest(C_TOUCH, object.entityPos, -160, -32, 16, 32, currentPlayer, -1, -1, 1, 1)
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, 16, -32, 160, 32, currentPlayer, -1, -1, 1, 1)
				end if

				if checkResult == true
					object.state = CHOPCHOP_ATTACKDELAY
					object.animation = 1
					object.timer = 16
					if player[currentPlayer].xpos < object.xpos
						object.xvel = -0x20000
					else
						object.xvel = 0x20000
					end if

					temp0 = object.ypos
					temp0 -= player[currentPlayer].ypos
					Abs(temp0)
					if temp0 > 0x100000
						if player[currentPlayer].ypos < object.ypos
							object.yvel = -0x8000
						else
							object.yvel = 0x8000
						end if
					end if
				end if
			end if
		next

		object.bubbleTimer++
		if object.bubbleTimer >= 240
			object.bubbleTimer = 0
		end if
		break

	case CHOPCHOP_ATTACKDELAY
		object.timer--
		if object.timer < 0
			object.state = CHOPCHOP_ATTACK
		end if
		break

	case CHOPCHOP_ATTACK
		object.xpos += object.xvel
		object.ypos += object.yvel

		object.bubbleTimer++
		if object.bubbleTimer >= 20
			object.bubbleTimer = 0
		end if
		break
		
	end switch

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

	if object.animation == 1
		object.frame = object.animationTimer
		object.frame >>= 3
		object.animationTimer++
		object.animationTimer &= 15
	end if

	if object.bubbleTimer == 0
		CreateTempObject(TypeName[Air Bubble], 2, object.xpos, object.ypos)
		object[tempObjectPos].drawOrder = 4
		object[tempObjectPos].yvel = -0x8800
		object[tempObjectPos].ypos += 0x60000
		if object.direction == FLIP_NONE
			object[tempObjectPos].xpos += 0x100000
		else
			object[tempObjectPos].xpos -= 0x100000
			object[tempObjectPos].angle = 256
		end if

		object[tempObjectPos].targetPlayer = 0xFFFF
		object[tempObjectPos].originPos.x = object[tempObjectPos].xpos
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -16, -12, 16, 12, 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("Zone03")
	if checkResult == true
		LoadSpriteSheet("ARZ/Objects.gif")
		SpriteFrame(-15, -12, 31, 24, 18, 78)
		SpriteFrame(-15, -12, 31, 24, 18, 103)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-15, -12, 31, 24, 209, 313)
		SpriteFrame(-15, -12, 31, 24, 242, 313)
	end if

	foreach (TypeName[Chop Chop], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		CallFunction(ChopChop_Setup)
	next

	SetTableValue(TypeName[Chop Chop], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(ChopChop_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(ChopChop_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
			checkResult &= 1
			break
			
		case 0 // direction
			GetBit(checkResult, object.propertyValue, 0)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		case 0 // direction
			CheckNotEqual(editor.variableValue, 0)
			SetBit(object.propertyValue, 0, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 0)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("ARZ/Objects.gif")
		SpriteFrame(-15, -12, 31, 24, 18, 78)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-15, -12, 31, 24, 209, 313)
	end if
	
	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
end event
