| 1 | using System.Collections; |
| 2 | using System.Collections.Generic; |
| 3 | using UnityEngine; |
| 4 | |
| 5 | public class Connectable : Grabbable |
| 6 | { |
| 7 | [SerializeField] private float connectRange = 0.15f; |
| 8 | [SerializeField] private Vector3 connectionOffset; |
| 9 | [SerializeField] private Vector3 rotationOffset; |
| 10 | [SerializeField] private Connector.ConnectorTypes ConnectorType; |
| 11 | |
| 12 | public bool ConnectTest; |
| 13 | |
| 14 | public Connector ConnectedConnector; |
| 15 | |
| 16 | void Update() |
| 17 | { |
| 18 | if(ConnectTest) |
| 19 | { |
| 20 | ConnectTest = false; |
| 21 | TryConnect(); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | public virtual void Activate() { } |
| 26 | |
| 27 | public virtual void OnConnect() |
| 28 | { |
| 29 | SFXSpawner.StartSFX(SFXSpawner.Sounds.Connect, transform.position); |
| 30 | } |
| 31 | |
| 32 | public virtual void OnDisconnect() |
| 33 | { |
| 34 | SFXSpawner.StartSFX(SFXSpawner.Sounds.Disconnect, transform.position); |
| 35 | } |
| 36 | |
| 37 | public override void Grab(ObjectGrabber grabber) |
| 38 | { |
| 39 | base.Grab(grabber); |
| 40 | |
| 41 | if(ConnectedConnector != null) |
| 42 | { |
| 43 | ConnectedConnector.Disconnect(this); |
| 44 | OnDisconnect(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public override void Release(Vector3 velocity, Vector3 torque) |
| 49 | { |
| 50 | base.Release(velocity, torque); |
| 51 | |
| 52 | TryConnect(); |
| 53 | } |
| 54 | |
| 55 | public void Eject() |
| 56 | { |
| 57 | if(ConnectedConnector != null) |
| 58 | { |
| 59 | ConnectedConnector = null; |
| 60 | UnfreezeRigidbody(); |
| 61 | |
| 62 | rigidbody.AddTorque(Random.insideUnitSphere * 10); |
| 63 | rigidbody.AddForce(Random.insideUnitSphere * 100); |
| 64 | |
| 65 | OnDisconnect(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | private void TryConnect() |
| 70 | { |
| 71 | Connector[] connectors = FindObjectsOfType<Connector>(); |
| 72 | Connector found = null; |
| 73 | float dist = connectRange; |
| 74 | |
| 75 | for (int i = 0; i < connectors.Length; i++) |
| 76 | { |
| 77 | float aDist = Vector3.Distance(transform.position, connectors[i].transform.position); |
| 78 | |
| 79 | if (aDist < dist && connectors[i].Connected == null && connectors[i].ConnectorType == ConnectorType) |
| 80 | { |
| 81 | dist = aDist; |
| 82 | found = connectors[i]; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (found != null) |
| 87 | { |
| 88 | ConnectedConnector = found; |
| 89 | rigidbody.isKinematic = true; |
| 90 | found.Connect(this); |
| 91 | |
| 92 | rigidbody.constraints = RigidbodyConstraints.FreezeAll; |
| 93 | |
| 94 | Vector3 offset = (found.ConnectionPoint.right * connectionOffset.x) + (found.ConnectionPoint.up * connectionOffset.y) + (found.ConnectionPoint.forward * connectionOffset.z); |
| 95 | |
| 96 | transform.position = found.ConnectionPoint.position + offset; |
| 97 | transform.rotation = found.ConnectionPoint.rotation; |
| 98 | |
| 99 | transform.Rotate(rotationOffset); |
| 100 | |
| 101 | OnConnect(); |
| 102 | } |
| 103 | } |
| 104 | } |