void CreateBuildingGhost() { GameObject g = Instantiate(buildingGhostPrefab, Vector2.zero, Quaternion.identity, buildingsParent); buildingGhost = g.GetComponent <BuildingGhost>(); buildingGhost.gameObject.SetActive(false); }
private void Update() { if (ghost != null) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit = new RaycastHit(); if (Physics.Raycast(ray, out hit, 256)) { Vector3 temp = hit.point; temp.y += ghost.transform.localScale.y / 2; ghost.transform.position = temp; } if (Input.GetMouseButtonDown(0)) { if (ghost.TryPlace()) { ghost = null; } } } }
void HandleConstructor() { for (int i = 0; i < myUnits.Count; i++) { if (myUnits[i].GetComponent <Constructor>() && myUnits[i].GetComponent <Constructor>().doingNothing()) { Constructor constructor = myUnits[i].GetComponent <Constructor>(); CreationBuilding buildingToBuild; if (UnityEngine.Random.value < 0.5f) { buildingToBuild = constructor.Buildings[0]; } else { buildingToBuild = constructor.Buildings[1]; } float money = myFaction.Equals(Faction.Blue) ? gameMaster.BlueWool : gameMaster.RedWool; if (buildingToBuild.Cost <= money) { GameObject tmp = Instantiate(buildingToBuild.BuildingGhost, initialBuildingPosition.position + Vector3.up * 0.5f, buildingToBuild.BuildingGhost.transform.rotation); BuildingGhost bdGhostInstantied = tmp.GetComponent <BuildingGhost>(); int cap = 0; RaycastHit ra; bool hittedSomeGO = true; Physics.SphereCast(bdGhostInstantied.transform.position + Vector3.up * 40, 5, -Vector3.up, out ra, 50); if (ra.collider.gameObject.GetComponent <RTSGameObject>()) { hittedSomeGO = true; } else { hittedSomeGO = false; } while (hittedSomeGO && cap < 7) { bdGhostInstantied.transform.position += Vector3.forward * -20; Physics.SphereCast(bdGhostInstantied.transform.position + Vector3.up * 40, 5, -Vector3.up, out ra, 50); if (ra.collider.gameObject.GetComponent <RTSGameObject>()) { hittedSomeGO = true; } else { hittedSomeGO = false; } cap++; } if (cap >= 7) { tooMuchBuilding = true; } else { constructor.SendBuildOrder(buildingToBuild.gameObject, bdGhostInstantied.gameObject, bdGhostInstantied.transform.position, buildingToBuild.gameObject.transform.rotation, true, false); } } else { constructor.SendDirectMoveOrder(constructorBasePosition.position); } } } }
public void Attack() { if (Time.time >= attackCoolDown) { //Detect hittables in range Collider[] gotHit = Physics.OverlapSphere(attackPoint.position, attackRange, hittableLayers); //Apply damage to each hit object foreach (Collider hit in gotHit) { Resource hitResource = hit.GetComponent <Resource>(); TreeScript hitTree = hit.GetComponent <TreeScript>(); BuildingGhost buildingGhost = hit.GetComponent <BuildingGhost>(); Animal animal = hit.GetComponent <Animal>(); UseableItem item = hit.GetComponent <UseableItem>(); FarmPlot farmPlot = hit.GetComponent <FarmPlot>(); Plant plant = hit.GetComponent <Plant>(); //Getting resources if (hitResource) { hitResource.health -= attackDamage; audioManager.PlaySound("Hit Marker"); } //Tree chopping if (hitTree) { hitTree.ChopTree(attackDamage); audioManager.PlaySound("Hit Marker"); } //Building ghosts if (buildingGhost) { if (buildingGhost.requiredResource == "Wood") { if (GameController.Instance.resourceController.wood >= 1) { buildingGhost.AddResources(1); } } if (buildingGhost.requiredResource == "Stone") { if (GameController.Instance.resourceController.stone >= 1) { buildingGhost.AddResources(1); } } } //Hit animal if (animal) { animal.TakeDamage(attackDamage, this.transform); audioManager.PlaySound("Smack"); //Debug.Log("Dealt " + damage + " dmg to " + hit.transform.name); } //Pickup item if (item) { if (itemInHand == null) { item.pickedUp = true; itemInHand = hit.transform; audioManager.PlaySound("Item Pickup"); } } //Farming if (farmPlot) { farmPlot.PlantSeed(); } if (plant) { plant.Harvest(); } } attackCoolDown = Time.time + (1.0f / attackSpeed); } }
public void Enable(GameInstance game) { game.State.Add(ghost = new BuildingGhost(blueprint)); }