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

//-------Aliases-------//
private alias 55 : TYPE_BATBOT

// Function declarations
reserve function Batbot_DebugDraw
reserve function Batbot_DebugSpawn

// Static Values

// Tables
public table Batbot_table48
	0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1
end table


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


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


event ObjectMain
	switch object.state
	case 0
		if object.priority != PRIORITY_XBOUNDS_DESTROY
			object.priority = PRIORITY_ACTIVE
		end if
		object.value8 = 1
		object.value1 = object.ypos
		object.prevAnimation = -1
		object.animation = 0
		object.state = 1
		break
	case 1
		object.angle += 8
		object.angle &= 511
		Sin(object.ypos, object.angle)
		object.ypos <<= 9
		object.ypos += object.value1
		object.animation = 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -96, -64, 96, 128, currentPlayer, 0, 0, 0, 0)
			if checkResult == 1
				object.animation = 1
			end if
			BoxCollisionTest(C_TOUCH, object.entityPos, -80, -64, 80, 96, currentPlayer, 0, 0, 0, 0)
			if checkResult == 1
				object.value7 = currentPlayer
				object.state = 2
			end if
		next
		break
	case 2
		if object.angle != 0
			object.angle += 8
			object.angle &= 511
			Sin(object.ypos, object.angle)
			object.ypos <<= 9
			object.ypos += object.value1
		end if
		object.value0++
		if object.value0 == 20
			object.value0 = 0
			object.value2 = object.xpos
			object.value3 = object.ypos
			currentPlayer = object.value7
			if object[currentPlayer].xpos > object.xpos
				object.value6 = 256
				object.value2 += 0x500000
				object.value11 = 1
				object.state = 4
			else
				object.value6 = 0
				object.value2 -= 0x500000
				object.value11 = 0
				object.state = 3
			end if
			object.value8 = 0
			object.animation = 2
		end if
		break
	case 3
		object.value6 += 4
		Cos(object.xpos, object.value6)
		object.xpos *= 0x2800
		object.xpos += object.value2
		Sin(object.ypos, object.value6)
		object.ypos *= 0x2800
		object.ypos += object.value3
		if object.value6 == 256
			object.value8 = 1
			object.animation = 0
			object.state = 1
		end if
		break
	case 4
		object.value6 -= 4
		Cos(object.xpos, object.value6)
		object.xpos *= 0x2800
		object.xpos += object.value2
		Sin(object.ypos, object.value6)
		object.ypos *= 0x2800
		object.ypos += object.value3
		if object.value6 == 0
			object.value8 = 1
			object.animation = 0
			object.state = 1
		end if
		break
	end switch
	CallFunction(PlayerObject_EnemyPop)
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -16, -12, 16, 12, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
		if checkResult == 1
			CallFunction(PlayerObject_BadnikBreak)
		end if
	next
	switch object.animation
	case 0
		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 1
		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_table48)
		object.direction ^= object.value11
		break
	case 2
		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.value11
		break
	end switch
	if object.value8 == 1
		object.value9 = object.value10
		object.value9 >>= 1
		object.value10++
		object.value10 &= 3
	end if
	if object.outOfBounds == 1
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.value4
		object.ypos = object.value5
		if object.outOfBounds == 1
			object.prevAnimation = -1
			object.animation = 0
			object.value8 = 1
			object.value0 = 0
			object.value10 = 0
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_ACTIVE_BOUNDS
			end if
			object.state = 1
		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.value8 == 1
		DrawSprite(object.value9)
	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].value4 = object[arrayPos0].xpos
		object[arrayPos0].value5 = 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

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
