/
GameGridManager.cs
150 lines (148 loc) · 5.35 KB
/
GameGridManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class GameGridManager : MonoBehaviour
{
GameTile[,] tiles;// = new GameTile[,];
// Use this for initialization
public GameObject destroyedTile;
private bool gridSpawned = false;
private GameTile selectedTile;
private int enemyDeathCount = 0;
//private MyGUIWindow window = new MyGUIWindow ();
//public GameObject killCountTextGameObject;
public LayerMask raycastMask;
public Text killCountText;
public AstarPath AStarGrid;
//Vector2 selectedTilePos;
void Start ()
{
//selectedTilePos = new Vector2 (0.0f, 0.0f);
//tiles = new GameTile[,]{};
/*MyGUIWindow window = gameObject.AddComponent<MyGUIWindow> ();
window.SetDimensions (new Vector2 (Screen.width / 2, 10), 200);
window.SetWindowName ("game grid control");
window.AddButton ("reparent tiles", this, "ReparentTiles");
//window.AddButton ("update the tile data", this, "TestTile");
*/
//window = gameObject.AddComponent<MyGUIWindow> ();
//window.SetDimensions (new Vector2 (10, 40), 100.0f);
//window.SetWindowName ("killcount");
//window.AddLabel (enemyDeathCount + "");
NotificationCenter.DefaultCenter.AddObserver (this, "GetAllTilesInGrid");
NotificationCenter.DefaultCenter.AddObserver (this, "DestroyTiles");
//NotificationCenter.DefaultCenter.AddObserver (this, "ScanAStarGrid");
NotificationCenter.DefaultCenter.AddObserver (this, "EnemyDestroyed");
NotificationCenter.DefaultCenter.AddObserver (this, "ScanAStarGrid");
}
// Update is called once per frame
void FixedUpdate ()
{
if (gridSpawned) {
//HandleInput ();
}
}
void EnemyDestroyed ()
{
enemyDeathCount++;
killCountText.text = "Kill Count: " + enemyDeathCount;
}
void ScanAStarGrid ()
{
AStarGrid.enabled = true;
AStarGrid.Scan ();
NotificationCenter.DefaultCenter.PostNotification (this, "SafeToPlay");
}
void DestroyTiles ()
{
StartCoroutine ("CoDestroyTiles");
}
void ReparentTiles ()
{
Transform tT = GameObject.FindGameObjectWithTag ("TestTag").transform;
int cC = transform.childCount;
for (int i = 0; i < cC; i++) {
transform.GetChild (i).SetParent (tT);
}
}
IEnumerator CoDestroyTiles ()
{
NotificationCenter.DefaultCenter.PostNotification (this, "Destroy");
yield return new WaitForSeconds (0.01f);
for (int ix =0; ix<tiles.GetLength(0); ix++) {
for (int iy =0; iy <tiles.GetLength(1); iy++) {
//GameTile tile = ;
if (!tiles [ix, iy]) {
GPoint gP = new GPoint (new Vector2 (ix + 1, iy + 1), GPoint.PType.TILE_DESTROYED);
GameObject newTile = (GameObject)Instantiate (destroyedTile, gP.GetWorldPosition (), Quaternion.identity);
newTile.name = "/x:" + gP.GetPosition ().x + "$/y:" + gP.GetPosition ().y + "$/t:" + gP.GetPType () + "$";
newTile.transform.parent = transform;
tiles [ix, iy] = newTile.GetComponent<GameTile> ();
tiles [ix, iy].SetData ();
}
}
}
AStarGrid.Scan ();
StopCoroutine ("CoDestroyTiles");
}
void SelectNewTile (Vector2 newPos)
{
if (selectedTile != null)
selectedTile.DeSelect ();
if (newPos != -Vector2.one) {
selectedTile = tiles [(int)newPos.x, (int)newPos.y];
selectedTile.Select ();
} else {
selectedTile = null;
}
}
void HandleInput ()
{
//Vector3 p = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 5));
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 100.0f, raycastMask)) {
Debug.DrawLine (ray.origin, hit.point);
//tiles [(int)selectedTilePos.x, (int)selectedTilePos.y].DeSelect ();
if (hit.collider.CompareTag ("Tile")) {
Vector2 pos = hit.transform.GetComponent<GameTile> ().position;
//if (pos != selectedTile.position)
SelectNewTile (pos);
if (Input.GetKeyDown (KeyCode.Space)) {
selectedTile.ToggleActive ();
}
}
} else {
SelectNewTile (-Vector2.one);
//tiles [(int)selectedTilePos.x, (int)selectedTilePos.y].DeSelect ();
}
}
void GetAllTilesInGrid ()
{
Debug.Log ("setting data and creating references to all tiles");
NotificationCenter.DefaultCenter.PostNotification (this, "SetData");
int maxX = 0, maxY = 0;
for (int i = 0; i < gameObject.transform.childCount; i++) {
GameTile tGT = gameObject.transform.GetChild (i).GetComponent<GameTile> ();
if (tGT.position.x > maxX/*tile x greater than last tile x*/) {
maxX = (int)tGT.position.x;
}
if (tGT.position.y > maxY/*tile y greater than last tile y*/) {
maxY = (int)tGT.position.y;
}
}
tiles = new GameTile[maxX + 1, maxY + 1];
for (int i = 0; i < gameObject.transform.childCount; i++) {
GameTile tGT = gameObject.transform.GetChild (i).GetComponent<GameTile> ();
tiles [(int)tGT.position.x, (int)tGT.position.y] = tGT;
}
gridSpawned = true;
//NotificationCenter.DefaultCenter.PostNotification (this, "ScanAStarGrid");
NotificationCenter.DefaultCenter.PostNotification (this, "CityDone");
}
void OnGui ()
{
GUI.Label (new Rect (Screen.width / 2, Screen.height / 2, 50, 50), "selected: " + selectedTile.position + selectedTile.type);
}
}