Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class ClockworkBomb : MonoBehaviour {
- public bool IsDebugging;
- public enum Direction { Up, Down, Left, Right }
- bool IsClockwise;
- Direction MovingDirection;
- float Transition;
- public float Speed;
- public AnimationCurve PositionCurve;
- float Position;
- // Access for rotating sensors.
- public GameObject Sensors;
- // The root position of the movement
- public GameObject Root;
- // The core position which moves in relation to the core.
- public GameObject Core;
- // Used to choose which sensors to use.
- public GameObject ClockwiseRelay;
- public GameObject CounterRelay;
- // Checks if an object is in the next cell
- public ClockworkSensor FrontCheck;
- // Checks if there's anything below the object.
- ClockworkSensor ClingCheck;
- ClockworkSensor LostClingCheck;
- // Checks if it's surrounded.
- // Also used to detect which surface to initiatally spin off of.
- public ClockworkSensor[] IsStuckCheck;
- // Verifies that the cling is officially lost by checking the nearest corner.
- Vector3 CorePosition;
- Direction FrontNext;
- Direction ClingNext;
- bool JustCling;
- float RoundX;
- float RoundY;
- int IsStuckCount;
- bool LostCling;
- // The electricity showing which wall it's clinging to.
- public GameObject WallShock;
- public Clockwork_Animation AnimationAccess;
- public ClockworkExplode ExplodeAccess;
- // This is used to get the values for initialization.
- public P_solidbrick p_solidbrickAccess;
- // Use this for initialization
- void Start ()
- {
- PositionCurve = AnimationCurve.Linear(0, 0, 1, 3.25f);
- CorePosition = Root.transform.position;
- Initialize(p_solidbrickAccess.Orientation == CreatedObject._Orientation.Vertical);
- }
- /// <summary>
- /// Initialize object with values from the p_solidbrickaccess.
- /// </summary>
- /// <param name="IsClockwise">Which direction will the bomb spin?</param>
- public void Initialize(bool Clockwise)
- {
- IsClockwise = Clockwise;
- AnimationAccess.SetAnimation(Clockwise);
- ClingRelay Access;
- if (IsClockwise)
- {
- Access = ClockwiseRelay.GetComponent<ClingRelay>();
- CounterRelay.SetActive(false);
- }
- else
- {
- Access = CounterRelay.GetComponent<ClingRelay>();
- ClockwiseRelay.SetActive(false);
- }
- ClingCheck = Access.ClingCheck;
- LostClingCheck = Access.LostClingCheck;
- #region Check If Stuck
- // If all four sensors are flagged, we know we're stuck and shouldn't bother trying to move.
- int index = 0;
- foreach (ClockworkSensor C in IsStuckCheck)
- {
- if (C.Hitting)
- break;
- index++;
- }
- #endregion
- // Check each cling to see which wall we should move along after being placed.
- switch (index)
- {
- case 0:
- // Left Cling
- if (IsClockwise)
- MovingDirection = Direction.Down;
- else
- MovingDirection = Direction.Up;
- break;
- case 1:
- // Right Cling
- if (IsClockwise)
- MovingDirection = Direction.Up;
- else
- MovingDirection = Direction.Down;
- break;
- case 2:
- // Upwards Cling
- if (IsClockwise)
- MovingDirection = Direction.Left;
- else
- MovingDirection = Direction.Right;
- break;
- case 3:
- // Downwards Cling
- if (IsClockwise)
- MovingDirection = Direction.Right;
- else
- MovingDirection = Direction.Left;
- break;
- }
- }
- private void FixedUpdate()
- {
- // Check to see if the bomb is stuck in an enclosed area as defined above.
- if (CheckStuck() < 4)
- {
- // Keep moving the bomb unless it's exploding, then stop the animation.
- if (!ExplodeAccess.IsExploding)
- {
- // Move forward.
- if (IsDebugging)
- Transition += (Time.deltaTime * 0.1f) * Speed;
- else
- Transition += (PlayerScript.StaticTimeStep * 0.1f) * Speed;
- }
- else
- {
- AnimationAccess.AnimationAccess.Stop();
- }
- // Once we've moved a whole session, adjust sensors and start again.
- if (Transition >= 1)
- {
- Root.transform.position = CorePosition;
- Transition = 0;
- JustCling = false;
- }
- // Forward sensor hit.
- if (FrontCheck.Hitting)
- {
- MovingDirection = FrontNext;
- Round();
- }
- // Cling sensor not hitting.
- if (!ClingCheck.Hitting && !JustCling)
- {
- // Verify that the lost sensor was because of a corner or a destroyed object.
- LostCling = VerifyCling();
- if (!LostCling)
- {
- MovingDirection = ClingNext;
- JustCling = true;
- Round();
- }
- }
- }
- // Adjust the direction of the sensors to match the direction.
- // Set the direction the bomb will turn when either a front or cling sensor has a state change.
- switch (MovingDirection)
- {
- case Direction.Right:
- Sensors.transform.eulerAngles = new Vector3(0, 0, 0);
- if (IsClockwise)
- {
- FrontNext = Direction.Up;
- ClingNext = Direction.Down;
- }
- else
- {
- FrontNext = Direction.Down;
- ClingNext = Direction.Up;
- }
- break;
- case Direction.Up:
- Sensors.transform.eulerAngles = new Vector3(0, 0, 270);
- if (IsClockwise)
- {
- FrontNext = Direction.Left;
- ClingNext = Direction.Right;
- }
- else
- {
- FrontNext = Direction.Right;
- ClingNext = Direction.Left;
- }
- break;
- case Direction.Left:
- Sensors.transform.eulerAngles = new Vector3(0, 0, 180);
- if (IsClockwise)
- {
- FrontNext = Direction.Down;
- ClingNext = Direction.Up;
- }
- else
- {
- FrontNext = Direction.Up;
- ClingNext = Direction.Down;
- }
- break;
- case Direction.Down:
- Sensors.transform.eulerAngles = new Vector3(0, 0, 90);
- if (IsClockwise)
- {
- FrontNext = Direction.Right;
- ClingNext = Direction.Left;
- }
- else
- {
- FrontNext = Direction.Left;
- ClingNext = Direction.Right;
- }
- break;
- }
- #region WallShock Related
- // Set the angle of the wallshock particle effect.
- WallShock.transform.eulerAngles = Sensors.transform.eulerAngles;
- if(IsClockwise)
- WallShock.transform.eulerAngles = new Vector3(0, 0, Sensors.transform.eulerAngles.z);
- else
- WallShock.transform.eulerAngles = new Vector3(0, 0, Sensors.transform.eulerAngles.z + 180);
- #endregion
- // Move the core.
- MoveCore(MovingDirection);
- Core.transform.position = CorePosition;
- // At the end of every fixed update, flip the stuck sensors off.
- // If the sensors are still stuck, they will automatically be reactivated before we observe them.
- ResetStuckSensors();
- }
- // If the cling is ever lost, we can use the verify cling to check and see if we actually lose the platform.
- // If we're just crawling around a corner, the verify cling will always detect wall we're clinging around.
- public bool VerifyCling()
- {
- if (!ClingCheck.Hitting && !LostClingCheck.Hitting)
- return true;
- else
- return false;
- }
- // Movement - Don't touch, they do not rely on clockwise type.
- public void MoveCore(Direction Dir)
- {
- switch (Dir)
- {
- case Direction.Right:
- CorePosition.x = Root.transform.position.x - PositionCurve.Evaluate(Transition);
- break;
- case Direction.Left:
- CorePosition.x = Root.transform.position.x + PositionCurve.Evaluate(Transition);
- break;
- case Direction.Up:
- CorePosition.y = Root.transform.position.y + PositionCurve.Evaluate(Transition);
- break;
- case Direction.Down:
- CorePosition.y = Root.transform.position.y - PositionCurve.Evaluate(Transition);
- break;
- }
- }
- // Snaps the core position to the nearest 3.25 x 3.25
- // Ideally this wouldn't be necessary, but since tiny variations can cause huge issues, we do it as a safey measure.
- public void Round()
- {
- RoundX = Mathf.Round((CorePosition.x / 3.25f)) * 3.25f;
- RoundY = Mathf.Round((CorePosition.y / 3.25f)) * 3.25f;
- CorePosition = new Vector3(RoundX, RoundY, CorePosition.z);
- Root.transform.position = CorePosition;
- //Debug.Log(CorePosition);
- }
- public void ResetStuckSensors()
- {
- foreach(ClockworkSensor C in IsStuckCheck)
- {
- C.Hitting = false;
- }
- }
- // Return the amount of stuck sensors are currently being hit.
- // If all four are hit, we know not to try to move the bomb.
- public int CheckStuck()
- {
- IsStuckCount = 0;
- foreach(ClockworkSensor C in IsStuckCheck)
- {
- if (C.Hitting)
- IsStuckCount++;
- }
- return IsStuckCount;
- }
- }
Add Comment
Please, Sign In to add comment