Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- namespace Game
- {
- public class PlayerTool : MonoBehaviour
- {
- private enum Mode
- {
- Gravity,
- Focus
- }
- [SerializeField]
- private Transform m_gravityPoint;
- private Mode m_mode = Mode.Gravity;
- private HashSet<GameObject> m_affectedObjects = new HashSet<GameObject>();
- private float m_focusBeamDistance = 0;
- public void AttractItems()
- {
- if (m_mode == Mode.Gravity)
- {
- Ray ray = new Ray(GetComponentInChildren<Camera>().transform.position, GetComponentInChildren<Camera>().transform.forward);
- RaycastHit[] hitInfo = Physics.SphereCastAll(ray, 1, 5, Utility.LayerManager.Items);
- if (hitInfo.Length != 0)
- {
- for (int i = 0; i < hitInfo.Length; i++)
- {
- if (!m_affectedObjects.Contains(hitInfo[i].collider.gameObject))
- {
- m_affectedObjects.Add(hitInfo[i].collider.gameObject);
- hitInfo[i].collider.gameObject.layer = 11;
- }
- }
- }
- }
- else if (m_mode == Mode.Focus)
- {
- if (m_affectedObjects.Count == 0)
- {
- Ray ray = new Ray(GetComponentInChildren<Camera>().transform.position, GetComponentInChildren<Camera>().transform.forward);
- RaycastHit hitInfo;
- if (Physics.Raycast(ray, out hitInfo, 5, Utility.LayerManager.Items))
- {
- m_affectedObjects.Add(hitInfo.collider.gameObject);
- m_focusBeamDistance = Vector3.Distance(GetComponentInChildren<Camera>().transform.position, hitInfo.collider.gameObject.transform.position);
- }
- }
- }
- }
- public void PushObjects(Vector3 _direction)
- {
- if (m_mode == Mode.Gravity)
- {
- foreach (GameObject item in m_affectedObjects)
- {
- item.GetComponent<Rigidbody>().AddForce(_direction.normalized * 5.0f, ForceMode.VelocityChange);
- }
- }
- else if (m_mode == Mode.Focus)
- {
- //foreach (GameObject item in m_affectedObjects)
- //{
- // item.GetComponent<Rigidbody>().AddForce(_direction.normalized * 5.0f, ForceMode.VelocityChange);
- //}
- }
- }
- public void ReleaseAllObjects()
- {
- if (m_mode == Mode.Gravity)
- {
- foreach (GameObject item in m_affectedObjects)
- {
- item.layer = 8;
- }
- }
- else if(m_mode == Mode.Focus)
- {
- m_focusBeamDistance = -1;
- }
- m_affectedObjects.Clear();
- }
- public void UpdateObjects()
- {
- if (m_mode == Mode.Gravity)
- {
- if (m_affectedObjects.Count != 0)
- {
- List<GameObject> removeObjects = new List<GameObject>(10);
- foreach (GameObject item in m_affectedObjects)
- {
- if (Vector3.Distance(item.transform.position, m_gravityPoint.position) > 2)
- {
- removeObjects.Add(item);
- }
- Vector3 pullDirection = (m_gravityPoint.position - item.transform.position);
- item.GetComponent<Rigidbody>().velocity = (pullDirection * 5.0f);
- }
- foreach (GameObject item in removeObjects)
- {
- item.layer = 8;
- m_affectedObjects.Remove(item);
- }
- }
- }
- else if(m_mode == Mode.Focus)
- {
- if (m_affectedObjects.Count != 0)
- {
- Debug.Log("Updating object");
- Vector3 targetPosition = GetComponentInChildren<Camera>().transform.forward * m_focusBeamDistance;
- GameObject targetObject = null;
- foreach (GameObject item in m_affectedObjects)
- {
- targetObject = item;
- break;
- }
- Vector3 pullDirection = ((GetComponentInChildren<Camera>().transform.forward * m_focusBeamDistance) - targetObject.transform.position).normalized;
- targetObject.GetComponent<Rigidbody>().velocity = (pullDirection * 5.0f);
- }
- }
- }
- public void SwitchMode(float _delta)
- {
- ReleaseAllObjects();
- m_mode = m_mode == Mode.Gravity ? Mode.Focus : Mode.Gravity;
- }
- void OnDrawGizmos()
- {
- if (m_mode == Mode.Gravity)
- {
- Gizmos.DrawSphere(m_gravityPoint.position, 0.2f);
- Gizmos.DrawWireSphere(m_gravityPoint.position, 2);
- }
- else if (m_mode == Mode.Focus)
- {
- Gizmos.DrawSphere(GetComponentInChildren<Camera>().transform.forward, 0.1f);
- }
- }
- void OnGUI()
- {
- GUILayout.Label( "\n" + m_mode.ToString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement