Skip to content

Instantly share code, notes, and snippets.

@lvcoc
Created September 23, 2019 01:36
Show Gist options
  • Save lvcoc/a42ddd596bacda74b6f823db892bf3c5 to your computer and use it in GitHub Desktop.
Save lvcoc/a42ddd596bacda74b6f823db892bf3c5 to your computer and use it in GitHub Desktop.
program road
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using Random = UnityEngine.Random;
public enum pathType
{
_00=0,
_01=1,
_11=2,
_10=3
}
[Serializable]
public class Node
{
public int s;
public int e;
public Vector2 pos;
public bool isFull = false;
public Node(int s, int e, Vector2 pos, bool isFull=false)
{
this.s = s;
this.e = e;
this.pos = pos;
this.isFull = isFull;
}
public Vector3 GetRealPos()
{
float x = pos.y * 32;
float z = pos.x * 32;
return new Vector3(x,0,z);
}
public pathType GetPathType()
{
if (s==0)
{
if (e==0)
{
return pathType._00;
}
else
{
return pathType._01;
}
}
else
{
if (e == 0)
{
return pathType._10;
}
else
{
return pathType._11;
}
}
}
}
public class draw : MonoBehaviour
{
[Header("Map")]
public int mapNum=2;
public int mapCount = 1;
[Range(0.1f,0.9f)]
public float rightRate = 0.5f;
public GameObject[] ROOT;//00 01
public GameObject[] path;//00 01 11 10
public GameObject ornament;
private List<Node> pathList = new List<Node>();
private List<Node> map = new List<Node>();
private bool nextMap;
[Header("Test")]
public GameObject grid;
public Node root;
public Node currentNode;
public List<GameObject> cubeList = new List<GameObject>();
public Transform cubeParent;
private List<GameObject> gridlist = new List<GameObject>();
private void Start()
{
root = null;
currentNode = null;
CreatePath(true);
}
void InitMap(int Index)
{
map.Clear();
int offset = Index * 3;
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 4; y++)
{
Node n = new Node(0,0,new Vector2(x+ offset, y+ offset));
map.Add(n);
}
}
}
void UpdateMap(int index)
{
InitMap(index);
for (int i = 0; i < pathList.Count; i++)
{
foreach (var item in map)
{
if (item.pos==pathList[i].pos)
{
item.isFull = true;
item.s = pathList[i].s;
item.e = pathList[i].e;
}
}
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
ResetMap();
}
if (mapCount<=mapNum)
{
if (nextMap)
{
GameObject gridGO = Instantiate(grid, root.GetRealPos(), Quaternion.identity);
gridlist.Add(gridGO);
CreatePath(false);
}
}
}
void ResetMap()
{
root = null;
currentNode = null;
mapCount = 1;
ClearMap();
ClearList(cubeList);
ClearList(gridlist);
CreatePath(true);
}
void CreatePath(bool isFirst)
{
pathList.Clear();
nextMap = false;
//root
createRoot(isFirst);
//path
for (int i = 0; i < 6; i++)
{
if (currentNode.e == 0)
{
Vector2 newPos = new Vector2(currentNode.pos.x + 1, currentNode.pos.y);//向右
if (Random.value < rightRate)
{
//Debug.Log("00");
currentNode = new Node(0, 0, newPos);
}
else
{
//Debug.Log("01");
currentNode = new Node(0, 1, newPos);
}
}
else
{
Vector2 newPos = new Vector2(currentNode.pos.x, currentNode.pos.y + 1);//向下
if (Random.value < rightRate)
{
//Debug.Log("10");
currentNode = new Node(1, 0, newPos);
}
else
{
//Debug.Log("11");
currentNode = new Node(1, 1, newPos);
}
}
pathList.Add(currentNode);
}
//check = true;
if (pathList[pathList.Count - 1].pos != new Vector2(3, 3) * mapCount)
{
ClearMap();
CreatePath(isFirst);
}
else
{
UpdateMap(mapCount-1);
instantiateMap(isFirst);
root = currentNode;
nextMap = true;
mapCount++;
}
}
void createRoot(bool isFirst)
{
if (isFirst)
{
if (Random.value < rightRate)
{
root = new Node(0, 0, new Vector2(0, 0));
}
else
{
root = new Node(0, 1, new Vector2(0, 0));
}
}
currentNode = root;
pathList.Add(currentNode);
}
void instantiatePath(Node currentNode,bool root=false)
{
GameObject[] preList = (root) ?ROOT:path;
GameObject pathGO = Instantiate(preList[(int)currentNode.GetPathType()]);
pathGO.transform.position = currentNode.GetRealPos();
cubeList.Add(pathGO);
pathGO.transform.SetParent(cubeParent);
}
void insOrnament(Node currentNode)
{
GameObject orGO = Instantiate(ornament);
orGO.transform.position = currentNode.GetRealPos();
cubeList.Add(orGO);
orGO.transform.SetParent(cubeParent);
}
void instantiateMap(bool isFirst)
{
//if (isFirst)
//{
// instantiatePath(pathList[0], true);
//}
//for (int i = 1; i < pathList.Count; i++)
//{
// instantiatePath(pathList[i]);
//}
if (isFirst)
{
instantiatePath(map[0], true);
}
for (int i = 1; i < map.Count; i++)
{
if (map[i].isFull)
{
instantiatePath(map[i]);
}
else
{
insOrnament(map[i]);
}
}
}
void ClearList(List<GameObject> list)
{
foreach (var item in list)
{
Destroy(item);
}
list.Clear();
}
void ClearMap()
{
pathList.Clear();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawGrid : MonoBehaviour
{
//开始 右00 下01
//路径 右右00 右下01 下下11 下右10
public Transform lt;
public Transform ld;
public Transform rt;
public Transform rd;
public bool drawStart;
[HideInInspector]
public Vector3 horOffset = Vector3.zero;
[HideInInspector]
public Vector3 verOffset = Vector3.zero;
float offset;
private void OnDrawGizmos()
{
if (drawStart)
{
offset = (lt.position.z - rt.position.z) / 4;
horOffset.z = offset;
verOffset.x = -offset;
Gizmos.color = Color.red;
Gizmos.DrawLine(lt.position, ld.position);
Gizmos.DrawLine(ld.position, rd.position);
Gizmos.DrawLine(rd.position, rt.position);
Gizmos.DrawLine(rt.position, lt.position);
Gizmos.color = Color.blue;
Gizmos.DrawLine(rt.position + verOffset, lt.position + verOffset);
Gizmos.DrawLine(rt.position + verOffset * 2, lt.position + verOffset * 2);
Gizmos.DrawLine(rt.position + verOffset * 3, lt.position + verOffset * 3);
Gizmos.DrawLine(rt.position + horOffset, rd.position + horOffset);
Gizmos.DrawLine(rt.position + horOffset * 2, rd.position + horOffset * 2);
Gizmos.DrawLine(rt.position + horOffset * 3, rd.position + horOffset * 3);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment