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

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

// Player Aliases
private alias object.entityPos : player.entityPos
private alias object.state : player.state
private alias object.xpos : player.xpos
private alias object.speed : player.speed

private alias object.value7  : player.invincibleTimer
private alias object.value8  : player.blinkTimer
private alias object.value37 : player.shield

// Shields
private alias 2 : SHIELD_BUBBLE

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function ChemicalDrop_BubbleHit
reserve function ChemicalDrop_CreateSplash


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

private function ChemicalDrop_BubbleHit
	if player[currentPlayer].shield != SHIELD_BUBBLE
		if player[currentPlayer].state != Player_State_Death
			arrayPos0 = player[currentPlayer].entityPos // So this arrayPos0 stuff moves the slot to P1's shield object but...
			arrayPos0 += playerCount                    // nothing exactly happens with it..?
			if player[currentPlayer].invincibleTimer == 0
				if player[currentPlayer].blinkTimer == 0
					player[currentPlayer].state = Player_State_GotHit
					if player[currentPlayer].xpos > object.xpos
						player[currentPlayer].speed =  0x20000
					else
						player[currentPlayer].speed = -0x20000
					end if
				end if
			end if
		end if
	end if
end function


private function ChemicalDrop_CreateSplash
	temp1 = object.yvel
	temp1 >>= 1
	FlipSign(temp1)
	PlaySfx(SfxName[Chemical Splash], false)
	object.type = TypeName[Blank Object]
	temp0 = 0
	while temp0 < 5
		CreateTempObject(TypeName[Chemical Splash], 0, object.xpos, object.ypos)
		object[tempObjectPos].drawOrder = 2

		Rand(object[tempObjectPos].xvel, 0x200)
		object[tempObjectPos].xvel -= 0x100
		object[tempObjectPos].xvel <<= 10
		if object[tempObjectPos].xvel < 0
			object[tempObjectPos].xvel += 0x20000
		end if
		object[tempObjectPos].xvel -= 0x10000

		if temp0 > 0
			Rand(object[tempObjectPos].yvel, 0x7FE)
			object[tempObjectPos].yvel -= 0x400
			object[tempObjectPos].yvel <<= 8
		end if
		object[tempObjectPos].yvel += temp1

		temp0++
	loop
end function


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

event ObjectUpdate
	object.ypos += object.yvel
	object.yvel += 0x3800

	temp1 = object.ypos
	temp1 >>= 16
	temp1 -= camera[0].ypos
	Abs(temp1)
	temp1 -= 15
	if temp1 >= screen.ycenter
		object.type = TypeName[Blank Object]
	end if

	ObjectTileCollision(CSIDE_FLOOR, 0, 11, PATH_A)
	if checkResult == true
		CallFunction(ChemicalDrop_CreateSplash)
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -6, -6, 6, 6, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			CallFunction(ChemicalDrop_BubbleHit)
			CallFunction(ChemicalDrop_CreateSplash)
		end if
	next
end event


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


event ObjectStartup
	CheckCurrentStageFolder("Zone02")
	if checkResult == true
		LoadSpriteSheet("CPZ/Objects.gif")
		SpriteFrame(-9, -18, 18, 29, 97, 189)
		SpriteFrame(-9, -18, 18, 29, 117, 189)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-9, -18, 18, 29, 1, 64)
		SpriteFrame(-9, -18, 18, 29, 21, 64)
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone02")
	if checkResult == true
		LoadSpriteSheet("CPZ/Objects.gif")
		SpriteFrame(-9, -18, 18, 29, 97, 189)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-9, -18, 18, 29, 1, 64)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
