Wolvenspud

EW - unitManager

Oct 10th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.71 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6.  
  7. public class unitManager : MonoBehaviour
  8. {
  9.     Camera cam;
  10.  
  11.     // to get picxel to unit ratio, raycast from both sides of the screen xddddddfded
  12.  
  13.     public Button b1;
  14.     public Button b2;
  15.     public Button b3;
  16.     public Button b4;
  17.  
  18.     public int cost1;
  19.     public int cost2;
  20.     public int cost3;
  21.     public int cost4;
  22.  
  23.     bool placing = false;
  24.     UnityEngine.Object curobj;
  25.  
  26.     public UnityEngine.Object unit1;
  27.     public UnityEngine.Object unit2;
  28.     public UnityEngine.Object unit3;
  29.     public UnityEngine.Object unit4;
  30.  
  31.     public float tileX = 2;
  32.     public float tileZ = 2;
  33.  
  34.     float curX;
  35.     float curZ;
  36.  
  37.     public Transform TempTrans;
  38.  
  39.     float i1;
  40.     float i2;
  41.  
  42.     int listval = 0;
  43.  
  44.     List<bool> grid = new List<bool>();
  45.  
  46.     List<Vector3> ListPos;
  47.  
  48.     public void Start()
  49.     {
  50.         cam = GetComponent<Camera>();
  51.         ListPos = new List<Vector3>();
  52.         SetBools();
  53.         SetTransforms();
  54.         Button button1 = b1.GetComponent<Button>();
  55.         button1.onClick.AddListener(Spawn1);
  56.         Button button2 = b2.GetComponent<Button>();
  57.         button2.onClick.AddListener(Spawn2);
  58.         Button button3 = b3.GetComponent<Button>();
  59.         button3.onClick.AddListener(Spawn3);
  60.         Button button4 = b4.GetComponent<Button>();
  61.         button4.onClick.AddListener(Spawn4);
  62.     }
  63.  
  64.     void Spawn1()
  65.     {
  66.         if (!placing)
  67.         {
  68.             placing = true;
  69.             globalValues.alterMoney(-cost1);
  70.             curobj = unit1;
  71.         }
  72.     }
  73.     void Spawn2()
  74.     {
  75.         if (!placing)
  76.         {
  77.             placing = true;
  78.             globalValues.alterMoney(-cost2);
  79.             curobj = unit2;
  80.         }
  81.     }
  82.     void Spawn3()
  83.     {
  84.         if (!placing)
  85.         {
  86.             placing = true;
  87.             globalValues.alterMoney(-cost3);
  88.             curobj = unit3;
  89.         }
  90.     }
  91.     void Spawn4()
  92.     {
  93.         if (!placing)
  94.         {
  95.             placing = true;
  96.             globalValues.alterMoney(-cost4);
  97.             curobj = unit4;
  98.         }
  99.     }
  100.  
  101.     public void Update()
  102.     {
  103.         if (placing && Input.GetKeyDown(KeyCode.Mouse0))
  104.         {
  105.             PlaceGrid(curobj);
  106.         }
  107.     }
  108.  
  109.     void SetBools()
  110.     {
  111.         for (int robbiewilliamsisdedrofl = 0; robbiewilliamsisdedrofl < 40; ++robbiewilliamsisdedrofl)
  112.         {
  113.             grid.Add(false);
  114.         }
  115.     }
  116.  
  117.     public void SetTransforms()
  118.     {
  119.         for (int i = 0; i < 40; i++)
  120.         {
  121.             i1 = i / 10;
  122.             i2 = i % 10;
  123.             //TempTrans.rotation = new Vector3(-90, 0 , 0);
  124.             /// revert y-value to 0 ///
  125.             ListPos.Add(new Vector3(i2 * tileX + 20, 1, (i1 - 2) * tileZ));
  126.             //Debug.Log(ListPos[i]);
  127.         }
  128.         //Debug.Log(ListPos.Count);
  129.         //for (int i = 0; i < ListPos.Count; i++)
  130.         //{
  131.         //    Debug.Log(ListPos[i]);
  132.         //}
  133.     }
  134.  
  135.     public void PlaceGrid(UnityEngine.Object unit)
  136.     {
  137.         RaycastHit hit;
  138.         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
  139.         //range = Convert.ToSingle(Input.mousePosition.y * Math.Tan(60)+1);
  140.         if (Physics.Raycast(ray, out hit, 30))
  141.         {
  142.  
  143.             curZ = hit.point.z;
  144.             curX = hit.point.x + 1.5f;
  145.  
  146.             //Debug.Log("Hit Points: " + curX + ", " + curZ);
  147.  
  148.  
  149.             listval = -1;
  150.  
  151.             //fix thse loops
  152.             if (curX > 20 && curX < 40 && curZ >= -5 && curZ <= 2)
  153.             {
  154.                 listval = 0;
  155.                 int valz = 0;
  156.                 int valx = 0;
  157.                 for (int z = -3; z <= 0; ++z)
  158.                 {
  159.                     if (curZ > (float)(z * tileZ) + 1 && curZ < (float)((z + 1) * tileZ) + 1)
  160.                     {
  161.                         listval += 10 * valz;
  162.                     }
  163.                     valz += 1;
  164.                 }
  165.                 for (int x = 10; x < 19; ++x)
  166.                 {
  167.                     if (curX > (float)(x * tileX) && curX < (float)((x + 1) * tileX))
  168.                     {
  169.                         listval += valx;
  170.                         //potentially break;
  171.                     }
  172.                     valx += 1;
  173.                 }
  174.             }
  175.            
  176.             if (listval > -1 && listval < grid.Count && grid[listval] == false)
  177.             {
  178.                 //Debug.Log("Listval: " + listval);
  179.                 //Debug.Log(grid[listval]);
  180.                 grid[listval] = true;
  181.                 TempTrans.position = ListPos[listval];
  182.                 Quaternion asd = Quaternion.identity;
  183.                 asd.eulerAngles = new Vector3(0, -90, 0);
  184.                 TempTrans.rotation = asd;
  185.                 //Debug.Log(TempTrans.position.x + " " + TempTrans.position.z);
  186.                 Instantiate(unit, TempTrans.position, TempTrans.rotation);
  187.             }
  188.         }
  189.         placing = false;
  190.     }
  191.  
  192.     public void UpdateGrid(Vector3 place)
  193.     {
  194.         float updX = place.x;
  195.         float updZ = place.z;
  196.         int updlistval = 0;
  197.         int valz = 0;
  198.         int valx = 0;
  199.         for (int z = -3; z <= 0; ++z)
  200.         {
  201.             if (curZ > (float)(z * tileZ) + 1 && curZ < (float)((z + 1) * tileZ) + 1)
  202.             {
  203.                 updlistval += 10 * valz;
  204.             }
  205.             valz += 1;
  206.         }
  207.         for (int x = 10; x < 19; ++x)
  208.         {
  209.             if (curX > (float)(x * tileX) && curX < (float)((x + 1) * tileX))
  210.             {
  211.                 updlistval += valx;
  212.                 //potentially break;
  213.             }
  214.             valx += 1;
  215.         }
  216.         grid[updlistval] = false;
  217.     }
  218. }
Add Comment
Please, Sign In to add comment