// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Crabmeat 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.startPos.x
private alias object.value2 : object.startPos.y
private alias object.value3 : object.canShoot

private alias 0 : CRABMEAT_MOVING
private alias 1 : CRABMEAT_IDLE
private alias 2 : CRABMEAT_SHOOT
private alias 3 : CRABMEAT_SHOOTDELAY
private alias 4 : CRABMEAT_REVIVE

private alias 0 : CRABMEAT_ANI_MOVING
private alias 1 : CRABMEAT_ANI_SHOOT
private alias 2 : CRABMEAT_ANI_IDLE

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

// Revive Enemy Aliases
private alias object.value47 : object.backupType
private alias object.value46 : object.backupState
private alias object.value44 : object.backupvalueA
private alias object.value43 : object.backupvalueB
private alias object.value0  : object.target

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function Crabmeat_DebugDraw
reserve function Crabmeat_DebugSpawn


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

private function Crabmeat_DebugDraw
	DrawSprite(0)
end function


private function Crabmeat_DebugSpawn
	CreateTempObject(TypeName[Crabmeat], 0, object.xpos, object.ypos)
	object[tempObjectPos].startPos.x = object.xpos
	object[tempObjectPos].startPos.y = object.ypos

	GetBit(temp0, object.direction, 0)
	if temp0 == 0
		object[tempObjectPos].direction = FACING_LEFT
		object[tempObjectPos].xvel = 0x8000
	else
		object[tempObjectPos].direction = FACING_LEFT
		object[tempObjectPos].xvel = -0x8000
	end if

	object[tempObjectPos].canShoot = false
end function


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

event ObjectUpdate
	switch object.state
	case CRABMEAT_MOVING
		object.priority = PRIORITY_ACTIVE

		if object.timer < 127
			object.timer++
			object.xpos += object.xvel
			if object.xvel > 0
				ObjectTileGrip(CSIDE_FLOOR, 14, 15, PATH_A)
			else
				ObjectTileGrip(CSIDE_FLOOR, -14, 15, PATH_A)
			end if

			if checkResult == false
				if object.canShoot == true
					object.state = CRABMEAT_SHOOT
				else
					object.state  = CRABMEAT_IDLE
					object.canShoot = true
				end if
				object.timer = 0
				object.animation = CRABMEAT_ANI_SHOOT
			end if
			ObjectTileGrip(CSIDE_FLOOR, 0, 15, PATH_A)
		else
			if object.canShoot == true
				object.state = CRABMEAT_SHOOT
			else
				object.state  = CRABMEAT_IDLE
				object.canShoot = true
			end if

			object.timer = 0
			object.animation = CRABMEAT_ANI_SHOOT
		end if
		break

	case CRABMEAT_IDLE
		if object.timer < 59
			object.timer++
		else
			object.timer = 0
			object.state = CRABMEAT_MOVING
			object.animation = CRABMEAT_ANI_MOVING
			object.animationTimer = 0
			FlipSign(object.xvel)
		end if
		break

	case CRABMEAT_SHOOT
		if object.timer < 59
			object.timer++
		else
			CreateTempObject(TypeName[Crabmeat Shot], 0, object.xpos, object.ypos)
			object[tempObjectPos].xpos -= 0x100000
			object[tempObjectPos].xvel = -0x10000
			object[tempObjectPos].yvel = -0x40000

			CreateTempObject(TypeName[Crabmeat Shot], 0, object.xpos, object.ypos)
			object[tempObjectPos].xpos += 0x100000
			object[tempObjectPos].xvel = 0x10000
			object[tempObjectPos].yvel = -0x40000

			object.timer = 0
			object.state++
			object.animation = CRABMEAT_ANI_IDLE
		end if
		break

	case CRABMEAT_SHOOTDELAY
		if object.timer < 59
			object.timer++
		else
			object.timer = 0
			object.state = CRABMEAT_MOVING
			object.animation = CRABMEAT_ANI_MOVING
			object.animationTimer = 0
			FlipSign(object.xvel)
		end if
		break
		
	case CRABMEAT_REVIVE
		object.startPos.x = object.xpos
		object.startPos.y = object.ypos
		
		if object.propertyValue == 1
			object.direction = FACING_LEFT
			object.xvel = 0x8000
		else
			object.direction = FACING_LEFT
			object.xvel = -0x8000
		end if
		
		object.canShoot = false
		object.state = CRABMEAT_MOVING
		object.timer = 0
		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
			if object.propertyValue == 1
				object.direction = FACING_LEFT
				object.xvel = 0x8000
			else
				object.direction = FACING_LEFT
				object.xvel = -0x8000
			end if

			object.canShoot = false
			object.state = CRABMEAT_MOVING
			object.timer = 0
			object.priority = PRIORITY_BOUNDS
			object.animation = CRABMEAT_ANI_MOVING
			object.animationTimer = 0
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -14, -14, 14, 14, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CreateTempObject(TypeName[ReviveEnemy], object.propertyValue, object.startPos.x, object.startPos.y)
			object[tempObjectPos].priority = PRIORITY_ACTIVE
			object[tempObjectPos].target = object.entityPos
			object[tempObjectPos].backupType = object.type
			object[tempObjectPos].backupState = CRABMEAT_REVIVE
			CallFunction(Player_BadnikBreak)
		end if
	next

	switch object.animation
	case CRABMEAT_ANI_MOVING
		object.frame = object.animationTimer
		object.frame >>= 4
		object.animationTimer++
		object.animationTimer %= 48
		break

	case CRABMEAT_ANI_SHOOT
		object.frame = 0
		break

	case CRABMEAT_ANI_IDLE
		object.frame = 3
		break
		
	end switch
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("GHZ/Objects.gif")
		SpriteFrame(-21, -16, 42, 31, 138, 157)
		SpriteFrame(-22, -16, 44, 31, 98, 189)
		SpriteFrame(-22, -16, 44, 31, 98, 221)
		SpriteFrame(-24, -15, 48, 30, 143, 189)
	end if

	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("SYZ/Objects.gif")
		SpriteFrame(-21, -16, 42, 31, 184, 1)
		SpriteFrame(-22, -16, 44, 31, 184, 34)
		SpriteFrame(-22, -16, 44, 31, 184, 66)
		SpriteFrame(-24, -15, 48, 30, 184, 173)
	end if

	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-21, -16, 42, 31, 168, 81)
		SpriteFrame(-22, -16, 44, 31, 211, 81)
		SpriteFrame(-22, -16, 44, 31, 211, 113)
		SpriteFrame(-24, -15, 48, 30, 162, 114)
	end if

	foreach (TypeName[Crabmeat], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].direction = FACING_LEFT
			object[arrayPos0].xvel = 0x8000
		else
			object[arrayPos0].direction = FACING_LEFT
			object[arrayPos0].xvel = -0x8000
		end if
		object[arrayPos0].canShoot = false
	next

	SetTableValue(TypeName[Crabmeat], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Crabmeat_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Crabmeat_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 // direction
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // direction
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("GHZ/Objects.gif")
		SpriteFrame(-21, -16, 42, 31, 138, 157)
	end if

	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("SYZ/Objects.gif")
		SpriteFrame(-21, -16, 42, 31, 184, 1)
	end if

	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-21, -16, 42, 31, 168, 81)
	end if

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
end event
