ComingHomePalmCS

using UnityEngine;

using System.Collections;

 

 

public class TreeOnBeach : ItemBase

{

public GameObject rucksack;

string EventID = "RucksackFallDown";

private Quaternion rotation;

private bool shaking = false;

private float timeShaking = 1.0f;

private float currentTimeShaking;

 

private ParticleSystem part;

 

public AudioClip hittingTree;

 

//On level load add physics to backpack if event RucksackFallDown has occured

void Start ()

{

 

if (!GWorld.TryRegisterEvent (EventID, "Hello"))

{

EventData tempData = new EventData();

GWorld.FindEvent(EventID,ref tempData);

 

if(tempData.hasEventOccured)

{

rucksack.AddComponent<Rigidbody> ();

}

 

}

 

rotation = transform.rotation;

 

part = GetComponentInChildren<ParticleSystem> ();

}

 

//on left click show comment to player

override public void OnInspect()

{

EventData tempData = new EventData();

GWorld.FindEvent(EventID,ref tempData);

 

if (!tempData.hasEventOccured)

{

DisplayComment ("Hee hee! That's a tiny palm tree.");

}

else

{

DisplayComment ("Hee hee! That's a tiny palm tree.");

}

}

 

//Make the tree shake by rotating the object, if shaking is set to true

void Update()

{

if (shaking)

{

currentTimeShaking -= Time.deltaTime;

 

float shakingIntensity = currentTimeShaking / timeShaking;

 

Vector3 newRot = new Vector3( Random.Range(-shakingIntensity,shakingIntensity), 0.0f, Random.Range(-shakingIntensity,shakingIntensity) );

transform.rotation = rotation * Quaternion.Euler(newRot);

 

if(currentTimeShaking <= 0.0f)

{

shaking = false;

}

 

}

}

 

//On interaction with palm, make the backpack fall down and shake the tree with sound and particles

override public void OnAction()

{

EventData tempData = new EventData();

GWorld.FindEvent(EventID,ref tempData);

 

if(!tempData.hasEventOccured)

{

GWorld.MarkEventDone (EventID);

rucksack.AddComponent<Rigidbody> ();

 

}

 

part.Play ();

AudioSource.PlayClipAtPoint (hittingTree, transform.position, 1.0f );

currentTimeShaking = timeShaking;

shaking = true;

}

 

}