void DroppedItem() { // Check position against all Vector3 endPosition = new Vector3(); float dist = 100000; bool slotPicked = false; Transform pickedSlot = null; foreach (Transform x in slots) { if (Vector3.Distance(transform.position, x.position) < dist && Vector3.Distance(transform.position, x.position) <= minDistance && x.GetComponent <TowerSlot>().chosen == false && x.GetComponent <TowerSlot>().locked == false) { endPosition = x.position; pickedSlot = x; slotPicked = true; } } if (slotPicked == true) // found a place to put it { pickedSlot.GetComponent <TowerSlot>().AssignTower(towerIndex); mySlot = pickedSlot.GetComponent <TowerSlot>(); transform.position = endPosition; assignedToSlot = true; } else // did not find a viable place to put the tower. { ResetPosition(); } }
/// <summary> /// Handler for clicking on tower slot buttons /// </summary> public void OpenCloseTowerMenu() { Refresh(); selectedSlot = TowerSlot.selectedSlot; selectedTower = null; if (!towerMenu.activeSelf) { towerMenu.SetActive(true); towerMenu.transform.position = selectedSlot.transform.position; } else { if (towerMenu.transform.position == selectedSlot.transform.position) { towerMenu.SetActive(false); } else { towerMenu.transform.position = selectedSlot.transform.position; } } if (selectedSlot.buildedTower != null) { selectedTower = selectedSlot.buildedTower; sellingPricePanel.SetActive(true); sellingPriceText.text = selectedTower.sellingPrice.ToString(); sellButton.SetActive(true); } }
private void CreateTowerSlots() { //TODO: Instantiate based on 2D design /* * for (int i = 0; i < 10; i++) * { * TowerSlot newTowerSlot = GameObject.Instantiate(towerSlotPrefab); * activeTowerSlots.Add(newTowerSlot); * * * TowerSlot newTowerSlot; * * newTowerSlot.Initialise( * () => activeTowerSlots.Remove(newTowerSlot), * () => currSelTowerSlotIndex = activeTowerSlots.IndexOf(newTowerSlot)); * } */ //These are assigned in Editor for now so in this case we simply rely on the slot initialisation for Clean up //Clean up //Create for (int i = 0; i < activeTowerSlots.Count; i++) { TowerSlot selTowerSlot = activeTowerSlots[i]; selTowerSlot.Initialise(() => activeTowerSlots.Remove(selTowerSlot)); } }
public void CreateTower() { TowerSlot[] towerLocations = path.GetTowerLocations(); int randomPos = Random.Range(0, towerLocations.Length); TowerSlot randomlySelectedLocation = towerLocations[randomPos]; if (randomlySelectedLocation.IsOccupied) { for (int i = randomPos + 1; i < towerLocations.Length; i++) { if (!towerLocations[i].IsOccupied) { randomlySelectedLocation = towerLocations[i]; Tower tower = Instantiate(towerPrototype, randomlySelectedLocation.Position, Quaternion.identity); tower.SetTowerProperties(Random.Range(Constants.MIN_TOWER_POWER, Constants.MAX_TOWER_POWER)); randomlySelectedLocation.SetTower(tower); return; } } } else { Tower tower = Instantiate(towerPrototype, randomlySelectedLocation.Position, Quaternion.identity); tower.SetTowerProperties(Random.Range(Constants.MIN_TOWER_POWER, Constants.MAX_TOWER_POWER)); randomlySelectedLocation.SetTower(tower); return; } Debug.Log("Cant put tower"); }
void GrabbedItem() { if (assignedToSlot == true) { mySlot.UnassignTower(towerIndex); mySlot = null; assignedToSlot = false; } }
private void UpdateTowerSlots() { for (int i = 0; i < activeTowerSlots.Count; i++) { TowerSlot currTowerSlot = activeTowerSlots[i]; currTowerSlot.OnUpdate(); } }
/// <summary> /// Handler for clicking the build button /// </summary> public void BuildTower() { selectedSlot = TowerSlot.selectedSlot; TowerButton selectedTowerButton = TowerButton.selectedTowerButton; Instantiate(selectedTowerButton.tower, selectedSlot.transform.position, Quaternion.identity); towerMenu.SetActive(false); balance.currencyAmount -= selectedTowerButton.towerData.buildPrice; }
public void SelectTowerSlot() { if (selectedSlot != this) { selectedSlot = this; } TowerMenu.Instance.OpenCloseTowerMenu(); }
public void ShowTowerUI(TowerSlot towerSlot) { if (currentSlot != null) { currentSlot.EnableRangeDisplay(false); } gameObject.SetActive(true); currentSlot = towerSlot; currentSlot.EnableRangeDisplay(true); transform.position = currentSlot.transform.position; lastOptionSelected = -1; PlaceIcons(); }
public TowerSlot[] CreateTowerLocations() { float pathLenght = transform.localScale.y; int possibleTowerLocationCount = Mathf.FloorToInt(pathLenght) - 1; for (int i = 0; i < possibleTowerLocationCount - 1; i++) { for (int j = 0; j < 2; j++) { TowerSlot towerSlot = Instantiate(towerSlotPrototype, transform); towerSlot.transform.localPosition = new Vector3(StartPosition.x + (2 * j - 1) / 1.5f, StartPosition.y - (i + 1) * 1f / possibleTowerLocationCount); towerLocations.Add(towerSlot); } } return(towerLocations.ToArray()); }
public void OnSlotEnter(int slotId) { if (Slots != null) { var slot = Slots.FirstOrDefault(s => s.Id == slotId); if (slot == null) { Debug.LogError($"Slot not found {slotId}"); } selectedSlot = slot; TowerPlacement.VerifyPlacement(slot.IsEmpty && (TowerPlacement.info != null && CanBuild(TowerPlacement.info.Price) ) ); } }
private void UpdateTowerPlacement() { if (placingTower) { TowerSlot hitTowerSlot = null; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); foreach (RaycastHit hit in Physics.RaycastAll(ray)) { hitTowerSlot = hit.collider.gameObject.GetComponent <TowerSlot>(); if (hitTowerSlot) { break; } } if (hitTowerSlot) { currSelTowerSlotIndex = activeTowerSlots.IndexOf(hitTowerSlot); } towerPreview.transform.position = currSelTowerSlot.transform.position; //ChangePreviewColour if (currSelTowerSlot.tower) { towerPreviewMeshRenderer.material.color = towerPreviewBlockedColour; } else { towerPreviewMeshRenderer.material.color = towerPreviewDefaultColour; } if (Input.GetMouseButtonDown(0)) { if (hitTowerSlot) { PlaceTower(); } StopPlacingTower(); } } }
void Update() { if (EventSystem.current.IsPointerOverGameObject()) { return; } if (Input.GetMouseButtonUp(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity, groundMask)) { if (selectedtower != null) { TowerSlot ts = hit.transform.gameObject.GetComponent <TowerSlot>(); if (ts) { ts.SetTower(selectedtower); selectedSlot = ts; } selectedtower = null; } else { TowerSlot ts = hit.transform.gameObject.GetComponent <TowerSlot>(); if (ts) { ts.activateUpgradePanel(); selectedSlot = ts; } } } } }
public void SetTowerSlot(TowerSlot ts) { _towerSlot = ts; }
private void OnTowerGhostSnapToSlot(TowerSlot towerSlot) { _buildIsAllowed = true; }
public void OnSlotExit() { TowerPlacement.VerifyPlacement(false); selectedSlot = null; }