123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class TowerCreate : MonoBehaviour {
- public GameObject _tower_cursor;
- GameObject Tower_Cursor;
- public Tower[] towerPrefabs;
- public LayerMask mouseMask;
- public int prefabSelect;
- public float gridIncrement = 2.5f;
- bool mouseOnScreen = false;
- Vector3 mouseGridPosition;
- bool Cancel = true;
- void Start () {
-
- Tower_Cursor = Instantiate(_tower_cursor, Vector3.zero, Quaternion.identity) as GameObject;
- Tower_Cursor.SetActive(false);
- }
-
-
- void Update() {
- mouseOnScreen = Input.mousePresent;
- bool mouseOnUI = EventSystem.current.IsPointerOverGameObject();
- if (!mouseOnUI)
- {
- if (mouseOnScreen)
- {
- RaycastHit hitInfo;
-
- bool mouseIsOverSomething = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, 100, mouseMask);
- mouseGridPosition = hitInfo.point;
- mouseGridPosition.x =
- Mathf.Round(mouseGridPosition.x / gridIncrement) * gridIncrement;
- mouseGridPosition.y =
- Mathf.Round(mouseGridPosition.y / 5) * 5+2.5f;
- mouseGridPosition.z =
- Mathf.Round(mouseGridPosition.z / gridIncrement) * gridIncrement;
- if (Input.GetMouseButtonDown(1) && !Cancel) { SelectCancel(); }
- }
-
- if (Tower_Cursor != null)
- {
-
- Tower_Cursor.transform.position = mouseGridPosition;
- Color cursorColor = new Color(0.58f, 0.15f, 0.15f, 0.2f);
- bool isBuildableGround = false;
- foreach (var col in Physics.OverlapBox(Tower_Cursor.transform.position, Tower_Cursor.transform.localScale/2))
- {
-
- if (col.tag == "Wall" || col.tag =="Enemy_Floor" || col.tag=="Tower")
- {
- cursorColor = new Color(0.58f, 0.15f, 0.15f, 0.2f);
- Tower_Cursor.GetComponent<Renderer>().material.SetColor("_TintColor", cursorColor);
- Tower_Cursor.transform.Find("bottom").GetComponentInChildren<Renderer>().material.SetColor("_TintColor", cursorColor);
-
- return;
- }
- else if (col.tag == "CreateFloor")
- {
-
- cursorColor = new Color(0.2f, 0.58f, 0.18f, 0.2f);
- isBuildableGround = true;
- }
-
- Tower_Cursor.GetComponent<Renderer>().material.SetColor("_TintColor", cursorColor);
-
- Tower_Cursor.transform.Find("bottom").GetComponentInChildren<Renderer>().material.SetColor("_TintColor", cursorColor);
- }
- if (Input.GetMouseButtonDown(0) && isBuildableGround)
- {
-
- PlaceTower(towerPrefabs[prefabSelect]);
- }
-
- }
- }
- }
-
-
-
-
- void PlaceTower(Tower tower)
- {
- if (!Tower_Cursor.activeInHierarchy) return;
-
- if (Nexus.CurrentInstance.UseMana(tower.upgradeCosts[0]))
- {
-
- Destroy(Tower_Cursor.GetComponentInChildren<Tower>().gameObject);
- Tower_Cursor.SetActive(false);
-
- Instantiate(tower, Tower_Cursor.transform.position - new Vector3(0, 2.5f, 0), Quaternion.identity);
- Cancel = true;
- }
- }
-
-
-
-
- public void SelectPrefab(int index)
- {
- while (index < 0) index += towerPrefabs.Length;
- while (index >= towerPrefabs.Length) index -= towerPrefabs.Length;
-
- if (!(Nexus.CurrentInstance.CurrentMana >= towerPrefabs[index].upgradeCosts[0])) { return; }
- prefabSelect = index;
-
- Tower tower_mode = Instantiate(towerPrefabs[prefabSelect], Tower_Cursor.transform.position-new Vector3(0,2.5f,0), Quaternion.identity) as Tower;
-
- tower_mode.GetComponentInChildren<Collider>().enabled = false;
-
- tower_mode.GetComponentInChildren<Renderer>().material.shader = Shader.Find("Particles/Additive");
-
- tower_mode.GetComponent<Tower>().enabled = false;
-
- tower_mode.transform.SetParent(Tower_Cursor.transform);
- Tower_Cursor.SetActive(true);
- Cancel = false;
- }
-
-
-
- void SelectCancel()
- {
- Destroy(Tower_Cursor.GetComponentInChildren<Tower>().gameObject);
- Tower_Cursor.SetActive(false);
- Cancel = true;
- }
- }
|