Advertisement
Sumsar

Untitled

Feb 6th, 2016
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.64 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. namespace Game
  5. {
  6.     public class PlayerTool : MonoBehaviour
  7.     {
  8.         private enum Mode
  9.         {
  10.             Gravity,
  11.             Focus
  12.         }
  13.  
  14.         [SerializeField]
  15.         private Transform m_gravityPoint;
  16.  
  17.         private Mode m_mode = Mode.Gravity;
  18.         private HashSet<GameObject> m_affectedObjects = new HashSet<GameObject>();
  19.         private float m_focusBeamDistance = 0;
  20.  
  21.         public void AttractItems()
  22.         {
  23.             if (m_mode == Mode.Gravity)
  24.             {
  25.                 Ray ray = new Ray(GetComponentInChildren<Camera>().transform.position, GetComponentInChildren<Camera>().transform.forward);
  26.  
  27.                 RaycastHit[] hitInfo = Physics.SphereCastAll(ray, 1, 5, Utility.LayerManager.Items);
  28.                 if (hitInfo.Length != 0)
  29.                 {
  30.                     for (int i = 0; i < hitInfo.Length; i++)
  31.                     {
  32.                         if (!m_affectedObjects.Contains(hitInfo[i].collider.gameObject))
  33.                         {
  34.                             m_affectedObjects.Add(hitInfo[i].collider.gameObject);
  35.                             hitInfo[i].collider.gameObject.layer = 11;
  36.                         }
  37.                     }
  38.                 }
  39.             }
  40.             else if (m_mode == Mode.Focus)
  41.             {
  42.                 if (m_affectedObjects.Count == 0)
  43.                 {
  44.                     Ray ray = new Ray(GetComponentInChildren<Camera>().transform.position, GetComponentInChildren<Camera>().transform.forward);
  45.                     RaycastHit hitInfo;
  46.  
  47.                     if (Physics.Raycast(ray, out hitInfo, 5, Utility.LayerManager.Items))
  48.                     {
  49.                         m_affectedObjects.Add(hitInfo.collider.gameObject);
  50.                         m_focusBeamDistance = Vector3.Distance(GetComponentInChildren<Camera>().transform.position, hitInfo.collider.gameObject.transform.position);
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.  
  56.         public void PushObjects(Vector3 _direction)
  57.         {
  58.             if (m_mode == Mode.Gravity)
  59.             {
  60.                 foreach (GameObject item in m_affectedObjects)
  61.                 {
  62.                     item.GetComponent<Rigidbody>().AddForce(_direction.normalized * 5.0f, ForceMode.VelocityChange);
  63.                 }
  64.             }
  65.             else if (m_mode == Mode.Focus)
  66.             {
  67.                 //foreach (GameObject item in m_affectedObjects)
  68.                 //{
  69.                 //    item.GetComponent<Rigidbody>().AddForce(_direction.normalized * 5.0f, ForceMode.VelocityChange);
  70.                 //}
  71.             }
  72.         }
  73.  
  74.         public void ReleaseAllObjects()
  75.         {
  76.             if (m_mode == Mode.Gravity)
  77.             {
  78.                 foreach (GameObject item in m_affectedObjects)
  79.                 {
  80.                     item.layer = 8;
  81.                 }                
  82.             }
  83.             else if(m_mode == Mode.Focus)
  84.             {
  85.                 m_focusBeamDistance = -1;
  86.             }
  87.  
  88.             m_affectedObjects.Clear();
  89.         }
  90.  
  91.         public void UpdateObjects()
  92.         {
  93.             if (m_mode == Mode.Gravity)
  94.             {
  95.                 if (m_affectedObjects.Count != 0)
  96.                 {
  97.                     List<GameObject> removeObjects = new List<GameObject>(10);
  98.                     foreach (GameObject item in m_affectedObjects)
  99.                     {
  100.                         if (Vector3.Distance(item.transform.position, m_gravityPoint.position) > 2)
  101.                         {
  102.                             removeObjects.Add(item);
  103.                         }
  104.  
  105.                         Vector3 pullDirection = (m_gravityPoint.position - item.transform.position);
  106.                         item.GetComponent<Rigidbody>().velocity = (pullDirection * 5.0f);
  107.                     }
  108.  
  109.                     foreach (GameObject item in removeObjects)
  110.                     {
  111.                         item.layer = 8;
  112.                         m_affectedObjects.Remove(item);
  113.                     }
  114.                 }
  115.             }
  116.             else if(m_mode == Mode.Focus)
  117.             {
  118.                 if (m_affectedObjects.Count != 0)
  119.                 {
  120.                     Debug.Log("Updating object");
  121.                     Vector3 targetPosition = GetComponentInChildren<Camera>().transform.forward * m_focusBeamDistance;
  122.                     GameObject targetObject = null;
  123.                     foreach (GameObject item in m_affectedObjects)
  124.                     {
  125.                         targetObject = item;
  126.                         break;
  127.                     }
  128.  
  129.                     Vector3 pullDirection = ((GetComponentInChildren<Camera>().transform.forward * m_focusBeamDistance) - targetObject.transform.position).normalized;
  130.                     targetObject.GetComponent<Rigidbody>().velocity = (pullDirection * 5.0f);
  131.                 }
  132.             }
  133.         }
  134.  
  135.         public void SwitchMode(float _delta)
  136.         {
  137.             ReleaseAllObjects();
  138.             m_mode = m_mode == Mode.Gravity ? Mode.Focus : Mode.Gravity;
  139.         }
  140.  
  141.         void OnDrawGizmos()
  142.         {
  143.             if (m_mode == Mode.Gravity)
  144.             {
  145.                 Gizmos.DrawSphere(m_gravityPoint.position, 0.2f);
  146.                 Gizmos.DrawWireSphere(m_gravityPoint.position, 2);
  147.             }
  148.             else if (m_mode == Mode.Focus)
  149.             {
  150.                 Gizmos.DrawSphere(GetComponentInChildren<Camera>().transform.forward, 0.1f);
  151.             }
  152.  
  153.         }
  154.  
  155.         void OnGUI()
  156.         {
  157.             GUILayout.Label( "\n" + m_mode.ToString());
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement