
// the PickupController component of the 'PickupSpawnPoints' gameObject
private var pickupController:PickupController;

// the PickupController component of the 'PickupSpawnPoints' gameObject
private var characterMotor:CharacterMotor;

// speed variables
public var minSpeed:int = 5;
public var maxSpeed:int = 35;
private var originalSpeed:int;


function Awake()
{
	// retrieve the PickupSpawnPoints gameObject
	var pickupSpawnPoints:GameObject = gameObject.Find("PickupSpawnPoints");
	
	// and then retreive the PickupController Component of the above PickupSpawnPoints gameObject
	pickupController = pickupSpawnPoints.GetComponent("PickupController");
	
	// retrieve the players CharacterMotor Component
	characterMotor = GetComponent(CharacterMotor);
	
	// retrieve the players original speed from the characterMotor
	originalSpeed = characterMotor.movement.maxForwardSpeed;
}

function OnControllerColliderHit (hit:ControllerColliderHit)
{
	// if the player hits a pickup
	if (hit.gameObject.tag == "Pickup")
	{
		// call the Collected(...) function of the PickupController Component (script) and 
		// pass the pickup we hit as the parameter for the function
		pickupController.Collected(hit.gameObject);
		
		// if the pickup is set to type 3 then adjust the players speed
		if (hit.gameObject.GetComponent(Pickup).type == 3)
		{
			// stop any coroutines already running
			StopCoroutine("AdjustSpeed");
			
			// create new coroutine for AdjustSpeed
			StartCoroutine("AdjustSpeed");
		}
	}
}

function AdjustSpeed()
{
	// retrieve random number between 0 and 1
	var rand:float = Random.Range(0.0, 1.0);
	
	if (rand <= 0.6) // 60% for speed boost
	{
		characterMotor.movement.maxForwardSpeed = maxSpeed;
	}
	else // 40% for slow down
	{
		characterMotor.movement.maxForwardSpeed = minSpeed;
	}
	
	// wait for 5-10 seconds before resetting the speed to the original speed
	yield WaitForSeconds(Random.Range(5,10));
	
	characterMotor.movement.maxForwardSpeed = originalSpeed;
}
