// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: H Push Spring Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 51 : TYPE_HPUSHSPRING

// Function declarations
reserve function HPushSpring_DebugDraw
reserve function HPushSpring_DebugSpawn

// Static Values

// Tables

function HPushSpring_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


function HPushSpring_DebugSpawn
	CreateTempObject(TypeName[H Push Spring], 0, object.xpos, object.ypos)
	object[tempObjectPos].xpos &= -0x10000
	object[tempObjectPos].ypos &= -0x10000
	object[tempObjectPos].direction = object.direction
end function


event ObjectMain
	switch object.state
	case 0
		if object.value2 == 1
			object.state = 1
		end if
		break
	case 1
		object.value1 += 0x4000
		if object.value1 > 0x120000
			object.value1 = 0x120000
		end if
		if object.value2 == 0
			object.state = 2
		end if
		break
	case 2
		object.value1 -= 0x40000
		if object.value1 <= 0
			object.value1 = 0
			object.state = 0
		end if
		if object.value2 == 1
			object.state = 1
		end if
		break
	end switch
	if object.direction == FLIP_NONE
		temp0 = -20
		temp1 = object.value1
		temp1 >>= 16
		FlipSign(temp1)
		temp1 += 22
	else
		temp0 = object.value1
		temp0 >>= 16
		temp0 -= 22
		temp1 = 20
	end if
	object.value2 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_BOX, object.entityPos, temp0, -16, temp1, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		switch checkResult
		case 2
			if object.direction == FLIP_X
				if object[currentPlayer].direction == FLIP_NONE
					object[currentPlayer].xpos += 0x20000
				end if
				if object[currentPlayer].pushing > 0
					object.value2 |= 1
				end if
				if object.state == 2
					if object.value1 > 0x10000
						object[currentPlayer].speed = -12
						object[currentPlayer].speed *= object.value1
						object[currentPlayer].speed /= 18
						object[currentPlayer].collisionMode = CMODE_FLOOR
						object[currentPlayer].pushing = 0
						object[currentPlayer].direction = object.direction
						object[currentPlayer].controlLock = 15
						if object[currentPlayer].state != PlayerObject_HandleRolling
							object[currentPlayer].state = PlayerObject_HandleGround
							object[currentPlayer].animation = ANI_RUNNING
						end if
						PlaySfx(SfxName[Spring], 0)
					end if
				end if
			end if
			break
		case 3
			if object.direction == FLIP_NONE
				if object[currentPlayer].direction == FLIP_X
					object[currentPlayer].xpos -= 0x20000
				end if
				if object[currentPlayer].pushing > 0
					object.value2 |= 1
				end if
				if object.state == 2
					if object.value1 > 0x10000
						object[currentPlayer].speed = 12
						object[currentPlayer].speed *= object.value1
						object[currentPlayer].speed /= 18
						object[currentPlayer].collisionMode = CMODE_FLOOR
						object[currentPlayer].pushing = 0
						object[currentPlayer].direction = object.direction
						object[currentPlayer].controlLock = 15
						if object[currentPlayer].state != PlayerObject_HandleRolling
							object[currentPlayer].state = PlayerObject_HandleGround
							object[currentPlayer].animation = ANI_RUNNING
						end if
						PlaySfx(SfxName[Spring], 0)
					end if
				end if
			end if
			break
		end switch
	next
end event


event ObjectDraw
	if object.value1 == 0
		DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	else
		if object.direction == FLIP_NONE
			temp0 = object.value1
			FlipSign(temp0)
			temp0 += object.xpos
			DrawSpriteFX(1, FX_FLIP, temp0, object.ypos)
			temp0 = object.value1
			temp0 *= -2
			temp0 /= 3
			temp0 += object.xpos
			DrawSpriteFX(2, FX_FLIP, temp0, object.ypos)
			temp0 = object.value1
			temp0 /= -3
			temp0 += object.xpos
			DrawSpriteFX(3, FX_FLIP, temp0, object.ypos)
			DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		else
			temp0 = object.value1
			temp0 += object.xpos
			DrawSpriteFX(1, FX_FLIP, temp0, object.ypos)
			temp0 = object.value1
			temp0 *= 2
			temp0 /= 3
			temp0 += object.xpos
			DrawSpriteFX(2, FX_FLIP, temp0, object.ypos)
			temp0 = object.value1
			temp0 /= 3
			temp0 += object.xpos
			DrawSpriteFX(3, FX_FLIP, temp0, object.ypos)
			DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		end if
	end if
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone07")
	if checkResult == 1
		LoadSpriteSheet("OOZ/Objects.gif")
		SpriteFrame(-20, -16, 40, 32, 272, 223)
		SpriteFrame(4, -16, 16, 32, 296, 223)
		SpriteFrame(-4, -16, 8, 32, 288, 223)
		SpriteFrame(-12, -16, 8, 32, 280, 223)
		SpriteFrame(-20, -16, 8, 32, 272, 223)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-20, -16, 40, 32, 756, 778)
		SpriteFrame(4, -16, 16, 32, 780, 778)
		SpriteFrame(-4, -16, 8, 32, 772, 778)
		SpriteFrame(-12, -16, 8, 32, 764, 778)
		SpriteFrame(-20, -16, 8, 32, 756, 778)
	end if
	foreach (TypeName[H Push Spring], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].direction = object[arrayPos0].propertyValue
		object[arrayPos0].direction &= FLIP_X
		object[arrayPos0].xpos &= -0x10000
		object[arrayPos0].ypos &= -0x10000
	next
	SetTableValue(TypeName[H Push Spring], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(HPushSpring_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(HPushSpring_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
