// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Flame Thrower 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.onTime
private alias object.value2 : object.offTime
private alias object.value3 : object.loopPoint
private alias object.value4 : object.lastFrame
private alias object.value5 : object.frameCount


// ========================
// Function Declarations
// ========================

reserve function FlameThrower_DebugDraw
reserve function FlameThrower_DebugSpawn


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

private table FlameThrower_hitboxTable
	-8,   23, 7,  40
	-8,   14, 7,  40
	-11,  2,  11, 40
	-11, -14, 11, 40
end table


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

private function FlameThrower_DebugDraw
	temp0 = object.direction
	temp0 *= 11
	object.direction <<= 1
	DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
	object.direction >>= 1
end function


private function FlameThrower_DebugSpawn
	CreateTempObject(TypeName[Flame Thrower], 0, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 5
	object[tempObjectPos].onTime = 128
	object[tempObjectPos].offTime = 128
	if object.direction == FLIP_NONE
		object[tempObjectPos].loopPoint = 0
		object[tempObjectPos].lastFrame = 11
	else
		object[tempObjectPos].loopPoint = 11
		object[tempObjectPos].lastFrame = 22
		object[tempObjectPos].frame = 11
		object[tempObjectPos].direction = FLIP_Y
	end if
	object[tempObjectPos].frameCount = object[tempObjectPos].loopPoint
	object[tempObjectPos].frameCount += 3
end function


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

event ObjectUpdate
	object.timer--
	if object.timer < 0
		if object.animation == 0
			object.timer = object.offTime
		else
			object.timer = object.onTime
		end if
		
		object.animation ^= 1
		if object.animation == 0
			PlaySfx(SfxName[Flame Thrower], false)
		end if
	end if

	if object.animation == 0
		if object.frame < object.lastFrame
			object.animationTimer++
			if object.animationTimer >= 4
				object.animationTimer = 0
				object.frame++
				if object.frame == object.lastFrame
					object.frame -= 2
				end if
			end if
		end if
	else
		if object.frame > object.loopPoint
			object.frame--
		end if
	end if

	if object.frame >= object.frameCount
		temp4 = object.frame
		temp4 -= object.frameCount
		temp4 <<= 1
		temp4 &= 252
		GetTableValue(temp0, temp4, FlameThrower_hitboxTable)
		temp4++
		GetTableValue(temp1, temp4, FlameThrower_hitboxTable)
		temp4++
		GetTableValue(temp2, temp4, FlameThrower_hitboxTable)
		temp4++
		GetTableValue(temp3, temp4, FlameThrower_hitboxTable)

		// See if the object is Y flipped or not
		GetBit(temp4, object.direction, 1)
		if temp4 == true
			FlipSign(temp1)
			FlipSign(temp3)
			temp4 = temp1
			temp1 = temp3
			temp3 = temp4
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, temp0, temp1, temp2, temp3, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_FireHit)
			end if
		next
	end if
end event


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


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-5, 40, 16, 16, 77, 460)
	SpriteFrame(-5, 32, 16, 24, 94, 452)
	SpriteFrame(-5, 32, 16, 24, 111, 452)
	SpriteFrame(-8, 17, 19, 39, 128, 437)
	SpriteFrame(-7, 17, 18, 39, 148, 437)
	SpriteFrame(-8, 9, 19, 47, 167, 429)
	SpriteFrame(-7, 9, 18, 47, 187, 429)
	SpriteFrame(-11, -6, 22, 62, 207, 414)
	SpriteFrame(-11, -6, 22, 62, 230, 414)
	SpriteFrame(-11, -22, 22, 78, 253, 398)
	SpriteFrame(-11, -23, 22, 79, 276, 397)
	SpriteFrame(-7, 40, 16, 16, 299, 460)
	SpriteFrame(-7, 32, 16, 24, 316, 452)
	SpriteFrame(-7, 32, 16, 24, 333, 452)
	SpriteFrame(-8, 17, 17, 39, 350, 437)
	SpriteFrame(-7, 17, 16, 39, 368, 437)
	SpriteFrame(-8, 9, 17, 47, 385, 429)
	SpriteFrame(-7, 9, 16, 47, 403, 429)
	SpriteFrame(-11, -6, 22, 62, 420, 414)
	SpriteFrame(-11, -6, 22, 62, 443, 414)
	SpriteFrame(-11, -22, 22, 78, 466, 398)
	SpriteFrame(-11, -23, 22, 79, 489, 397)

	foreach (TypeName[Flame Thrower], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 5
		object[arrayPos0].onTime = object[arrayPos0].propertyValue
		object[arrayPos0].onTime &= 0xF0
		object[arrayPos0].onTime <<= 1
		object[arrayPos0].offTime = object[arrayPos0].propertyValue
		object[arrayPos0].offTime &= 0x0F
		object[arrayPos0].offTime <<= 5
		GetBit(temp0, object[arrayPos0].direction, 1)
		if temp0 == FLIP_NONE
			object[arrayPos0].loopPoint = 0
			object[arrayPos0].lastFrame = 11
		else
			object[arrayPos0].loopPoint = 11
			object[arrayPos0].lastFrame = 22
			object[arrayPos0].frame = 11
		end if
		object[arrayPos0].frameCount = object[arrayPos0].loopPoint
		object[arrayPos0].frameCount += 3
	next

	SetTableValue(TypeName[Flame Thrower], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(FlameThrower_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(FlameThrower_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // onTime
			checkResult = object.propertyValue
			checkResult &= 0xF0
			checkResult >>= 4
			break
			
		case 1 // offTime
			checkResult = object.propertyValue
			checkResult &= 0x0F
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // onTime
			temp0 = editor.variableValue
			temp0 &= 0xF
			temp0 <<= 4

			object.propertyValue &= 0x0F
			object.propertyValue |= temp0
			break
			
		case 1 // offTime
			temp0 = editor.variableValue
			temp0 &= 0xF

			object.propertyValue &= 0xF0
			object.propertyValue |= temp0
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(temp0, object.direction, 1)
	if temp0 == 0
		object.frame = 0
	else
		object.frame = 11
	end if

	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-5, 40, 16, 16, 77, 460)
	SpriteFrame(-5, 32, 16, 24, 94, 452)
	SpriteFrame(-5, 32, 16, 24, 111, 452)
	SpriteFrame(-8, 17, 19, 39, 128, 437)
	SpriteFrame(-7, 17, 18, 39, 148, 437)
	SpriteFrame(-8, 9, 19, 47, 167, 429)
	SpriteFrame(-7, 9, 18, 47, 187, 429)
	SpriteFrame(-11, -6, 22, 62, 207, 414)
	SpriteFrame(-11, -6, 22, 62, 230, 414)
	SpriteFrame(-11, -22, 22, 78, 253, 398)
	SpriteFrame(-11, -23, 22, 79, 276, 397)
	SpriteFrame(-7, 40, 16, 16, 299, 460)
	SpriteFrame(-7, 32, 16, 24, 316, 452)
	SpriteFrame(-7, 32, 16, 24, 333, 452)
	SpriteFrame(-8, 17, 17, 39, 350, 437)
	SpriteFrame(-7, 17, 16, 39, 368, 437)
	SpriteFrame(-8, 9, 17, 47, 385, 429)
	SpriteFrame(-7, 9, 16, 47, 403, 429)
	SpriteFrame(-11, -6, 22, 62, 420, 414)
	SpriteFrame(-11, -6, 22, 62, 443, 414)
	SpriteFrame(-11, -22, 22, 78, 466, 398)
	SpriteFrame(-11, -23, 22, 79, 489, 397)

	AddEditorVariable("onTime")
	SetActiveVariable("onTime")
	AddEnumVariable("0 Frames", 0)
	AddEnumVariable("32 Frames", 1)
	AddEnumVariable("64 Frames", 2)
	AddEnumVariable("96 Frames", 3)
	AddEnumVariable("128 Frames", 4)
	AddEnumVariable("160 Frames", 5)
	AddEnumVariable("192 Frames", 6)
	AddEnumVariable("224 Frames", 7)
	AddEnumVariable("256 Frames", 8)
	AddEnumVariable("288 Frames", 9)
	AddEnumVariable("320 Frames", 10)
	AddEnumVariable("352 Frames", 11)
	AddEnumVariable("384 Frames", 12)
	AddEnumVariable("416 Frames", 13)
	AddEnumVariable("448 Frames", 14)
	AddEnumVariable("480 Frames", 15)

	AddEditorVariable("offTime")
	SetActiveVariable("offTime")
	AddEnumVariable("0 Frames", 0)
	AddEnumVariable("32 Frames", 1)
	AddEnumVariable("64 Frames", 2)
	AddEnumVariable("96 Frames", 3)
	AddEnumVariable("128 Frames", 4)
	AddEnumVariable("160 Frames", 5)
	AddEnumVariable("192 Frames", 6)
	AddEnumVariable("224 Frames", 7)
	AddEnumVariable("256 Frames", 8)
	AddEnumVariable("288 Frames", 9)
	AddEnumVariable("320 Frames", 10)
	AddEnumVariable("352 Frames", 11)
	AddEnumVariable("384 Frames", 12)
	AddEnumVariable("416 Frames", 13)
	AddEnumVariable("448 Frames", 14)
	AddEnumVariable("480 Frames", 15)
end event
