Advertisement
CassataGames

Clockwork Bomb

May 21st, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.08 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 IsDebug;
  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 bool SetPlayer;
  62.     public PlayerScript PlayerAccess; // This is really important for being able to determin if the game started yet.
  63.     public P_solidbrick p_solidbrickAccess;
  64.     public bool IsPlaying;
  65.  
  66.     public ClockworkExplode ExplodeAccess;
  67.  
  68.     // Use this for initialization
  69.     void Start ()
  70.     {
  71.         PositionCurve = AnimationCurve.Linear(0, 0, 1, 3.25f);
  72.         CorePosition = Root.transform.position;
  73.         Initialize(p_solidbrickAccess.Orientation == CreatedObject._Orientation.Vertical);
  74.     }
  75.  
  76.     /// <summary>
  77.     /// Called once we know it's safe to start moving with the proper spin.
  78.     /// </summary>
  79.     /// <param name="IsClockwise">Which direction will the bomb spin?</param>
  80.     public void Initialize(bool Clockwise)
  81.     {
  82.         IsClockwise = Clockwise;
  83.         AnimationAccess.SetAnimation(Clockwise);
  84.         ClingRelay Access;
  85.         if (IsClockwise)
  86.         {
  87.             Access = ClockwiseRelay.GetComponent<ClingRelay>();
  88.             CounterRelay.SetActive(false);
  89.         }
  90.         else
  91.         {
  92.             Access = CounterRelay.GetComponent<ClingRelay>();
  93.             ClockwiseRelay.SetActive(false);
  94.         }
  95.         ClingCheck = Access.ClingCheck;
  96.         LostClingCheck = Access.LostClingCheck;
  97.  
  98.         #region Check If Stuck
  99.         // If all four sensors are flagged, we know we're stuck and shouldn't bother trying to move.
  100.         int index = 0;
  101.         foreach (ClockworkSensor C in IsStuckCheck)
  102.         {
  103.             if (C.Hitting)
  104.                 break;
  105.             index++;
  106.         }
  107.         #endregion
  108.  
  109.         // Check each cling to see which wall we should move along after being placed.
  110.         switch (index)
  111.         {
  112.             case 0:
  113.                 // Left Cling
  114.                 if (IsClockwise)
  115.                     MovingDirection = Direction.Down;
  116.                 else
  117.                     MovingDirection = Direction.Up;
  118.                 break;
  119.             case 1:
  120.                 // Right Cling
  121.                 if (IsClockwise)
  122.                     MovingDirection = Direction.Up;
  123.                 else
  124.                     MovingDirection = Direction.Down;
  125.                 break;
  126.             case 2:
  127.                 // Upwards Cling
  128.                 if (IsClockwise)
  129.                     MovingDirection = Direction.Left;
  130.                 else
  131.                     MovingDirection = Direction.Right;
  132.                 break;
  133.             case 3:
  134.                 // Downwards Cling
  135.                 if (IsClockwise)
  136.                     MovingDirection = Direction.Right;
  137.                 else
  138.                     MovingDirection = Direction.Left;
  139.                 break;
  140.         }
  141.     }
  142.  
  143.     private void FixedUpdate()
  144.     {
  145.         if (!IsDebug)
  146.         {
  147.             if (SetPlayer)
  148.                 if (PlayerAccess != null)
  149.                     IsPlaying = !PlayerAccess.IsPaused;
  150.                 else
  151.                     print("PlayerScript Access not found on " + gameObject);
  152.             else
  153.             {
  154.                 PlayerAccess = GameObject.Find("Player").GetComponent<PlayerScript>();
  155.                 SetPlayer = true;
  156.             }
  157.         }
  158.         // Check to see if the bomb is stuck in an enclosed area as defined above.
  159.         if (CheckStuck() < 4)
  160.         {
  161.             // Keep moving the bomb unless it's exploding, then stop the animation.
  162.             if (!ExplodeAccess.IsExploding)
  163.             {
  164.                 // Move forward.
  165.                 if (!IsDebug && IsPlaying)
  166.                     Transition += (PlayerAccess.myTimeStep * 0.1f) * Speed;
  167.                 if (IsDebug)
  168.                     Transition += (Time.deltaTime * 0.1f) * Speed;
  169.             }
  170.             else
  171.             {
  172.                 AnimationAccess.AnimationAccess.Stop();
  173.             }
  174.             // Once we've moved a whole session, adjust sensors and start again.
  175.             if (Transition >= 1)
  176.             {
  177.                 Root.transform.position = CorePosition;
  178.                 Transition = 0;
  179.                 JustCling = false;
  180.             }
  181.             // Forward sensor hit.
  182.             if (FrontCheck.Hitting)
  183.             {
  184.                 MovingDirection = FrontNext;
  185.                 Round();
  186.             }
  187.             // Cling sensor not hitting.
  188.             if (!ClingCheck.Hitting && !JustCling)
  189.             {
  190.                 // Verify that the lost sensor was because of a corner or a destroyed object.
  191.                 LostCling = VerifyCling();
  192.                 if (!LostCling)
  193.                 {
  194.                     MovingDirection = ClingNext;
  195.                     JustCling = true;
  196.                     Round();
  197.                 }
  198.             }
  199.         }
  200.  
  201.         // Adjust the direction of the sensors to match the direction.
  202.         // Set the direction the bomb will turn when either a front or cling sensor has a state change.
  203.         switch (MovingDirection)
  204.         {
  205.             case Direction.Right:
  206.                 Sensors.transform.eulerAngles = new Vector3(0, 0, 0);
  207.                 if (IsClockwise)
  208.                 {
  209.                     FrontNext = Direction.Up;
  210.                     ClingNext = Direction.Down;
  211.                 }
  212.                 else
  213.                 {
  214.                     FrontNext = Direction.Down;
  215.                     ClingNext = Direction.Up;
  216.                 }
  217.                 break;
  218.             case Direction.Up:
  219.                 Sensors.transform.eulerAngles = new Vector3(0, 0, 270);
  220.                 if (IsClockwise)
  221.                 {
  222.                     FrontNext = Direction.Left;
  223.                     ClingNext = Direction.Right;
  224.                 }
  225.                 else
  226.                 {
  227.                     FrontNext = Direction.Right;
  228.                     ClingNext = Direction.Left;
  229.                 }
  230.                 break;
  231.             case Direction.Left:
  232.                 Sensors.transform.eulerAngles = new Vector3(0, 0, 180);
  233.                 if (IsClockwise)
  234.                 {
  235.                     FrontNext = Direction.Down;
  236.                     ClingNext = Direction.Up;
  237.                 }
  238.                 else
  239.                 {
  240.                     FrontNext = Direction.Up;
  241.                     ClingNext = Direction.Down;
  242.                 }
  243.                 break;
  244.             case Direction.Down:
  245.                 Sensors.transform.eulerAngles = new Vector3(0, 0, 90);
  246.                 if (IsClockwise)
  247.                 {
  248.                     FrontNext = Direction.Right;
  249.                     ClingNext = Direction.Left;
  250.                 }
  251.                 else
  252.                 {
  253.                     FrontNext = Direction.Left;
  254.                     ClingNext = Direction.Right;
  255.                 }
  256.                 break;
  257.         }
  258.  
  259.         #region WallShock Related
  260.         // Set the angle of the wallshock particle effect.
  261.         WallShock.transform.eulerAngles = Sensors.transform.eulerAngles;
  262.                
  263.         if(IsClockwise)
  264.             WallShock.transform.eulerAngles = new Vector3(0, 0, Sensors.transform.eulerAngles.z);
  265.         else
  266.             WallShock.transform.eulerAngles = new Vector3(0, 0, Sensors.transform.eulerAngles.z + 180);
  267.         #endregion
  268.  
  269.         // Move the core.
  270.         MoveCore(MovingDirection);
  271.         Core.transform.position = CorePosition;
  272.  
  273.         // At the end of every fixed update, flip the stuck sensors off.
  274.         // If the sensors are still stuck, they will automatically be reactivated before we observe them.
  275.         ResetStuckSensors();
  276.     }
  277.  
  278.     // If the cling is ever lost, we can use the verify cling to check and see if we actually lose the platform.
  279.     // If we're just crawling around a corner, the verify cling will always detect wall we're clinging around.
  280.     public bool VerifyCling()
  281.     {
  282.         if (!ClingCheck.Hitting && !LostClingCheck.Hitting)
  283.             return true;
  284.         else
  285.             return false;
  286.     }
  287.  
  288.     // Movement - Don't touch, they do not rely on clockwise type.
  289.     public void MoveCore(Direction Dir)
  290.     {
  291.         switch (Dir)
  292.         {
  293.             case Direction.Right:
  294.                 CorePosition.x = Root.transform.position.x - PositionCurve.Evaluate(Transition);
  295.                 break;
  296.             case Direction.Left:
  297.                 CorePosition.x = Root.transform.position.x + PositionCurve.Evaluate(Transition);
  298.                 break;
  299.             case Direction.Up:
  300.                 CorePosition.y = Root.transform.position.y + PositionCurve.Evaluate(Transition);
  301.                 break;
  302.             case Direction.Down:
  303.                 CorePosition.y = Root.transform.position.y - PositionCurve.Evaluate(Transition);
  304.                 break;
  305.         }
  306.     }
  307.  
  308.     // Snaps the core position to the nearest 3.25 x 3.25
  309.     // Ideally this wouldn't be necessary, but since tiny variations can cause huge issues, we do it as a safey measure.
  310.     public void Round()
  311.     {
  312.         RoundX = Mathf.Round((CorePosition.x / 3.25f)) * 3.25f;
  313.         RoundY = Mathf.Round((CorePosition.y / 3.25f)) * 3.25f;
  314.         CorePosition = new Vector3(RoundX, RoundY, CorePosition.z);
  315.         //Debug.Log(CorePosition);
  316.     }
  317.  
  318.     public void ResetStuckSensors()
  319.     {
  320.         foreach(ClockworkSensor C in IsStuckCheck)
  321.         {
  322.             C.Hitting = false;
  323.         }
  324.     }
  325.  
  326.     // Return the amount of stuck sensors are currently being hit.
  327.     // If all four are hit, we know not to try to move the bomb.
  328.     public int CheckStuck()
  329.     {
  330.         IsStuckCount = 0;
  331.         foreach(ClockworkSensor C in IsStuckCheck)
  332.         {
  333.             if (C.Hitting)
  334.                 IsStuckCount++;
  335.         }
  336.         return IsStuckCount;
  337.     }
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement