// ----------------------------------
// RSDK Project: Sonic 2
// 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


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

private function VBlock_DebugDraw
	DrawSprite(0)
end function


private function VBlock_DebugSpawn
	CreateTempObject(TypeName[V Block], 0, object.xpos, object.ypos)
	object[tempObjectPos].drawPos.y 	= object.ypos
	object[tempObjectPos].propertyValue = object.direction
	if object[tempObjectPos].propertyValue == 0
		object[tempObjectPos].ypos -= 0x600000
	else
		object[tempObjectPos].ypos += 0x600000
	end if
end function


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

event ObjectUpdate
	temp6 = object.ypos
	if object.ypos >= object.drawPos.y
		object.yvel -= 0x400
	else
		object.yvel += 0x400
	end if
	object.ypos += object.yvel

	temp0 = object.ypos
	temp6 &= 0xFFFF0000
	object.collisionOffset.y = object.ypos
	object.collisionOffset.y &= 0xFFFF0000
	object.collisionOffset.y -= temp6

	object.ypos = temp6
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_SOLID2, 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.yvel > 0
					CallFunction(Player_Kill)
				end if
			end if
			break
			
		end switch
	next

	object.ypos = temp0
end event


event ObjectDraw
	DrawSprite(0)
end event


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

	foreach (TypeName[V Block], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawPos.y = object[arrayPos0].ypos
		if object[arrayPos0].propertyValue == 0
			object[arrayPos0].ypos -= 0x600000
		else
			object[arrayPos0].ypos += 0x600000
		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
			break
			
		case 0 // startDir
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // startDir
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("CNZ/Objects.gif")
	SpriteFrame(-32, -32, 64, 64, 82, 34)
	
	// Some VBlocks have their dir FLIP_Y attr set in the scene, that's irrelevant though
	AddEditorVariable("startDir")
	SetActiveVariable("startDir")
	AddEnumVariable("Down", 0)
	AddEnumVariable("Up", 1)
end event
