Advertisement
CassataGames

Clinging Bombs

Mar 27th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.12 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TickerbombControl : MonoBehaviour {
  5.  
  6.     public bool SetPlayer;
  7.     public PlayerScript PlayerAccess; // This is really important for being able to determin if the game started yet.
  8.     public bool IsPlaying;
  9.  
  10.     public enum Clock { Clockwise, Counterclockwise }
  11.     public Clock ClockType;
  12.     public bool SetAnimation;
  13.  
  14.     // There are two ways the tickerbomb detects walls.
  15.     // * Initial is for detecting our initial position.
  16.     public GameObject InitialGameObject; // So we can disable it when we're done.
  17.     public ColliderTester InitialAccess;
  18.     // * Clinging is for riding on the outside of walls.
  19.     public ColliderTester ClingingAccess;
  20.     // * Forward is for detecting when we hit a wall.
  21.     public ColliderTester ForwardAccess;
  22.     // This is for detecting if we hit ticker bricks we're clinging to
  23.     public ColliderTester ClingTickerBrick;
  24.  
  25.     public GameObject ClingDetectBrickClockwise;
  26.     public GameObject ClingDetectBrickCounterClockwise;
  27.  
  28.     public bool Initialize;     // This is used because we "Start" is called before we know our edge.
  29.     private bool FirstCheck;    // This is so we know we set our initial movement.
  30.  
  31.     private bool TopEdge;
  32.     private bool LeftEdge;
  33.     private bool BottomEdge;
  34.     private bool RightEdge;
  35.    
  36.     public enum Movement { NotSet, MovingLeft, MovingDown, MovingRight, MovingUp, }
  37.     public Movement MovementPath;
  38.     public bool ForwardHit;
  39.  
  40.     public float speed;
  41.     private SpeedControl.SpeedDirections Speed;
  42.    
  43.     public enum ClingAngles { Bottom, Left, Top, Right }
  44.     public ClingAngles ClingAngle;
  45.     public bool ClingHitting;
  46.  
  47.     public bool ResetForward;
  48.     public bool ResetCling;
  49.  
  50.     public bool EXPLODE;
  51.     public Color NotTriggered;
  52.     public Color Triggered;
  53.     public Renderer LightTrigger;
  54.     public GameObject TickerExplodeTrigger;
  55.     public Animation RotatingAnimation;
  56.  
  57.     public GameObject ExplodeEffect;
  58.     private float timeExplode;
  59.     public float ExplodeTime; // Set time to explode.
  60.  
  61.     //-------------------------//
  62.     //---[[   CLOCKWISE   ]]---//
  63.     //-------------------------//
  64.     public void SpinningClockwise()
  65.     {
  66.         // Sets the animation [*]
  67.         if (!SetAnimation)
  68.         {
  69.             ClingDetectBrickClockwise.SetActive(true);
  70.             ClingDetectBrickCounterClockwise.SetActive(false);
  71.             ClingTickerBrick = ClingDetectBrickClockwise.GetComponent<ColliderTester>();
  72.  
  73.             RotatingAnimation.Play("Spinning Spikes_Clockwise");
  74.             SetAnimation = true;
  75.         }
  76.         // Sets the clinging angle depending on which way we're moving. [**]
  77.         switch (MovementPath)
  78.         {
  79.             case Movement.MovingDown:
  80.                 ClingAngle = ClingAngles.Left;
  81.                 break;
  82.             case Movement.MovingLeft:
  83.                 ClingAngle = ClingAngles.Top;
  84.                 break;
  85.             case Movement.MovingUp:
  86.                 ClingAngle = ClingAngles.Right;
  87.                 break;
  88.             case Movement.MovingRight:
  89.                 ClingAngle = ClingAngles.Bottom;
  90.                 break;
  91.         }
  92.         // Set our initial movement path after we're sure it's settled in. [***]
  93.         if (Initialize & !FirstCheck)
  94.         {
  95.             if (TopEdge)
  96.                 MovementPath = Movement.MovingLeft;
  97.             else if (LeftEdge)
  98.                 MovementPath = Movement.MovingDown;
  99.             else if (BottomEdge)
  100.                 MovementPath = Movement.MovingRight;
  101.             else if (RightEdge)
  102.                 MovementPath = Movement.MovingUp;
  103.             else
  104.                 MovementPath = Movement.MovingRight;
  105.             ClingHitting = true;
  106.             InitialGameObject.SetActive(false);
  107.             FirstCheck = true;
  108.         }
  109.         CheckForward();
  110.         CheckCling();
  111.     }
  112.     //-------------------------//
  113.     //-[[ COUNTER CLOCKWISE ]]-//
  114.     //-------------------------//
  115.     public void SpinningCounterClockwise()
  116.     {
  117.         // Sets the animation [*]
  118.         if (!SetAnimation)
  119.         {
  120.             ClingDetectBrickCounterClockwise.SetActive(true);
  121.             ClingDetectBrickClockwise.SetActive(false);
  122.             ClingTickerBrick = ClingDetectBrickCounterClockwise.GetComponent<ColliderTester>();
  123.  
  124.             RotatingAnimation.Play("Spinning Spikes_CounterClockwise");
  125.             SetAnimation = true;
  126.         }
  127.         // Sets the clinging angle depending on which way we're moving. [**]
  128.         switch (MovementPath)
  129.         {
  130.             case Movement.MovingDown:
  131.                 ClingAngle = ClingAngles.Right;
  132.                 break;
  133.             case Movement.MovingLeft:
  134.                 ClingAngle = ClingAngles.Bottom;
  135.                 break;
  136.             case Movement.MovingUp:
  137.                 ClingAngle = ClingAngles.Left;
  138.                 break;
  139.             case Movement.MovingRight:
  140.                 ClingAngle = ClingAngles.Top;
  141.                 break;
  142.         }
  143.         // Set our initial movement path after we're sure it's settled in. [***]
  144.         if (Initialize & !FirstCheck)
  145.         {
  146.             if (TopEdge)
  147.                 MovementPath = Movement.MovingRight;
  148.             else if (LeftEdge)
  149.                 MovementPath = Movement.MovingUp;
  150.             else if (BottomEdge)
  151.                 MovementPath = Movement.MovingLeft;
  152.             else if (RightEdge)
  153.                 MovementPath = Movement.MovingUp;
  154.             else
  155.                 MovementPath = Movement.MovingLeft;
  156.             ClingHitting = true;
  157.             InitialGameObject.SetActive(false);
  158.             FirstCheck = true;
  159.         }
  160.         CheckForward();
  161.         CheckCling();
  162.     }
  163.  
  164.     public void CheckEdges()
  165.     {
  166.         TopEdge = InitialAccess.HitTop;
  167.         BottomEdge = InitialAccess.HitBottom;
  168.         LeftEdge = InitialAccess.HitLeft;
  169.         RightEdge = InitialAccess.HitRight;
  170.     }
  171.     public void SetSensors()
  172.     {
  173.         switch (ClingAngle)
  174.         {
  175.             case ClingAngles.Bottom:
  176.                 ClingingAccess.transform.rotation = RotationalShorthand.bottom;
  177.                 // Set forward sensor
  178.                 if (ClockType == Clock.Clockwise)
  179.                     ForwardAccess.transform.rotation = RotationalShorthand.right; // Clockwise
  180.                 else
  181.                     ForwardAccess.transform.rotation = RotationalShorthand.left; // Counter Clockwise
  182.                 break;
  183.             case ClingAngles.Left:
  184.                 ClingingAccess.transform.rotation = RotationalShorthand.left;
  185.                 // Set forward sensor
  186.                 if (ClockType == Clock.Clockwise)
  187.                     ForwardAccess.transform.rotation = RotationalShorthand.bottom; // Clockwise
  188.                 else
  189.                     ForwardAccess.transform.rotation = RotationalShorthand.top; // Counter Clockwise
  190.                 break;
  191.             case ClingAngles.Top:
  192.                 ClingingAccess.transform.rotation = RotationalShorthand.top;
  193.                 // Set forward sensor
  194.                 if (ClockType == Clock.Clockwise)
  195.                     ForwardAccess.transform.rotation = RotationalShorthand.left; // Clockwise
  196.                 else
  197.                     ForwardAccess.transform.rotation = RotationalShorthand.right; // Counter Clockwise
  198.                 break;
  199.             case ClingAngles.Right:
  200.                 ClingingAccess.transform.rotation = RotationalShorthand.right;
  201.                 // Set forward sensor
  202.                 if (ClockType == Clock.Clockwise)
  203.                     ForwardAccess.transform.rotation = RotationalShorthand.top; // Clockwise
  204.                 else
  205.                     ForwardAccess.transform.rotation = RotationalShorthand.bottom; // Counter Clockwise
  206.                 break;
  207.         }
  208.     }
  209.     public void CheckForward()
  210.     {
  211.         if (!ForwardHit)
  212.             ResetForward = false;
  213.  
  214.         ForwardHit = ForwardAccess.HittingAnything;
  215.         if (IsPlaying & ForwardHit & Initialize & !ResetForward)
  216.         {
  217.  
  218.             if (ForwardAccess.CheckForTag("TickerSurface"))
  219.                 EXPLODE = true;
  220.  
  221.             switch (MovementPath)
  222.             {
  223.                 case Movement.MovingRight:
  224.                     if (ClockType == Clock.Clockwise)
  225.                         MovementPath = Movement.MovingUp;
  226.                     else
  227.                         MovementPath = Movement.MovingDown;
  228.                     break;
  229.                 case Movement.MovingUp:
  230.                     if (ClockType == Clock.Clockwise)
  231.                         MovementPath = Movement.MovingLeft;
  232.                     else
  233.                         MovementPath = Movement.MovingRight;
  234.                     break;
  235.                 case Movement.MovingLeft:
  236.                     if (ClockType == Clock.Clockwise)
  237.                         MovementPath = Movement.MovingDown;
  238.                     else
  239.                         MovementPath = Movement.MovingUp;
  240.                     break;
  241.                 case Movement.MovingDown:
  242.                     if (ClockType == Clock.Clockwise)
  243.                         MovementPath = Movement.MovingRight;
  244.                     else
  245.                         MovementPath = Movement.MovingLeft;
  246.                     break;
  247.             }
  248.             ResetForward = true;
  249.         }
  250.     }
  251.     public void CheckCling()
  252.     {
  253.         if (ClingHitting)
  254.             ResetCling = false;
  255.  
  256.         ClingHitting = ClingingAccess.HittingAnything;
  257.         if (IsPlaying & !ClingHitting & Initialize & !ResetCling)
  258.         {
  259.             switch (MovementPath)
  260.             {
  261.                 case Movement.MovingUp:
  262.                     if (ClockType == Clock.Clockwise)
  263.                         MovementPath = Movement.MovingRight;
  264.                     else
  265.                         MovementPath = Movement.MovingLeft;
  266.                     break;
  267.                 case Movement.MovingRight:
  268.                     if (ClockType == Clock.Clockwise)
  269.                         MovementPath = Movement.MovingDown;
  270.                     else
  271.                         MovementPath = Movement.MovingUp;
  272.                     break;
  273.                 case Movement.MovingDown:
  274.                     if (ClockType == Clock.Clockwise)
  275.                         MovementPath = Movement.MovingLeft;
  276.                     else
  277.                         MovementPath = Movement.MovingRight;
  278.                     break;
  279.                 case Movement.MovingLeft:
  280.                     if (ClockType == Clock.Clockwise)
  281.                         MovementPath = Movement.MovingUp;
  282.                     else
  283.                         MovementPath = Movement.MovingDown;
  284.                     break;
  285.             }
  286.             ResetCling = true;
  287.         }
  288.  
  289.         if (ClingTickerBrick.HittingAnything)
  290.         {
  291.             if (ClingTickerBrick.CheckForTag("TickerSurface"))
  292.                 EXPLODE = true;
  293.         }
  294.     }
  295.     public void PerformMovement()
  296.     {
  297.         switch (MovementPath)
  298.         {
  299.             case Movement.MovingRight:
  300.                 transform.Translate(Speed.RightSpeed);
  301.                 break;
  302.             case Movement.MovingUp:
  303.                 transform.Translate(Speed.UpSpeed);
  304.                 break;
  305.             case Movement.MovingDown:
  306.                 transform.Translate(Speed.DownSpeed);
  307.                 break;
  308.             case Movement.MovingLeft:
  309.                 transform.Translate(Speed.LeftSpeed);
  310.                 break;
  311.         }
  312.     }
  313.     public void CheckExplode()
  314.     {
  315.         if (EXPLODE)
  316.         {
  317.             LightTrigger.material.SetColor("_Color", Triggered);
  318.             Instantiate(TickerExplodeTrigger, transform.position, transform.rotation);
  319.             timeExplode = timeExplode + Time.deltaTime;
  320.             RotatingAnimation.Stop();
  321.         }
  322.         else
  323.         {
  324.             LightTrigger.material.SetColor("_Color", NotTriggered);
  325.         }
  326.  
  327.         if (timeExplode > ExplodeTime)
  328.         {
  329.             Instantiate(ExplodeEffect, transform.position, transform.rotation);
  330.             Destroy(gameObject);
  331.         }
  332.     }
  333.  
  334.     void FixedUpdate ()
  335.     {
  336.         if (SetPlayer)
  337.             if (PlayerAccess != null)
  338.                 IsPlaying = !PlayerAccess.IsPaused;
  339.             else
  340.                 print("PlayerScript Access not found on " + gameObject);
  341.         else
  342.         {
  343.             PlayerAccess = GameObject.Find("Player").GetComponent<PlayerScript>();
  344.             SetPlayer = true;
  345.         }
  346.  
  347.         Speed = new SpeedControl.SpeedDirections(speed); // Set our quickhand
  348.  
  349.         if (!Initialize) // Don't do anything until we're initialized.
  350.             Initialize = InitialAccess.Initialized;
  351.  
  352.         CheckExplode(); // Checks to see if it's exploding.
  353.         CheckEdges(); // Checks the edge collisions
  354.         SetSensors(); // Set the position of our sensors
  355.         switch (ClockType) // Set everything in motion
  356.         {
  357.             case Clock.Clockwise:
  358.                 SpinningClockwise();
  359.                 break;
  360.             case Clock.Counterclockwise:
  361.                 SpinningCounterClockwise();
  362.                 break;
  363.         }
  364.         if (IsPlaying & !EXPLODE)
  365.             PerformMovement();
  366.     }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement