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

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

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

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

reserve function ConveyorWheel_DebugDraw
reserve function ConveyorWheel_DebugSpawn

private function ConveyorWheel_DebugDraw
	DrawSprite(0)
end function


private function ConveyorWheel_DebugSpawn
	CreateTempObject(TypeName[Conveyor Wheel], 0, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 5
end function


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

event ObjectUpdate
	// Check interaction with players
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)

		BoxCollisionTest(C_TOUCH, object.entityPos, -64, -64, 64, 64, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			if player[currentPlayer].gravity == 0
				if player[currentPlayer].state == Player_State_GlideSlide
					player[currentPlayer].state = Player_State_Ground
				end if

				if player[currentPlayer].state == Player_State_GlideDrop
					player[currentPlayer].state = Player_State_Ground
				end if

				if player[currentPlayer].speed < 0x40000
					player[currentPlayer].speed = 0x40000
				else
					if player[currentPlayer].speed > 0xF0000
						player[currentPlayer].speed = 0xF0000
					end if
				end if
			end if
		end if
	next

	object.angle += 4
	object.angle &= 0x1FF
end event


event ObjectDraw
	Cos(temp0, object.angle)
	temp0 *= 0xC00
	temp0 += object.xpos

	Sin(temp1, object.angle)
	temp1 *= 0xC00
	temp1 += object.ypos

	DrawSpriteXY(0, temp0, temp1)
end event


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

	// Wheel frame
	SpriteFrame(-7, -7, 14, 14, 65, 123)

	foreach (TypeName[Conveyor Wheel], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 5
	next

	// Add this object to the debug item list
	SetTableValue(TypeName[Conveyor Wheel], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(ConveyorWheel_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(ConveyorWheel_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")

	// Wheel frame
	SpriteFrame(-7, -7, 14, 14, 65, 123)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
