Guest User

Untitled

a guest
Aug 13th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.06 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5.  
  6. public class MapController : MonoBehaviour
  7. {
  8.     public TextAsset[] levelData;
  9.  
  10.     public GameObject floor;
  11.     public GameObject wall;
  12.     private GameObject selectedUnit;
  13.     private GameObject highlightedTile;
  14.     private GameObject previousTile;
  15.  
  16.     public List<GameObject> tiles;
  17.  
  18.     private Color blankColor;
  19.     private Color originalColor;
  20.     private Color highlightColor = new Color(1f, 1f, 1f, 1f);
  21.  
  22.     private Vector3 movePosition;
  23.  
  24.     private StringReader reader;
  25.  
  26.     private int currentLevel;
  27.     private int nextNameNumber = 0;
  28.  
  29.     private TeamOneUnitController TOUCont;
  30.     private GFHexGrid GFHex;
  31.     private GFGrid levelGrid;
  32.  
  33.     private void Awake()
  34.     {
  35.         levelGrid = GetComponent<GFGrid>();
  36.         tiles = new List<GameObject>();
  37.         TOUCont = GameObject.Find("PlayerOneUnitController").GetComponent<TeamOneUnitController>();
  38.         GFHex = gameObject.GetComponent<GFHexGrid>();
  39.     }
  40.  
  41.     private void Start()
  42.     {
  43.         BuildLevel(levelData[currentLevel], levelGrid);
  44.         Debug.Log("start " + originalColor);
  45.     }
  46.  
  47.     void Update()
  48.     {
  49.         MouseRaycastInputs();
  50.         HighlightTiles();
  51.     }
  52.  
  53.     public void BuildLevel(TextAsset levelData, GFGrid levelGrid)
  54.     {
  55.         reader = new StringReader(levelData.text);
  56.         string line;
  57.         int row = 0;
  58.  
  59.         while ((line = reader.ReadLine()) != null)
  60.         {
  61.             for (int column = 0; column < line.Length; column++)
  62.             {
  63.                 Vector3 targetPosition = levelGrid.GridToWorld(new Vector3(column, 0, row));
  64.                 CreateHex(line[column], targetPosition);
  65.             }
  66.  
  67.             row++;
  68.         }
  69.     }
  70.  
  71.     public void CreateHex(char letter, Vector3 targetPosition)
  72.     {
  73.         GameObject spawn = null;
  74.  
  75.         switch (letter)
  76.         {
  77.             case 'f':
  78.             {
  79.                 spawn = floor;
  80.                 break;
  81.             }
  82.             case 'w':
  83.             {
  84.                 spawn = wall;
  85.                 break;
  86.             }
  87.         }
  88.        
  89.         if (spawn)
  90.         {
  91.             GameObject obj = Instantiate(spawn, targetPosition, Quaternion.identity) as GameObject;
  92.             obj.name = "Floor Tile[" + nextNameNumber + "]";
  93.             nextNameNumber++;
  94.             tiles.Add(obj);
  95.         }
  96.     }
  97.  
  98.     void MouseRaycastInputs()
  99.     {
  100.         if (Input.GetMouseButtonUp(0))
  101.         {
  102.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  103.             RaycastHit hitInfo;
  104.             int unit_LayerMask = 1 << 8;
  105.  
  106.             if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, unit_LayerMask))
  107.             {
  108.                 selectedUnit = hitInfo.transform.gameObject;
  109.                 print(selectedUnit.name + " selected");
  110.             }
  111.         }
  112.  
  113.         if (Input.GetMouseButtonUp(1))
  114.         {
  115.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  116.             RaycastHit hitInfo;
  117.             int ground_LayerMask = 1 << 9;
  118.  
  119.             if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, ground_LayerMask))
  120.             {
  121.                 if(selectedUnit != null)
  122.                 {
  123.                     movePosition = hitInfo.transform.position;
  124.                     MoveObject(selectedUnit, movePosition);
  125.                 }
  126.  
  127.                 else
  128.                 {
  129.                     print("No unit selected");
  130.                 }
  131.             }
  132.         }
  133.     }
  134.  
  135.  
  136.     void HighlightTiles()
  137.     {
  138.         Ray hRay = Camera.main.ScreenPointToRay(Input.mousePosition);
  139.         RaycastHit hHitInfo;
  140.         int ground_LayerMask = 1 << 9;
  141.  
  142.         while(Physics.Raycast(hRay, out hHitInfo, Mathf.Infinity, ground_LayerMask))
  143.         {
  144.             highlightedTile = hHitInfo.collider.gameObject;
  145.  
  146.             if(previousTile == null)
  147.             {
  148.                 previousTile = highlightedTile;
  149.             }
  150.  
  151.             if(highlightedTile != previousTile)
  152.             {
  153.                 previousTile.GetComponent<MeshRenderer>().material.color = originalColor;
  154.                 previousTile = null;
  155.                 Debug.Log("previousTile = null");
  156.             }
  157.  
  158.             if(originalColor == blankColor)
  159.             {
  160.                 originalColor = highlightedTile.GetComponent<MeshRenderer>().material.color;
  161.                 Debug.Log("reset to originalColor " + originalColor);
  162.             }
  163.  
  164.             highlightedTile.GetComponent<MeshRenderer>().material.color = highlightColor;
  165.             Debug.Log("While loop running");
  166.             return;
  167.         }
  168.  
  169.         if(highlightedTile != null)
  170.         {
  171.             highlightedTile.GetComponent<MeshRenderer>().material.color = originalColor;
  172.             highlightedTile = null;
  173.             previousTile = null;
  174.             Debug.Log("highlightedTile = null");
  175.         }
  176.     }
  177.  
  178.     void MoveObject(GameObject unit, Vector3 movePosition)
  179.     {
  180.         unit.transform.position = movePosition + new Vector3(0, 1, 0);
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment