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

//-------Aliases-------//
private alias 70 : TYPE_TURRET

// Function declarations
reserve function Turret_DebugDraw
reserve function Turret_DebugSpawn

// Static Values

// Tables

function Turret_DebugDraw
	DrawSprite(0)
end function


function Turret_DebugSpawn
	CreateTempObject(TypeName[Turret], 0, object.xpos, object.ypos)
end function


event ObjectMain
	if object.state == 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].ypos >= object.ypos
				temp0 = object[currentPlayer].xpos
				temp0 -= object.xpos
				Abs(temp0)
				if temp0 < 0x600000
					temp0 = object[currentPlayer].ypos
					temp0 -= object.ypos
					if temp0 < 0x900000
						object.value0 = 2
						object.state++
					end if
				end if
			end if
		next
	else
		temp1 = 0x7FFFFFFF
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = object[currentPlayer].xpos
			temp0 -= object.xpos
			Abs(temp0)
			if temp0 < temp1
				if temp0 >= 0x200000
					if object[currentPlayer].xpos < object.xpos
						object.frame = 1
					else
						object.frame = 2
					end if
				else
					object.frame = 0
				end if
			end if
		next
		object.value0--
		if object.value0 == 0
			object.value0 = 96
			PlaySfx(SfxName[Octus Shot], 0)
			CreateTempObject(TypeName[Turret Bullet], 0, object.xpos, object.ypos)
			switch object.frame
			case 0
				object[tempObjectPos].ypos += 0x180000
				object[tempObjectPos].yvel = 0x10000
				break
			case 1
				object[tempObjectPos].xpos -= 0x110000
				object[tempObjectPos].ypos += 0x100000
				object[tempObjectPos].xvel = -0x10000
				object[tempObjectPos].yvel = 0x10000
				break
			case 2
				object[tempObjectPos].xpos += 0x110000
				object[tempObjectPos].ypos += 0x100000
				object[tempObjectPos].xvel = 0x10000
				object[tempObjectPos].yvel = 0x10000
				break
			end switch
		end if
	end if
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone11")
	if checkResult == 1
		LoadSpriteSheet("SCZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 38, 431, 88)
		SpriteFrame(-17, -16, 33, 32, 430, 127)
		SpriteFrame(-16, -16, 33, 32, 430, 160)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 38, 934, 574)
		SpriteFrame(-17, -16, 33, 32, 933, 613)
		SpriteFrame(-16, -16, 33, 32, 933, 646)
	end if
	SetTableValue(TypeName[Turret], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Turret_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Turret_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
