private float getConnection(BuildableNode node, BuildableNode motherNode) { float connections = 0; if (node == null || node.GetPathChildren() == null || node.GetPathChildren().Count == 0) { return(0); } else { foreach (GameObject child in node.GetPathChildren()) { if (child == null) { continue; } BuildableNode childNode = child.GetComponent <BuildableNode>(); if (childNode != null && childNode.IsRoot && childNode != motherNode) { connections += 1; } else { connections += getConnection(childNode, motherNode); } } } return(connections); }
// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, float.PositiveInfinity, LayerMask.GetMask("Buildable"))) { Transform objectHit = hit.transform; BuildableNode buildable = hit.transform.gameObject.GetComponentInChildren <BuildableNode>(); if (buildable != null) { buildable.BuildBuilding(); // buildable.GetNeighbours().ForEach(n => { // n.GetComponent<BuildableNode>().ChangeColor(); // }); } // hit.transform.gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.blue); // Do something with the object that was hit by the raycast. } } float fov = Camera.main.fieldOfView; fov -= Input.GetAxis("Mouse ScrollWheel") * CameraScrollSpeed; fov = Mathf.Clamp(fov, 45, 120); Camera.main.fieldOfView = fov; }
// Update is called once per frame void Update() { if (buildingModeOn && currentNode != null && Input.GetKeyUp(KeyCode.E)) { bool allowBuild = NodeManager.main.IsBuildingAllowed(transform.position, currentNode.transform.position); if (allowBuild) { NodeManager.main.DeselectAll(); BuildableNode newNode = NodeManager.main.SpawnNode(transform.position); //currentNode.SetPathParent(newNode.gameObject); currentNode.DeletePathChild(GameManager.main.Player.gameObject); newNode.DeletePathChild(GameManager.main.Player.gameObject); newNode.SetPathParent(currentNode.gameObject); currentNode.SetPathChild(newNode.gameObject); //currentNode.SetUnbuildable(); currentNode.Deselect(); Disable(); } else { BuildableNode node = NodeManager.main.AttemptConnection(transform.position, currentNode.transform.position); if (node != null) { NodeManager.main.DeselectAll(); //currentNode.SetPathParent(node.gameObject); currentNode.SetPathChild(node.gameObject); node.SetPathParent(currentNode.gameObject); currentNode.SetUnbuildable(); currentNode.Deselect(); Disable(); } } } }
/// <summary> /// Show message that indicates player may go into building mode. /// Check if given node is closer than the current one, if so, set that as current. /// </summary> /// <param name="distance"></param> /// <param name="node"></param> public void ShowBuildMessage(float distance, BuildableNode node) { if (!buildingModeOn) { if (currentNode == null) { currentNode = node; currentNode.Select(); uiCanBuildIndicator.SetActive(true); } else { Vector2 currentNodePosition = new Vector2(currentNode.transform.position.x, currentNode.transform.position.z); Vector2 currentPosition = new Vector2(transform.position.x, transform.position.z); float currentNodeDistance = Vector2.Distance(currentPosition, currentNodePosition); if (currentNodeDistance > distance) { currentNode.Deselect(); currentNode = node; currentNode.Select(); uiCanBuildIndicator.SetActive(true); } } } }
// Update is called once per frame void Update() { if (Time.time - lastNodeCheck > nodeCheckInterval) { float distance = 99999f; lastNodeCheck = Time.time; NodeManager nodemngr = GameManager.main.NodeManager; List <BuildableNode> nodes = nodemngr.GetNodes(false); foreach (BuildableNode node in nodes) { if (node == null) { continue; } if (Vector3.Distance(transform.position, node.transform.position) < distance) { distance = Vector3.Distance(transform.position, node.transform.position); closest = node; } } } if (closest != null) { Vector3 dir = (closest.transform.position - transform.position).normalized * 0.1f; body.AddForce(dir * .5f, ForceMode.VelocityChange); } }
// Use this for initialization void Start() { gameObject.SetActive(false); body = GetComponent <Rigidbody>(); closest = null; body.isKinematic = true; }
public void SelectNode(BuildableNode newNode) { foreach (BuildableNode node in nodes) { node.Deselect(); } newNode.Select(); }
public BuildableNode SpawnNode(Vector3 position) { BuildableNode newNode = Instantiate(nodePrefab); newNode.gameObject.SetActive(true); newNode.transform.position = new Vector3(position.x, nodePrefab.transform.position.y, position.z); nodes.Add(newNode); return(newNode); }
/// <summary> /// Deselect current node and disable "can build" indicator. /// </summary> /// <param name="node"></param> public void ClearCurrentNode(BuildableNode node) { if (currentNode == node) { uiCanBuildIndicator.SetActive(false); currentNode.Deselect(); currentNode = null; } }
private void ScanForNeighbour(BuildableNode node, Vector3 direction) { RaycastHit hit; if (Physics.Raycast(node.gameObject.transform.position, direction, out hit, Mathf.Infinity, LayerMask.GetMask("Buildable"))) { node.GetNeighbours().Add(hit.transform.gameObject); } }
private void InitBuildableNode(int i, int j, GameObject newBuildable) { BuildableNode bn = newBuildable.GetComponentInChildren <BuildableNode>(); if (bn != null) { bn.Init(i, j); nodes.Add(bn); } }
public void DeleteParent(bool deleteChild = true) { if (parent == null) { return; } BuildableNode obj = parent.GetComponent <BuildableNode>(); if (obj == null) { return; } if (deleteChild) { obj.DeletePathChild(parent, false); } parent = null; }
public void DeleteChild(GameObject node, bool deleteParent = true) { if (children.Count <= 0) { return; } if (deleteParent && node != null) { BuildableNode obj = node.GetComponent <BuildableNode>(); if (obj != null) { obj.DeletePathParent(false); } } children.Remove(node); }
void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "EnergyNode") { if (closest == null) { return; } Vector3 dir = (transform.position - closest.transform.position).normalized; body.AddForce(dir * 2f, ForceMode.Impulse); if (isAttacking && (Time.time - lastHit > hitInterval)) { lastHit = Time.time; BuildableNode node = collision.gameObject.transform.parent.GetComponent <BuildableNode>(); if (node != null) { node.ReduceHealth(damage); } } } }
public float GetConnections() { BuildableNode motherNode = nodes[0]; return(getConnection(motherNode, motherNode)); }
public bool IsCurrentNode(BuildableNode node) { return(currentNode == node); }