CassataGames

Clockwork Bomb

May 24th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.56 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ClockworkBomb : MonoBehaviour {
  6.  
  7.     public bool IsDebugging;
  8.  
  9.     public enum Direction { Up, Down, Left, Right }
  10.  
  11.     bool IsClockwise;
  12.     Direction MovingDirection;
  13.  
  14.     float Transition;
  15.  
  16.     public float Speed;
  17.  
  18.     public AnimationCurve PositionCurve;
  19.     float Position;
  20.  
  21.     // Access for rotating sensors.
  22.     public GameObject Sensors;
  23.     // The root position of the movement
  24.     public GameObject Root;
  25.     // The core position which moves in relation to the core.
  26.     public GameObject Core;
  27.  
  28.     // Used to choose which sensors to use.
  29.     public GameObject ClockwiseRelay;
  30.     public GameObject CounterRelay;
  31.  
  32.     // Checks if an object is in the next cell
  33.     public ClockworkSensor FrontCheck;
  34.     // Checks if there's anything below the object.
  35.     ClockworkSensor ClingCheck;
  36.     ClockworkSensor LostClingCheck;
  37.  
  38.     // Checks if it's surrounded.
  39.     // Also used to detect which surface to initiatally spin off of.
  40.     public ClockworkSensor[] IsStuckCheck;
  41.     // Verifies that the cling is officially lost by checking the nearest corner.
  42.  
  43.     Vector3 CorePosition;
  44.  
  45.     Direction FrontNext;
  46.     Direction ClingNext;
  47.  
  48.     bool JustCling;
  49.  
  50.     float RoundX;
  51.     float RoundY;
  52.  
  53.     int IsStuckCount;
  54.  
  55.     bool LostCling;
  56.  
  57.     // The electricity showing which wall it's clinging to.
  58.     public GameObject WallShock;
  59.     public Clockwork_Animation AnimationAccess;
  60.  
  61.     public ClockworkExplode ExplodeAccess;
  62.  
  63.     // This is used to get the values for initialization.
  64.     public P_solidbrick p_solidbrickAccess;
  65.  
  66.     // Use this for initialization
  67.     void Start ()
  68.     {
  69.         PositionCurve = AnimationCurve.Linear(0, 0, 1, 3.25f);
  70.         CorePosition = Root.transform.position;
  71.  
  72.         Initialize(p_solidbrickAccess.Orientation == CreatedObject._Orientation.Vertical);
  73.     }
  74.  
  75.     /// <summary>
  76.     /// Initialize object with values from the p_solidbrickaccess.
  77.     /// </summary>
  78.     /// <param name="IsClockwise">Which direction will the bomb spin?</param>
  79.     public void Initialize(bool Clockwise)
  80.     {
  81.         IsClockwise = Clockwise;
  82.         AnimationAccess.SetAnimation(Clockwise);
  83.         ClingRelay Access;
  84.         if (IsClockwise)
  85.         {
  86.             Access = ClockwiseRelay.GetComponent<ClingRelay>();
  87.             CounterRelay.SetActive(false);
  88.         }
  89.         else
  90.         {
  91.             Access = CounterRelay.GetComponent<ClingRelay>();
  92.             ClockwiseRelay.SetActive(false);
  93.         }
  94.         ClingCheck = Access.ClingCheck;
  95.         LostClingCheck = Access.LostClingCheck;
  96.  
  97.         #region Check If Stuck
  98.         // If all four sensors are flagged, we know we're stuck and shouldn't bother trying to move.
  99.         int index = 0;
  100.         foreach (ClockworkSensor C in IsStuckCheck)
  101.         {
  102.             if (C.Hitting)
  103.                 break;
  104.             index++;
  105.         }
  106.         #endregion
  107.  
  108.         // Check each cling to see which wall we should move along after being placed.
  109.         switch (index)
  110.         {
  111.             case 0:
  112.                 // Left Cling
  113.                 if (IsClockwise)
  114.                     MovingDirection = Direction.Down;
  115.                 else
  116.                     MovingDirection = Direction.Up;
  117.                 break;
  118.             case 1:
  119.                 // Right Cling
  120.                 if (IsClockwise)
  121.                     MovingDirection = Direction.Up;
  122.                 else
  123.                     MovingDirection = Direction.Down;
  124.                 break;
  125.             case 2:
  126.                 // Upwards Cling
  127.                 if (IsClockwise)
  128.                     MovingDirection = Direction.Left;
  129.                 else
  130.                     MovingDirection = Direction.Right;
  131.                 break;
  132.             case 3:
  133.                 // Downwards Cling
  134.                 if (IsClockwise)
  135.                     MovingDirection = Direction.Right;
  136.                 else
  137.                     MovingDirection = Direction.Left;
  138.                 break;
  139.         }
  140.     }
  141.  
  142.     private void FixedUpdate()
  143.     {
  144.         // Check to see if the bomb is stuck in an enclosed area as defined above.
  145.         if (CheckStuck() < 4)
  146.         {
  147.             // Keep moving the bomb unless it's exploding, then stop the animation.
  148.             if (!ExplodeAccess.IsExploding)
  149.             {
  150.                 // Move forward.
  151.                 if (IsDebugging)
  152.                     Transition += (Time.deltaTime * 0.1f) * Speed;
  153.                 else
  154.                     Transition += (PlayerScript.StaticTimeStep * 0.1f) * Speed;
  155.             }
  156.             else
  157.             {
  158.                 AnimationAccess.AnimationAccess.Stop();
  159.             }
  160.             // Once we've moved a whole session, adjust sensors and start again.
  161.             if (Transition >= 1)
  162.             {
  163.                 Root.transform.position = CorePosition;
  164.                 Transition = 0;
  165.                 JustCling = false;
  166.             }
  167.             // Forward sensor hit.
  168.             if (FrontCheck.Hitting)
  169.             {
  170.                 MovingDirection = FrontNext;
  171.                 Round();
  172.             }
  173.             // Cling sensor not hitting.
  174.             if (!ClingCheck.Hitting && !JustCling)
  175.             {
  176.                 // Verify that the lost sensor was because of a corner or a destroyed object.
  177.                 LostCling = VerifyCling();
  178.                 if (!LostCling)
  179.                 {
  180.                     MovingDirection = ClingNext;
  181.                     JustCling = true;
  182.                     Round();
  183.                 }
  184.             }
  185.         }
  186.  
  187.         // Adjust the direction of the sensors to match the direction.
  188.         // Set the direction the bomb will turn when either a front or cling sensor has a state change.
  189.         switch (MovingDirection)
  190.         {
  191.             case Direction.Right:
  192.                 Sensors.transform.eulerAngles = new Vector3(0, 0, 0);
  193.                 if (IsClockwise)
  194.                 {
  195.                     FrontNext = Direction.Up;
  196.                     ClingNext = Direction.Down;
  197.                 }
  198.                 else
  199.                 {
  200.                     FrontNext = Direction.Down;
  201.                     ClingNext = Direction.Up;
  202.                 }
  203.                 break;
  204.             case Direction.Up:
  205.                 Sensors.transform.eulerAngles = new Vector3(0, 0, 270);
  206.                 if (IsClockwise)
  207.                 {
  208.                     FrontNext = Direction.Left;
  209.                     ClingNext = Direction.Right;
  210.                 }
  211.                 else
  212.                 {
  213.                     FrontNext = Direction.Right;
  214.                     ClingNext = Direction.Left;
  215.                 }
  216.                 break;
  217.             case Direction.Left:
  218.                 Sensors.transform.eulerAngles = new Vector3(0, 0, 180);
  219.                 if (IsClockwise)
  220.                 {
  221.                     FrontNext = Direction.Down;
  222.                     ClingNext = Direction.Up;
  223.                 }
  224.                 else
  225.                 {
  226.                     FrontNext = Direction.Up;
  227.                     ClingNext = Direction.Down;
  228.                 }
  229.                 break;
  230.             case Direction.Down:
  231.                 Sensors.transform.eulerAngles = new Vector3(0, 0, 90);
  232.                 if (IsClockwise)
  233.                 {
  234.                     FrontNext = Direction.Right;
  235.                     ClingNext = Direction.Left;
  236.                 }
  237.                 else
  238.                 {
  239.                     FrontNext = Direction.Left;
  240.                     ClingNext = Direction.Right;
  241.                 }
  242.                 break;
  243.         }
  244.  
  245.         #region WallShock Related
  246.         // Set the angle of the wallshock particle effect.
  247.         WallShock.transform.eulerAngles = Sensors.transform.eulerAngles;
  248.                
  249.         if(IsClockwise)
  250.             WallShock.transform.eulerAngles = new Vector3(0, 0, Sensors.transform.eulerAngles.z);
  251.         else
  252.             WallShock.transform.eulerAngles = new Vector3(0, 0, Sensors.transform.eulerAngles.z + 180);
  253.         #endregion
  254.  
  255.         // Move the core.
  256.         MoveCore(MovingDirection);
  257.         Core.transform.position = CorePosition;
  258.  
  259.         // At the end of every fixed update, flip the stuck sensors off.
  260.         // If the sensors are still stuck, they will automatically be reactivated before we observe them.
  261.         ResetStuckSensors();
  262.     }
  263.  
  264.     // If the cling is ever lost, we can use the verify cling to check and see if we actually lose the platform.
  265.     // If we're just crawling around a corner, the verify cling will always detect wall we're clinging around.
  266.     public bool VerifyCling()
  267.     {
  268.         if (!ClingCheck.Hitting && !LostClingCheck.Hitting)
  269.             return true;
  270.         else
  271.             return false;
  272.     }
  273.  
  274.     // Movement - Don't touch, they do not rely on clockwise type.
  275.     public void MoveCore(Direction Dir)
  276.     {
  277.         switch (Dir)
  278.         {
  279.             case Direction.Right:
  280.                 CorePosition.x = Root.transform.position.x - PositionCurve.Evaluate(Transition);
  281.                 break;
  282.             case Direction.Left:
  283.                 CorePosition.x = Root.transform.position.x + PositionCurve.Evaluate(Transition);
  284.                 break;
  285.             case Direction.Up:
  286.                 CorePosition.y = Root.transform.position.y + PositionCurve.Evaluate(Transition);
  287.                 break;
  288.             case Direction.Down:
  289.                 CorePosition.y = Root.transform.position.y - PositionCurve.Evaluate(Transition);
  290.                 break;
  291.         }
  292.     }
  293.  
  294.     // Snaps the core position to the nearest 3.25 x 3.25
  295.     // Ideally this wouldn't be necessary, but since tiny variations can cause huge issues, we do it as a safey measure.
  296.     public void Round()
  297.     {
  298.         RoundX = Mathf.Round((CorePosition.x / 3.25f)) * 3.25f;
  299.         RoundY = Mathf.Round((CorePosition.y / 3.25f)) * 3.25f;
  300.         CorePosition = new Vector3(RoundX, RoundY, CorePosition.z);
  301.         Root.transform.position = CorePosition;
  302.         //Debug.Log(CorePosition);
  303.     }
  304.  
  305.     public void ResetStuckSensors()
  306.     {
  307.         foreach(ClockworkSensor C in IsStuckCheck)
  308.         {
  309.             C.Hitting = false;
  310.         }
  311.     }
  312.  
  313.     // Return the amount of stuck sensors are currently being hit.
  314.     // If all four are hit, we know not to try to move the bomb.
  315.     public int CheckStuck()
  316.     {
  317.         IsStuckCount = 0;
  318.         foreach(ClockworkSensor C in IsStuckCheck)
  319.         {
  320.             if (C.Hitting)
  321.                 IsStuckCount++;
  322.         }
  323.         return IsStuckCount;
  324.     }
  325. }
Add Comment
Please, Sign In to add comment