Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Terrain Grid
- using UnityEngine;
- using System.Collections;
- using Gamelogic;
- using Gamelogic.Grids;
- [RequireComponent(typeof(MeshCollider))]
- public class TerrainGrid : GLMonoBehaviour {
- public TerrainCell terrainCell;
- private RectGrid<TerrainCell> grid;
- private IMap3D<RectPoint> map;
- public int width = 10;
- public int height = 10;
- public bool buildOnStart = false;
- void Start () {
- if(buildOnStart)
- BuildGrid();
- }
- [ContextMenu("Build Grid")]
- private void BuildGrid() {
- grid = RectGrid<TerrainCell>.Rectangle(width-1, height-1);
- map = new RectMap(new Vector2(1f, 1f)).To3DXZ();
- foreach (RectPoint point in grid) {
- TerrainCell cell = Instantiate(terrainCell);
- Vector3 worldPoint = map[point];
- cell.transform.parent = this.gameObject.transform;
- cell.transform.localScale = Vector3.one;
- cell.transform.localPosition = worldPoint + new Vector3(0.5f, 10f, 0.5f);
- cell.Generate();
- cell.HighlightOn = false;
- grid[point] = cell;
- }
- }
- void Update () {
- if (Input.GetMouseButtonDown(0)) {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit)) {
- Vector3 worldPosition = this.gameObject.transform.InverseTransformPoint(hit.point);
- RectPoint gridPoint = new RectPoint((int)hit.point.x, (int)hit.point.z);
- if (grid.Contains(gridPoint)) {
- Debug.Log("Found");
- grid[gridPoint].HighlightOn = !grid[gridPoint].HighlightOn;
- grid[gridPoint].__UpdatePresentation(true);
- } else {
- Debug.Log("couldn't find grid point");
- }
- } else {
- Debug.Log("Didn't hit anything");
- }
- }
- }
- }
- //Cell class
- using UnityEngine;
- using System.Collections;
- using Gamelogic.Grids;
- using System.Collections.Generic;
- public class TerrainCell : MeshTileCell {
- public Material material;
- private MeshFilter meshFilter;
- private MeshRenderer meshRenderer;
- private Mesh mesh;
- private float yheight = 0.01f; //prevent fighting
- [HideInInspector]
- public Vector3 topLeft;
- [HideInInspector]
- public Vector3 topRight;
- [HideInInspector]
- public Vector3 bottomRight;
- [HideInInspector]
- public Vector3 bottomLeft;
- public void Generate() {
- meshFilter = this.gameObject.AddComponent<MeshFilter>();
- meshRenderer = this.gameObject.AddComponent<MeshRenderer>();
- mesh = new Mesh();
- meshFilter.mesh = mesh;
- meshRenderer.material = material;
- List<Vector3> verts = new List<Vector3>();
- topLeft = new Vector3(-0.5f, 0f, -0.5f);
- topRight = new Vector3(0.5f, 0f, -0.5f);
- bottomLeft = new Vector3(-0.5f, 0f, 0.5f);
- bottomRight = new Vector3(0.5f, 0f, 0.5f);
- topLeft = GetHeightAtRaycast(topLeft);
- topRight = GetHeightAtRaycast(topRight);
- bottomLeft = GetHeightAtRaycast(bottomLeft);
- bottomRight = GetHeightAtRaycast(bottomRight);
- verts.Add(topLeft);
- verts.Add(topRight);
- verts.Add(bottomLeft);
- verts.Add(bottomRight);
- List<int> triangles = new List<int>();
- triangles.Add(2);
- triangles.Add(1);
- triangles.Add(0);
- triangles.Add(2);
- triangles.Add(3);
- triangles.Add(1);
- List<Color32> colors = new List<Color32>();
- colors.Add(new Color32(0, 0, 0, 0));
- colors.Add(new Color32(0, 0, 0, 0));
- colors.Add(new Color32(0, 0, 0, 0));
- colors.Add(new Color32(0, 0, 0, 0));
- mesh.vertices = verts.ToArray();
- mesh.triangles = triangles.ToArray();
- mesh.colors32 = colors.ToArray();
- mesh.Optimize();
- mesh.RecalculateBounds();
- mesh.RecalculateNormals();
- Vector3 pos = this.gameObject.transform.localPosition;
- pos.y = yheight;
- this.gameObject.transform.localPosition = pos;
- }
- private Vector3 GetHeightAtRaycast(Vector3 position) {
- Ray ray = new Ray(position + this.gameObject.transform.position, Vector3.down);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit)) {
- position.y = hit.point.y;
- return position;
- } else {
- Debug.LogError("Raycast failed, this isn't allowed to happen!");
- return Vector3.zero;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement