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

//-------Aliases-------//
private alias 57 : TYPE_FLASHER

// Function declarations
reserve function Flasher_DebugDraw
reserve function Flasher_DebugSpawn

// Static Values

// Tables
public table Flasher_table41
	256, 416, 520, 645, 768, 832, 912, 0x440, 0xF000
end table

public table Flasher_table42
	1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1
end table

public table Flasher_table43
	1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0
	1, 0, 1, 0, 1, 0, 1, 0, 1, 0
end table

public table Flasher_table44
	2, 3, 4, 3, 2, 0, 2, 0, 3, 0, 4, 0, 3, 0
end table

public table Flasher_table45
	4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1
	0, 0, 0, 0
end table


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


function Flasher_DebugSpawn
	CreateTempObject(TypeName[Flasher], 0, object.xpos, object.ypos)
end function


event ObjectMain
	switch object.state
	case 0
		object.value0++
		if object.value0 == 64
			object.value0 = 128
			object.xvel = -0x10000
			object.yvel = 0x4000
			object.speed = 512
			object.state = 1
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_ACTIVE
			end if
		end if
		CallFunction(PlayerObject_EnemyPop)
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
			if checkResult == 1
				CallFunction(PlayerObject_BadnikBreak)
			end if
		next
		break
	case 1
		if object.value0 > 0
			object.value0--
			if object.xvel < 0
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if
			GetTableValue(temp0, object.value2, Flasher_table41)
			object.value1++
			if object.value1 == temp0
				GetTableValue(object.value3, object.value4, Flasher_table42)
				if object.value3 == 1
					FlipSign(object.speed)
				end if
				object.value4++
				GetTableValue(object.value3, object.value4, Flasher_table42)
				if object.value3 == 1
					FlipSign(object.yvel)
				end if
				object.value4++
				object.value4 &= 15
				object.value2++
				if object.value2 == 10
					object.value2 = 0
					object.value1 = 0
				end if
			end if
			object.xvel += object.speed
			object.xpos += object.xvel
			object.ypos += object.yvel
		else
			object.state = 2
		end if
		CallFunction(PlayerObject_EnemyPop)
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
			if checkResult == 1
				CallFunction(PlayerObject_BadnikBreak)
			end if
		next
		break
	case 2
		GetTableValue(object.frame, object.value0, Flasher_table43)
		object.value0++
		if object.value0 == 26
			object.value0 = 0
			object.state = 3
		end if
		CallFunction(PlayerObject_EnemyPop)
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if object[currentPlayer].value37 != 4
				if checkResult == 1
					CallFunction(PlayerObject_LightningHit)
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
					if checkResult == 1
						CallFunction(PlayerObject_BadnikBreak)
					end if
				end if
			else
				if checkResult == 1
					CallFunction(PlayerObject_BadnikBreak)
				end if
			end if
		next
		break
	case 3
		GetTableValue(object.frame, object.animationTimer, Flasher_table44)
		object.animationTimer++
		if object.animationTimer == 14
			object.animationTimer = 6
		end if
		object.value0++
		if object.value0 == 128
			object.value0 = 0
			object.state = 4
		end if
		CallFunction(PlayerObject_EnemyPop)
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].value37 != 4
				BoxCollisionTest(C_TOUCH, object.entityPos, -12, -12, 12, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					CallFunction(PlayerObject_LightningHit)
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
					if checkResult == 1
						CallFunction(PlayerObject_BadnikBreak)
					end if
				end if
			else
				BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				if checkResult == 1
					CallFunction(PlayerObject_BadnikBreak)
				end if
			end if
		next
		break
	case 4
		GetTableValue(object.frame, object.value0, Flasher_table45)
		object.value0++
		if object.value0 == 20
			object.value0 = 128
			object.state = 1
		end if
		CallFunction(PlayerObject_EnemyPop)
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if object[currentPlayer].value37 != 4
				if checkResult == 1
					CallFunction(PlayerObject_LightningHit)
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
					if checkResult == 1
						CallFunction(PlayerObject_BadnikBreak)
					end if
				end if
			else
				if checkResult == 1
					CallFunction(PlayerObject_BadnikBreak)
				end if
			end if
		next
		break
	end switch
	if object.outOfBounds == 1
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.value5
		object.ypos = object.value6
		if object.outOfBounds == 1
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.value0 = 0
				object.frame = 0
				object.state = 0
				object.priority = PRIORITY_ACTIVE_BOUNDS
			end if
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if
end event


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


event ObjectStartup
	CheckCurrentStageFolder("Zone06")
	if checkResult == 1
		LoadSpriteSheet("MCZ/Objects.gif")
		SpriteFrame(-16, -8, 23, 15, 1, 1)
		SpriteFrame(-16, -8, 23, 15, 25, 1)
		SpriteFrame(-16, -8, 24, 16, 1, 17)
		SpriteFrame(-16, -12, 28, 24, 1, 34)
		SpriteFrame(-16, -16, 32, 32, 1, 59)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -8, 23, 15, 1, 1)
		SpriteFrame(-16, -8, 23, 15, 25, 1)
		SpriteFrame(-16, -8, 24, 16, 1, 17)
		SpriteFrame(-16, -12, 28, 24, 1, 34)
		SpriteFrame(-16, -16, 32, 32, 1, 59)
	end if
	foreach (TypeName[Flasher], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value5 = object[arrayPos0].xpos
		object[arrayPos0].value6 = object[arrayPos0].ypos
	next
	SetTableValue(TypeName[Flasher], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Flasher_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Flasher_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
