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

// ========================
// Aliases
// ========================

private alias object.value1 : object.size
private alias object.value2 : object.hasSplash


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

reserve function Waterfall_DebugDraw
reserve function Waterfall_DebugSpawn


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

private function Waterfall_DebugDraw
	DrawSprite(7)
end function


private function Waterfall_DebugSpawn
	CreateTempObject(TypeName[Waterfall], 7, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 5
	object[tempObjectPos].inkEffect = INK_ALPHA
	object[tempObjectPos].alpha = 0xA0
end function


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

event ObjectUpdate
	if object.propertyValue == 0
		object.size = stage.waterLevel
		object.size -= object.iypos
		object.size += 16
		if object.size > 256
			object.size = 256
		end if
	end if
end event


event ObjectDraw
	if object.propertyValue == 0
		EditFrame(0, -16, -16, 32, object.size, 223, 0)
	end if

	DrawSpriteFX(object.frame, FX_INK, object.xpos, object.ypos)
	if object.hasSplash == true
		temp0 = object.size
		temp0 <<= 16
		temp0 += object.ypos
		DrawSpriteXY(16, object.xpos, temp0)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 16, 223, 0)
	SpriteFrame(-16, -16, 32, 16, 223, 0)
	SpriteFrame(-16, -16, 32, 32, 223, 0)
	SpriteFrame(-16, -16, 32, 48, 223, 0)
	SpriteFrame(-16, -16, 32, 64, 223, 0)
	SpriteFrame(-16, -16, 32, 80, 223, 0)
	SpriteFrame(-16, -16, 32, 96, 223, 0)
	SpriteFrame(-16, -16, 32, 112, 223, 0)
	SpriteFrame(-16, -16, 32, 16, 223, 32)
	SpriteFrame(-16, -16, 32, 16, 223, 32)
	SpriteFrame(-16, -16, 32, 32, 223, 32)
	SpriteFrame(-16, -16, 32, 48, 223, 32)
	SpriteFrame(-16, -16, 32, 64, 223, 32)
	SpriteFrame(-16, -16, 32, 80, 223, 32)
	SpriteFrame(-16, -16, 32, 96, 223, 32)
	SpriteFrame(-16, -16, 32, 112, 223, 32)
	SpriteFrame(-24, -40, 48, 24, 174, 231) // Splash - #16

	foreach (TypeName[Waterfall], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 5
		object[arrayPos0].inkEffect = INK_ALPHA
		object[arrayPos0].alpha = 0xA0

		object[arrayPos0].size = object[arrayPos0].propertyValue
		object[arrayPos0].size &= 7
		if object[arrayPos0].size == 0
			object[arrayPos0].size++
		end if
		object[arrayPos0].size <<= 4

		temp0 = object[arrayPos0].propertyValue
		temp0 &= 7

		temp1 = object[arrayPos0].propertyValue
		temp1 >>= 3
		if temp1 < 2
			object[arrayPos0].hasSplash = false
		else
			object[arrayPos0].hasSplash = true
		end if
		temp1 &= 1
		temp1 <<= 3
		temp1 += temp0
		object[arrayPos0].frame = temp1
		object[arrayPos0].propertyValue &= 7
	next

	SetTableValue(TypeName[Waterfall], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Waterfall_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Waterfall_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 // size
			checkResult = object.propertyValue
			checkResult &= 7
			break
			
		case 1 // hasSplash
			GetBit(checkResult, object.propertyValue, 4)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // size
			temp0 = 7
			Not(temp0)
			object.propertyValue &= temp0
			editor.variableValue &= 7

			object.propertyValue |= editor.variableValue
			break
			
		case 1 // hasSplash
			CheckNotEqual(editor.variableValue, 0)
			SetBit(object.propertyValue, 4, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	temp0 = object.propertyValue
	temp0 &= 7

	object.size = temp0
	if object.size == 0
		object.size++
	end if
	object.size <<= 4

	temp1 = object.propertyValue
	temp1 >>= 3
	if temp1 < 2
		object.hasSplash = false
	else
		object.hasSplash = true
	end if
	temp1 &= 1
	temp1 <<= 3
	temp1 += temp0
	object.frame = temp1

	DrawSprite(object.frame)

	if object.hasSplash == true
		temp0 = object.size
		temp0 <<= 16
		temp0 += object.ypos
		DrawSpriteXY(16, object.xpos, temp0)
	end if
end event


event RSDKLoad
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 16, 223, 0)
	SpriteFrame(-16, -16, 32, 16, 223, 0)
	SpriteFrame(-16, -16, 32, 32, 223, 0)
	SpriteFrame(-16, -16, 32, 48, 223, 0)
	SpriteFrame(-16, -16, 32, 64, 223, 0)
	SpriteFrame(-16, -16, 32, 80, 223, 0)
	SpriteFrame(-16, -16, 32, 96, 223, 0)
	SpriteFrame(-16, -16, 32, 112, 223, 0)
	SpriteFrame(-16, -16, 32, 16, 223, 32)
	SpriteFrame(-16, -16, 32, 16, 223, 32)
	SpriteFrame(-16, -16, 32, 32, 223, 32)
	SpriteFrame(-16, -16, 32, 48, 223, 32)
	SpriteFrame(-16, -16, 32, 64, 223, 32)
	SpriteFrame(-16, -16, 32, 80, 223, 32)
	SpriteFrame(-16, -16, 32, 96, 223, 32)
	SpriteFrame(-16, -16, 32, 112, 223, 32)
	SpriteFrame(-24, -40, 48, 24, 174, 231)

	AddEditorVariable("size")
	SetActiveVariable("size")
	AddEnumVariable("Dynamic (Use WaterLevel)", 0)
	AddEnumVariable("16 pixels", 1)
	AddEnumVariable("32 pixels", 2)
	AddEnumVariable("48 pixels", 3)
	AddEnumVariable("64 pixels", 4)
	AddEnumVariable("80 pixels", 5)
	AddEnumVariable("96 pixels", 6)
	AddEnumVariable("112 pixels", 7)

	AddEditorVariable("hasSplash")
	SetActiveVariable("hasSplash")
	AddEnumVariable("false", false)
	AddEnumVariable("true", true)
end event
