Advertisement
CassataGames

Player Class

May 8th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.23 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerScript : MonoBehaviour
  5. {
  6.     public PlayerClass Player;
  7.     public float myTimeStep = 0.01f;
  8.  
  9.     public LevelAttributes.GameColors DebugStartColor;
  10.  
  11.     // Use this for initialization
  12.     void Start ()
  13.     {
  14.         Player.CreatePlayer(new Vector3(999,999,999), DebugStartColor, gameObject); // Creates the player class with all its attributes
  15.     }
  16.    
  17.     // Update is called once per frame
  18.     void FixedUpdate ()
  19.     {
  20.         Player.UpdatePlayer(myTimeStep); // All player code takes place in its class
  21.     }
  22.  
  23.     [System.Serializable]
  24.     public class PlayerClass
  25.     {
  26.         // Called at the start of the scene //
  27.         public void CreatePlayer(Vector3 StartingPosition, LevelAttributes.GameColors StartingColor, GameObject PlayerObject)
  28.         {
  29.             Player = PlayerObject;
  30.             //position = StartingPosition; // Sets the starting position of the player
  31.             color = StartingColor; // Sets the players starting color            
  32.             mainModel = GameObject.Find("player model");
  33.         }
  34.         public void ForcePosition(Vector3 ForcedPosition)
  35.         {
  36.             position = ForcedPosition;
  37.             Debug.Log("Forced position.");
  38.             movingDown = true;
  39.         }
  40.  
  41.         public GameObject Player;
  42.         // Base player attributes
  43.         public bool movingDown;
  44.         public PlayerSpeedClass speed;
  45.         public Vector3 position;
  46.         public bool bouncing; // Is the player bouncing?
  47.         public float bounceAmount; // Offset of players movement while bouncing
  48.  
  49.         public LevelAttributes.GameColors color;
  50.         public LevelAttributes.PlayerStates state;
  51.        
  52.         // Prefabs for player
  53.         public GameObject PrefabChangeLight;
  54.         public ChangeLight ChangeLightAccess;
  55.         public PlayerSmokeClass PrefabsSmoke;
  56.         public PlayerDeathClass PrefabsDeath;
  57.         GameObject mainModel;
  58.  
  59.         // Bring level scripts into focus
  60.         public GameObject LevelScripts;
  61.         public LevelControl LevelControlAccess;
  62.  
  63.         // Functions for controlling player
  64.         public void UpdatePlayer(float TimeStep) // Should be called every Update to control player
  65.         {
  66.             #region Set LevelScripts if Null
  67.             if (LevelScripts == null)
  68.             {
  69.                 LevelScripts = GameObject.Find("Level Scripts");
  70.                 LevelControlAccess = LevelScripts.GetComponent<LevelControl>();
  71.             }
  72.             #endregion
  73.             speed.ThrustState(Input.GetButton("Fire1")); // Set thruststate to the button state.
  74.             #region Vertical Movement
  75.             if (movingDown)
  76.             {
  77.                 position.y = position.y + ((speed.VerticalSpeed() * -1) * TimeStep);
  78.             }
  79.             else
  80.             {
  81.                 position.y = position.y + ((speed.VerticalSpeed() * 1) * TimeStep);
  82.             }
  83.             #endregion
  84.             #region Horizontal Movement
  85.             #region When Not Bouncing
  86.             if (!bouncing)
  87.             {
  88.                 if (Input.GetAxis("Horizontal") > 0) // If axis is above 0, add to acceleration value
  89.                 {
  90.                     speed.AccelerationValue = speed.AccelerationValue + (4f * TimeStep);
  91.                     if (speed.AccelerationValue > 1)
  92.                         speed.AccelerationValue = 1;
  93.                 }
  94.                 else if (Input.GetAxis("Horizontal") < 0) // If axis is below 0, subtract from acceleration value
  95.                 {
  96.                     speed.AccelerationValue = speed.AccelerationValue - (4f * TimeStep);
  97.                     if (speed.AccelerationValue < -1)
  98.                         speed.AccelerationValue = -1;
  99.                 }
  100.             }
  101.             #endregion
  102.             #region Brings player speed back to 0
  103.             if (!Input.GetButton("Horizontal")) // If neither axis is being held, bring acceleration value to 0
  104.             {
  105.                 if (speed.AccelerationValue > 0)
  106.                     speed.AccelerationValue = speed.AccelerationValue - (6f * TimeStep);
  107.  
  108.                 if (speed.AccelerationValue < 0)
  109.                     speed.AccelerationValue = speed.AccelerationValue + (6f * TimeStep);
  110.  
  111.                 if (speed.AccelerationValue > -0.2f && speed.AccelerationValue < 0.2f)
  112.                     speed.AccelerationValue = 0f;
  113.             }
  114.             #endregion
  115.             #region When Bouncing
  116.             if (bounceAmount > 0) // If bouncing is above 0, start subtracting
  117.             {
  118.                 bounceAmount = bounceAmount - (6 * TimeStep);
  119.             }
  120.             else if (bounceAmount < 0)
  121.             {
  122.                 bounceAmount = bounceAmount + (6 * TimeStep); // If bouncing is over 0, start adding
  123.             }
  124.             if (bounceAmount > -0.15f && bounceAmount < 0.15f) // If bouncing is near 0, stop bouncing
  125.             {
  126.                 bounceAmount = 0f;
  127.                 bouncing = false;
  128.             }
  129.             #endregion
  130.             // Set these values to our X position
  131.             position.x = position.x + ((speed.AccelerationValue + bounceAmount)* speed.setHorizontal * -1) * TimeStep;
  132.             #endregion
  133.             Player.transform.position = position; // Set our player to the vector we've been using up to this point.
  134.             #region Color Switch
  135.             PrefabsSmoke.ResetSmoke();
  136.             switch (color)
  137.             {
  138.                 case LevelAttributes.GameColors.Red:
  139.                     mainModel.renderer.material.color = Color.red;
  140.                     PrefabsSmoke.redSmoke.particleEmitter.emit = true;
  141.                     break;
  142.                 case LevelAttributes.GameColors.Yellow:
  143.                     mainModel.renderer.material.color = Color.yellow;
  144.                     PrefabsSmoke.yellowSmoke.particleEmitter.emit = true;
  145.                     break;
  146.                 case LevelAttributes.GameColors.Green:
  147.                     mainModel.renderer.material.color = Color.green;
  148.                     PrefabsSmoke.greenSmoke.particleEmitter.emit = true;
  149.                     break;
  150.                 case LevelAttributes.GameColors.Blue:
  151.                     mainModel.renderer.material.color = Color.blue;
  152.                     PrefabsSmoke.blueSmoke.particleEmitter.emit = true;
  153.                     break;
  154.                 case LevelAttributes.GameColors.Purple:
  155.                     mainModel.renderer.material.color = Color.magenta;
  156.                     PrefabsSmoke.purpleSmoke.particleEmitter.emit = true;
  157.                     break;
  158.                 case LevelAttributes.GameColors.Black:
  159.                     mainModel.renderer.material.color = Color.black;
  160.                     break;
  161.                 case LevelAttributes.GameColors.Colorless:
  162.                     mainModel.renderer.material.color = Color.white;
  163.                     break;
  164.             }
  165.             #endregion
  166.         }
  167.         public void ChangeColor(LevelAttributes.GameColors SetColor)
  168.         {
  169.             color = SetColor;
  170.             ChangeLightAccess.BrightFlash();
  171.         }
  172.         public void KillPlayer(LevelAttributes.GameColors DeathColor)
  173.         {
  174.             switch (DeathColor)
  175.             {
  176.                 case LevelAttributes.GameColors.Red:
  177.                     Instantiate(PrefabsDeath.redDeath, position, Quaternion.Euler(new Vector3()));
  178.                     Player.SetActiveRecursively(false);
  179.                     LevelControlAccess.DiedRestart();
  180.                     break;
  181.                 case LevelAttributes.GameColors.Yellow:
  182.                     Instantiate(PrefabsDeath.yellowDeath, position, Quaternion.Euler(new Vector3()));
  183.                     Player.SetActiveRecursively(false);
  184.                     LevelControlAccess.DiedRestart();
  185.                     break;
  186.                 case LevelAttributes.GameColors.Green:
  187.                     Instantiate(PrefabsDeath.greenDeath, position, Quaternion.Euler(new Vector3()));
  188.                     Player.SetActiveRecursively(false);
  189.                     LevelControlAccess.DiedRestart();
  190.                     break;
  191.                 case LevelAttributes.GameColors.Blue:
  192.                     Instantiate(PrefabsDeath.blueDeath, position, Quaternion.Euler(new Vector3()));
  193.                     Player.SetActiveRecursively(false);
  194.                     LevelControlAccess.DiedRestart();
  195.                     break;
  196.                 case LevelAttributes.GameColors.Purple:
  197.                     Instantiate(PrefabsDeath.purpleDeath, position, Quaternion.Euler(new Vector3()));
  198.                     Player.SetActiveRecursively(false);
  199.                     LevelControlAccess.DiedRestart();
  200.                     break;
  201.             }
  202.         }
  203.         public void HitTop() // Called from SensorScript to move down
  204.         {
  205.             movingDown = true;
  206.         }
  207.         public void HitBottom() // Called from SensorScript to move up
  208.         {
  209.             movingDown = false;
  210.         }
  211.         public void HitLeft() // Called when player hits a surface from the left
  212.         {
  213.             bounceAmount = (speed.AccelerationValue * -1) + 0.7f;
  214.             speed.AccelerationValue = 0;
  215.             bouncing = true;
  216.         }
  217.         public void HitRight() // Called when player hits a surface from the right
  218.         {
  219.             bounceAmount = (speed.AccelerationValue * -1) - 0.7f;
  220.             speed.AccelerationValue = 0;
  221.             bouncing = true;
  222.         }
  223.     }
  224.  
  225.     [System.Serializable]
  226.     public class PlayerSmokeClass
  227.     {
  228.         public void ResetSmoke()
  229.         {
  230.             redSmoke.particleEmitter.emit = false;
  231.             yellowSmoke.particleEmitter.emit = false;
  232.             greenSmoke.particleEmitter.emit = false;
  233.             blueSmoke.particleEmitter.emit = false;
  234.             purpleSmoke.particleEmitter.emit = false;
  235.         }
  236.         public GameObject redSmoke;
  237.         public GameObject yellowSmoke;
  238.         public GameObject greenSmoke;
  239.         public GameObject blueSmoke;
  240.         public GameObject purpleSmoke;
  241.     }
  242.     [System.Serializable]
  243.     public class PlayerDeathClass
  244.     {
  245.         public GameObject redDeath;
  246.         public GameObject yellowDeath;
  247.         public GameObject greenDeath;
  248.         public GameObject blueDeath;
  249.         public GameObject purpleDeath;
  250.     }
  251.     [System.Serializable]
  252.     public class PlayerSpeedClass
  253.     {
  254.         public void ThrustState(bool Thrusting) // Called in UpdatePlayer
  255.         {
  256.             if (!Thrusting)
  257.                 realVerticalSpeed = setVertical;       // Normal speed
  258.             else
  259.                 realVerticalSpeed = setVertical_Thrust;// High speed
  260.         }
  261.         public float VerticalSpeed() // Returns the real speed
  262.         {
  263.             return realVerticalSpeed;
  264.         }
  265.         public float GetAccelCurve(float AccelValue)
  266.         {
  267.             return AccelerationCurve.Evaluate(AccelValue);
  268.         }
  269.  
  270.         public float realVerticalSpeed; // The actual speed of the player
  271.  
  272.         public float setVertical; // Sets the normal non-thrusting speed
  273.         public float setVertical_Thrust; // Set the high speed of a thrusting player
  274.         public AnimationCurve AccelerationCurve;
  275.         public float AccelerationValue;
  276.         public float setHorizontal; // Set the horizontal speed        
  277.     }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement