-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildingPlacerBehavior.cs
143 lines (114 loc) · 5.99 KB
/
BuildingPlacerBehavior.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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
public class BuildingPlacerBehavior : MonoBehaviour {
[SerializeField]
List<GameObject> buildings;
int building = 0;
GridBehavior grid;
MeshFilter filter;
Terrain terrain;
float[,] heights;
bool currentMouseState = false;
bool lastMouseState = false;
bool isBuilding = false;
// Use this for initialization
void Start () {
grid = GameObject.FindGameObjectsWithTag("Grid")[0].GetComponent<GridBehavior>();
terrain = FindObjectsOfType<Terrain>()[0];
heights = terrain.terrainData.GetHeights(0,0,terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapWidth);
filter = gameObject.AddComponent<MeshFilter>();
}
// Update is called once per frame
void Update () {
currentMouseState = Input.GetMouseButton(0);
for (int i = 49; i < 58; i++)
{
if (Input.GetKey((KeyCode)i))
{
building = i - 49;
isBuilding = true;
}
}
if (Input.GetMouseButton(1) && isBuilding)
{
isBuilding = false;
transform.position = Vector3.down * 100.0f;
}
if (isBuilding)
{
filter.sharedMesh = buildings[building].GetComponent<MeshFilter>().sharedMesh;
transform.localScale = buildings[building].transform.localScale;
RaycastHit[] hits = Physics.RaycastAll(Camera.main.ScreenPointToRay(Input.mousePosition));
foreach (RaycastHit h in hits)
{
if (h.collider.gameObject.GetComponents<Terrain>().Length > 0)
{
bool isPlacable = true;
int extendsX = buildings[building].GetComponent<BuildingBehavior>().extendsX;
int extendsZ = buildings[building].GetComponent<BuildingBehavior>().extendsZ;
Vector3 worldPosition = h.point;
worldPosition.x = Mathf.Round(worldPosition.x) - 0.5f;
worldPosition.z = Mathf.Round(worldPosition.z) - 0.5f;
this.transform.position = worldPosition;
Building.BuildingType bType = buildings[building].GetComponent<BuildingBehavior>().getType();
for (int x = 0; x < extendsX; x++)
{
for (int z = 0; z < extendsZ; z++)
{
float ix = (((Mathf.Round(worldPosition.x + x + 0.5f) - extendsZ / 2) * terrain.terrainData.heightmapWidth / terrain.terrainData.size.x) + terrain.terrainData.heightmapWidth / 2) / terrain.terrainData.heightmapWidth;
float iz = (((Mathf.Round(worldPosition.z + z + 0.5f) - extendsX / 2) * terrain.terrainData.heightmapHeight / terrain.terrainData.size.z) + terrain.terrainData.heightmapHeight / 2) / terrain.terrainData.heightmapHeight;
float steepness = terrain.terrainData.GetSteepness(ix, iz);
int gridType = grid.CheckCollision(worldPosition.x + x - extendsX / 2, worldPosition.z + z - extendsZ / 2);
if ((bType != Building.BuildingType.Connector && gridType != (int)GridBehavior.GridElement.Free) || steepness > 20f || (bType == Building.BuildingType.Connector && gridType != (int)GridBehavior.GridElement.Connector))
{
isPlacable = false;
break;
}
}
}
if (isPlacable)
{
GetComponent<Renderer>().materials[0].SetColor("_EmissionColor", Color.green);
if (currentMouseState && !lastMouseState)
{
Object tmp = Instantiate(buildings[building], worldPosition, Quaternion.identity);
for (int x = 0; x < extendsX; x++)
{
for (int z = 0; z < extendsZ; z++)
{
grid.SetCollision(worldPosition.x + x - extendsX / 2, worldPosition.z + z - extendsZ / 2, ((GameObject)tmp).GetComponent<BuildingBehavior>().GetInstanceID());
int ix = (int)((Mathf.Round(worldPosition.x + x + 0.5f - extendsX / 2)) * terrain.terrainData.heightmapWidth / terrain.terrainData.size.x) + terrain.terrainData.heightmapWidth / 2;
int iz = (int)((Mathf.Round(worldPosition.z + z + 0.5f - extendsZ / 2)) * terrain.terrainData.heightmapHeight / terrain.terrainData.size.z) + terrain.terrainData.heightmapHeight / 2;
heights[iz, ix] = worldPosition.y / terrain.terrainData.size.y;
}
}
foreach (Vector2 en in buildings[building].GetComponent<BuildingBehavior>().entrances)
{
if (grid.CheckCollision(worldPosition.x + en.x, worldPosition.z + en.y) == (int)GridBehavior.GridElement.Free)
grid.SetCollision(worldPosition.x + en.x, worldPosition.z + en.y, (int)GridBehavior.GridElement.Connector);
}
// terrain.terrainData.SetHeights(0, 0, heights);
//Destroy(gameObject);
}
}
else
GetComponent<Renderer>().materials[0].SetColor("_EmissionColor", Color.red);
}
}
}
lastMouseState = currentMouseState;
}
void OnCollisionStay()
{
Debug.Log(gameObject.name + "collided with another object");
}
bool CheckCollision()
{
return true;
}
void GetBuildingExtends()
{
}
}