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

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

private alias object.value0 : object.timeAttackOnly

private alias object.value0 : object.width
private alias object.value1 : object.height

// Player Aliases
private alias object.type : player.type
private alias object.state : player.state
private alias object.gravity : player.gravity
private alias object.animation : player.animation

// Modes
private alias 2 : MODE_TIMEATTACK


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

event ObjectUpdate
	temp0 = object.width
	FlipSign(temp0)
	temp1 = object.height
	FlipSign(temp1)
	
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if player[currentPlayer].state != Player_State_LedgePullUp
			if player[currentPlayer].animation != ANI_FANROTATE
				BoxCollisionTest(C_SOLID, object.entityPos, temp0, temp1, object.width, object.height, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				switch checkResult
				case COL_NONE
					temp0 += 2
					temp1 += 2
					temp2 = object.width
					temp3 = object.height
					temp2 -= 2
					temp3 -= 2
					BoxCollisionTest(C_TOUCH, object.entityPos, temp0, temp1, temp2, temp3, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
					if checkResult == true
						player[currentPlayer].gravity = GRAVITY_GROUND
					end if
					break

				case COL_BOTTOM
					if player[currentPlayer].gravity == GRAVITY_GROUND
						CallFunction(Player_Kill)
					end if
					break
				end switch
			end if
		end if
	next
end event


event ObjectDraw
	if player[0].type == TypeName[Debug Mode]
		temp0 = object.xpos
		temp1 = object.ypos

		temp2 = object.propertyValue
		temp4 = temp2
		temp4 &= 0xF0
		temp4 <<= 15
		temp2 >>= 4
		temp2++
		temp6 = temp2

		temp3 = object.propertyValue
		temp3 &= 0x0F
		temp5 = temp3
		temp5 <<= 19
		temp3++

		temp0 -= temp4
		temp4 = temp0
		temp1 -= temp5
		while temp3 > 0
			while temp2 > 0
				DrawSpriteXY(0, temp0, temp1)
				temp0 += 0x100000
				temp2--
			loop
			temp2 = temp6
			temp0 = temp4
			temp1 += 0x100000
			temp3--
		loop
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -7, 16, 14, 1, 176)
	
	foreach (TypeName[WFZ Inv Block], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].timeAttackOnly == true
			if options.gameMode != MODE_TIMEATTACK
				object[arrayPos0].type = TypeName[Blank Object]
			end if
		end if
		
		// Do note that these don't draw anything normally, they only display in Debug Mode
		object[arrayPos0].drawOrder = 5
		
		object[arrayPos0].width = object[arrayPos0].propertyValue
		object[arrayPos0].width &= 0xF0
		object[arrayPos0].width += 16
		object[arrayPos0].width >>= 1

		object[arrayPos0].height = object[arrayPos0].propertyValue
		object[arrayPos0].height &= 0xF
		object[arrayPos0].height++
		object[arrayPos0].height <<= 3
	next
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 // width
			checkResult = object.propertyValue
			checkResult >>= 4
			checkResult &= 0xF
			break
			
		case 1 // height
			checkResult = object.propertyValue
			checkResult &= 0xF
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // width
			temp0 = editor.variableValue
			temp0 &= 0xF
			temp0 <<= 4
			
			object.propertyValue &= 0x0F
			object.propertyValue |= temp0
			break
			
		case 1 // height
			temp0 = editor.variableValue
			temp0 &= 0xF

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


event RSDKDraw
	temp0 = object.xpos
	temp1 = object.ypos

	temp2 = object.propertyValue
	temp4 = temp2
	temp4 &= 0xF0
	temp4 <<= 15
	temp2 >>= 4
	temp2++
	temp6 = temp2

	temp3 = object.propertyValue
	temp3 &= 0x0F
	temp5 = temp3
	temp5 <<= 19
	temp3++
		
	temp0 -= temp4
	temp4 = temp0
	temp1 -= temp5
	while temp3 > 0
		while temp2 > 0
			DrawSpriteXY(0, temp0, temp1)
			temp0 += 0x100000
			temp2--
		loop
		temp2 = temp6
		temp0 = temp4
		temp1 += 0x100000
		temp3--
	loop
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -7, 16, 14, 1, 176)

	AddEditorVariable("width")
	AddEditorVariable("height")
	
	SetVariableAlias(ALIAS_VAR_VAL0, "timeAttackOnly")
end event
