Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerScript : MonoBehaviour
- {
- public PlayerClass Player;
- public float myTimeStep = 0.01f;
- public LevelAttributes.GameColors DebugStartColor;
- // Use this for initialization
- void Start ()
- {
- Player.CreatePlayer(new Vector3(999,999,999), DebugStartColor, gameObject); // Creates the player class with all its attributes
- }
- // Update is called once per frame
- void FixedUpdate ()
- {
- Player.UpdatePlayer(myTimeStep); // All player code takes place in its class
- }
- [System.Serializable]
- public class PlayerClass
- {
- // Called at the start of the scene //
- public void CreatePlayer(Vector3 StartingPosition, LevelAttributes.GameColors StartingColor, GameObject PlayerObject)
- {
- Player = PlayerObject;
- //position = StartingPosition; // Sets the starting position of the player
- color = StartingColor; // Sets the players starting color
- mainModel = GameObject.Find("player model");
- }
- public void ForcePosition(Vector3 ForcedPosition)
- {
- position = ForcedPosition;
- Debug.Log("Forced position.");
- movingDown = true;
- }
- public GameObject Player;
- // Base player attributes
- public bool movingDown;
- public PlayerSpeedClass speed;
- public Vector3 position;
- public bool bouncing; // Is the player bouncing?
- public float bounceAmount; // Offset of players movement while bouncing
- public LevelAttributes.GameColors color;
- public LevelAttributes.PlayerStates state;
- // Prefabs for player
- public GameObject PrefabChangeLight;
- public ChangeLight ChangeLightAccess;
- public PlayerSmokeClass PrefabsSmoke;
- public PlayerDeathClass PrefabsDeath;
- GameObject mainModel;
- // Bring level scripts into focus
- public GameObject LevelScripts;
- public LevelControl LevelControlAccess;
- // Functions for controlling player
- public void UpdatePlayer(float TimeStep) // Should be called every Update to control player
- {
- #region Set LevelScripts if Null
- if (LevelScripts == null)
- {
- LevelScripts = GameObject.Find("Level Scripts");
- LevelControlAccess = LevelScripts.GetComponent<LevelControl>();
- }
- #endregion
- speed.ThrustState(Input.GetButton("Fire1")); // Set thruststate to the button state.
- #region Vertical Movement
- if (movingDown)
- {
- position.y = position.y + ((speed.VerticalSpeed() * -1) * TimeStep);
- }
- else
- {
- position.y = position.y + ((speed.VerticalSpeed() * 1) * TimeStep);
- }
- #endregion
- #region Horizontal Movement
- #region When Not Bouncing
- if (!bouncing)
- {
- if (Input.GetAxis("Horizontal") > 0) // If axis is above 0, add to acceleration value
- {
- speed.AccelerationValue = speed.AccelerationValue + (4f * TimeStep);
- if (speed.AccelerationValue > 1)
- speed.AccelerationValue = 1;
- }
- else if (Input.GetAxis("Horizontal") < 0) // If axis is below 0, subtract from acceleration value
- {
- speed.AccelerationValue = speed.AccelerationValue - (4f * TimeStep);
- if (speed.AccelerationValue < -1)
- speed.AccelerationValue = -1;
- }
- }
- #endregion
- #region Brings player speed back to 0
- if (!Input.GetButton("Horizontal")) // If neither axis is being held, bring acceleration value to 0
- {
- if (speed.AccelerationValue > 0)
- speed.AccelerationValue = speed.AccelerationValue - (6f * TimeStep);
- if (speed.AccelerationValue < 0)
- speed.AccelerationValue = speed.AccelerationValue + (6f * TimeStep);
- if (speed.AccelerationValue > -0.2f && speed.AccelerationValue < 0.2f)
- speed.AccelerationValue = 0f;
- }
- #endregion
- #region When Bouncing
- if (bounceAmount > 0) // If bouncing is above 0, start subtracting
- {
- bounceAmount = bounceAmount - (6 * TimeStep);
- }
- else if (bounceAmount < 0)
- {
- bounceAmount = bounceAmount + (6 * TimeStep); // If bouncing is over 0, start adding
- }
- if (bounceAmount > -0.15f && bounceAmount < 0.15f) // If bouncing is near 0, stop bouncing
- {
- bounceAmount = 0f;
- bouncing = false;
- }
- #endregion
- // Set these values to our X position
- position.x = position.x + ((speed.AccelerationValue + bounceAmount)* speed.setHorizontal * -1) * TimeStep;
- #endregion
- Player.transform.position = position; // Set our player to the vector we've been using up to this point.
- #region Color Switch
- PrefabsSmoke.ResetSmoke();
- switch (color)
- {
- case LevelAttributes.GameColors.Red:
- mainModel.renderer.material.color = Color.red;
- PrefabsSmoke.redSmoke.particleEmitter.emit = true;
- break;
- case LevelAttributes.GameColors.Yellow:
- mainModel.renderer.material.color = Color.yellow;
- PrefabsSmoke.yellowSmoke.particleEmitter.emit = true;
- break;
- case LevelAttributes.GameColors.Green:
- mainModel.renderer.material.color = Color.green;
- PrefabsSmoke.greenSmoke.particleEmitter.emit = true;
- break;
- case LevelAttributes.GameColors.Blue:
- mainModel.renderer.material.color = Color.blue;
- PrefabsSmoke.blueSmoke.particleEmitter.emit = true;
- break;
- case LevelAttributes.GameColors.Purple:
- mainModel.renderer.material.color = Color.magenta;
- PrefabsSmoke.purpleSmoke.particleEmitter.emit = true;
- break;
- case LevelAttributes.GameColors.Black:
- mainModel.renderer.material.color = Color.black;
- break;
- case LevelAttributes.GameColors.Colorless:
- mainModel.renderer.material.color = Color.white;
- break;
- }
- #endregion
- }
- public void ChangeColor(LevelAttributes.GameColors SetColor)
- {
- color = SetColor;
- ChangeLightAccess.BrightFlash();
- }
- public void KillPlayer(LevelAttributes.GameColors DeathColor)
- {
- switch (DeathColor)
- {
- case LevelAttributes.GameColors.Red:
- Instantiate(PrefabsDeath.redDeath, position, Quaternion.Euler(new Vector3()));
- Player.SetActiveRecursively(false);
- LevelControlAccess.DiedRestart();
- break;
- case LevelAttributes.GameColors.Yellow:
- Instantiate(PrefabsDeath.yellowDeath, position, Quaternion.Euler(new Vector3()));
- Player.SetActiveRecursively(false);
- LevelControlAccess.DiedRestart();
- break;
- case LevelAttributes.GameColors.Green:
- Instantiate(PrefabsDeath.greenDeath, position, Quaternion.Euler(new Vector3()));
- Player.SetActiveRecursively(false);
- LevelControlAccess.DiedRestart();
- break;
- case LevelAttributes.GameColors.Blue:
- Instantiate(PrefabsDeath.blueDeath, position, Quaternion.Euler(new Vector3()));
- Player.SetActiveRecursively(false);
- LevelControlAccess.DiedRestart();
- break;
- case LevelAttributes.GameColors.Purple:
- Instantiate(PrefabsDeath.purpleDeath, position, Quaternion.Euler(new Vector3()));
- Player.SetActiveRecursively(false);
- LevelControlAccess.DiedRestart();
- break;
- }
- }
- public void HitTop() // Called from SensorScript to move down
- {
- movingDown = true;
- }
- public void HitBottom() // Called from SensorScript to move up
- {
- movingDown = false;
- }
- public void HitLeft() // Called when player hits a surface from the left
- {
- bounceAmount = (speed.AccelerationValue * -1) + 0.7f;
- speed.AccelerationValue = 0;
- bouncing = true;
- }
- public void HitRight() // Called when player hits a surface from the right
- {
- bounceAmount = (speed.AccelerationValue * -1) - 0.7f;
- speed.AccelerationValue = 0;
- bouncing = true;
- }
- }
- [System.Serializable]
- public class PlayerSmokeClass
- {
- public void ResetSmoke()
- {
- redSmoke.particleEmitter.emit = false;
- yellowSmoke.particleEmitter.emit = false;
- greenSmoke.particleEmitter.emit = false;
- blueSmoke.particleEmitter.emit = false;
- purpleSmoke.particleEmitter.emit = false;
- }
- public GameObject redSmoke;
- public GameObject yellowSmoke;
- public GameObject greenSmoke;
- public GameObject blueSmoke;
- public GameObject purpleSmoke;
- }
- [System.Serializable]
- public class PlayerDeathClass
- {
- public GameObject redDeath;
- public GameObject yellowDeath;
- public GameObject greenDeath;
- public GameObject blueDeath;
- public GameObject purpleDeath;
- }
- [System.Serializable]
- public class PlayerSpeedClass
- {
- public void ThrustState(bool Thrusting) // Called in UpdatePlayer
- {
- if (!Thrusting)
- realVerticalSpeed = setVertical; // Normal speed
- else
- realVerticalSpeed = setVertical_Thrust;// High speed
- }
- public float VerticalSpeed() // Returns the real speed
- {
- return realVerticalSpeed;
- }
- public float GetAccelCurve(float AccelValue)
- {
- return AccelerationCurve.Evaluate(AccelValue);
- }
- public float realVerticalSpeed; // The actual speed of the player
- public float setVertical; // Sets the normal non-thrusting speed
- public float setVertical_Thrust; // Set the high speed of a thrusting player
- public AnimationCurve AccelerationCurve;
- public float AccelerationValue;
- public float setHorizontal; // Set the horizontal speed
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement