Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- public class MapController : MonoBehaviour
- {
- public TextAsset[] levelData;
- public GameObject floor;
- public GameObject wall;
- private GameObject selectedUnit;
- private GameObject highlightedTile;
- private GameObject previousTile;
- public List<GameObject> tiles;
- private Color blankColor;
- private Color originalColor;
- private Color highlightColor = new Color(1f, 1f, 1f, 1f);
- private Vector3 movePosition;
- private StringReader reader;
- private int currentLevel;
- private int nextNameNumber = 0;
- private TeamOneUnitController TOUCont;
- private GFHexGrid GFHex;
- private GFGrid levelGrid;
- private void Awake()
- {
- levelGrid = GetComponent<GFGrid>();
- tiles = new List<GameObject>();
- TOUCont = GameObject.Find("PlayerOneUnitController").GetComponent<TeamOneUnitController>();
- GFHex = gameObject.GetComponent<GFHexGrid>();
- }
- private void Start()
- {
- BuildLevel(levelData[currentLevel], levelGrid);
- Debug.Log("start " + originalColor);
- }
- void Update()
- {
- MouseRaycastInputs();
- HighlightTiles();
- }
- public void BuildLevel(TextAsset levelData, GFGrid levelGrid)
- {
- reader = new StringReader(levelData.text);
- string line;
- int row = 0;
- while ((line = reader.ReadLine()) != null)
- {
- for (int column = 0; column < line.Length; column++)
- {
- Vector3 targetPosition = levelGrid.GridToWorld(new Vector3(column, 0, row));
- CreateHex(line[column], targetPosition);
- }
- row++;
- }
- }
- public void CreateHex(char letter, Vector3 targetPosition)
- {
- GameObject spawn = null;
- switch (letter)
- {
- case 'f':
- {
- spawn = floor;
- break;
- }
- case 'w':
- {
- spawn = wall;
- break;
- }
- }
- if (spawn)
- {
- GameObject obj = Instantiate(spawn, targetPosition, Quaternion.identity) as GameObject;
- obj.name = "Floor Tile[" + nextNameNumber + "]";
- nextNameNumber++;
- tiles.Add(obj);
- }
- }
- void MouseRaycastInputs()
- {
- if (Input.GetMouseButtonUp(0))
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hitInfo;
- int unit_LayerMask = 1 << 8;
- if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, unit_LayerMask))
- {
- selectedUnit = hitInfo.transform.gameObject;
- print(selectedUnit.name + " selected");
- }
- }
- if (Input.GetMouseButtonUp(1))
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hitInfo;
- int ground_LayerMask = 1 << 9;
- if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, ground_LayerMask))
- {
- if(selectedUnit != null)
- {
- movePosition = hitInfo.transform.position;
- MoveObject(selectedUnit, movePosition);
- }
- else
- {
- print("No unit selected");
- }
- }
- }
- }
- void HighlightTiles()
- {
- Ray hRay = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hHitInfo;
- int ground_LayerMask = 1 << 9;
- while(Physics.Raycast(hRay, out hHitInfo, Mathf.Infinity, ground_LayerMask))
- {
- highlightedTile = hHitInfo.collider.gameObject;
- if(previousTile == null)
- {
- previousTile = highlightedTile;
- }
- if(highlightedTile != previousTile)
- {
- previousTile.GetComponent<MeshRenderer>().material.color = originalColor;
- previousTile = null;
- Debug.Log("previousTile = null");
- }
- if(originalColor == blankColor)
- {
- originalColor = highlightedTile.GetComponent<MeshRenderer>().material.color;
- Debug.Log("reset to originalColor " + originalColor);
- }
- highlightedTile.GetComponent<MeshRenderer>().material.color = highlightColor;
- Debug.Log("While loop running");
- return;
- }
- if(highlightedTile != null)
- {
- highlightedTile.GetComponent<MeshRenderer>().material.color = originalColor;
- highlightedTile = null;
- previousTile = null;
- Debug.Log("highlightedTile = null");
- }
- }
- void MoveObject(GameObject unit, Vector3 movePosition)
- {
- unit.transform.position = movePosition + new Vector3(0, 1, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment