public void addGate(Gate g) { if (!contains(g)) { GateDrawerLoader l = GameObject.Instantiate(GateDrawerItemPref, transform).GetComponent <GateDrawerLoader>(); l.gate = g; l.load(); gateLoaders.Add(l); } }
public void completeLevel(Level l) { clear(); text.text = string.Format(str, l.levelName); for (int i = 0; i < l.rewards.Length; i++) { GateDrawerLoader loader = GameObject.Instantiate(gateDrawerItemPref, rewardsPanel.transform).GetComponent <GateDrawerLoader>(); loader.gate = l.rewards[i]; loader.load(); rewards.Add(loader.gameObject); } Enabled = true; }
// Only Ports and Gates should be selected! public void select() { if (playing) { return; } if (Time.time - lastSelectionTime <= ACTION_COOLDOWN) { return; } lastSelectionTime = Time.time; Ray ray = Camera.main.ScreenPointToRay(mousePos); PointerEventData pointerData = new PointerEventData(EventSystem.current); pointerData.position = mousePos; List <RaycastResult> results = new List <RaycastResult>(); EventSystem.current.RaycastAll(pointerData, results); for (int i = 0; i < results.Count; i++) { if (results[i].gameObject == null) { continue; } if (selected != null) { // Handle what happens when port is selected if (selected.GetType().IsSubclassOf(typeof(Port))) { Port hitPort = results[i].gameObject.GetComponent <Port>(); if (hitPort != null) { // Both are ports, make sure they aren't the same type if (!selected.GetType().Equals(hitPort.GetType())) { if (selected.GetType().Equals(typeof(InputPort))) { ((OutputPort)hitPort).addNext((InputPort)selected); audioSource.PlayOneShot(wireConnectSound); deselect(); return; } else { ((OutputPort)selected).addNext((InputPort)hitPort); audioSource.PlayOneShot(wireConnectSound); deselect(); return; } } } } } else // Selected null, select what we clicked on { Port hitPort = results[i].gameObject.GetComponent <Port>(); GateInstance gateInstance = results[i].gameObject.GetComponent <GateInstance>(); GateDrawerLoader gateDrawerItem = results[i].gameObject.GetComponent <GateDrawerLoader>(); if (hitPort != null) { selected = hitPort; audioSource.PlayOneShot(wireConnectSound); return; } else if (gateInstance != null) { selected = gateInstance; audioSource.PlayOneShot(gatePickupSound); return; } else if (gateDrawerItem != null) { GateLoader loader = GameObject.Instantiate(gatePrefab, Vector3.zero, Quaternion.identity).GetComponent <GateLoader>(); loader.gate = gateDrawerItem.gate; loader.load(); selected = loader.Instance; audioSource.PlayOneShot(gatePickupSound); return; } } } RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 100); if (selected != null) { // Handle what happens when port is selected if (selected.GetType().IsSubclassOf(typeof(Port))) { if (hit.collider != null) { Port hitPort = hit.collider.GetComponent <Port>(); if (hitPort != null) { // Both are ports, make sure they aren't the same type if (!selected.GetType().Equals(hitPort.GetType())) { if (selected.GetType().Equals(typeof(InputPort))) { ((OutputPort)hitPort).addNext((InputPort)selected); audioSource.PlayOneShot(wireConnectSound); deselect(); return; } else { ((OutputPort)selected).addNext((InputPort)hitPort); audioSource.PlayOneShot(wireConnectSound); deselect(); return; } } } } else { removeSelected(); return; } } else if (selected.GetType().IsAssignableFrom(typeof(GateInstance))) { deselect(); audioSource.PlayOneShot(gateDropSound); return; } } else if (hit.collider != null) // Selected null, select what we clicked on { Port hitPort = hit.collider.GetComponent <Port>(); GateInstance gateInstance = hit.collider.GetComponent <GateInstance>(); if (hitPort != null) { selected = hitPort; audioSource.PlayOneShot(wireConnectSound); return; } else if (gateInstance != null) { selected = gateInstance; audioSource.PlayOneShot(gatePickupSound); return; } } if (selected != null) { removeSelected(); } }