// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Eggman Hammer Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

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

private alias object.value0  : object.originPos.x
private alias object.value1  : object.originPos.y
private alias object.value2  : object.oscillationAngle
private alias object.value3  : object.timer
private alias object.value4  : object.boundsR
private alias object.value5  : object.boundsL
// value6 is unused
private alias object.value7  : object.boundsOffset
private alias object.value8  : object.hideFlames
private alias object.value9  : object.flameFrame
private alias object.value10 : object.flameAnimTimer
private alias object.value11 : object.arrowSlot
private alias object.value12 : object.arrowVelocity
private alias object.value13 : object.hammerOffset.x
private alias object.value14 : object.hammerOffset.y

// States
private alias 0 : EGGMANHAMMER_ENTEREGGMAN
private alias 1 : EGGMANHAMMER_MOVING
private alias 2 : EGGMANHAMMER_PREPARESLAM
private alias 3 : EGGMANHAMMER_SLAM
private alias 4 : EGGMANHAMMER_DESTROYED
private alias 5 : EGGMANHAMMER_DEBRIS

// EggmanTotemPole Aliases
private alias object.value3 : eggmanTotemPole.timer
private alias object.value8 : eggmanTotemPole.offset
private alias object.value9 : eggmanTotemPole.eyePos

// EggmanArrow Aliases
private alias object.value0 : eggmanArrow.targetPos.x
private alias object.value1 : eggmanArrow.parent

private alias 0 : EGGMANARROW_MOVING


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

reserve function EggmanHammer_Oscillate


// ========================
// Tables
// ========================

private table EggmanHammer_arrowOffsets
	-0x280000, -0x80000, 0x180000, 0x380000
end table


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

private function EggmanHammer_Oscillate
	object.ypos = object.originPos.y
	Sin256(temp0, object.oscillationAngle)
	temp0 <<= 10
	object.ypos += temp0
	object.oscillationAngle += 2
	object.oscillationAngle &= 255

	object[-1].xpos = object.xpos
	object[-1].ypos = object.ypos
	object[-1].direction = object.direction
end function


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

event ObjectUpdate
	switch object.state
	case EGGMANHAMMER_ENTEREGGMAN
		object.originPos.y += 0x10000

		object.timer--
		if object.timer == 0
			object.state++
		end if

		CallFunction(EggmanHammer_Oscillate)
		break

	case EGGMANHAMMER_MOVING
		object.animationTimer = 0
		if object.direction == FLIP_NONE
			if object.xpos > object.boundsR
				object.xpos -= object.boundsOffset
			else
				object.state++
			end if
		else
			if object.xpos < object.boundsL
				object.xpos += object.boundsOffset
			else
				object.state = EGGMANHAMMER_PREPARESLAM
			end if
		end if

		CallFunction(EggmanHammer_Oscillate)
		break

	case EGGMANHAMMER_PREPARESLAM
		CallFunction(EggmanHammer_Oscillate)
		if object.oscillationAngle == 0xC0
			object.state = EGGMANHAMMER_SLAM
		end if
		break

	case EGGMANHAMMER_SLAM
		object.animationTimer++
		switch object.animationTimer
		case 1
			object.frame = 4
			PlaySfx(SfxName[Crusher], false)
			break

		case 7
			object.frame = 5

			arrayPos0 = object.entityPos
			if object.direction == FLIP_NONE
				arrayPos0 += 4
			else
				arrayPos0 += 5
			end if
			object[arrayPos0].state = 2
			eggmanTotemPole[arrayPos0].timer = 32
			eggmanTotemPole[arrayPos0].offset = -0x10000

			arrayPos1 = object[+1].entityPos
			arrayPos1 += object.arrowSlot
			object.arrowSlot++
			object.arrowSlot %= 3

			object[arrayPos1].type = TypeName[Eggman Arrow]
			object[arrayPos1].xpos = object[arrayPos0].xpos
			Rand(temp0, 4)
			GetTableValue(object[arrayPos1].ypos, temp0, EggmanHammer_arrowOffsets)
			eggmanTotemPole[arrayPos0].eyePos = object[arrayPos1].ypos
			object[arrayPos1].ypos += object[arrayPos0].ypos
			if object.direction == FLIP_NONE
				eggmanArrow[arrayPos1].targetPos.x = object[+5].xpos
				eggmanArrow[arrayPos1].targetPos.x -= 0x280000
				object[arrayPos1].xpos += 0x100000
			else
				eggmanArrow[arrayPos1].targetPos.x = object[+4].xpos
				eggmanArrow[arrayPos1].targetPos.x += 0x280000
				object[arrayPos1].xpos -= 0x100000
			end if
			object[arrayPos1].direction = object.direction
			object[arrayPos1].drawOrder = 2
			eggmanArrow[arrayPos1].parent = object[+1].entityPos
			eggmanArrow[arrayPos1].parent += object.arrowSlot
			object[arrayPos1].yvel = 0
			object[arrayPos1].frame = 0
			object[arrayPos1].animation = 0
			object[arrayPos1].animationTimer = 0
			object[arrayPos1].xvel = object.arrowVelocity
			object[arrayPos1].state = EGGMANARROW_MOVING
			break

		case 13
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				if object.direction == FLIP_NONE
					BoxCollisionTest(C_TOUCH, object.entityPos, -88, -24, -56, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, 56, -24, 88, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				end if

				if checkResult == true
					CallFunction(Player_Hit)
				end if
			next
			break

		case 22
			object.frame = 4
			break

		case 29
			object.frame = 3
			break

		case 32
			object.direction ^= FLIP_X
			object.state = EGGMANHAMMER_MOVING
			break
			
		end switch

		CallFunction(EggmanHammer_Oscillate)
		break

	case EGGMANHAMMER_DESTROYED
		break

	case EGGMANHAMMER_DEBRIS
		object.hammerOffset.x += object.xvel
		object.hammerOffset.y += object.yvel
		object.yvel += 0x3800
		break
		
	end switch

	object.flameAnimTimer++
	object.flameAnimTimer &= 3
	object.flameFrame = object.flameAnimTimer
	object.flameFrame >>= 1
end event


event ObjectDraw
	DrawSpriteFX(2, FX_FLIP, object.xpos, object.ypos)
	if object.hideFlames != true
		DrawSpriteFX(object.flameFrame, FX_FLIP, object.xpos, object.ypos)
	end if

	if object.animation != 1
		temp0 = object.xpos
		if object.direction == FLIP_NONE
			temp0 -= 0x280000
		else
			temp0 += 0x280000
		end if
		temp0 += object.hammerOffset.x

		temp1 = object.ypos
		temp1 += 0x40000
		temp1 += object.hammerOffset.y
		DrawSpriteFX(object.frame, FX_ROTATE, temp0, temp1)
	end if
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("ARZ/Objects.gif")
		SpriteFrame(-15, 24, 46, 16, 78, 183)	// Flames 1 - #0
		SpriteFrame(-16, 24, 48, 8, 207, 162)	// Flames 2 - #1
		SpriteFrame(-44, -28, 76, 52, 1, 147)	// EggMobile - #2
		SpriteFrame(-50, -49, 54, 53, 1, 202)	// Hammer (Idle) - #3
		SpriteFrame(-49, -43, 49, 55, 56, 200)	// Hammer (Slamming) - #4
		SpriteFrame(-48, -24, 48, 48, 106, 207)	// Hammer (Slammed) - #5
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-15, 24, 46, 16, 299, 5)	// Flames 1 - #0
		SpriteFrame(-16, 24, 48, 8, 299, 22)	// Flames 2 - #1
		SpriteFrame(-44, -28, 76, 52, 222, 5)	// EggMobile - #2
		SpriteFrame(-50, -49, 54, 53, 255, 58)	// Hammer (Idle) - #3
		SpriteFrame(-49, -43, 49, 55, 310, 56)	// Hammer (Slamming) - #4
		SpriteFrame(-48, -24, 48, 48, 348, 1) 	// Hammer (Slammed) - #5
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("ARZ/Objects.gif")
		SpriteFrame(-44, -28, 76, 52, 1, 147)	// EggMobile
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-44, -28, 76, 52, 222, 5)	// EggMobile, but duller!
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
