Advertisement
DMeville

Untitled

Mar 26th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.65 KB | None | 0 0
  1. //Terrain Grid
  2. using UnityEngine;
  3. using System.Collections;
  4. using Gamelogic;
  5. using Gamelogic.Grids;
  6.  
  7. [RequireComponent(typeof(MeshCollider))]
  8. public class TerrainGrid : GLMonoBehaviour {
  9.  
  10.     public TerrainCell terrainCell;
  11.     private RectGrid<TerrainCell> grid;
  12.     private IMap3D<RectPoint> map;
  13.  
  14.     public int width = 10;
  15.     public int height = 10;
  16.     public bool buildOnStart = false;
  17.  
  18.     void Start () {
  19.     if(buildOnStart)
  20.             BuildGrid();
  21.     }
  22.  
  23.     [ContextMenu("Build Grid")]
  24.     private void BuildGrid() {
  25.         grid = RectGrid<TerrainCell>.Rectangle(width-1, height-1);
  26.         map = new RectMap(new Vector2(1f, 1f)).To3DXZ();
  27.  
  28.         foreach (RectPoint point in grid) {
  29.             TerrainCell cell = Instantiate(terrainCell);
  30.             Vector3 worldPoint = map[point];
  31.  
  32.             cell.transform.parent = this.gameObject.transform;
  33.             cell.transform.localScale = Vector3.one;
  34.             cell.transform.localPosition = worldPoint + new Vector3(0.5f, 10f, 0.5f);
  35.            
  36.             cell.Generate();
  37.             cell.HighlightOn = false;
  38.             grid[point] = cell;
  39.         }
  40.     }
  41.    
  42.     void Update () {
  43.         if (Input.GetMouseButtonDown(0)) {
  44.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  45.             RaycastHit hit;
  46.  
  47.             if (Physics.Raycast(ray, out hit)) {
  48.                 Vector3 worldPosition = this.gameObject.transform.InverseTransformPoint(hit.point);
  49.                 RectPoint gridPoint = new RectPoint((int)hit.point.x, (int)hit.point.z);
  50.  
  51.                 if (grid.Contains(gridPoint)) {
  52.                     Debug.Log("Found");
  53.                     grid[gridPoint].HighlightOn = !grid[gridPoint].HighlightOn;
  54.                     grid[gridPoint].__UpdatePresentation(true);
  55.                 } else {
  56.                     Debug.Log("couldn't find grid point");
  57.                 }
  58.  
  59.             } else {
  60.                 Debug.Log("Didn't hit anything");
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66.  
  67. //Cell class
  68. using UnityEngine;
  69. using System.Collections;
  70. using Gamelogic.Grids;
  71. using System.Collections.Generic;
  72.  
  73. public class TerrainCell : MeshTileCell {
  74.  
  75.     public Material material;
  76.  
  77.     private MeshFilter meshFilter;
  78.     private MeshRenderer meshRenderer;
  79.     private Mesh mesh;
  80.  
  81.     private float yheight = 0.01f; //prevent fighting
  82.  
  83.     [HideInInspector]
  84.     public Vector3 topLeft;
  85.     [HideInInspector]
  86.     public Vector3 topRight;
  87.     [HideInInspector]
  88.     public Vector3 bottomRight;
  89.     [HideInInspector]
  90.     public Vector3 bottomLeft;
  91.  
  92.     public void Generate() {
  93.         meshFilter = this.gameObject.AddComponent<MeshFilter>();
  94.         meshRenderer = this.gameObject.AddComponent<MeshRenderer>();
  95.         mesh = new Mesh();
  96.         meshFilter.mesh = mesh;
  97.         meshRenderer.material = material;
  98.  
  99.         List<Vector3> verts = new List<Vector3>();
  100.  
  101.         topLeft = new Vector3(-0.5f, 0f, -0.5f);
  102.         topRight = new Vector3(0.5f, 0f, -0.5f);
  103.         bottomLeft = new Vector3(-0.5f, 0f, 0.5f);
  104.         bottomRight = new Vector3(0.5f, 0f, 0.5f);
  105.  
  106.         topLeft = GetHeightAtRaycast(topLeft);
  107.         topRight = GetHeightAtRaycast(topRight);
  108.         bottomLeft = GetHeightAtRaycast(bottomLeft);
  109.         bottomRight = GetHeightAtRaycast(bottomRight);
  110.  
  111.  
  112.         verts.Add(topLeft);
  113.         verts.Add(topRight);
  114.         verts.Add(bottomLeft);
  115.         verts.Add(bottomRight);
  116.  
  117.         List<int> triangles = new List<int>();
  118.  
  119.         triangles.Add(2);
  120.         triangles.Add(1);
  121.         triangles.Add(0);
  122.  
  123.         triangles.Add(2);
  124.         triangles.Add(3);
  125.         triangles.Add(1);
  126.  
  127.         List<Color32> colors = new List<Color32>();
  128.         colors.Add(new Color32(0, 0, 0, 0));
  129.         colors.Add(new Color32(0, 0, 0, 0));
  130.         colors.Add(new Color32(0, 0, 0, 0));
  131.         colors.Add(new Color32(0, 0, 0, 0));
  132.  
  133.         mesh.vertices = verts.ToArray();
  134.         mesh.triangles = triangles.ToArray();
  135.         mesh.colors32 = colors.ToArray();
  136.         mesh.Optimize();
  137.         mesh.RecalculateBounds();
  138.         mesh.RecalculateNormals();
  139.  
  140.         Vector3 pos = this.gameObject.transform.localPosition;
  141.         pos.y = yheight;
  142.         this.gameObject.transform.localPosition = pos;
  143.     }
  144.  
  145.     private Vector3 GetHeightAtRaycast(Vector3 position) {
  146.         Ray ray = new Ray(position + this.gameObject.transform.position, Vector3.down);
  147.         RaycastHit hit;
  148.         if (Physics.Raycast(ray, out hit)) {
  149.             position.y = hit.point.y;
  150.             return position;
  151.         } else {
  152.             Debug.LogError("Raycast failed, this isn't allowed to happen!");
  153.             return Vector3.zero;
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement