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

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

private alias object.value1 : object.startPos.y

private alias 0 : CHOPPER_ANI_JUMP
private alias 1 : CHOPPER_ANI_BITE
private alias 2 : CHOPPER_ANI_FALL

// 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 Chopper_DebugDraw
reserve function Chopper_DebugSpawn


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

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


private function Chopper_DebugSpawn
	CreateTempObject(TypeName[Chopper], 0, object.xpos, object.ypos)
	
	// Choppers don't normally have the capability to be turned around in the scene (aside from the actual dir attr), but they made it so that they can be turned around in Debug Mode
	// Neat
	GetBit(temp0, object.direction, 0)
	object[tempObjectPos].direction = temp0
	
	object[tempObjectPos].startPos.y = object.ypos
	object[tempObjectPos].yvel = -0x70000
end function


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

event ObjectUpdate
	object.ypos += object.yvel
	object.yvel += 0x1800

	if object.yvel < -0x38000
		object.animation = CHOPPER_ANI_JUMP
	else
		if object.yvel > 0x38000
			object.animation = CHOPPER_ANI_FALL
		else
			object.animation = CHOPPER_ANI_BITE
		end if
	end if

	if object.ypos > object.startPos.y
		object.ypos = object.startPos.y
		object.yvel = -0x70000
	end if

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

	switch object.animation
	case CHOPPER_ANI_JUMP
		object.frame = object.animationTimer
		object.frame >>= 3
		object.animationTimer++
		object.animationTimer &= 15
		break

	case CHOPPER_ANI_BITE
		object.frame = object.animationTimer
		object.frame >>= 3
		object.animationTimer += 2
		object.animationTimer &= 15
		break

	case CHOPPER_ANI_FALL
		object.frame = 0
		break
		
	end switch
end event


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


event ObjectStartup
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("GHZ/Objects.gif")
		SpriteFrame(-14, -15, 30, 32, 98, 94)
		SpriteFrame(-14, -15, 30, 32, 129, 94)
	end if

	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-14, -15, 30, 32, 106, 81)
		SpriteFrame(-14, -15, 30, 32, 137, 81)
	end if

	foreach (TypeName[Chopper], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		object[arrayPos0].yvel = -0x70000
	next

	SetTableValue(TypeName[Chopper], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Chopper_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Chopper_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-14, -15, 30, 32, 106, 81)
	else
		LoadSpriteSheet("GHZ/Objects.gif")
		SpriteFrame(-14, -15, 30, 32, 98, 94)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
