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

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

private alias object.value1  : object.localPos.x
private alias object.value2  : object.localPos.y
private alias object.value3  : object.localPos.z
private alias object.value4  : object.worldPos.x
private alias object.value5  : object.worldPos.y
private alias object.value6  : object.worldPos.z

private alias object.value7  : object.shadowsEnabled
private alias object.value8  : object.localShadowPos.x
private alias object.value9  : object.localShadowPos.y
private alias object.value11 : object.worldShadowPos.x
private alias object.value12 : object.worldShadowPos.y
private alias object.value13 : object.worldShadowPos.z
private alias object.value14 : object.worldShadowPos.u

// Halfpipe Aliases
private alias object.value13 : halfpipe.playerPos.z

// Player Aliases
private alias object.state : player.state
private alias object.gravity : player.gravity

private alias object.value7  : player.invincibilityTimer
private alias object.value14 : player.localPos.z
private alias object.value15 : player.localPos.x
private alias object.value16 : player.localPos.y

// Face Buffer Flag Aliases
private alias 5 : FACE_TEXTURED_C
private alias 6 : FACE_TEXTURED_C_BLEND


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

event ObjectUpdate
	arrayPos0 = scene3D.vertexCount
	arrayPos1 = scene3D.faceCount
	
	if object.shadowsEnabled == true
		faceBuffer[arrayPos1].flag = FACE_TEXTURED_C_BLEND
		faceBuffer[arrayPos1].a = arrayPos0
		vertexBuffer[arrayPos0].x = object.worldShadowPos.x
		vertexBuffer[arrayPos0].y = object.worldShadowPos.y
		vertexBuffer[arrayPos0].z = object.worldShadowPos.z
		vertexBuffer[arrayPos0].u = object.worldShadowPos.u
		vertexBuffer[arrayPos0].v = 50
		arrayPos0++

		faceBuffer[arrayPos1].b = arrayPos0
		vertexBuffer[arrayPos0].x = object.worldShadowPos.x
		vertexBuffer[arrayPos0].y = object.worldShadowPos.y
		vertexBuffer[arrayPos0].z = object.worldShadowPos.z
		temp0 = object[0].ypos
		temp0 -= 0xE00000
		object.scale = object.ypos
		object.scale -= temp0
		if object.scale > 0
			object.scale >>= 14
			object.scale += 0x400
		else
			object.scale = 0x400
		end if
		vertexBuffer[arrayPos0].u = object.scale
		vertexBuffer[arrayPos0].v = object.scale
		arrayPos0++

		faceBuffer[arrayPos1].c = arrayPos0
		vertexBuffer[arrayPos0].x = object.worldShadowPos.x
		vertexBuffer[arrayPos0].y = object.worldShadowPos.y
		vertexBuffer[arrayPos0].z = object.worldShadowPos.z
		vertexBuffer[arrayPos0].u = 17
		vertexBuffer[arrayPos0].v = 16
		arrayPos0++

		faceBuffer[arrayPos1].d = arrayPos0
		vertexBuffer[arrayPos0].x = object.worldShadowPos.x
		vertexBuffer[arrayPos0].y = object.worldShadowPos.y
		vertexBuffer[arrayPos0].z = object.worldShadowPos.z
		arrayPos0++

		arrayPos1++
		scene3D.vertexCount += 4
		scene3D.faceCount++
	end if
	
	faceBuffer[arrayPos1].flag = FACE_TEXTURED_C
	faceBuffer[arrayPos1].a = arrayPos0
	vertexBuffer[arrayPos0].x = object.worldPos.x
	vertexBuffer[arrayPos0].y = object.worldPos.y
	vertexBuffer[arrayPos0].z = object.worldPos.z
	vertexBuffer[arrayPos0].u = 83
	vertexBuffer[arrayPos0].v = 50
	arrayPos0++

	faceBuffer[arrayPos1].b = arrayPos0
	vertexBuffer[arrayPos0].x = object.worldPos.x
	vertexBuffer[arrayPos0].y = object.worldPos.y
	vertexBuffer[arrayPos0].z = object.worldPos.z
	temp0 = object[0].ypos
	temp0 -= 0x1000000
	temp1 = object.ypos
	temp1 -= temp0
	if temp1 > 0
		temp1 >>= 14
		temp1 += 0x600
	else
		temp1 = 0x600
	end if
	vertexBuffer[arrayPos0].u = temp1
	vertexBuffer[arrayPos0].v = temp1
	arrayPos0++

	faceBuffer[arrayPos1].c = arrayPos0
	vertexBuffer[arrayPos0].x = object.worldPos.x
	vertexBuffer[arrayPos0].y = object.worldPos.y
	vertexBuffer[arrayPos0].z = object.worldPos.z
	vertexBuffer[arrayPos0].u = 16
	vertexBuffer[arrayPos0].v = 16
	arrayPos0++

	faceBuffer[arrayPos1].d = arrayPos0
	vertexBuffer[arrayPos0].x = object.worldPos.x
	vertexBuffer[arrayPos0].y = object.worldPos.y
	vertexBuffer[arrayPos0].z = object.worldPos.z
	scene3D.vertexCount += 4
	scene3D.faceCount++

	if object.localPos.z < halfpipe[0].playerPos.z
		object.type = TypeName[Blank Object]
	else
		foreach (TypeName[Player Object], currentPlayer, ACTIVE_ENTITIES)
			temp1 = object.localPos.z
			temp1 -= 0x20000
			temp2 = object.localPos.z
			temp2 += 0x20000
			if player[currentPlayer].localPos.z > temp1
				if player[currentPlayer].localPos.z < temp2
					temp0 = player[currentPlayer].localPos.x
					temp0 -= object.localPos.x
					temp1 = temp0
					temp1 *= temp0
					
					temp0 = player[currentPlayer].localPos.y
					temp0 -= object.localPos.y
					temp2 = temp0
					temp2 *= temp0
					temp1 += temp2

					if temp1 < 0xC40000
						object.type = TypeName[Explosion]
						object.animationTimer = 0
						object.frame = 0

						if player[currentPlayer].state != Player_State_Hurt
							if player[currentPlayer].invincibilityTimer == 0
								CallFunction(Player_LoseRings)
								if player[currentPlayer].gravity == 0
									player[currentPlayer].state = Player_State_Hurt
								else
									player[currentPlayer].invincibilityTimer = 60
								end if
							end if
						end if
						
						PlaySfx(SfxName[Bomb], false)
					end if
				end if
			end if
		next
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Special/Objects.gif")
	foreach (TypeName[Bomb], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].angle = object[arrayPos0].ixpos
		object[arrayPos0].angle -= 448
		object[arrayPos0].angle &= 0x1FF

		Sin(object[arrayPos0].localPos.x, object[arrayPos0].angle)
		object[arrayPos0].localPos.x *= -48
		object[arrayPos0].localPos.x >>= 1

		Cos(object[arrayPos0].localPos.y, object[arrayPos0].angle)
		object[arrayPos0].localPos.y *= -48
		object[arrayPos0].localPos.y >>= 1

		if object[arrayPos0].localPos.y < -0x800
			object[arrayPos0].shadowsEnabled = true

			Sin(object[arrayPos0].localShadowPos.x, object[arrayPos0].angle)
			object[arrayPos0].localShadowPos.x *= -60
			object[arrayPos0].localShadowPos.x >>= 1

			Cos(object[arrayPos0].localShadowPos.y, object[arrayPos0].angle)
			object[arrayPos0].localShadowPos.y *= -60
			object[arrayPos0].localShadowPos.y >>= 1

			object[arrayPos0].worldShadowPos.u = 116
			if object[arrayPos0].angle > 32
				if object[arrayPos0].angle < 480
					if object[arrayPos0].angle < 256
						object[arrayPos0].worldShadowPos.u = 149
					else
						object[arrayPos0].worldShadowPos.u = 182
					end if
				end if
			end if
		end if
	next
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 233, 17)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
