Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.74 KB | None | 0 0
  1. GridGen:
  2.  
  3. using System;
  4. using UnityEngine;
  5.  
  6. public class GridGen : MonoBehaviour
  7. {
  8.  
  9.     [SerializeField] public float cubeSize = 1f;
  10.     [SerializeField] public float cubeVisualSize = 0.8f;
  11.  
  12.     //[NonSerialized] public int[,,] cubeArray = new int [10, 10, 10]; //old
  13.     private Data3D[,,] cubeArray = new Data3D [10, 10, 10]; //new
  14.  
  15.     public GameObject linkedObject;
  16.  
  17.     bool isRunning = false;
  18.     void Start()
  19.     {
  20.         isRunning = true;
  21.  
  22.         ForAllInGrid((x, y, z) =>
  23.         {
  24.             SetDataAt(x, y, z, new Data3D());
  25.         });
  26.  
  27.         ReadyCubes();
  28.     }
  29.  
  30.     //Constructor
  31.     public Data3D GetDataAt(Vector3Int pos )
  32.     {
  33.         return GetDataAt(pos.x, pos.y, pos.z);
  34.     }
  35.  
  36.     public Data3D GetDataAt(int x, int y, int z)
  37.     {
  38.         return cubeArray[x, y, z];
  39.     }
  40.  
  41.     public void SetDataAt(int x, int y, int z, Data3D data)
  42.     {
  43.         cubeArray[x, y, z] = data;
  44.         data.locationX = x;
  45.         data.locationX = y;
  46.         data.locationX = z;
  47.     }
  48.  
  49.     public void SetBlockState(int state, Data3D data)
  50.     {
  51.          data.blockState = state;
  52.     }
  53.    
  54.     // helper function loop through rows, cols and zees to create a cube of cubes
  55.     void ForAllInGrid(Action<int,int,int> myAction)
  56.     {
  57.         for (int z = 0; z < 10; z++)
  58.         {
  59.             for (int y = 0; y < 10; y++)
  60.             {
  61.                 for (int x = 0; x < 10; x++)
  62.                 {
  63.                     myAction(x, y, z);
  64.                 }
  65.             }
  66.         }
  67.     }//end ForAll
  68.  
  69.     void ReadyCubes()
  70.     {
  71.         //print("is this working 0");
  72.         if(isRunning == false) return;
  73.        
  74.         ForAllInGrid((x, y, z) =>
  75.         {
  76.            var gridLocation = GetDataAt(x,y,z);
  77.            var col = gridLocation.blockState == 0 ? new Color32(0,255,0,10) : new Color32(255,0,0,10);
  78.  
  79.            CreateCubeAt(x, y, z, col);
  80.  
  81.            //print("is this working 1");
  82.         });
  83.     }
  84.  
  85.     public void CreateCubeAt(int x, int y, int z, Color col)
  86.     {
  87.         Vector3 pos = new Vector3(cubeSize*x,cubeSize*y,cubeSize*z);
  88.         pos += transform.position;
  89.         Vector3 size = new Vector3(cubeVisualSize,cubeVisualSize,cubeVisualSize);
  90.         var newCube = Instantiate(linkedObject, new Vector3(pos.x, pos.y, pos.z), Quaternion.identity);
  91.         newCube.name = "GridCube " + x + "," + y + ","  + z;
  92.         newCube.GetComponent<GridCubeScript>().gridPosition = new Vector3Int(x, y, z);
  93.         newCube.GetComponent<Renderer>().material.color = col;
  94.  
  95.         //print("is this working 2");
  96.     }//end CreateCube
  97.  
  98. }//end class
  99.  
  100. ------------------------------------------------------------------------------------------------------------
  101. Player:
  102.  
  103. using UnityEngine;
  104. using System;
  105.  
  106. public class Player : MonoBehaviour
  107. {
  108.     [SerializeField] private GridGen gridGen;
  109.  
  110.     [NonSerialized] Vector3Int gridPosition = new Vector3Int(0,1,0);
  111.  
  112.     // Start is called before the first frame update
  113.     void Start()
  114.     {
  115.         this.transform.position = gridPosition;
  116.     }
  117.  
  118.     // Update is called once per frame
  119.     void Update()
  120.     {
  121.         if(Input.GetKeyDown(KeyCode.D))
  122.         {
  123.            
  124.             Data3D nextLocation = gridGen.GetDataAt(gridPosition.x+1, gridPosition.y, gridPosition.z);
  125.            
  126.             if(nextLocation.blockState == 0)
  127.             {
  128.                 transform.Translate(Vector3.right * gridGen.cubeSize);
  129.                 gridPosition.x++;
  130.             }
  131.             else
  132.             {
  133.                 print("can't move there D");
  134.                
  135.             }
  136.         }
  137.  
  138.     }//end update
  139. }//end class
  140.  
  141. -------------------------------------------------------------------------------------
  142. GridCubeScript:
  143.  
  144. using System.Collections;
  145. using System.Collections.Generic;
  146. using UnityEngine;
  147. using System;
  148.  
  149. public class GridCubeScript : MonoBehaviour
  150. {
  151.     [SerializeField] private GameObject gridGen;
  152.  
  153.     [NonSerialized] public GameObject touchingObj;
  154.  
  155.     [NonSerialized] public bool keepForMoving = false;
  156.  
  157.     [NonSerialized] public Vector3Int gridPosition;
  158.  
  159.  
  160.  
  161.     private void OnTriggerEnter(Collider other)
  162.     {
  163.         if(other.GetComponent<ObjectPropertiesScript>().wall == true || other.GetComponent<ObjectPropertiesScript>().floor == true)
  164.         {
  165.             Data3D data = gridGen.GetComponent<GridGen>().GetDataAt(gridPosition);
  166.             gridGen.GetComponent<GridGen>().SetBlockState(1, data); // 0 = walkable, 1 = blocked, 2 = keep collision detection
  167.  
  168.             this.GetComponent<Renderer>().material.color = new Color32(255,0,0,50);
  169.         }
  170.         if(other.GetComponent<ObjectPropertiesScript>().floor == true)
  171.         {
  172.             Data3D data = gridGen.GetComponent<GridGen>().GetDataAt(gridPosition);
  173.             gridGen.GetComponent<GridGen>().SetBlockState(1, data); // 0 = walkable, 1 = blocked, 2 = keep collision detection
  174.  
  175.             this.GetComponent<Renderer>().material.color = new Color32(100,0,0,50);
  176.         }
  177.  
  178.         touchingObj = other.gameObject;
  179.         //print(touchingObj.name + this.name);
  180.     }
  181.  
  182.     private void OnTriggerExit(Collider other)
  183.     {
  184.             Data3D data = gridGen.GetComponent<GridGen>().GetDataAt(gridPosition);
  185.             gridGen.GetComponent<GridGen>().SetBlockState(0, data); // 0 = walkable, 1 = blocked, 2 = keep collision detection
  186.  
  187.         this.GetComponent<Renderer>().material.color = new Color32(255,255,255,50);
  188.     }
  189. }//end class
  190.  
  191. ----------------------------------------------------------------------------------------
  192. Data3D:
  193.  
  194. public class Data3D
  195. {
  196.     // 0 = walkable, 1 = blocked, 2 = keep collision detection
  197.     public int blockState = 0;
  198.     public int locationX;
  199.     public int locationY;
  200.     public int locationZ;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement