Skip to content

Instantly share code, notes, and snippets.

@afjk
Created December 7, 2016 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afjk/09912989ca2d709d41630e72ee2195a0 to your computer and use it in GitHub Desktop.
Save afjk/09912989ca2d709d41630e72ee2195a0 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MazeMakerGrow : MonoBehaviour {
public bool growOnStart = false;
public int xMax = 11;
public int yMax = 11;
public int zMax = 11;
public GameObject roadObject;
public GameObject preRoadObject = null;
private GameObject mMazeBase;
private bool makeMazeComplete = false;
private bool isActive = false;
public delegate void MazeMakerEventHandler(object sender);
public event MazeMakerEventHandler MakeComplete;
// Use this for initialization
void Start ()
{
if ( growOnStart )
{
Grow();
}
}
public void Grow()
{
isActive = true;
}
// Update is called once per frame
void Update () {
if( isActive)
{
isActive = false;
// 進行可能な方向を得る
Vector3 direction = GetGoDirection();
if (direction == Vector3.zero)
{
GoBack();
}
else
{
// 進行可能な方向に2セル進む
GoAhead(direction);
}
}
}
Vector3 GetGoDirection()
{
Vector3 result = Vector3.zero;
List<Ray> rayList = new List<Ray>();
Ray ray;
RaycastHit hit;
float distance = 2.0f;
float radious = 1.0f;
ray = new Ray(transform.position, this.transform.forward);
if( !Physics.SphereCast(ray, radious, out hit, distance) )
{
rayList.Add(ray);
}
ray = new Ray(transform.position, -this.transform.forward);
if (!Physics.SphereCast(ray, radious, out hit, distance))
{
rayList.Add(ray);
}
ray = new Ray(transform.position, this.transform.right);
if (!Physics.SphereCast(ray, radious, out hit, distance))
{
rayList.Add(ray);
}
ray = new Ray(transform.position, -this.transform.right);
if (!Physics.SphereCast(ray, radious, out hit, distance))
{
rayList.Add(ray);
}
ray = new Ray(transform.position, this.transform.up);
if (!Physics.SphereCast(ray, radious, out hit, distance))
{
rayList.Add(ray);
}
ray = new Ray(transform.position, -this.transform.up);
if (!Physics.SphereCast(ray, radious, out hit, distance))
{
rayList.Add(ray);
}
if( rayList.Count > 0 )
{
int randIndex = Random.Range(0, rayList.Count);
result = rayList[randIndex].direction;
}
Debug.Log(result);
return result;
}
bool isStart()
{
if( preRoadObject == null )
{
return true;
}
else
{
return false;
}
}
void GoBack()
{
Debug.Log("GoBack:in");
// 進行方向が見つからない場合
// 2セル戻る
// スタート地点なら生成終了
if (isStart())
{
Debug.Log("Maze Generate Complete!");
makeMazeComplete = true;
// 完了通知
// OnMakeComplete();
return;
}
else
{
// 戻る
if( preRoadObject!= null )
{
preRoadObject.GetComponent<MazeMakerGrow>().Grow();
}
}
}
void GoAhead( Vector3 direction )
{
GameObject copySpaceCube1 = Object.Instantiate(roadObject) as GameObject;
copySpaceCube1.transform.position = this.transform.position + direction * 1.0f;
Destroy(copySpaceCube1.GetComponent<MazeMakerGrow>());
GameObject copySpaceCube2 = Object.Instantiate(roadObject) as GameObject;
copySpaceCube2.transform.position = this.transform.position + direction * 2.0f;
copySpaceCube2.GetComponent<MazeMakerGrow>().preRoadObject = this.gameObject;
copySpaceCube2.GetComponent<MazeMakerGrow>().Grow();
}
/* 迷路生成完了通知 */
public virtual void OnMakeComplete()
{
if (MakeComplete != null)
{
MakeComplete(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment