void SwitchPart(int partIndex) { if (currentPartIndex == partIndex || partIndex >= partPrefabs.Length) { return; } if (cursor != null) { Destroy(cursor.gameObject); } if (placingPart) { currentHitPart = null; placingPart = false; } currentPartIndex = partIndex; cursor = GameObject.Instantiate <AttachablePart> (partPrefabs [currentPartIndex]); cursor.Initialize(true); List <Renderer> renderers = cursor.GetRenderers(); for (int i = 0; i < renderers.Count; i++) { renderers [i].material = cursorMaterial; } cursor.SetCollidersEnabled(false); partSelectionUI.HighlightText(currentPartIndex); }
void HandleOnJointBreak(AttachablePart argBrokenPart) { if (argBrokenPart == connectedPart) { AttachablePart.OnJointBreak -= HandleOnJointBreak; Destroy(fixedJoint); } }
public void SetupJoint(AttachablePart argConnectedPart) { connectedPart = argConnectedPart; fixedJoint = this.gameObject.AddComponent <FixedJoint>(); fixedJoint.connectedBody = connectedPart.GetComponent <Rigidbody>(); fixedJoint.enableCollision = false; PlayerController.instance.AttachPart(this); AttachablePart.OnJointBreak += HandleOnJointBreak; }
void FireJointBreak() { AttachablePart.OnJointBreak -= HandleOnJointBreak; Destroy(fixedJoint); if (AttachablePart.OnJointBreak != null) { AttachablePart.OnJointBreak(this); } }
void GenerateKeysToParts() { for (int i = 0; i < attachedParts.Count; i++) { if (attachedParts [i].interactive) { KeyCode keyToUse = GenerateRandomKeyCode(); AttachablePart part = attachedParts[i]; activeParts.Add(new Part(keyToUse, part)); GameObject keyMarker = Instantiate(keyMarkerCanvas, attachedParts [i].transform.position, Quaternion.identity) as GameObject; keyMarker.transform.SetParent(attachedParts [i].gameObject.transform); keyMarker.transform.localPosition = new Vector3(0f, 0f, 2.0f); KeyMarker keyMark = keyMarker.GetComponent <KeyMarker>(); keyMark.markerText.text = keyToUse.ToString(); if (part is Thruster) { Thruster thruster = (Thruster)part; // Lateral thrusters will have x or y thrust if (thruster.thrustAxis.x != 0 || thruster.thrustAxis.y != 0) { keyMark.markerText.color = Color.yellow; } else { keyMark.markerText.color = Color.green; } } else if (part is TurretScript) { keyMark.markerText.color = Color.red; } } } }
// Update is called once per frame void Update() { CheckInput(); if (placingPart) { if (PlayerController.instance.gameStarted) { Destroy(cursor.gameObject); cursor = null; currentHitPart = null; placingPart = false; } else { if (Input.GetKey(KeyCode.Q)) { cursor.transform.RotateAround(cursor.transform.position, cursor.transform.forward, -rotationSpeed * Time.deltaTime); } else if (Input.GetKey(KeyCode.E)) { cursor.transform.RotateAround(cursor.transform.position, cursor.transform.forward, rotationSpeed * Time.deltaTime); } } } if (!PlayerController.instance.gameStarted) { if (!placingPart) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 1000f, layerMask.value, QueryTriggerInteraction.Ignore)) { Debug.DrawLine(transform.position, hit.point, Color.red, 3.0f); currentHitPart = hit.collider.GetComponent <AttachablePart> (); if (currentHitPart == null) { currentHitPart = hit.collider.transform.parent.GetComponent <AttachablePart> (); } if (currentHitPart != null) { cursor.gameObject.SetActive(true); cursor.transform.position = hit.point; cursor.transform.forward = hit.normal; if (Input.GetMouseButtonDown(0)) { placingPart = true; } } } else { cursor.gameObject.SetActive(false); } } else if (placingPart) { if (Input.GetMouseButtonDown(0)) { AttachablePart newPart = GameObject.Instantiate <AttachablePart> (partPrefabs[currentPartIndex]); newPart.transform.position = cursor.transform.position; newPart.transform.rotation = cursor.transform.rotation; newPart.Initialize(false); newPart.SetupJoint(currentHitPart); currentHitPart = null; placingPart = false; } } } }
public void AttachPart(AttachablePart partToAttach) { attachedParts.Add(partToAttach); }
public Part(KeyCode _key, AttachablePart _partToAdd) { keyToActivate = _key; attachedPart = _partToAdd; }