Ejemplo n.º 1
0
	/*
	 *	On Update do: 
	 *	>>Attached?
	 *		- Check if tomato should be discarted (average speed above given threshold
	 * 		- Check if tomato should be put in the box
	 * 		- Move (follow hand)
	 * 	>>Not Attached?
	 * 		- Check if it should be attached to players hand
	 * 		- Move (to position / around the position)
	 */	
	void Update () {
		/*
		 * Movement Handler
		 */
		currentPosition = transform.position;	// Set the current position of the Tomato

		switch (state) {
			case TomatoState.notActive:			// Try to reach target
				applyGravity();
				if (speed.y<0)					// If tomato is falling
					flapWings();
					applySpeed();
				// If the tomato goes close to the target set as Active
				if (Vector3.Distance(currentPosition, targetPosition.position)<1f){
					musicSource.clip = selected;
					musicSource.Play();
					state = TomatoState.Active;
					SelectedAnim.Play();		// Play the animation for selection	
					tomatoSpeed *= 0.3f;
					}
				break;
			case TomatoState.Active:			// Try to keep still near target
				applyGravity();
				// If tomato is really falling & is below the target
				if (speed.y<0.5f && currentPosition.y < targetPosition.position.y)				
					flapWings();
					applySpeed();
				break;
			case TomatoState.Attached:			// Stay attached
					moveToParentHolder();		// Move to the parentHolder position
				break;
			case TomatoState.Dropped:			// Drop
					applyGravity();				// Apply gravitational force to speed
					applySpeed();				// Apply the speed to position
					checkForDeletion();			// Check whether the object is off screen and delete if so
				break;
			case TomatoState.Inbox:				// Destroy Self
				Destroy(this.gameObject);		// Kill the gameObject (Tomato)
				break;
		}
	 
		 // If the tomato is attached to either hand Check whether the average speed indicates it should be thrown away

		if (state.Equals(TomatoState.Attached)) 
		{
			// Calculate the average speed over the last 10 frames (units/sec)
			// Frame-to-frame speed values are kept in an Array (speedValues) which is limited to 10 elements
			float averageSpeed = 0;
			currentPosition = transform.position;
			speedValues.Insert(0,(float) Mathf.Abs(currentPosition.x - oldPosition.x)/Time.deltaTime);		// Insert the new speed first
			if (speedValues.Count>10) speedValues.RemoveAt(10);												// Remove the last element
			foreach(float speed in speedValues){															// Calculate the average speed
				averageSpeed += speed/10;
			}	
			
			// If the average speed in the duration of the last 10 frames exceeds 
			// the input threshold, discard the tomato object  
			if (averageSpeed  >= speedThreshold)
			{
				musicSource.clip = canceled;
				musicSource.Play();
				setSpeed();							// Set the speed variable to the actual frame-to-frame speed
				state = TomatoState.Dropped;
			}
		
		}
		
		oldPosition = currentPosition;				// The old position will be the current position		

		Ray myFrontRay = new Ray(transform.position,Vector3.back);
		Ray myBacktRay = new Ray(transform.position,-1*Vector3.back);
		if (Physics.Raycast(myFrontRay,out hittingFront,100f) || Physics.Raycast(myBacktRay,out hittingBack,100f))
		{
			// Get the hitting object that got the trigger (prefer front)
			if (hittingFront.transform != null) hitting = hittingFront;
			else hitting = hittingBack;
			
			if (state.Equals(TomatoState.Active) && hitting.transform.tag == "SelectingBodyPart" && hitting.transform.GetComponentInChildren<ItemInHand>()!=null && hitting.transform.GetComponentInChildren<ItemInHand>().containing == null )
			{
				musicSource.clip = selected;
				musicSource.Play();
				state = TomatoState.Attached;	
				gameObject.SampleAnimation(SelectedAnim.clip, 0);		// Set the selected animation to frame 0 (nothing visible)
				parentHolder = hitting.transform;
				// Find the ItemInHand script on the hand and set the containing object
				parentHolder.GetComponentInChildren<ItemInHand>().containing = this.transform;
				tomatoMaker.setInstantiated(number, false);				// Create a new tomato
			}
		
			// The tomato is already attached to the hand , now just check that you are hitting the basket 
			if (state.Equals(TomatoState.Attached) && hitting.transform.name == "Select")
			{
				// Put the value in the tomato in the basket 
				hitting.transform.GetComponent<BoxContaining>().AddValue(number);
				parentHolder = hitting.transform;	// Set the box as the parent holder
				moveToParentHolder();				// Move to the parent holder position
				state = TomatoState.Inbox;
			}
		}	
	}