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

//-------Aliases-------//
private alias 62 : TYPE_SPEARBLOCK

// Function declarations
reserve function SpearBlock_Function137
reserve function SpearBlock_DebugDraw
reserve function SpearBlock_DebugSpawn

// Static Values
public value SpearBlock_value41 = 0;

// Tables

function SpearBlock_Function137
	object[arrayPos0].value1 = 0
	object[arrayPos0].value2 = 0
	object[arrayPos0].value0 = 0
	object[arrayPos0].state = 0
	object[arrayPos0].priority = PRIORITY_ACTIVE_BOUNDS
end function


function SpearBlock_DebugDraw
	DrawSprite(4)
end function


function SpearBlock_DebugSpawn
	temp0 = object[0].value17
	temp0 -= SpearBlock_value41
	CreateTempObject(TypeName[Spear Block], temp0, object.xpos, object.ypos)
	arrayPos0 = object[tempObjectPos].entityPos
	CallFunction(SpearBlock_Function137)
end function


event ObjectMain
	switch object.state
	case 0
		temp0 = oscillation
		temp0 &= 127
		if temp0 == 0
			temp0 = oscillation
			temp0 >>= 7
			temp0 += object.propertyValue
			temp0 &= 3
			object.frame = temp0
			PlaySfx(SfxName[Spear], 0)
			object.priority = PRIORITY_ACTIVE
			object.value0 = 4
			object.state = 1
		end if
		break
	case 1
		switch object.frame
		case 0
			object.value2 -= 0x80000
			break
		case 1
			object.value1 += 0x80000
			break
		case 2
			object.value2 += 0x80000
			break
		case 3
			object.value1 -= 0x80000
			break
		end switch
		object.value0--
		if object.value0 == 0
			object.state = 2
		end if
		break
	case 2
		temp0 = oscillation
		temp0 &= 63
		if temp0 == 0
			PlaySfx(SfxName[Spear], 0)
			object.priority = PRIORITY_ACTIVE
			object.value0 = 4
			object.state = 3
		end if
		break
	case 3
		switch object.frame
		case 0
			object.value2 += 0x80000
			break
		case 1
			object.value1 -= 0x80000
			break
		case 2
			object.value2 -= 0x80000
			break
		case 3
			object.value1 += 0x80000
			break
		end switch
		object.value0--
		if object.value0 == 0
			object.state = 0
		end if
		break
	end switch
	temp0 = object.xpos
	temp1 = object.ypos
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 16, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		object.xpos += object.value1
		object.ypos += object.value2
		switch object.frame
		case 0
		case 2
			BoxCollisionTest(C_TOUCH, object.entityPos, -4, -16, 4, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			break
		case 1
		case 3
			BoxCollisionTest(C_TOUCH, object.entityPos, -16, -4, 16, 4, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			break
		end switch
		if checkResult == 1
			CallFunction(PlayerObject_Hit)
		end if
		object.xpos = temp0
		object.ypos = temp1
	next
	if object.outOfBounds == 1
		arrayPos0 = object.entityPos
		CallFunction(SpearBlock_Function137)
	end if
end event


event ObjectDraw
	temp0 = object.xpos
	temp0 += object.value1
	temp1 = object.ypos
	temp1 += object.value2
	DrawSpriteXY(object.frame, temp0, temp1)
	DrawSprite(4)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone09")
	if checkResult == 1
		LoadSpriteSheet("MPZ/Objects.gif")
		SpriteFrame(-4, -16, 8, 32, 34, 102)
		SpriteFrame(-16, -4, 32, 8, 34, 84)
		SpriteFrame(-4, -16, 8, 32, 43, 102)
		SpriteFrame(-16, -4, 32, 8, 34, 93)
		SpriteFrame(-16, -16, 32, 32, 52, 102)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-4, -16, 8, 32, 859, 615)
		SpriteFrame(-16, -4, 32, 8, 893, 648)
		SpriteFrame(-4, -16, 8, 32, 868, 615)
		SpriteFrame(-16, -4, 32, 8, 893, 657)
		SpriteFrame(-16, -16, 32, 32, 926, 541)
	end if
	foreach (TypeName[Spear Block], arrayPos0, ALL_ENTITIES)
		CallFunction(SpearBlock_Function137)
	next
	temp0 = 0
	SpearBlock_value41 = DebugMode_ObjCount
	while temp0 < 4
		SetTableValue(TypeName[Spear Block], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(SpearBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(SpearBlock_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

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