Advertisement
Guest User

plrFramework

a guest
May 11th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.49 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. //Classes so we don't need a few hundred scripts
  5. internal class input
  6. {
  7.     //Values acessed when declared
  8.     public float Tilth;
  9.     //Debounces
  10.     public bool flipping;
  11.     public bool jumping;
  12.     public bool jumped;
  13.     public bool jchk;
  14.  
  15.     //Get our inputs and overwrite values to persist those inputs for the frame
  16.     public void getInput()
  17.     {
  18.         if (Input.GetKey(KeyCode.D)) //Simple if, elseif, else conditional. GetKey also works for continuous input
  19.         {
  20.             Tilth = Mathf.Lerp(Tilth, 1, Time.smoothDeltaTime * 10); //A lerp for keyboard users, to make it feel smooth
  21.         }
  22.         else if (Input.GetKey(KeyCode.A))
  23.         {
  24.             Tilth = Mathf.Lerp(Tilth, -1, Time.smoothDeltaTime * 10); // ^
  25.         }
  26.         else { Tilth = Mathf.Lerp(Tilth, 0, Time.smoothDeltaTime * 10); } //^^
  27.  
  28.         if (Input.GetKey(KeyCode.Space))
  29.         {
  30.             if (!jumping) { jchk = true; jumping = true; }
  31.         }
  32.     }
  33.  
  34.     //Update the player based on those inputs
  35.     public void updateP(GameObject p,float speed,float jpow)
  36.     {
  37.         //If it's not 0 and currently not completely flipped then
  38.         if (Mathf.Round(Tilth) != 0 && (Tilth < 0 && p.GetComponent<SpriteRenderer>().flipX != true) || (Tilth > 0 && p.GetComponent<SpriteRenderer>().flipX != false))
  39.         {
  40.             flipping = true; //Start flipping, this is a debounce
  41.             p.transform.localScale = Vector3.Lerp(p.transform.localScale, new Vector3(0.02f, .75f, 1), Time.deltaTime * 10); //ANIMOOT [in]
  42.         }
  43.         else { flipping = false;  p.transform.localScale = Vector3.Lerp(p.transform.localScale, new Vector3(.75f, .75f, 1), Time.deltaTime * 10); } //If not flipping then return to normal
  44.  
  45.         //If approximatly done flipping but flipping then actually flip
  46.         if (p.transform.localScale.x < 0.075 && Tilth < 0 && flipping) { p.GetComponent<SpriteRenderer>().flipX = true;}
  47.         if (p.transform.localScale.x < 0.075 && Tilth > 0 && flipping) { p.GetComponent<SpriteRenderer>().flipX = false;}
  48.  
  49.         //Same conditions as above, but now we animate [out]
  50.         if (p.transform.localScale.x < 0.075 && flipping ) {
  51.             p.transform.localScale = Vector3.Lerp(p.transform.localScale, new Vector3(.75f, .75f, 1),Time.deltaTime * 10);
  52.         }
  53.  
  54.         //If approximatly done and flipping then stop flipping
  55.         if ((Mathf.Approximately(.75f, p.transform.localScale.x) || p.transform.localScale.x > .73f) && flipping)
  56.         {
  57.             flipping = false;
  58.         }
  59.  
  60.         if (jumping && !jumped && !jchk)
  61.         {
  62.             p.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jpow));
  63.             jumped = true;
  64.         }
  65.  
  66.         // Set velocity to speed*tilt smoothly
  67.         p.GetComponent<Rigidbody2D>().velocity = Vector2.Lerp(p.GetComponent<Rigidbody2D>().velocity,new Vector2(speed*Tilth, p.GetComponent<Rigidbody2D>().velocity.y),Time.smoothDeltaTime*15);
  68.     }
  69. }
  70.  
  71. internal class camr
  72. {
  73.     //Allow you to get and set the camera
  74.     public Camera cam { get; set; }
  75.  
  76.     //Last camera position, for the camera follow
  77.     private Vector3 lCamP; //Which stops at around the end of the screen, and follows smoothly
  78.  
  79.     //Function for updating the camera position
  80.     public void updateCam(Vector3 newPos)
  81.     {
  82.         cam.transform.position = Vector3.Slerp(lCamP, newPos, Time.smoothDeltaTime*7.5f); //Slerp feels a bit better than Lerp
  83.         lCamP = cam.transform.position; //Overwrite the old position for the last camera position
  84.     }
  85.  
  86.     //Initialization function that I never used
  87.     public camr()
  88.     {      
  89.     }
  90. }
  91.  
  92. internal class physics
  93. {
  94.     public bool grounded(Vector3 position)
  95.     {
  96.         if (Physics2D.Raycast(position + new Vector3(0, -1.5f, 0), Vector2.down, .0025f)) //Raycast to maybe see the ground
  97.         {
  98.             return true;
  99.         }
  100.         else return false;
  101.     }
  102. }
  103.  
  104. //Required stuffs
  105. public class playerFramework : MonoBehaviour {
  106.  
  107.     //Value declaration
  108.     public GameObject plr;
  109.     public Camera camObj;
  110.     public Vector3 camOffset;
  111.     public float movespeed_default;
  112.     public float jumppow_default;
  113.     //Non-configurable values
  114.     private camr cam; //Create our camera controller
  115.     private input controller; //Create our input controller
  116.     private physics phys; //Create our physics controller (Prolly could just be functions but meh)
  117.  
  118.     // Initialization function
  119.     void Start ()
  120.     {
  121.         //Loading the classes
  122.         cam = new camr();
  123.         controller = new input();
  124.         phys = new physics();
  125.  
  126.         //Setting the camera
  127.         cam.cam = camObj;
  128.     }
  129.    
  130.  
  131.     void Update ()
  132.     {
  133.         //Call the updates for things like camera position and getting input
  134.         cam.updateCam(plr.transform.position + camOffset);
  135.         controller.getInput();
  136.         controller.updateP(plr,movespeed_default,jumppow_default);
  137.     }
  138.  
  139.     void LateUpdate()
  140.     {
  141.         if (controller.jchk)
  142.         {
  143.             if (phys.grounded(plr.transform.position))
  144.             {
  145.                 controller.jchk = false;
  146.             }
  147.             else { controller.jumping = false; controller.jumped = false; controller.jchk = false; }
  148.         }
  149.  
  150.         if (controller.jumped && !controller.jchk)
  151.         {
  152.             if (phys.grounded(plr.transform.position))
  153.             {
  154.                 controller.jumping = false;
  155.                 controller.jumped = false;
  156.             }
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement