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

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

private alias object.value0 : object.timer
private alias object.value1 : object.hitboxLeft
private alias object.value3 : object.hitboxTop
private alias object.value2 : object.hitboxRight
private alias object.value4 : object.hitboxBottom
private alias object.value5 : object.originPos.x

// States
private alias 0 : HPZEGGMANMINE_SETUP
private alias 1 : HPZEGGMANMINE_SMALL_FALL
private alias 2 : HPZEGGMANMINE_SMALL_RISE
private alias 3 : HPZEGGMANMINE_SMALL_FLOAT
private alias 4 : HPZEGGMANMINE_SMALL_EXPLODE
private alias 5 : HPZEGGMANMINE_BIG_FALL
private alias 6 : HPZEGGMANMINE_BIG_FOLLOW
private alias 7 : HPZEGGMANMINE_BIG_EXPLODE
private alias 8 : HPZEGGMANMINE_BIG_EXPLODED

private alias 0 : HPZEGGMANMINE_SMALL
private alias 2 : HPZEGGMANMINE_LARGE

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


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

event ObjectUpdate
	switch object.state
	case HPZEGGMANMINE_SETUP
		if object.propertyValue == HPZEGGMANMINE_SMALL
			object.hitboxLeft = -12
			object.hitboxRight = 12
			object.hitboxTop = -12
			object.hitboxBottom = 12
		else
			object.hitboxLeft = -16
			object.hitboxRight = 16
			object.hitboxTop = -24
			object.hitboxBottom = 16
		end if
		object.state++
		break

	case HPZEGGMANMINE_SMALL_FALL
		object.ypos += object.yvel
		object.yvel += 0x1800

		temp0 = stage.waterLevel
		temp0 <<= 16
		if object.ypos > temp0
			CreateTempObject(TypeName[Water Splash], 0, object.xpos, temp0)
			PlaySfx(SfxName[Water Splash], false)
			object[tempObjectPos].drawOrder = 4
			object.yvel >>= 2
			if object.propertyValue == HPZEGGMANMINE_SMALL
				Sin(temp1, oscillation)
				temp1 >>= 5
				temp1 <<= 16
				object.originPos.x = temp1
				object.originPos.x += object.xpos
				object.state++
			else
				object.state = HPZEGGMANMINE_BIG_FALL
			end if
		end if
		break

	case HPZEGGMANMINE_SMALL_RISE
		object.ypos += object.yvel
		object.yvel -= 0x1800

		temp0 = stage.waterLevel
		temp0 <<= 16
		if object.ypos <= temp0
			object.ypos = temp0
			object.yvel = 0
			object.state++
		end if

		Sin(object.xpos, oscillation)
		object.xpos >>= 5
		object.xpos <<= 16
		FlipSign(object.xpos)
		object.xpos += object.originPos.x
		break

	case HPZEGGMANMINE_SMALL_FLOAT
		object.ypos = stage.waterLevel
		object.ypos <<= 16
		Sin(object.xpos, oscillation)
		object.xpos >>= 5
		object.xpos <<= 16
		FlipSign(object.xpos)
		object.xpos += object.originPos.x
		break

	case HPZEGGMANMINE_SMALL_EXPLODE
		object.timer++
		object.frame = object.animationTimer
		object.frame >>= 5
		object.animationTimer += object.timer
		object.animationTimer &= 63
		if object.timer == 60
			object.hitboxTop -= 16
			object.type = TypeName[Blank Object]

			CreateTempObject(TypeName[Explosion], 0, object.xpos, object.ypos)
			object[tempObjectPos].drawOrder = 4

			CreateTempObject(TypeName[Explosion], 0, object.xpos, object.ypos)
			object[tempObjectPos].ypos -= 0x180000
			object[tempObjectPos].drawOrder = 4

			CreateTempObject(TypeName[Water Splash], 0, object.xpos, object.ypos)
			object[tempObjectPos].drawOrder = 4

			PlaySfx(SfxName[Explosion], false)
		end if
		break

	case HPZEGGMANMINE_BIG_FALL
		object.ypos += object.yvel
		object.yvel -= 0x1000

		temp0 = stage.waterLevel
		temp0 <<= 16
		if object.ypos <= temp0
			object.ypos = temp0
			object.yvel = 0
			object.state++
		end if
		break

	case HPZEGGMANMINE_BIG_FOLLOW
		object.ypos = stage.waterLevel
		object.ypos <<= 16

		temp0 = player[0].xpos
		temp0 -= 0x20000
		temp1 = player[0].xpos
		temp1 += 0x20000

		if object.xpos < temp0
			object.xpos += 0x8000
			object.direction = FLIP_X
		end if

		if object.xpos > temp1
			object.xpos -= 0x8000
			object.direction = FLIP_NONE
		end if

		object.timer++
		if object.timer == 180
			object.timer = 0
			object.state++
		end if
		break

	case HPZEGGMANMINE_BIG_EXPLODE
		temp0 = player[0].xpos
		temp0 -= 0x20000
		temp1 = player[0].xpos
		temp1 += 0x20000
		if object.xpos < temp0
			object.xpos += 0x8000
			object.direction = FLIP_X
		end if

		if object.xpos > temp1
			object.xpos -= 0x8000
			object.direction = FLIP_NONE
		end if

		object.timer++
		object.frame = object.animationTimer
		object.frame >>= 5
		object.animationTimer += object.timer
		object.animationTimer &= 63
		if object.timer == 60
			object.timer = 0
			object.state++
			object.hitboxLeft = -32
			object.hitboxRight = 32

			arrayPos0 = object.entityPos
			arrayPos0--
			ResetObjectEntity(arrayPos0, TypeName[Water Geyser], true, object.xpos, object.ypos)
			object[arrayPos0].drawOrder = 4

			CreateTempObject(TypeName[Explosion], 0, object.xpos, object.ypos)
			object[tempObjectPos].drawOrder = 4

			CreateTempObject(TypeName[Water Splash], 0, object.xpos, object.ypos)
			object[tempObjectPos].drawOrder = 4

			PlaySfx(SfxName[Big Explosion], false)
		end if
		break

	case HPZEGGMANMINE_BIG_EXPLODED
		temp0 = object.timer
		temp0 %= 6
		if temp0 == 5
			temp1 = object.xpos
			temp1 -= 0x180000
			temp2 = object.xpos
			temp2 += 0x180000

			CreateTempObject(TypeName[Smoke Puff], 0, temp1, object.ypos)
			object[tempObjectPos].drawOrder = 4

			CreateTempObject(TypeName[Smoke Puff], 0, temp2, object.ypos)
			object[tempObjectPos].drawOrder = 4

			object.ypos -= 0x180000
			object.hitboxTop -= 24
			CreateTempObject(TypeName[Explosion], 0, object.xpos, object.ypos)
			object[tempObjectPos].drawOrder = 4
		end if

		object.timer++
		if object.timer == 24
			object.type = TypeName[Blank Object]
		end if
		break
		
	end switch

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, object.hitboxLeft, object.hitboxTop, object.hitboxRight, object.hitboxBottom, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			CallFunction(Player_Hit)
		end if
	next

	if object.outOfBounds == true
		object.type = TypeName[Blank Object]
	end if
end event


event ObjectDraw
	if object.state < HPZEGGMANMINE_BIG_EXPLODED
		temp0 = object.propertyValue
		temp0 += object.frame
		DrawSprite(temp0)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 443, 99)
	SpriteFrame(-16, -16, 32, 32, 367, 222)
	SpriteFrame(-24, -24, 48, 48, 353, 66)
	SpriteFrame(-24, -24, 48, 48, 400, 206)
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 443, 99)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
