// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Newtron Fly Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

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

private alias object.propertyValue : object.appearFrame

private alias object.value1 : object.startPos.x
private alias object.value2 : object.startPos.y

private alias 0 : NEWTRONFLY_WAITFORPLAYER
private alias 1 : NEWTRONFLY_APPEARING
private alias 2 : NEWTRONFLY_APPEARED
private alias 3 : NEWTRONFLY_MOVING

private alias 0 : NEWTRONFLY_ANI_IDLE
private alias 1 : NEWTRONFLY_ANI_APPEAR
private alias 2 : NEWTRONFLY_ANI_MOVING

// Player Aliases
private alias object.xpos    : player.xpos
private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function NewtronFly_DebugDraw
reserve function NewtronFly_DebugSpawn


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

private function NewtronFly_DebugDraw
	DrawSprite(object.direction)
end function


private function NewtronFly_DebugSpawn
	CreateTempObject(TypeName[Newtron Fly], object.direction, object.xpos, object.ypos)
	object[tempObjectPos].inkEffect = INK_ALPHA
	object[tempObjectPos].alpha = 0
	object[tempObjectPos].startPos.x = object.xpos
	object[tempObjectPos].startPos.y = object.ypos
end function


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

event ObjectUpdate
	switch object.state
	case NEWTRONFLY_WAITFORPLAYER
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -64, 128, 64, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				object.state = NEWTRONFLY_APPEARING
			end if
		next
		break

	case NEWTRONFLY_APPEARING
		object.priority = PRIORITY_ACTIVE
		if object.alpha < 0xF8
			object.alpha += 4
			if player[0].xpos > object.xpos
				object.appearFrame = FLIP_NONE
			else
				object.appearFrame = FLIP_X
			end if
		else
			object.alpha = 0xFF
			object.animation = NEWTRONFLY_ANI_APPEAR
			if object.appearFrame == FLIP_NONE
				object.xvel =  0x20000
			else
				object.xvel = -0x20000
			end if
			object.direction = object.appearFrame
			object.state++
		end if
		break

	case NEWTRONFLY_APPEARED
		object.ypos += object.yvel
		object.yvel += 0x3800
		ObjectTileCollision(CSIDE_FLOOR, 0, 8, PATH_A)
		if checkResult == true
			object.yvel = 0
			object.animationTimer = 0
			object.animation = NEWTRONFLY_ANI_MOVING
			object.state++
		end if
		break

	case NEWTRONFLY_MOVING
		object.xpos += object.xvel
		ObjectTileGrip(CSIDE_FLOOR, 0, 8, PATH_A)
		break
		
	end switch

	if object.outOfBounds == true
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.startPos.x
		object.ypos = object.startPos.y
		if object.outOfBounds == true
			object.yvel = 0
			object.xpos = object.startPos.x
			object.ypos = object.startPos.y
			object.animation = NEWTRONFLY_ANI_IDLE
			object.animationTimer = 0
			object.priority = PRIORITY_BOUNDS
			object.alpha = 0
			object.state = NEWTRONFLY_WAITFORPLAYER
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		switch object.state
		default
		case NEWTRONFLY_WAITFORPLAYER
		case NEWTRONFLY_APPEARING
			break

		case NEWTRONFLY_APPEARED
		case NEWTRONFLY_MOVING
			BoxCollisionTest(C_TOUCH, object.entityPos, -16, -8, 16, 8, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
			if checkResult == true
				CallFunction(Player_BadnikBreak)
			end if
			break
			
		end switch
	next

	switch object.animation
	case NEWTRONFLY_ANI_IDLE
		break

	case NEWTRONFLY_ANI_APPEAR
		if object.animationTimer < 41
			object.frame = object.animationTimer
			object.frame /= 20
			object.animationTimer++
		end if
		object.frame += 2
		break

	case NEWTRONFLY_ANI_MOVING
		object.frame = object.animationTimer
		object.frame /= 3
		object.frame += 5
		object.animationTimer++
		object.animationTimer %= 6
		break
		
	end switch
end event


event ObjectDraw
	switch object.animation
	case NEWTRONFLY_ANI_IDLE
		DrawSpriteFX(object.appearFrame, FX_INK, object.xpos, object.ypos)
		break

	case NEWTRONFLY_ANI_APPEAR
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
		break

	case NEWTRONFLY_ANI_MOVING
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break
		
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("GHZ/Objects2.gif")
		SpriteFrame(-20, -20, 39, 39, 161, 1)
		SpriteFrame(-19, -20, 39, 39, 201, 1)
		SpriteFrame(-18, -20, 37, 35, 218, 41)
		SpriteFrame(-15, -18, 36, 22, 1, 41)
		SpriteFrame(-20, -16, 40, 16, 38, 41)
		SpriteFrame(-28, -9, 8, 8, 79, 41)
		SpriteFrame(-34, -10, 14, 8, 88, 41)
	end if

	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-20, -20, 39, 39, 1, 124)
		SpriteFrame(-19, -20, 39, 39, 41, 124)
		SpriteFrame(-18, -20, 37, 35, 81, 128)
		SpriteFrame(-15, -18, 36, 22, 1, 204)
		SpriteFrame(-20, -16, 40, 16, 38, 204)
		SpriteFrame(-28, -9, 8, 8, 79, 204)
		SpriteFrame(-34, -10, 14, 8, 88, 204)
	end if

	foreach (TypeName[Newtron Fly], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		object[arrayPos0].inkEffect = INK_ALPHA
		object[arrayPos0].alpha = 0
	next

	SetTableValue(TypeName[Newtron Fly], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(NewtronFly_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(NewtronFly_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-20, -20, 39, 39, 1, 124)
	else
		LoadSpriteSheet("GHZ/Objects2.gif")
		SpriteFrame(-20, -20, 39, 39, 161, 1)
	end if
	
	// Not to be set from the editor
	// It's set to 0 because that's how it was in the original game (the two Newtrons were the same obj type, behaviour was subtype)
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
	
	// TODO: double check that val1 isn't used by this object;
	// it's set in the editor in m006
	// also prop val goes up to two there (y-flip), is that normal?
end event
