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

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

private alias object.value0 : object.startPos.x
private alias object.value1 : object.startPos.y

private alias 0 : RAFT_FALLING
private alias 1 : RAFT_FLOATING

// Player Aliases
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function Raft_DebugDraw
reserve function Raft_DebugSpawn


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

private function Raft_DebugDraw
	DrawSprite(0)
end function


private function Raft_DebugSpawn
	CreateTempObject(TypeName[Raft], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
end function


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

event ObjectUpdate
	temp0 = object.xpos
	temp0 &= 0xFFFF0000
	temp1 = object.ypos
	temp1 &= 0xFFFF0000
	temp7 = stage.waterLevel
	temp7 <<= 16

	if object.state == RAFT_FLOATING
		object.ypos = temp7
	else
		object.ypos += object.yvel
		object.yvel += 0x1800

		if object.ypos >= temp7
			if object.direction == FLIP_NONE
				if object.xvel > 0x10000
					object.xvel = 0x10000
				end if
			else
				if object.xvel < -0x10000
					object.xvel = -0x10000
				end if
			end if
			object.state++
		end if
	end if

	object.xpos += object.xvel
	if object.direction == FLIP_NONE
		ObjectTileCollision(CSIDE_LWALL,  32, 12, PATH_A)
	else
		ObjectTileCollision(CSIDE_RWALL, -32, 12, PATH_A)
	end if

	temp4 = object.xpos
	temp5 = object.ypos

	temp2 = object.xpos
	temp2 &= 0xFFFF0000
	temp2 -= temp0
	
	temp3 = object.ypos
	temp3 &= 0xFFFF0000
	temp3 -= temp1

	object.xpos = temp0
	object.ypos = temp1
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_PLATFORM, object.entityPos, -32, -8, 32, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			if object.xvel == 0
				// If this Raft isn't moving yet, then make it start moving
				if object.direction == FLIP_NONE
					object.xvel = 0x10000
				else
					object.xvel = -0x10000
				end if
			end if
			
			player[currentPlayer].xpos += temp2
			player[currentPlayer].ypos += temp3
		end if
	next
	object.xpos = temp4
	object.ypos = temp5

	if object.outOfBounds == true
		if object.startPos.x == 0 // Debug Mode doesn't set startpos so bye bye debug obj!
			object.type = TypeName[Blank Object]
		else
			object.xpos = object.startPos.x
			object.ypos = object.startPos.y
			if object.outOfBounds == false
				object.xpos = temp4
				object.ypos = temp5
			end if
		end if
	end if
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("ARZ/Objects.gif")
	SpriteFrame(-32, -8, 64, 16, 126, 191)

	SetTableValue(TypeName[Raft], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Raft_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Raft_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++

	foreach (TypeName[Raft], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
	next
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("ARZ/Objects.gif")
	SpriteFrame(-32, -8, 64, 16, 126, 191)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
