// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Ball Hog Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

// value0 is unused...
private alias object.value1 : object.timer
private alias object.value2 : object.originY

// Ball Hog Bomb Aliases
private alias object.value0 : object.time
private alias object.value1 : object.bouncePos

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


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

reserve function BallHog_DebugDraw
reserve function BallHog_DebugSpawn


// ========================
// Tables
// ========================

public table BallHog_frameTable
	0, 2, 3, 2, 0, 2, 3, 2, 0, 2, 3, 2, 0, 1, 0
end table

public table BallHog_frameDelayTable
	20, 20, 10, 10, 20, 20, 10, 10, 20, 20, 10, 10, 20, 10
end table


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

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


private function BallHog_DebugSpawn
	CreateTempObject(TypeName[Ball Hog], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
	object[tempObjectPos].propertyValue = 1
end function


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

event ObjectUpdate
	
	// Check interaction with Players
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -12, -18, 12, 18, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next
	
	if object.frame == 3
		object.ypos += object.yvel
		object.yvel += 0x95C0
		
		if object.ypos < object.originY
			object.animationTimer = 2
		end if
	end if
	
	// Animate the Object
	object.animationTimer--
	if object.animationTimer <= 0
		GetTableValue(object.animationTimer, object.timer, BallHog_frameDelayTable)
		GetTableValue(object.frame, object.timer, BallHog_frameTable)
		object.timer++
		if object.timer >= 14
			object.timer = 0
		end if
		
		if object.frame == 3
			object.yvel = -0x29999
			object.ypos += object.yvel
			object.yvel += 0x95C0
		end if
		
		if object.frame == 1
			// Create a Bomb
			
			CreateTempObject(TypeName[Ball Hog Bomb], 0, object.xpos, object.ypos)
			if object.direction == FLIP_NONE
				object[tempObjectPos].xpos -= 0x40000
				object[tempObjectPos].xvel = -0x10000
			else
				object[tempObjectPos].xpos += 0x40000
				object[tempObjectPos].xvel = 0x10000
			end if
			
			object[tempObjectPos].ypos += 0xC0000
			
			// Make the Bomb bounce 13 pixels below the Ball Hog
			object[tempObjectPos].bouncePos = object.ypos
			object[tempObjectPos].bouncePos += 0xD0000
			
			object[tempObjectPos].time = object.propertyValue
			object[tempObjectPos].time *= 60
		end if
	end if
end event


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


event ObjectStartup
	CheckCurrentStageFolder("Zone06")
	if checkResult == true
		LoadSpriteSheet("SBZ/Objects.gif")
		SpriteFrame(-11, -17, 22, 37, 1, 170)
		SpriteFrame(-11, -17, 22, 37, 24, 170)
		SpriteFrame(-12, -12, 24, 32, 47, 175)
		SpriteFrame(-11, -20, 22, 40, 72, 167)
	end if
	
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-11, -17, 22, 37, 76, 292)
		SpriteFrame(-11, -17, 22, 37, 99, 292)
		SpriteFrame(-12, -12, 24, 32, 122, 297)
		SpriteFrame(-11, -20, 22, 40, 147, 289)
	end if
	
	foreach (TypeName[Ball Hog], arrayPos0, ALL_ENTITIES)
		temp0 = object[arrayPos0].propertyValue
		temp0 &= 0x80
		if temp0 != 0
			object[arrayPos0].direction = FLIP_X
			object[arrayPos0].propertyValue &= 0x7F
		end if
		
		object[arrayPos0].originY = object[arrayPos0].ypos
	next
	
	SetTableValue(TypeName[Ball Hog], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(BallHog_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(BallHog_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 // bombTime
			checkResult = object.propertyValue
			checkResult &= 0x7F
			break
			
		case 1 // direction
			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 // bombTime
			object.propertyValue &= 0x80
			object.propertyValue |= editor.variableValue
			break
			
		case 1 // direction
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 7, checkResult)
			break
			
		end switch
	end if
end event


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


event RSDKLoad
	CheckCurrentStageFolder("Zone06")
	if checkResult == true
		LoadSpriteSheet("SBZ/Objects.gif")
		SpriteFrame(-11, -17, 22, 37, 1, 170)
	end if
	
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-11, -17, 22, 37, 76, 292)
	end if
	
	AddEditorVariable("bombTime")
	
	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
end event
