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

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

private alias object.propertyValue : object.fireDirection

private alias object.value0 : object.timer
private alias object.value1 : object.fireInDir

private alias 0 : NEWTRONSHOOT_WAITFORPLAYER
private alias 1 : NEWTRONSHOOT_APPEAR
private alias 2 : NEWTRONSHOOT_SHOOT
private alias 3 : NEWTRONSHOOT_SHOOTDELAY
private alias 4 : NEWTRONSHOOT_DISAPPEAR

private alias 0 : NEWTRONSHOOT_ANI_IDLE
private alias 1 : NEWTRONSHOOT_ANI_SHOOT

// 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


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

reserve function NewtronShoot_DebugDraw
reserve function NewtronShoot_DebugSpawn


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

private function NewtronShoot_DebugDraw
	DrawSprite(object.direction)
end function


private function NewtronShoot_DebugSpawn
	CreateTempObject(TypeName[Newtron Shoot], object.direction, object.xpos, object.ypos)
	object[tempObjectPos].inkEffect = INK_ALPHA
	object[tempObjectPos].alpha = 0
end function


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

event ObjectUpdate
	switch object.state
	case NEWTRONSHOOT_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
#platform: USE_ORIGINS
				if game.playMode != BOOT_PLAYMODE_MISSION
#endplatform
					object.state = NEWTRONSHOOT_APPEAR
					if player[currentPlayer].xpos < object.xpos
						// Firing to the left
						object.fireDirection = FLIP_X
					else
						// Firing to the right
						object.fireDirection = FLIP_NONE
					end if
#platform: USE_ORIGINS
				else
					// fireInDir is set from the editor, in Missions where the Newtrons should only fire in a specific direction (M006)
					if object.fireInDir != false
						// woah that's actually like, a really cool way of doing it, especially when compared to how Origins does a bunch of other stuff
						CheckLower(player[currentPlayer].xpos, object.xpos)
						CheckEqual(object.fireDirection, checkResult)
						object.state = checkResult
					else
						CheckLower(player[currentPlayer].xpos, object.xpos)
						object.fireDirection = checkResult
						object.state = NEWTRONSHOOT_APPEAR
					end if
				end if
#endplatform
			end if
		next
		break

	case NEWTRONSHOOT_APPEAR
		if object.alpha < 0xF8
			object.alpha += 4
		else
			object.alpha = 0xFF
			object.state++
		end if
		break

	case NEWTRONSHOOT_SHOOT
		if object.timer < 29
			object.timer++
		else
			object.timer = 0
			CreateTempObject(TypeName[Newtron Shot], 0, object.xpos, object.ypos)
			if object.fireDirection == FLIP_X
				// Shooting left
				
				object[tempObjectPos].xpos -= 0x140000
				object[tempObjectPos].xvel = -0x20000
			else
				// Shooting right
				
				object[tempObjectPos].xpos += 0x140000
				object[tempObjectPos].xvel = 0x20000
			end if
			object[tempObjectPos].ypos -= 0x80000
			object.animation = NEWTRONSHOOT_ANI_SHOOT
			object.state++
		end if
		break

	case NEWTRONSHOOT_SHOOTDELAY
		if object.timer < 59
			if object.timer == 15
				object.animation = NEWTRONSHOOT_ANI_IDLE
			end if
			object.timer++
		else
			object.timer = 0
			object.alpha = 0xF8
			object.state++
		end if
		break

	case NEWTRONSHOOT_DISAPPEAR
		if object.alpha > 0
			object.alpha -= 4
		else
			object.type = TypeName[Blank Object]
		end if
		break
		
	end switch

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		switch object.state
		default
		case NEWTRONSHOOT_WAITFORPLAYER
		case NEWTRONSHOOT_APPEAR
		case NEWTRONSHOOT_DISAPPEAR
			break

		case NEWTRONSHOOT_SHOOT
		case NEWTRONSHOOT_SHOOTDELAY
			BoxCollisionTest(C_TOUCH, object.entityPos, -12, -14, 12, 14, 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
end event


event ObjectDraw
	switch object.animation
	case NEWTRONSHOOT_ANI_IDLE
		// Fire Direction matches with a Sprite Frame (0 for right, 1 for left)
		// FX effects don't allow for flipping sprites, which is why we used pre-flipped sprites instead
		DrawSpriteFX(object.fireDirection, FX_INK, object.xpos, object.ypos)
		break

	case NEWTRONSHOOT_ANI_SHOOT
		// Same from above applies here too about Fire Direction, yup
		temp0 = object.fireDirection
		temp0 += 2
		DrawSprite(temp0)
		break
		
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("GHZ/Objects2.gif")
		SpriteFrame(-20, -20, 39, 39, 1, 1)   // 0 - Idle facing right frame
		SpriteFrame(-19, -20, 39, 39, 81, 1)  // 1 - Idle facing left frame
		SpriteFrame(-20, -20, 39, 39, 41, 1)  // 2 - Shooting facing left
		SpriteFrame(-19, -20, 39, 39, 121, 1) // 3 - Shoting facing right frame
	end if
	
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-20, -20, 39, 39, 1, 164)   // 0 - Idle facing right frame
		SpriteFrame(-19, -20, 39, 39, 81, 164)  // 1 - Idle facing left frame
		SpriteFrame(-20, -20, 39, 39, 41, 164)  // 2 - Shooting facing left
		SpriteFrame(-19, -20, 39, 39, 121, 164) // 3 - Shoting facing right frame
	end if
	
	foreach (TypeName[Newtron Shoot], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].inkEffect = INK_ALPHA
		object[arrayPos0].alpha = 0
	next
	
	SetTableValue(TypeName[Newtron Shoot], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(NewtronShoot_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(NewtronShoot_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
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 // fireInDir
			GetBit(checkResult, object.fireInDir, 0)
			break
			
		case 1 // fireDirection
			GetBit(checkResult, object.propertyValue, 0)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // fireInDir
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.fireInDir, 0, checkResult)
			break
			
		case 1 // fireDirection
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 0, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(temp0, object.propertyValue, 0)
	DrawSprite(temp0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-20, -20, 39, 39, 1, 164)
		SpriteFrame(-19, -20, 39, 39, 81, 164)
	else
		LoadSpriteSheet("GHZ/Objects2.gif")
		SpriteFrame(-20, -20, 39, 39, 1, 1)
		SpriteFrame(-19, -20, 39, 39, 81, 1)
	end if
	
	// If the Newtron should only Shoot when the player is in a specific direction
	// Only used in Origins's Mission Mode
	AddEditorVariable("fireInDir")
	SetActiveVariable("fireInDir")
	AddEnumVariable("False", 0)
	AddEnumVariable("True", 1)
	
	// Related to above, also only used in Mission Mode
	// Otherwise, it's ignored
	AddEditorVariable("fireDirection")
	SetActiveVariable("fireDirection")
	AddEnumVariable("Right", 0)
	AddEnumVariable("Left", 1)
end event
