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.

76 lines
2.6 KiB

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