GibTreaty

Object Roster

Jun 10th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using UnityEngine.Events;
  4.  
  5. public class ObjectRoster : MonoBehaviour {
  6.  
  7.     public bool isControlling;
  8.  
  9.     public float spacing = 1;
  10.     public float angleOffset = 180;
  11.  
  12.     public float snapDelay = .5f;
  13.     public float snapTimer;
  14.  
  15.     [SerializeField]
  16.     GameObject _previousGameObject;
  17.  
  18.     public RosterEvent onChangeObject;
  19.  
  20.     List<GameObject> gameObjects = new List<GameObject>();
  21.  
  22.     #region Properties
  23.     protected int CurrentTurretIndex {
  24.         get {
  25.             if(gameObjects.Count == 0) return -1;
  26.  
  27.             return (int)(NearestAngle / SectionAngle);
  28.         }
  29.     }
  30.  
  31.     public GameObject CurrentGameObject {
  32.         get {
  33.             return CurrentTurretIndex > -1 ? gameObjects[CurrentTurretIndex] : null;
  34.         }
  35.     }
  36.  
  37.     public float NearestAngle {
  38.         get { return WrapAngle(Mathf.Round(transform.localEulerAngles.y / SectionAngle) * SectionAngle); }
  39.     }
  40.  
  41.     public float SectionAngle {
  42.         get { return gameObjects.Count > 0 ? 360f / gameObjects.Count : 0; }
  43.     }
  44.     #endregion
  45.  
  46.     public virtual void AddGameObject(GameObject gameObject) {
  47.         gameObjects.Add(gameObject);
  48.     }
  49.  
  50.     protected virtual void Update() {
  51.         if(isControlling) {
  52.             float scroll = Input.GetAxis("Mouse ScrollWheel");
  53.  
  54.             if(Mathf.Abs(scroll) > float.Epsilon)
  55.                 Spin(Input.GetAxis("Mouse ScrollWheel") * 5);
  56.         }
  57.  
  58.         if(gameObjects.Count > 0) {
  59.             if(snapTimer > 0)
  60.                 snapTimer = Mathf.Max(snapTimer - Time.deltaTime, 0);
  61.  
  62.             if(snapTimer == 0) {
  63.                 Quaternion toAngle = Quaternion.Euler(transform.localEulerAngles.x, NearestAngle, transform.localEulerAngles.z);
  64.  
  65.                 transform.rotation = Quaternion.RotateTowards(transform.localRotation, toAngle, Time.deltaTime * Quaternion.Angle(transform.localRotation, toAngle) * Mathf.PI);
  66.  
  67.                 SetCurrentGameObject();
  68.             }
  69.  
  70.             float angle = SectionAngle;
  71.  
  72.             for(int i = 0; i < gameObjects.Count; i++) {
  73.                 float currentAngle = angle * i;
  74.                 Vector3 position = new Vector3(Mathf.Sin(currentAngle * Mathf.Deg2Rad), 0, Mathf.Cos(currentAngle * Mathf.Deg2Rad));
  75.  
  76.                 gameObjects[i].transform.position = transform.position + (transform.rotation * Quaternion.Euler(0, angleOffset, 0) * position * spacing);
  77.             }
  78.         }
  79.     }
  80.  
  81.     public virtual void Spin(float speed) {
  82.         if(snapTimer == 0)
  83.             SetCurrentGameObject(null);
  84.  
  85.         snapTimer = snapDelay;
  86.  
  87.         transform.Rotate(0, speed, 0, Space.Self);
  88.     }
  89.  
  90.     protected virtual void SetCurrentGameObject() {
  91.         SetCurrentGameObject(CurrentGameObject);
  92.     }
  93.     protected virtual void SetCurrentGameObject(GameObject gameObject) {
  94.         if(_previousGameObject == gameObject) return;
  95.  
  96.         _previousGameObject = gameObject;
  97.  
  98.         if(onChangeObject != null) onChangeObject.Invoke(gameObject);
  99.     }
  100.  
  101.     protected static float WrapAngle(float angle) {
  102.         float value = angle %= 360;
  103.  
  104.         if(value < 0) angle += 360;
  105.  
  106.         return angle;
  107.     }
  108.  
  109.     [System.Serializable]
  110.     public class RosterEvent : UnityEvent<GameObject> { }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment