Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using System.Collections.Generic;
- public class unitManager : MonoBehaviour
- {
- Camera cam;
- // to get picxel to unit ratio, raycast from both sides of the screen xddddddfded
- public Button b1;
- public Button b2;
- public Button b3;
- public Button b4;
- public int cost1;
- public int cost2;
- public int cost3;
- public int cost4;
- bool placing = false;
- UnityEngine.Object curobj;
- public UnityEngine.Object unit1;
- public UnityEngine.Object unit2;
- public UnityEngine.Object unit3;
- public UnityEngine.Object unit4;
- public float tileX = 2;
- public float tileZ = 2;
- float curX;
- float curZ;
- public Transform TempTrans;
- float i1;
- float i2;
- int listval = 0;
- List<bool> grid = new List<bool>();
- List<Vector3> ListPos;
- public void Start()
- {
- cam = GetComponent<Camera>();
- ListPos = new List<Vector3>();
- SetBools();
- SetTransforms();
- Button button1 = b1.GetComponent<Button>();
- button1.onClick.AddListener(Spawn1);
- Button button2 = b2.GetComponent<Button>();
- button2.onClick.AddListener(Spawn2);
- Button button3 = b3.GetComponent<Button>();
- button3.onClick.AddListener(Spawn3);
- Button button4 = b4.GetComponent<Button>();
- button4.onClick.AddListener(Spawn4);
- }
- void Spawn1()
- {
- if (!placing)
- {
- placing = true;
- globalValues.alterMoney(-cost1);
- curobj = unit1;
- }
- }
- void Spawn2()
- {
- if (!placing)
- {
- placing = true;
- globalValues.alterMoney(-cost2);
- curobj = unit2;
- }
- }
- void Spawn3()
- {
- if (!placing)
- {
- placing = true;
- globalValues.alterMoney(-cost3);
- curobj = unit3;
- }
- }
- void Spawn4()
- {
- if (!placing)
- {
- placing = true;
- globalValues.alterMoney(-cost4);
- curobj = unit4;
- }
- }
- public void Update()
- {
- if (placing && Input.GetKeyDown(KeyCode.Mouse0))
- {
- PlaceGrid(curobj);
- }
- }
- void SetBools()
- {
- for (int robbiewilliamsisdedrofl = 0; robbiewilliamsisdedrofl < 40; ++robbiewilliamsisdedrofl)
- {
- grid.Add(false);
- }
- }
- public void SetTransforms()
- {
- for (int i = 0; i < 40; i++)
- {
- i1 = i / 10;
- i2 = i % 10;
- //TempTrans.rotation = new Vector3(-90, 0 , 0);
- /// revert y-value to 0 ///
- ListPos.Add(new Vector3(i2 * tileX + 20, 1, (i1 - 2) * tileZ));
- //Debug.Log(ListPos[i]);
- }
- //Debug.Log(ListPos.Count);
- //for (int i = 0; i < ListPos.Count; i++)
- //{
- // Debug.Log(ListPos[i]);
- //}
- }
- public void PlaceGrid(UnityEngine.Object unit)
- {
- RaycastHit hit;
- Ray ray = cam.ScreenPointToRay(Input.mousePosition);
- //range = Convert.ToSingle(Input.mousePosition.y * Math.Tan(60)+1);
- if (Physics.Raycast(ray, out hit, 30))
- {
- curZ = hit.point.z;
- curX = hit.point.x + 1.5f;
- //Debug.Log("Hit Points: " + curX + ", " + curZ);
- listval = -1;
- //fix thse loops
- if (curX > 20 && curX < 40 && curZ >= -5 && curZ <= 2)
- {
- listval = 0;
- int valz = 0;
- int valx = 0;
- for (int z = -3; z <= 0; ++z)
- {
- if (curZ > (float)(z * tileZ) + 1 && curZ < (float)((z + 1) * tileZ) + 1)
- {
- listval += 10 * valz;
- }
- valz += 1;
- }
- for (int x = 10; x < 19; ++x)
- {
- if (curX > (float)(x * tileX) && curX < (float)((x + 1) * tileX))
- {
- listval += valx;
- //potentially break;
- }
- valx += 1;
- }
- }
- if (listval > -1 && listval < grid.Count && grid[listval] == false)
- {
- //Debug.Log("Listval: " + listval);
- //Debug.Log(grid[listval]);
- grid[listval] = true;
- TempTrans.position = ListPos[listval];
- Quaternion asd = Quaternion.identity;
- asd.eulerAngles = new Vector3(0, -90, 0);
- TempTrans.rotation = asd;
- //Debug.Log(TempTrans.position.x + " " + TempTrans.position.z);
- Instantiate(unit, TempTrans.position, TempTrans.rotation);
- }
- }
- placing = false;
- }
- public void UpdateGrid(Vector3 place)
- {
- float updX = place.x;
- float updZ = place.z;
- int updlistval = 0;
- int valz = 0;
- int valx = 0;
- for (int z = -3; z <= 0; ++z)
- {
- if (curZ > (float)(z * tileZ) + 1 && curZ < (float)((z + 1) * tileZ) + 1)
- {
- updlistval += 10 * valz;
- }
- valz += 1;
- }
- for (int x = 10; x < 19; ++x)
- {
- if (curX > (float)(x * tileX) && curX < (float)((x + 1) * tileX))
- {
- updlistval += valx;
- //potentially break;
- }
- valx += 1;
- }
- grid[updlistval] = false;
- }
- }
Add Comment
Please, Sign In to add comment