// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Batbot 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.originPos.y
private alias object.value2  : object.landPos.x
private alias object.value3  : object.landPos.y
private alias object.value4  : object.startPos.x
private alias object.value5  : object.startPos.y
private alias object.value6  : object.swoopAngle
private alias object.value7  : object.targetPlayer
private alias object.value8  : object.showJet
private alias object.value9  : object.jetFrame
private alias object.value10 : object.jetAnimTimer
private alias object.value11 : object.swoopDir

// States
private alias 0 : BATBOT_SETUP
private alias 1 : BATBOT_IDLE
private alias 2 : BATBOT_ATTACK
private alias 3 : BATBOT_SWOOP_LEFT
private alias 4 : BATBOT_SWOOP_RIGHT

// Animations
private alias 0 : BATBOT_ANI_FLAP
private alias 1 : BATBOT_ANI_SPIN
private alias 2 : BATBOT_ANI_SWOOP

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

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 Batbot_DebugDraw
reserve function Batbot_DebugSpawn


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

private table Batbot_directionTable
	0 // FLIP_NONE
	0 // FLIP_NONE
	0 // FLIP_NONE
	0 // FLIP_NONE
	0 // FLIP_NONE
	0 // FLIP_NONE
	0 // FLIP_NONE
	0 // FLIP_NONE
	0 // FLIP_NONE
	1 // FLIP_X
	1 // FLIP_X
	1 // FLIP_X
end table


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

private function Batbot_DebugDraw
	DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
	DrawSprite(0)
end function


private function Batbot_DebugSpawn
	CreateTempObject(TypeName[Batbot], 0, object.xpos, object.ypos)
end function


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

event ObjectUpdate
	switch object.state
	case BATBOT_SETUP
		if object.priority != PRIORITY_XBOUNDS_DESTROY
			object.priority = PRIORITY_ACTIVE
		end if
		object.showJet = true
		object.originPos.y = object.ypos
		object.prevAnimation = -1
		object.animation = BATBOT_ANI_FLAP
		object.state = BATBOT_IDLE
		break

	case BATBOT_IDLE
		object.angle += 8
		object.angle &= 0x1FF
		Sin(object.ypos, object.angle)
		object.ypos <<= 9
		object.ypos += object.originPos.y

		object.animation = BATBOT_ANI_FLAP
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -96, -64, 96, 128, currentPlayer, 0, 0, 0, 0)
			if checkResult == true
				object.animation = BATBOT_ANI_SPIN
			end if

			BoxCollisionTest(C_TOUCH, object.entityPos, -80, -64, 80, 96, currentPlayer, 0, 0, 0, 0)
			if checkResult == true
				object.targetPlayer = currentPlayer
				object.state = BATBOT_ATTACK
			end if
		next
		break

	case BATBOT_ATTACK
		if object.angle != 0
			object.angle += 8
			object.angle &= 0x1FF
			Sin(object.ypos, object.angle)
			object.ypos <<= 9
			object.ypos += object.originPos.y
		end if

		object.timer++
		if object.timer == 20
			object.timer = 0
			object.landPos.x = object.xpos
			object.landPos.y = object.ypos

			currentPlayer = object.targetPlayer
			if player[currentPlayer].xpos > object.xpos
				object.swoopAngle = 0x100
				object.landPos.x += 0x500000
				object.swoopDir = FLIP_X
				object.state = BATBOT_SWOOP_RIGHT
			else
				object.swoopAngle = 0x000
				object.landPos.x -= 0x500000
				object.swoopDir = FLIP_NONE
				object.state = BATBOT_SWOOP_LEFT
			end if

			object.showJet = false
			object.animation = BATBOT_ANI_SWOOP
		end if
		break

	case BATBOT_SWOOP_LEFT
		object.swoopAngle += 4

		Cos(object.xpos, object.swoopAngle)
		object.xpos *= 0x2800
		object.xpos += object.landPos.x

		Sin(object.ypos, object.swoopAngle)
		object.ypos *= 0x2800
		object.ypos += object.landPos.y

		if object.swoopAngle == 256
			object.showJet = true
			object.animation = BATBOT_ANI_FLAP
			object.state = BATBOT_IDLE
		end if
		break

	case BATBOT_SWOOP_RIGHT
		object.swoopAngle -= 4

		Cos(object.xpos, object.swoopAngle)
		object.xpos *= 0x2800
		object.xpos += object.landPos.x

		Sin(object.ypos, object.swoopAngle)
		object.ypos *= 0x2800
		object.ypos += object.landPos.y

		if object.swoopAngle == 0
			object.showJet = true
			object.animation = BATBOT_ANI_FLAP
			object.state = BATBOT_IDLE
		end if
		break
		
	end switch

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -16, -12, 16, 12, currentPlayer, 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 BATBOT_ANI_FLAP
		if object.animation != object.prevAnimation
			object.prevAnimation = object.animation
			object.animationTimer = 0
			object.frame = 2
		else
			object.animationTimer++
			if object.animationTimer == 8
				object.animationTimer = 0
				object.frame++
				if object.frame > 5
					object.frame = 5
				end if
			end if
		end if
		break

	case BATBOT_ANI_SPIN
		if object.animation != object.prevAnimation
			object.prevAnimation = object.animation
			object.animationTimer = 0
			object.frame = 6
		else
			object.animationTimer++
			if object.animationTimer == 4
				object.animationTimer = 0
				object.frame++
				if object.frame > 10
					object.frame = 6
				end if
			end if
		end if

		GetTableValue(object.direction, object.frame, Batbot_directionTable)
		object.direction ^= object.swoopDir
		break

	case BATBOT_ANI_SWOOP
		if object.animation != object.prevAnimation
			object.prevAnimation = object.animation
			object.animationTimer = 0
			object.frame = 11
		else
			object.animationTimer++
			if object.animationTimer == 4
				object.animationTimer = 0
				object.frame++
				if object.frame > 15
					object.frame = 11
				end if
			end if
		end if

		object.direction = object.swoopDir
		break
		
	end switch

	if object.showJet == true
		object.jetFrame = object.jetAnimTimer
		object.jetFrame >>= 1
		object.jetAnimTimer++
		object.jetAnimTimer &= 3
	end if

	if object.outOfBounds == true
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.startPos.x
		object.ypos = object.startPos.y
		if object.outOfBounds == true
			object.prevAnimation = -1
			object.animation = BATBOT_ANI_FLAP
			object.showJet = true
			object.timer = 0
			object.jetAnimTimer = 0
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_BOUNDS
			end if
			object.state = BATBOT_IDLE
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
	if object.showJet == true
		DrawSprite(object.jetFrame)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-3, 10, 6, 5, 206, 1)
	SpriteFrame(-3, 10, 6, 3, 213, 1)
	SpriteFrame(-37, -16, 74, 27, 60, 1)
	SpriteFrame(-35, -16, 70, 27, 135, 1)
	SpriteFrame(-37, -16, 74, 27, 60, 1)
	SpriteFrame(-29, -16, 58, 27, 1, 1)
	SpriteFrame(-35, -16, 70, 27, 135, 1)
	SpriteFrame(-19, -16, 38, 27, 1, 29)
	SpriteFrame(-8, -16, 16, 26, 40, 29)
	SpriteFrame(-8, -16, 16, 26, 40, 29)
	SpriteFrame(-19, -16, 38, 27, 1, 29)
	SpriteFrame(-35, -16, 70, 27, 57, 29)
	SpriteFrame(-19, -16, 38, 27, 128, 29)
	SpriteFrame(-7, -16, 14, 27, 168, 29)
	SpriteFrame(-7, -16, 14, 27, 183, 29)
	SpriteFrame(-19, -16, 38, 27, 1, 57)

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

	SetTableValue(TypeName[Batbot], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Batbot_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Batbot_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(1)
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-3, 10, 6, 5, 206, 1)
	SpriteFrame(-29, -16, 58, 27, 1, 1)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
