Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GridGen:
- using System;
- using UnityEngine;
- public class GridGen : MonoBehaviour
- {
- [SerializeField] public float cubeSize = 1f;
- [SerializeField] public float cubeVisualSize = 0.8f;
- //[NonSerialized] public int[,,] cubeArray = new int [10, 10, 10]; //old
- private Data3D[,,] cubeArray = new Data3D [10, 10, 10]; //new
- public GameObject linkedObject;
- bool isRunning = false;
- void Start()
- {
- isRunning = true;
- ForAllInGrid((x, y, z) =>
- {
- SetDataAt(x, y, z, new Data3D());
- });
- ReadyCubes();
- }
- //Constructor
- public Data3D GetDataAt(Vector3Int pos )
- {
- return GetDataAt(pos.x, pos.y, pos.z);
- }
- public Data3D GetDataAt(int x, int y, int z)
- {
- return cubeArray[x, y, z];
- }
- public void SetDataAt(int x, int y, int z, Data3D data)
- {
- cubeArray[x, y, z] = data;
- data.locationX = x;
- data.locationX = y;
- data.locationX = z;
- }
- public void SetBlockState(int state, Data3D data)
- {
- data.blockState = state;
- }
- // helper function loop through rows, cols and zees to create a cube of cubes
- void ForAllInGrid(Action<int,int,int> myAction)
- {
- for (int z = 0; z < 10; z++)
- {
- for (int y = 0; y < 10; y++)
- {
- for (int x = 0; x < 10; x++)
- {
- myAction(x, y, z);
- }
- }
- }
- }//end ForAll
- void ReadyCubes()
- {
- //print("is this working 0");
- if(isRunning == false) return;
- ForAllInGrid((x, y, z) =>
- {
- var gridLocation = GetDataAt(x,y,z);
- var col = gridLocation.blockState == 0 ? new Color32(0,255,0,10) : new Color32(255,0,0,10);
- CreateCubeAt(x, y, z, col);
- //print("is this working 1");
- });
- }
- public void CreateCubeAt(int x, int y, int z, Color col)
- {
- Vector3 pos = new Vector3(cubeSize*x,cubeSize*y,cubeSize*z);
- pos += transform.position;
- Vector3 size = new Vector3(cubeVisualSize,cubeVisualSize,cubeVisualSize);
- var newCube = Instantiate(linkedObject, new Vector3(pos.x, pos.y, pos.z), Quaternion.identity);
- newCube.name = "GridCube " + x + "," + y + "," + z;
- newCube.GetComponent<GridCubeScript>().gridPosition = new Vector3Int(x, y, z);
- newCube.GetComponent<Renderer>().material.color = col;
- //print("is this working 2");
- }//end CreateCube
- }//end class
- ------------------------------------------------------------------------------------------------------------
- Player:
- using UnityEngine;
- using System;
- public class Player : MonoBehaviour
- {
- [SerializeField] private GridGen gridGen;
- [NonSerialized] Vector3Int gridPosition = new Vector3Int(0,1,0);
- // Start is called before the first frame update
- void Start()
- {
- this.transform.position = gridPosition;
- }
- // Update is called once per frame
- void Update()
- {
- if(Input.GetKeyDown(KeyCode.D))
- {
- Data3D nextLocation = gridGen.GetDataAt(gridPosition.x+1, gridPosition.y, gridPosition.z);
- if(nextLocation.blockState == 0)
- {
- transform.Translate(Vector3.right * gridGen.cubeSize);
- gridPosition.x++;
- }
- else
- {
- print("can't move there D");
- }
- }
- }//end update
- }//end class
- -------------------------------------------------------------------------------------
- GridCubeScript:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- public class GridCubeScript : MonoBehaviour
- {
- [SerializeField] private GameObject gridGen;
- [NonSerialized] public GameObject touchingObj;
- [NonSerialized] public bool keepForMoving = false;
- [NonSerialized] public Vector3Int gridPosition;
- private void OnTriggerEnter(Collider other)
- {
- if(other.GetComponent<ObjectPropertiesScript>().wall == true || other.GetComponent<ObjectPropertiesScript>().floor == true)
- {
- Data3D data = gridGen.GetComponent<GridGen>().GetDataAt(gridPosition);
- gridGen.GetComponent<GridGen>().SetBlockState(1, data); // 0 = walkable, 1 = blocked, 2 = keep collision detection
- this.GetComponent<Renderer>().material.color = new Color32(255,0,0,50);
- }
- if(other.GetComponent<ObjectPropertiesScript>().floor == true)
- {
- Data3D data = gridGen.GetComponent<GridGen>().GetDataAt(gridPosition);
- gridGen.GetComponent<GridGen>().SetBlockState(1, data); // 0 = walkable, 1 = blocked, 2 = keep collision detection
- this.GetComponent<Renderer>().material.color = new Color32(100,0,0,50);
- }
- touchingObj = other.gameObject;
- //print(touchingObj.name + this.name);
- }
- private void OnTriggerExit(Collider other)
- {
- Data3D data = gridGen.GetComponent<GridGen>().GetDataAt(gridPosition);
- gridGen.GetComponent<GridGen>().SetBlockState(0, data); // 0 = walkable, 1 = blocked, 2 = keep collision detection
- this.GetComponent<Renderer>().material.color = new Color32(255,255,255,50);
- }
- }//end class
- ----------------------------------------------------------------------------------------
- Data3D:
- public class Data3D
- {
- // 0 = walkable, 1 = blocked, 2 = keep collision detection
- public int blockState = 0;
- public int locationX;
- public int locationY;
- public int locationZ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement