You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.5 KiB

7 years ago
  1. #if !UNITY_FLASH
  2. using UnityEngine;
  3. using System.Collections;
  4. using DentedPixel;
  5. public class GeneralEventsListenersCS : MonoBehaviour {
  6. Vector3 towardsRotation;
  7. float turnForLength = 0.5f;
  8. float turnForIter = 0f;
  9. Color fromColor;
  10. // It's best to make this a public enum that you use throughout your project, so every class can have access to it
  11. public enum MyEvents{
  12. CHANGE_COLOR,
  13. JUMP,
  14. LENGTH
  15. }
  16. void Awake(){
  17. LeanTween.LISTENERS_MAX = 100; // This is the maximum of event listeners you will have added as listeners
  18. LeanTween.EVENTS_MAX = (int)MyEvents.LENGTH; // The maximum amount of events you will be dispatching
  19. fromColor = GetComponent<Renderer>().material.color;
  20. }
  21. void Start () {
  22. // Adding Listeners, it's best to use an enum so your listeners are more descriptive but you could use an int like 0,1,2,...
  23. LeanTween.addListener(gameObject, (int)MyEvents.CHANGE_COLOR, changeColor);
  24. LeanTween.addListener(gameObject, (int)MyEvents.JUMP, jumpUp);
  25. }
  26. // ****** Event Listening Methods
  27. void jumpUp( LTEvent e ){
  28. GetComponent<Rigidbody>().AddRelativeForce(Vector3.forward * 300f);
  29. }
  30. void changeColor( LTEvent e ){
  31. Transform tran = (Transform)e.data;
  32. float distance = Vector3.Distance( tran.position, transform.position);
  33. Color to = new Color(Random.Range(0f,1f),0f,Random.Range(0f,1f));
  34. LeanTween.value( gameObject, fromColor, to, 0.8f ).setLoopPingPong(1).setDelay(distance*0.05f).setOnUpdate(
  35. (Color col)=>{
  36. GetComponent<Renderer>().material.color = col;
  37. }
  38. );
  39. }
  40. // ****** Physics / AI Stuff
  41. void OnCollisionEnter(Collision collision) {
  42. if(collision.gameObject.layer!=2)
  43. towardsRotation = new Vector3(0f, Random.Range(-180, 180), 0f);
  44. }
  45. void OnCollisionStay(Collision collision) {
  46. if(collision.gameObject.layer!=2){
  47. turnForIter = 0f;
  48. turnForLength = Random.Range(0.5f, 1.5f);
  49. }
  50. }
  51. void FixedUpdate(){
  52. if(turnForIter < turnForLength){
  53. GetComponent<Rigidbody>().MoveRotation( GetComponent<Rigidbody>().rotation * Quaternion.Euler(towardsRotation * Time.deltaTime ) );
  54. turnForIter += Time.deltaTime;
  55. }
  56. GetComponent<Rigidbody>().AddRelativeForce(Vector3.forward * 4.5f);
  57. }
  58. // ****** Key and clicking detection
  59. void OnMouseDown(){
  60. if(Input.GetKey( KeyCode.J )){ // Are you also pressing the "j" key while clicking
  61. LeanTween.dispatchEvent((int)MyEvents.JUMP);
  62. }else{
  63. LeanTween.dispatchEvent((int)MyEvents.CHANGE_COLOR, transform); // with every dispatched event, you can include an object (retrieve this object with the *.data var in LTEvent)
  64. }
  65. }
  66. }
  67. #endif