Advertisement
Guest User

test

a guest
May 28th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.11 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using FMOD.Studio;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     public float maxSpeed;                  //The speed of the player and segway.
  8.     public float minSpeed;
  9.     private float SPEED;                    //The local variable of SPEED so we only need to multiply it by fixedDeltaTime once.
  10.     public float maxAcceleration;           //The acceleration.
  11.     private float acceleration;
  12.     public float maxDeceleration;
  13.     private float deceleration;
  14.     public float tiltSpeed;                 //The speed at which the player and segway tilts.
  15.     public float fallTilt;                  //The tilt at which the player falls off the segway regardless of their attempts to stay on.
  16.     public bool gravityOn;                  //Whether or not gravity effects the object.
  17.     public float gravityOnset;              //The angle at which gravity begins to affect the player.
  18.     public float gravityIntensity;          //How intense gravity is on the players rotation.
  19.     public float gravityBias;
  20.     //private Quaternion rotGoal;           //The rotation goal for slerp and lerp.
  21.  
  22.     [HideInInspector]
  23.     public float xLock;                     //The x coordinate the player is locked to on straightaways.
  24.  
  25.     public GameObject ragdollPrefab;        //The players' ragdoll prefab to be used on death.
  26.     public string joystickYInput;           //The joystick y coordinate input.
  27.  
  28.     public KeyCode forwardKey;              //Debugging forward key.
  29.     public KeyCode backwardKey;             //Debugging backwards key.
  30.  
  31.     private Rigidbody rb;                   //The rigidbody component.
  32.     private float joystickAlpha = 0.25f;    //The joysticks' alpha.  
  33.     private float leftJoyYPerc;             //The joysticks' x movement.
  34.  
  35.     //Pathing Variables.
  36.     public GameObject pathnodeList;         //The list of nodes which the player travels along.
  37.     [HideInInspector]
  38.     public int currNode;                    //The node the player is currently traveling towards.
  39.     [HideInInspector]
  40.     public int nextNode;                    //The node the player will next travel towards.
  41.     [HideInInspector]
  42.     public int prevNode;                    //The node the player has last reached.
  43.     //[HideInInspector]
  44.     //public RigidbodyConstraints origRestraints; //The constraints which the player must abide by.
  45.     [HideInInspector]
  46.     public bool backwardsLock;              //True == the player cannot go backwards.
  47.     [HideInInspector]
  48.     public Vector3 oldVelocity;             //The old velocity.
  49.     private Vector3 startRot;
  50.  
  51.     public GameObject warningSpot;
  52.  
  53.     //Destruction Variables
  54.     public GameObject segway;
  55.     public GameObject lance;
  56.     public GameObject armourChest;
  57.  
  58.     //Ragdoll Exaggeration
  59.     [HideInInspector]
  60.     public bool hitByLance;
  61.     [HideInInspector]
  62.     public bool resetHitByLance;
  63.     public float hitByLanceTimeLeniency;
  64.     private float exagTimer;
  65.  
  66.     //FMOD
  67.     FMOD.Studio.EventInstance Warning;
  68.     FMOD.Studio.ParameterInstance Player;
  69.     FMOD.Studio.ParameterInstance Urgency;
  70.     FMOD.Studio.ParameterInstance TurnOn;
  71.     FMOD.Studio.ParameterInstance TurnOff;
  72.  
  73.     //Animations
  74.     private Animator animator;
  75.     //private float origAniSpeed;
  76.     public float percLeanAniAngle;      //Percentage the player needs to be in order to play the falling off animations
  77.  
  78.     public AllCrowdController mySide;
  79.     public AllCrowdController oppSide;
  80.  
  81.     void Awake()
  82.     {
  83.         rb = GetComponent<Rigidbody>();
  84.         xLock = rb.position[0];
  85.         startRot = rb.rotation.eulerAngles;
  86.         backwardsLock = false;
  87.         exagTimer = hitByLanceTimeLeniency;
  88.         rb.centerOfMass += new Vector3(0.0f, 1.0f, 0.0f);
  89.  
  90.         //Set the player's nodes.
  91.         prevNode = 0;
  92.         currNode = 1;
  93.         nextNode = 2;
  94.  
  95.         //origRestraints = rb.constraints;
  96.         animator = this.GetComponent<Animator>();
  97.         //origAniSpeed = animator.speed;
  98.         animator.SetFloat("LeanAngle", 0.0f);
  99.     }
  100.  
  101.     void Start()
  102.     {
  103.         //FMOD
  104.         Warning = FMOD_StudioSystem.instance.GetEvent("event:/Segway/Warning");
  105.         if(Warning.getParameter("Player", out Player) != FMOD.RESULT.OK)
  106.         {
  107.             Debug.LogError("player param not found");
  108.             return;
  109.         }
  110.         if(Warning.getParameter("Urgency", out Urgency) != FMOD.RESULT.OK)
  111.         {
  112.             Debug.LogError("urgency param not found");
  113.             return;
  114.         }
  115.         if(Warning.getParameter("TurnOn", out TurnOn) != FMOD.RESULT.OK)
  116.         {
  117.             Debug.LogError("turnon param not found");
  118.             return;
  119.         }
  120.         if(Warning.getParameter("TurnOff", out TurnOff) != FMOD.RESULT.OK)
  121.         {
  122.             Debug.LogError("turnoff param not found");
  123.             return;
  124.         }
  125.         if(tag == "P1")
  126.         {
  127.             Player.setValue(1);
  128.         } else{
  129.             Player.setValue(2);
  130.         }
  131.  
  132.         if(warningSpot == null && GameObject.Find ("FMODListener") != null)
  133.         {
  134.             warningSpot = GameObject.Find("FMODListener");
  135.         }
  136.  
  137.         TurnOn.setValue(1);
  138.         TurnOff.setValue(0);
  139.         Warning.start ();
  140.         TurnOn.setValue(0);
  141.  
  142.         PLAYBACK_STATE playback;
  143.         Warning.getPlaybackState(out playback);
  144.     }
  145.  
  146.     void FixedUpdate()
  147.     {
  148.         Update3DAttributes ();
  149.  
  150.  
  151.         if (!LoopManager.Instance.getLocked(this.gameObject))
  152.         {
  153.             Vector3 dirToCurrNode = (pathnodeList.transform.GetChild(currNode).position - rb.position).normalized;
  154.             leftJoyYPerc = Input.GetAxisRaw(joystickYInput);       //Get the left-stick's Y
  155.  
  156.             SPEED = maxSpeed * Time.fixedDeltaTime;
  157.             acceleration = maxAcceleration * Time.fixedDeltaTime;
  158.  
  159.             //Debug Movement w/o controller
  160.             if (Input.GetKey(forwardKey))
  161.             {
  162.                 leftJoyYPerc = 1.0f;
  163.             }
  164.             else if (Input.GetKey(backwardKey))
  165.             {
  166.                 leftJoyYPerc = -1.0f;
  167.             }
  168.  
  169.             //Stop backwards movement if Pathnode has locked it.
  170.             if (leftJoyYPerc < 0.0f && backwardsLock)
  171.             {
  172.                 leftJoyYPerc = 0.0f;
  173.             }
  174.  
  175.             //Avoid joystick input error with an alpha. (My joystick reads ~0.03 when at default)
  176.             if (leftJoyYPerc >= joystickAlpha)
  177.             {
  178.                 Vector3 possibleVel = rb.velocity + dirToCurrNode * (acceleration * leftJoyYPerc);
  179.                 if (possibleVel.magnitude > SPEED && Vector3.Angle( rb.velocity, dirToCurrNode ) <= 180.0f )              //If we're going faster than the designated max speed.
  180.                 {
  181.                     rb.velocity = dirToCurrNode * SPEED;                  //Set us at the max speed.
  182.                 }
  183.                 else
  184.                 {
  185.                     rb.velocity = possibleVel;
  186.                 }
  187.  
  188.                 //Lean forward depending on the amount the left controllers' stick is pushed.
  189.                 if (leftJoyYPerc * fallTilt >= rb.rotation.eulerAngles[0] || rb.rotation.eulerAngles[0] > fallTilt)
  190.                 {
  191.                     SlerpRot(Quaternion.Euler(leftJoyYPerc * fallTilt, rb.rotation.eulerAngles[1], 0.0f), Time.fixedDeltaTime * tiltSpeed);
  192.                 }
  193.             }
  194.             else if (leftJoyYPerc <= -joystickAlpha)
  195.             {
  196.                 float sp = minSpeed * Time.fixedDeltaTime;
  197.                 deceleration = maxDeceleration * Time.fixedDeltaTime;
  198.                 //Lean backward depending on the amount the left controllers' stick is pulled.
  199.                 Vector3 possibleVel = rb.velocity + dirToCurrNode * (deceleration * leftJoyYPerc);
  200.                 if( possibleVel.magnitude < sp && Vector3.Angle( rb.velocity, dirToCurrNode ) <= 180.0f )
  201.                 {
  202.                     rb.velocity = dirToCurrNode * sp;
  203.                 }
  204.                 else
  205.                 {
  206.                     rb.velocity = possibleVel;
  207.                 }
  208.  
  209.                 if (leftJoyYPerc * fallTilt <= rb.rotation.eulerAngles[0] - 360.0f || rb.rotation.eulerAngles[0] < fallTilt)
  210.                 {
  211.                     SlerpRot(Quaternion.Euler(leftJoyYPerc * fallTilt, rb.rotation.eulerAngles[1], 0.0f), Time.fixedDeltaTime * tiltSpeed);
  212.                 }
  213.             }
  214.  
  215.             if (Mathf.Abs(rb.rotation.eulerAngles[0] - 180.0f) <= 180.0f - fallTilt || Mathf.Abs(rb.rotation.eulerAngles[2] - 180.0f) <= 180.0f - fallTilt) //If the Player is tilting too far off the base rotation.
  216.             {
  217.                 FallOff();                                                                      //Reset the level.
  218.             }
  219.  
  220.             float rotIntensity = (rb.rotation.eulerAngles[0] >= 180.0f) ? (rb.rotation.eulerAngles[0] - 360.0f) / fallTilt : rb.rotation.eulerAngles[0] / fallTilt;
  221.             Urgency.setValue(Mathf.Abs(rotIntensity));
  222.  
  223.             if (Mathf.Abs(rotIntensity) >= percLeanAniAngle)         //Falling
  224.             {
  225.                 //animator.SetFloat("LeanAngle", rotIntensity);
  226.                 animator.SetBool("isFalling", true);
  227.             }
  228.             else
  229.             {
  230.                 animator.SetBool("isFalling", false);
  231.                 //animator.SetFloat("LeanAngle", 0.0f);
  232.             }
  233.  
  234.             //Falls forward/backward gradually because of gravity.
  235.             if (gravityOn)  //Allows for turning off of gravity for easy debugging.
  236.             {
  237.                 if ((rb.rotation.eulerAngles[0] >= gravityOnset) || (rb.rotation.eulerAngles[0] <= 360.0f - gravityOnset))    //If the player is past the point where gravity is applied.
  238.                 {
  239.                     float gBias = (rb.rotation.eulerAngles[0] >= 180.0f) ? -gravityBias : gravityBias;
  240.                     rb.AddRelativeTorque(new Vector3((rotIntensity + gBias) * gravityIntensity * Time.fixedDeltaTime, 0.0f, 0.0f));
  241.                 }
  242.                 else if (Mathf.Abs(leftJoyYPerc) < joystickAlpha)
  243.                 {
  244.                     SlerpRot(Quaternion.Euler(0.0f, rb.rotation.eulerAngles[1], 0.0f), Time.fixedDeltaTime);
  245.                 }
  246.             }
  247.  
  248.             if (Vector3.Angle(transform.forward, rb.velocity) <= 90.0f)
  249.             {
  250.                 rb.drag = 0.0f;
  251.             }
  252.             else
  253.             {
  254.                 rb.drag = 2.0f;
  255.             }
  256.  
  257.             //Orient towards goal node.
  258.             Quaternion lookRot = Quaternion.LookRotation(pathnodeList.transform.GetChild(currNode).position - rb.position, transform.up);
  259.             SlerpRot(Quaternion.Euler(new Vector3(rb.rotation.eulerAngles[0], lookRot.eulerAngles[1], rb.rotation.eulerAngles[2])), Time.fixedDeltaTime * 7.0f);
  260.         }
  261.         else
  262.         {
  263.             SlerpRot(Quaternion.Euler(0.0f, startRot[1], startRot[2]), tiltSpeed * Time.fixedDeltaTime);
  264.             animator.SetBool("isFalling", false);
  265.             //animator.SetFloat("LeanAngle", 0.0f);
  266.         }
  267.  
  268.         if (Vector3.Angle(transform.forward, rb.velocity) <= 90.0f)    //Adjust velocity to go towards next node if knocked backwards.
  269.         {
  270.             rb.velocity = (pathnodeList.transform.GetChild(currNode).position - rb.position).normalized * rb.velocity.magnitude;
  271.         }
  272.         else                                                            //Adjust velocity to go towards previous node if knocked backwards.
  273.         {
  274.             rb.velocity = (pathnodeList.transform.GetChild(prevNode).position - rb.position).normalized * rb.velocity.magnitude;
  275.         }
  276.         oldVelocity = rb.velocity;
  277.  
  278.         rb.rotation = Quaternion.Euler(rb.rotation.eulerAngles[0], startRot[1], startRot[2]);
  279.     }
  280.  
  281.     void Update()
  282.     {
  283.         if( resetHitByLance )
  284.         {
  285.             if( exagTimer <= 0 )
  286.             {
  287.                 resetHitByLance = false;
  288.                 hitByLance = false;
  289.                 exagTimer = hitByLanceTimeLeniency;
  290.             }
  291.             else
  292.             {
  293.                 exagTimer -= Time.deltaTime;
  294.             }
  295.         }
  296.     }
  297.  
  298.     void SlerpRot( Quaternion rotGoal, float delta )
  299.     {
  300.         rb.rotation = Quaternion.Slerp(rb.rotation, rotGoal, delta);
  301.     }
  302.  
  303.     void FallOff()
  304.     {
  305.         mySide.RoundOverFell(oppSide);
  306.  
  307.         TurnOn.setValue(0);
  308.         TurnOff.setValue(1);
  309.         //Detach Segway from player and bring it back to default position.
  310.         IndependentSegway seg = segway.GetComponent<IndependentSegway>();
  311.         if( seg )
  312.         {
  313.             seg.DetachFromPlayer();
  314.         }
  315.  
  316.         //Detach lance from player and let it travel it's own trajectory.
  317.         if (lance)
  318.         {
  319.             lance.transform.parent = null;
  320.             Rigidbody lanceRB = lance.AddComponent<Rigidbody>();
  321.             lance.AddComponent<FallCollision>();
  322.             if (lanceRB)
  323.             {
  324.                 lanceRB.velocity = rb.velocity;
  325.             }
  326.             lance.layer = 0;
  327.         }
  328.  
  329.         if(armourChest)
  330.         {
  331.             chestArmour aDet = armourChest.GetComponent<chestArmour>();
  332.             if( aDet)
  333.             {
  334.                 aDet.breakApart(rb.velocity);
  335.             }
  336.         }
  337.  
  338.         //Delete the player and create a ragdoll to simulate the player falling.
  339.  
  340.         GameObject ragdoll = (GameObject)GameObject.Instantiate( ragdollPrefab, rb.position, rb.rotation );
  341.         ragdoll.tag = gameObject.tag;
  342.  
  343.         if( hitByLance == true )
  344.         {
  345.             PlayerRagdoll pRag = ragdoll.GetComponent<PlayerRagdoll>();
  346.             if( pRag )
  347.             {
  348.                 pRag.knockedOffByLance = true;
  349.             }
  350.         }
  351.  
  352.         ScoreManager.AddScore(this.gameObject, 1);
  353.         LoopManager.Instance.roundEnd();
  354.  
  355.         Destroy( this.gameObject );
  356.     }
  357.  
  358.     public void TurnWarningOff()
  359.     {
  360.         TurnOn.setValue(0);
  361.         TurnOff.setValue(1);
  362.     }
  363.  
  364.     public void TurnWarningOn()
  365.     {
  366.         TurnOn.setValue(0);
  367.         TurnOff.setValue(0);
  368.     }
  369.  
  370.     void Update3DAttributes()
  371.     {
  372.  
  373.         if(warningSpot == null && GameObject.Find ("FMODListener") != null)
  374.         {
  375.             warningSpot = GameObject.Find("FMODListener");
  376.         }
  377.  
  378.         if (Warning != null && Warning.isValid())
  379.         {
  380.             var attributes = FMOD.Studio.UnityUtil.to3DAttributes(warningSpot);        
  381.             if(Warning.set3DAttributes(attributes) != FMOD.RESULT.OK)
  382.             {
  383.                 Debug.LogError("could not set 3D sound attributes");
  384.                 return;
  385.             }
  386.            
  387.         }
  388.     }  
  389.  
  390.     void OnDestroy()
  391.     {
  392.         if(Warning != null)
  393.         {
  394.             Warning.stop (STOP_MODE.IMMEDIATE);
  395.             Warning.release();
  396.         }
  397.     }
  398.  
  399. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement