void Build() { GhostBuilding gb = currentObject.GetComponent <GhostBuilding>(); if (!gb.IsTriggered() && Input.GetMouseButtonDown(0)) { if (currentObject == wallGhost) { if (goldPile.getGold() > wallCost) { goldPile.deductGold(wallCost); GameObject wall = (GameObject)Instantiate(wallGhostPrefab, currentObject.transform.position, currentObject.transform.rotation); } else { // NOT ENOUGH MINERALS Debug.Log("NOT ENOUGH MINERALS: " + goldPile.getGold() + ", Need: " + (wallCost + 1)); } } else if (currentObject == gateGhost) { if (goldPile.getGold() > gateCost) { goldPile.deductGold(gateCost); GameObject gate = (GameObject)Instantiate(gateGhostPrefab, currentObject.transform.position, currentObject.transform.rotation); } else { // NOT ENOUGH MINERALS Debug.Log("NOT ENOUGH MINERALS: " + goldPile.getGold() + ", Need: " + (gateCost + 1)); } } else if (currentObject == towerGhost) { if (goldPile.getGold() > towerCost) { goldPile.deductGold(towerCost); GameObject tower = (GameObject)Instantiate(towerGhostPrefab, currentObject.transform.position, currentObject.transform.rotation); } else { // NOT ENOUGH MINERALS Debug.Log("NOT ENOUGH MINERALS: " + goldPile.getGold() + ", Need: " + (towerCost + 1)); } } else if (currentObject == barracksGhost) { if (goldPile.getGold() > barracksCost) { goldPile.deductGold(barracksCost); GameObject barracks = (GameObject)Instantiate(barracksGhostPrefab, currentObject.transform.position, currentObject.transform.rotation); } else { // NOT ENOUGH MINERALS Debug.Log("NOT ENOUGH MINERALS: " + goldPile.getGold() + ", Need: " + (barracksCost + 1)); } } } }
void Start() { for (int i = 0; i < buildingArray.Length; i++) { Entitas e = buildingArray[i].GetComponent <Entitas>(); buildingList.Add(e.entityName, buildingArray[i]); } for (int i = 0; i < ghostBuildingArray.Length; i++) { GhostBuilding gb = ghostBuildingArray[i].GetComponent <GhostBuilding>(); ghostBuildingList.Add(gb.Bname, ghostBuildingArray[i]); } buildingArray = null; ghostBuildingArray = null; }
void ShowGhost() { int layerMask = 1 << 9; layerMask = ~layerMask; // Set color GhostBuilding gb = currentObject.GetComponent <GhostBuilding>(); if (gb.IsTriggered() || GetCost(currentObject) > goldPile.getGold()) { wallGhost.GetComponent <Renderer>().material = failBuildMat; gateGhost.GetComponent <Renderer>().material = failBuildMat; Renderer[] rends = towerGhost.GetComponentsInChildren <Renderer>(); foreach (Renderer rend in rends) { Material[] mats = rend.materials; for (int i = 0; i < mats.Length; i++) { mats [i] = failBuildMat; } rend.materials = mats; } rends = barracksGhost.GetComponentsInChildren <Renderer>(); foreach (Renderer rend in rends) { Material[] mats = rend.materials; for (int i = 0; i < mats.Length; i++) { mats [i] = failBuildMat; } rend.materials = mats; } } else { wallGhost.GetComponent <Renderer>().material = correctBuildMat; gateGhost.GetComponent <Renderer>().material = correctBuildMat; Renderer[] rends = towerGhost.GetComponentsInChildren <Renderer>(); foreach (Renderer rend in rends) { Material[] mats = rend.materials; for (int i = 0; i < mats.Length; i++) { mats [i] = correctBuildMat; } rend.materials = mats; } rends = barracksGhost.GetComponentsInChildren <Renderer>(); foreach (Renderer rend in rends) { Material[] mats = rend.materials; for (int i = 0; i < mats.Length; i++) { mats [i] = correctBuildMat; } rend.materials = mats; } } RaycastHit hit; Ray ray = cam.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)); if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask)) { if (currentObject == barracksGhost) { currentObject.transform.position = new Vector3(hit.point.x, hit.point.y - 0.5f, hit.point.z); } else { currentObject.transform.position = hit.point; } currentObject.transform.position = new Vector3(currentObject.transform.position.x, currentObject.transform.position.y + 0.5f, currentObject.transform.position.z); } }
/// <summary> /// checks for selection commands (unit and buildidng selection) /// </summary> void selection() { if (Input.GetKey(KeyCode.LeftShift)) { isShift = true; } else { isShift = false; } if (Input.GetMouseButtonDown(0) && !buildingGUI && !buildingBlocked) { selectionBoxStart = CalcUtil.getMousePos(); isDrawSelectionBox = true; isClickSelection = true; } if (Input.GetMouseButtonDown(1) && buildingGUI) { buildingGUI = false; Destroy(buildingManager.getCurrentGhostBuilding().gameObject); buildingManager.addCurrentGhostBuilding(null); buildingManager.SetCurrentGhostBuildingName(null); } if (Input.GetMouseButtonDown(0) && buildingGUI) { GhostBuilding gh = buildingManager.getCurrentGhostBuilding().GetComponent <GhostBuilding>(); if (!buildingBlocked && gh.isBuildable()) { GameObject b = spawner.spawnBuildingForGhost(gh.transform.position); List <Grid> grids = gh.GetComponent <GhostBuilding>().blockFieldOn(b); b.GetComponent <Building>().setGridsOn(grids); if (gh.GetComponent <GhostBuilding>().isStorage()) { b.GetComponent <Building>().setStorageGrids(gh.GetComponent <GhostBuilding>().getStoragesOn()); } ressourceManager.removeRessources(gh.GetComponent <GhostBuilding>().getRessourceRequirements()); buildingGUI = false; Destroy(buildingManager.getCurrentGhostBuilding().gameObject); if (isShift) { spawner.spawnGhostForGhost(gh.transform.position); buildingGUI = true; } else { buildingManager.addCurrentGhostBuilding(null); buildingManager.SetCurrentGhostBuildingName(null); } } } if (Input.GetMouseButtonUp(0)) { selectionBoxEnd = CalcUtil.getMousePos(); isDrawSelectionBox = false; if (!isClickSelection) { if (!isShift) { foreach (GameObject go in unitManager.getUnits()) { go.GetComponent <Unit>().setSelected(false); } } if (selectionBoxStart.x >= selectionBoxEnd.x) { float f = selectionBoxStart.x; selectionBoxStart.x = selectionBoxEnd.x; selectionBoxEnd.x = f; } if (selectionBoxStart.y <= selectionBoxEnd.y) { float f = selectionBoxStart.y; selectionBoxStart.y = selectionBoxEnd.y; selectionBoxEnd.y = f; } foreach (GameObject go in unitManager.getUnits()) { if (go.transform.position.x >= selectionBoxStart.x && go.transform.position.x <= selectionBoxEnd.x && go.transform.position.y <= selectionBoxStart.y && go.transform.position.y >= selectionBoxEnd.y) { go.GetComponent <Unit>().setSelected(true); } } } else { if (!isShift) { foreach (GameObject go in unitManager.getUnits()) { go.GetComponent <Unit>().setSelected(false); } foreach (GameObject go in buildingManager.getBuildings()) { go.GetComponent <Building>().setSelected(false); } } // foreach (GameObject go in unitManager.getUnits()) // { // if(selectionBoxEnd.x <= go.transform.position.x + go.GetComponent<Unit>().getTriggerCollider().size.x && // selectionBoxEnd.x >= go.transform.position.x - go.GetComponent<Unit>().getTriggerCollider().size.x && // selectionBoxEnd.y <= go.transform.position.y + go.GetComponent<Unit>().getTriggerCollider().size.y && // selectionBoxEnd.y >= go.transform.position.y - go.GetComponent<Unit>().getTriggerCollider().size.y) // { // go.GetComponent<Unit>().setSelected(true); // } // } //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //RaycastHit2D hit = Physics2D.Raycast(ray.origin,ray.direction,Mathf.Infinity); Vector2 mousePos = CalcUtil.getMousePos(); Grid grid = mapGrid.getGridOn(mousePos); Building bo = null; try { bo = grid.getBlockingObject().GetComponent <Building>(); } catch { } if (bo != null) { bo.setSelected(true); } //if (hit) // { // foreach (GameObject go in unitManager.getUnits()) // { // if (hit.collider.gameObject == go) // { // hit.collider.transform.gameObject.GetComponent<Unit>().setSelected(true); // } // } // foreach (GameObject go in buildingManager.getBuildings()) // { // if (hit.collider.gameObject == go) // { // hit.collider.transform.gameObject.GetComponent<Building>().setSelected(true); // } // } // } // foreach (GameObject go in buildingManager.getBuildings()) // { // if (selectionBoxEnd.x <= go.transform.position.x + go.GetComponent<Building>().getTriggerCollider().size.x && // selectionBoxEnd.x >= go.transform.position.x - go.GetComponent<Building>().getTriggerCollider().size.x && // selectionBoxEnd.y <= go.transform.position.y + go.GetComponent<Building>().getTriggerCollider().size.y && // selectionBoxEnd.y >= go.transform.position.y - go.GetComponent<Building>().getTriggerCollider().size.y) // { // go.GetComponent<Building>().setSelected(true); // } // } // isClickSelection = false; } } }