// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Redz 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.attackDelay

// States
private alias 0 : REDZ_WALK
private alias 1 : REDZ_TURN
private alias 2 : REDZ_PREPAREATTACK
private alias 3 : REDZ_ATTACK

// Animations
private alias 0 : REDZ_ANI_MOVING
private alias 1 : REDZ_ANI_STILL

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

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function Redz_DebugDraw
reserve function Redz_DebugSpawn


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

private function Redz_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


private function Redz_DebugSpawn
	CreateTempObject(TypeName[Redz], 0, object.xpos, object.ypos)
	if object.direction == FACING_RIGHT
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].xvel = -0x8000
	else
		object[tempObjectPos].direction = FLIP_X
		object[tempObjectPos].xvel = 0x8000
	end if
end function


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

event ObjectUpdate
	switch object.state
	case REDZ_WALK
		if object.priority != PRIORITY_XBOUNDS_DESTROY
			object.priority = PRIORITY_ACTIVE
		end if

		object.xpos += object.xvel
		if object.direction == FLIP_NONE
			ObjectTileGrip(CSIDE_FLOOR, -8, 16, PATH_A)
		else
			ObjectTileGrip(CSIDE_FLOOR, 8, 16, PATH_A)
		end if

		if checkResult == false
			object.state = REDZ_TURN
			object.animation = REDZ_ANI_STILL
			object.animationTimer = 0
			object.frame = 0
		end if

		if stage.actNum == 2
			// Different behaviour for Proto Palace, where we should never attack
			object.attackDelay = 300
		end if

		if object.attackDelay < 1
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				if object.direction == FLIP_NONE
					BoxCollisionTest(C_TOUCH, object.entityPos, -64, -96, 0, -8, currentPlayer, 0, 0, 0, 0)
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, 0, -96,  64, -8, currentPlayer, 0, 0, 0, 0)
				end if

				if checkResult == true
					object.state = REDZ_PREPAREATTACK
					object.animation = REDZ_ANI_STILL
					object.frame = 0
					object.attackDelay = 60
				end if
			next
		else
			object.attackDelay--
		end if
		break

	case REDZ_TURN
		if object.timer < 59
			object.timer++
		else
			object.timer = 0
			object.state = REDZ_WALK
			object.animation = REDZ_ANI_MOVING
			object.direction ^= FLIP_X
			FlipSign(object.xvel)
		end if
		break

	case REDZ_PREPAREATTACK
		if object.timer < 30
			object.timer++
		else
			object.timer = 0
			object.frame = 4
			object.state = REDZ_ATTACK
			PlaySfx(SfxName[Flame Thrower], false)
		end if
		break

	case REDZ_ATTACK
		if object.timer < 90
			temp0 = object.timer
			temp0 &= 3
			if temp0 == 0
				CreateTempObject(TypeName[Redz Fire], 0, object.xpos, object.ypos)
				object[tempObjectPos].drawOrder = 4
				object[tempObjectPos].ypos -= 0x40000
				temp0 = object.timer
				temp0 <<= 3
				temp0 &= 0x1FF
				Sin(object.angle, temp0)
				object.angle >>= 5
				object.angle -= 0x30
				object.angle &= 0x1FF
				Cos(object[tempObjectPos].xvel, object.angle)
				object[tempObjectPos].xvel <<= 8
				Sin(object[tempObjectPos].yvel, object.angle)
				object[tempObjectPos].yvel <<= 8
				if object.direction == FLIP_NONE
					object[tempObjectPos].xpos -= 0x80000
					FlipSign(object[tempObjectPos].xvel)
				else
					object[tempObjectPos].xpos += 0x80000
				end if
			end if

			object.timer++
		else
			object.timer = 0
			object.frame = 0
			object.state = REDZ_WALK
			object.animation = REDZ_ANI_MOVING
		end if
		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 == 0
				object.direction = FLIP_NONE
				object.xvel = -0x8000
			else
				object.direction = FLIP_X
				object.xvel = 0x8000
			end if

			object.timer = 0
			object.animation = REDZ_ANI_MOVING
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_BOUNDS
			end if
			
			object.state = REDZ_WALK
		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
			CallFunction(Player_BadnikBreak)
		end if
	next

	if object.animation == REDZ_ANI_MOVING
		object.frame = object.animationTimer
		object.frame /= 10
		object.animationTimer++
		object.animationTimer %= 40
	end if
end event


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


event ObjectStartup
	LoadSpriteSheet("HPZ/Objects.gif")

	SpriteFrame(-16, -16, 31, 32, 1, 118)   // 0 - Standing frame

	SpriteFrame(-16, -16, 30, 32, 1, 85)    // 1 - 3 - Walking frames
	SpriteFrame(-16, -16, 31, 32, 1, 118)
	SpriteFrame(-16, -16, 32, 32, 1, 151)

	SpriteFrame(-16, -16, 31, 32, 143, 140) // 4 - Firing fire frame

	foreach (TypeName[Redz], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		if object[arrayPos0].propertyValue == 0
			object[arrayPos0].direction = FLIP_NONE
			object[arrayPos0].xvel = -0x8000
		else
			object[arrayPos0].direction = FLIP_X
			object[arrayPos0].xvel = 0x8000
		end if
	next

	SetTableValue(TypeName[Redz], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Redz_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Redz_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
			checkResult &= 1
			break
			
		case 0 // direction
			GetBit(checkResult, object.propertyValue, 0)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		case 0 // direction
			CheckNotEqual(editor.variableValue, 0)
			SetBit(object.propertyValue, 0, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 0)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-16, -16, 31, 32, 1, 118)

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