// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Chemical Ball 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.interval
private alias object.value2 : object.moveType
private alias object.value3 : object.distance
private alias object.value4 : object.acceleration
private alias object.value6 : object.newYVel
private alias object.value7 : object.startPos.x
private alias object.value8 : object.startPos.y

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


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

reserve function ChemicalBall_HandleSetup
reserve function ChemicalBall_DebugDraw
reserve function ChemicalBall_DebugSpawn
reserve function ChemicalBall_TryPlayChemicalSfx


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

private value ChemicalBall_startDebugID = 0


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

private function ChemicalBall_HandleSetup
	object[arrayPos0].xpos = object[arrayPos0].startPos.x
	object[arrayPos0].ypos = object[arrayPos0].startPos.y
	object[arrayPos0].timer = object[arrayPos0].interval

	if object[arrayPos0].direction == FLIP_NONE
		object[arrayPos0].distance = 0x600000
		object[arrayPos0].acceleration = 0xB00
	else
		object[arrayPos0].distance = -0x600000
		object[arrayPos0].acceleration = -0xB00
	end if

	object[arrayPos0].newYVel = -0x48000
	object[arrayPos0].xvel = 0
	object[arrayPos0].yvel = -0x48000
	object[arrayPos0].state = 0

	if object[arrayPos0].priority != PRIORITY_XBOUNDS_DESTROY
		object[arrayPos0].priority = PRIORITY_BOUNDS
	end if
end function


private function ChemicalBall_DebugDraw
	DrawSprite(0)
end function


private function ChemicalBall_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= ChemicalBall_startDebugID
	CreateTempObject(TypeName[Chemical Ball], 0, object.xpos, object.ypos)

	arrayPos0 = object[tempObjectPos].entityPos
	object[arrayPos0].startPos.x = object.xpos
	object[arrayPos0].startPos.y = object.ypos
	object[arrayPos0].moveType = temp0
	object[arrayPos0].moveType >>= 3
	object[arrayPos0].direction = object.direction
	object[arrayPos0].interval = temp0
	object[arrayPos0].interval &= 7
	CallFunction(ChemicalBall_HandleSetup)
end function


private function ChemicalBall_TryPlayChemicalSfx
	temp0 = screen.xcenter
	temp0 += 8
	temp1 = screen.ycenter
	temp1 += 8
	CheckCameraProximity(object.ixpos, object.iypos, temp0, temp1)
	if checkResult == true
		PlaySfx(SfxName[Chemical Jump], false)
	end if
end function


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

event ObjectUpdate
	object.priority = PRIORITY_ACTIVE
	if object.state == 0
		object.timer--
		if object.timer < 0
			object.state++
			object.timer = 59
			CallFunction(ChemicalBall_TryPlayChemicalSfx)
		end if
	else
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x1800

		if object.moveType == 0
			object.xvel += object.acceleration
			if object.yvel == 0
				FlipSign(object.acceleration)
			end if

			if object.ypos >= object.startPos.y
				object.yvel = object.newYVel
				object.xvel = 0
				object.state--
			end if
		else
			if object.yvel == 0
				object.xpos += object.distance
			end if

			if object.yvel == 0x18000
				CallFunction(ChemicalBall_TryPlayChemicalSfx)
			end if

			if object.ypos == object.startPos.y
				object.yvel = object.newYVel
				object.xpos = object.startPos.x
				FlipSign(object.acceleration)
				CallFunction(ChemicalBall_TryPlayChemicalSfx)
			end if
		end if
	end if

	temp0 = object.distance
	temp0 >>= 1
	temp0 += object.startPos.x
	temp0 >>= 16
	temp1 = screen.xcenter
	temp1 += 192
	CheckCameraProximity(temp0, 0, temp1, -1)
	if checkResult == false
		arrayPos0 = object.entityPos
		CallFunction(ChemicalBall_HandleSetup)
	end if

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


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("CPZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 207, 125)

	foreach (TypeName[Chemical Ball], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos

		GetBit(object[arrayPos0].moveType, object[arrayPos0].propertyValue, 7)

		GetBit(object[arrayPos0].direction, object[arrayPos0].propertyValue, 6)

		object[arrayPos0].interval = object[arrayPos0].propertyValue
		object[arrayPos0].interval &= 0x3F
		CallFunction(ChemicalBall_HandleSetup)
	next

	temp0 = 0
	ChemicalBall_startDebugID = DebugMode_ObjCount
	while temp0 < 16
		SetTableValue(TypeName[Chemical Ball], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(ChemicalBall_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(ChemicalBall_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
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 // interval
			checkResult = object.propertyValue
			checkResult &= 0x3F
			break
			
		case 1 // direction
			GetBit(checkResult, object.propertyValue, 6)
			break
			
		case 2 // type
			GetBit(checkResult, object.propertyValue, 7)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // interval
			object.propertyValue = editor.variableValue
			object.propertyValue &= 0x3F
			break
			
		case 1 // direction
			CheckNotEqual(editor.variableValue, 0)
			SetBit(object.propertyValue, 6, checkResult)
			break
			
		case 2 // type
			CheckNotEqual(editor.variableValue, 0)
			SetBit(object.propertyValue, 7, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("CPZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 207, 125)

	AddEditorVariable("interval")
	SetActiveVariable("interval")

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("No Flip", 0)
	AddEnumVariable("Flip X", 1)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Arc", 0)
	AddEnumVariable("Vertical", 1)
end event
