public Transform RandomWorkObject(Transform transform) { List <Transform> workObject_List = new List <Transform>(); for (int i = 0; i < workRoot.childCount; i++) { ConstructSlot constructSlot = workRoot.GetChild(i).GetComponent <ConstructSlot>(); Transform transformConstruct = workRoot.GetChild(i); if (constructSlot.construct.constructType == ConstructType.Work) { workObject_List.Add(transformConstruct); } } if (workObject_List.Count > 0) { int rnd = UnityEngine.Random.Range(0, workObject_List.Count - 1); return(workObject_List[rnd]); } else { return(transform); } }
public Transform FindNearWorkObject(Transform transform) { Dictionary <float, Transform> workObject_Dic = new Dictionary <float, Transform>(); for (int i = 0; i < workRoot.childCount; i++) { ConstructSlot constructSlot = workRoot.GetChild(i).GetComponent <ConstructSlot>(); Transform transformConstruct = workRoot.GetChild(i); if (constructSlot.construct.constructType == ConstructType.Work) { float distance = Vector3.Distance(transformConstruct.position, transform.position); if (!workObject_Dic.ContainsKey(distance)) { workObject_Dic.Add(distance, transformConstruct); } } } if (workObject_Dic.Count > 0) { float keyR = workObject_Dic.Min(e => e.Key); return(workObject_Dic[keyR]); } else { return(transform); } }
public void FindGround() { Vector3 origin = new Vector3(transform.position.x, transform.position.y - (transform.localScale.y / 2), transform.position.z); Ray ray = new Ray(origin, Vector3.down); RaycastHit hit; Debug.DrawRay(ray.origin, ray.direction * 100, Color.green); if (Physics.Raycast(ray, out hit, Mathf.Infinity, LayerMask.GetMask("Ground"))) { transform.parent.GetComponent <ConstructControl>().height = hit.point.y + (transform.localScale.y / 2) + 0.1f; Debug.DrawRay(ray.origin, new Vector3(0, -hit.distance, 0), Color.blue); if (hit.collider.GetComponent <ConstructSlot>().aboveConstruct == null) { canPlacement = true; constructSlot = hit.collider.GetComponent <ConstructSlot>(); hit.collider.GetComponent <MeshRenderer>().material.color = Color.green; if (temp == null) { temp = hit.collider.GetComponent <MeshRenderer>(); } if (hit.collider.GetComponent <MeshRenderer>() != temp) { temp.material.color = Color.clear; } temp = hit.collider.GetComponent <MeshRenderer>(); } else { canPlacement = false; constructSlot = null; hit.collider.GetComponent <MeshRenderer>().material.color = Color.red; if (temp == null) { temp = hit.collider.GetComponent <MeshRenderer>(); } if (hit.collider.GetComponent <MeshRenderer>() != temp) { temp.material.color = Color.clear; } temp = hit.collider.GetComponent <MeshRenderer>(); } } else { canPlacement = false; constructSlot = null; } }
private void MoveConstruct() { if (construct != null) { if (construct.GetComponent <ConstructSlot>().construct.constructType != ConstructType.Ground) { construct.GetComponent <ConstructControl>().FindGround(); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit) && hit.collider.GetComponent <ConstructSlot>() != null) { if (hit.collider.GetComponent <ConstructSlot>().aboveConstruct == null && hit.collider.GetComponent <ConstructSlot>().construct.constructType == ConstructType.Ground) { ConstructSlot ConstructSlot = hit.collider.GetComponent <ConstructSlot>(); construct.GetComponent <ConstructControl>().Move(new Vector2(ConstructSlot.transform.position.x, ConstructSlot.transform.position.z)); } if (Input.GetMouseButtonDown(0)) { if (construct.GetComponent <ConstructControl>().canPlacement) { construct.GetComponent <ConstructSlot>().UpdateInfomation(); Placement(); } else { Debug.LogWarning("Can't Placement"); } } else if (Input.GetKeyDown(KeyCode.Escape)) { CancelPlacement(); } MapManager.instance.UpdateMapLayer(); } } else if (construct.GetComponent <ConstructSlot>().construct.constructType == ConstructType.Ground) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit) && hit.collider.GetComponent <ConstructSlot>() != null && ( hit.collider.GetComponent <ConstructSlot>().construct.constructType == ConstructType.Ground)) { if (Input.GetMouseButtonDown(0)) { hit.collider.GetComponent <ConstructSlot>().constructData.constructID = construct.GetComponent <ConstructSlot>().constructData.constructID; hit.collider.GetComponent <MeshRenderer>().material = construct.GetComponent <MeshRenderer>().material; Destroy(construct.gameObject); construct = null; } MapManager.instance.UpdateMapLayer(); } } } else if (construct == null) { if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit) && hit.collider.GetComponent <ConstructControl>() != null && hit.collider.GetComponent <ConstructSlot>().construct.constructType != ConstructType.Ground) { hit.collider.GetComponent <ConstructControl>().ResetUnderConstructSlot(); construct = hit.collider.gameObject; } } } }