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

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

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

// Player Aliases
private alias object.state : player.state
private alias object.xpos : player.xpos
private alias object.animation : player.animation

private alias object.value1  : player.timer


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

reserve function HBlock_DebugDraw
reserve function HBlock_DebugSpawn


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

private function HBlock_DebugDraw
	DrawSprite(0)
end function


private function HBlock_DebugSpawn
	CreateTempObject(TypeName[H Block], 0, object.xpos, object.ypos)
	object[tempObjectPos].drawPos.x = object.xpos
	object[tempObjectPos].propertyValue = object.direction
	if object[tempObjectPos].propertyValue == FACING_RIGHT
		object[tempObjectPos].xpos -= 0x600000
	else
		object[tempObjectPos].xpos += 0x600000
	end if
end function


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

event ObjectUpdate
	temp6 = object.xpos
	if object.xpos >= object.drawPos.x
		object.xvel -= 0x400
	else
		object.xvel += 0x400
	end if
	object.xpos += object.xvel

	temp0 = object.xpos
	temp6 &= 0xFFFF0000
	object.collisionOffset.x = object.xpos
	object.collisionOffset.x &= 0xFFFF0000
	object.collisionOffset.x -= temp6

	object.xpos = temp6
	
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		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].xpos += object.collisionOffset.x
			break

		case COL_LEFT
		case COL_RIGHT
			if player[currentPlayer].state == Player_State_Climb
				player[currentPlayer].timer 	= 0
				player[currentPlayer].animation = ANI_GLIDING_DROP
				player[currentPlayer].state 	= Player_State_GlideDrop
			end if
			break
			
		end switch
	next

	object.xpos = temp0
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("CNZ/Objects.gif")
	SpriteFrame(-32, -32, 64, 64, 82, 34)
	foreach (TypeName[H Block], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawPos.x = object[arrayPos0].xpos
		if object[arrayPos0].propertyValue == 0
			object[arrayPos0].xpos -= 0x600000
		else
			object[arrayPos0].xpos += 0x600000
		end if
	next
	SetTableValue(TypeName[H Block], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(HBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(HBlock_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 HBlocks have their dir FLIP_X attr set in the scene, that's irrelevant though
	AddEditorVariable("startDir")
	SetActiveVariable("startDir")
	AddEnumVariable("Right", 0)
	AddEnumVariable("Left", 1)
end event
