// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Eggman Wheel 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.xOffset
private alias object.value2 : object.parent

// States
private alias 0 : EHZEGGMANWHEEL_ATTATCHED
private alias 1 : EHZEGGMANWHEEL_IDLE
private alias 2 : EHZEGGMANWHEEL_BOUNCE

// Car Aliases
private alias object.value4 : eggmanCar.carPos.y

// Eggman States
private alias 1 : EHZEGGMAN_FLYIN
private alias 3 : EHZEGGMAN_STARTCAR

// Path ID Aliases
private alias 0 : PATH_A


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

event ObjectUpdate
	switch object.state
	case EHZEGGMANWHEEL_ATTATCHED
		arrayPos0 = object.parent

		object.xpos = object[arrayPos0].xpos
		if object.direction == FLIP_NONE
			object.xpos += object.xOffset
		else
			object.xpos -= object.xOffset
		end if

		object.ypos = object[arrayPos0].ypos
		object.ypos += 0xC0000
		ObjectTileGrip(CSIDE_FLOOR, 0, 17, PATH_A)

		if object[arrayPos0].state != EHZEGGMAN_FLYIN
			if object[arrayPos0].state != EHZEGGMAN_STARTCAR
				object.timer++
				object.timer &= 3
				object.frame = object.timer
				object.frame >>= 1

				if object.xOffset == -0x2C0000
					object.frame += 2
				end if
			end if
		end if

		if object.xOffset != -0x2C0000
			eggmanCar[arrayPos0].carPos.y += object.ypos
		end if
		break

	case EHZEGGMANWHEEL_IDLE
		break

	case EHZEGGMANWHEEL_BOUNCE
		if object.yvel >= 0
			ObjectTileCollision(CSIDE_FLOOR, 0, 17, PATH_A)
			if checkResult == true
				FlipSign(object.yvel)
			end if
		end if

		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x3800

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


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("EHZ/Objects.gif")
		
		// Wheel Frames
		SpriteFrame(-16, -16, 32, 32, 1, 143)
		SpriteFrame(-16, -16, 32, 32, 34, 143)
		SpriteFrame(-16, -16, 32, 32, 1, 176)
		SpriteFrame(-16, -16, 32, 32, 34, 176)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		
		// Wheel Frames
		SpriteFrame(-16, -16, 32, 32, 123, 58)
		SpriteFrame(-16, -16, 32, 32, 156, 58)
		SpriteFrame(-16, -16, 32, 32, 189, 58)
		SpriteFrame(-16, -16, 32, 32, 222, 58)
	end if
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("EHZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 32, 1, 143)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 32, 123, 58)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
