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

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

private alias object.value1 : object.drawPos.y
private alias object.value2 : object.collisionOffset.y

// Player Aliases
private alias object.ypos : player.ypos
private alias object.gravity : player.gravity

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

reserve function VBlock_DebugDraw
reserve function VBlock_DebugSpawn


// ========================
// Static Values
// ========================

private value VBlock_unusedValue = 0 // Prolly VBlock_startDebugID if I had to guess


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

private function VBlock_DebugDraw
	DrawSprite(0)
end function


private function VBlock_DebugSpawn
	CreateTempObject(TypeName[V Block], object.direction, object.xpos, object.ypos)
	if object[tempObjectPos].propertyValue == 1
		object[tempObjectPos].ypos -= 0x200000
	else
		object[tempObjectPos].ypos -= 0x1F0000
	end if
end function


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

event ObjectUpdate
	object.angle = SYZSetup_oscillation
	object.angle *= 0x200
	object.angle /= 356
	if object.propertyValue == 1
		object.angle += 0x100
	end if
	object.angle &= 0x1FF

	Sin(object.collisionOffset.y, object.angle)
	object.collisionOffset.y <<= 12
	object.collisionOffset.y += object.ypos
	object.collisionOffset.y &= 0xFFFF0000
	object.collisionOffset.y -= object.drawPos.y

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp0 = object.ypos
		object.ypos = object.drawPos.y

		BoxCollisionTest(C_SOLID, object.entityPos, -32, -32, 32, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		switch checkResult
		case COL_TOP
			player[currentPlayer].ypos += object.collisionOffset.y
			break

		case COL_BOTTOM
			if player[currentPlayer].gravity == GRAVITY_GROUND
				if object.angle > 384
					CallFunction(Player_Kill)
				end if

				if object.angle < 128
					CallFunction(Player_Kill)
				end if
			end if
			break
			
		end switch

		object.ypos = temp0
	next
end event


event ObjectDraw
	Sin(object.drawPos.y, object.angle)
	object.drawPos.y <<= 12
	object.drawPos.y += object.ypos
	object.drawPos.y &= 0xFFFF0000
	DrawSpriteXY(0, object.xpos, object.drawPos.y)
end event


event ObjectStartup
	LoadSpriteSheet("SYZ/Objects.gif")
	SpriteFrame(-32, -32, 64, 64, 119, 34)

	foreach (TypeName[V Block], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].ypos -= 0x200000
		else
			object[arrayPos0].ypos -= 0x1F0000
		end if
	next

	SetTableValue(TypeName[V Block], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(VBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(VBlock_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
			checkResult &= 1
			break
			
		case 0 // startDir
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		case 0 // startDir
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
	
	if editor.showGizmos == true
		editor.drawingOverlay = true
		
		// Draw this object's range of movement
		temp0 = -0x200000
		temp0 += object.ypos
		DrawLine(object.xpos, object.ypos, object.xpos, temp0, 255, 255, 255)
		
		editor.drawingOverlay = false
	end if
end event


event RSDKLoad
	LoadSpriteSheet("SYZ/Objects.gif")
	SpriteFrame(-32, -32, 64, 64, 119, 34)

	AddEditorVariable("startDir")
	SetActiveVariable("startDir")
	AddEnumVariable("Up", 0)
	AddEnumVariable("Down", 1)
end event
