Created
December 7, 2016 01:04
-
-
Save afjk/ad0e8fd7c5d0f0047dde5f5c2a4b7063 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class MazeMakerGrowInObj : MonoBehaviour { | |
public bool growOnStart = false; | |
public int xMax = 11; | |
public int yMax = 11; | |
public int zMax = 11; | |
public GameObject coverObject; | |
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(coverObject == null ) | |
{ | |
Collider[] colliders = Physics.OverlapBox(transform.position, new Vector3(0.5f, 0.5f, 0.5f)); | |
foreach (Collider item in colliders) | |
{ | |
if (item.gameObject != this.gameObject ) | |
{ | |
coverObject = item.gameObject; | |
break; | |
} | |
// item.GetComponent<MeshRenderer>().material.color = Color.green; | |
} | |
} | |
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); | |
} | |
} | |
} | |
bool InCoverObj(Vector3 pos) | |
{ | |
bool result = false; | |
if (coverObject) | |
{ | |
Collider[] colliders = Physics.OverlapBox(pos, new Vector3(0.5f, 0.5f, 0.5f)); | |
foreach (Collider item in colliders) | |
{ | |
if (item.gameObject == coverObject) | |
{ | |
result = true; | |
break; | |
} | |
else if( item.transform.root.gameObject == coverObject) | |
{ | |
result = true; | |
break; | |
} | |
// item.GetComponent<MeshRenderer>().material.color = Color.green; | |
} | |
} | |
else | |
{ | |
result = true; | |
} | |
return result; | |
} | |
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)) | |
{ | |
if( InCoverObj(transform.position + ray.direction * 2.5f ) ) | |
rayList.Add(ray); | |
} | |
ray = new Ray(transform.position, -this.transform.forward); | |
if (!Physics.SphereCast(ray, radious, out hit, distance)) | |
{ | |
if (InCoverObj(transform.position + ray.direction * 2.5f)) | |
rayList.Add(ray); | |
} | |
ray = new Ray(transform.position, this.transform.right); | |
if (!Physics.SphereCast(ray, radious, out hit, distance)) | |
{ | |
if (InCoverObj(transform.position + ray.direction * 2.5f)) | |
rayList.Add(ray); | |
} | |
ray = new Ray(transform.position, -this.transform.right); | |
if (!Physics.SphereCast(ray, radious, out hit, distance)) | |
{ | |
if (InCoverObj(transform.position + ray.direction * 2.5f)) | |
rayList.Add(ray); | |
} | |
ray = new Ray(transform.position, this.transform.up); | |
if (!Physics.SphereCast(ray, radious, out hit, distance)) | |
{ | |
if (InCoverObj(transform.position + ray.direction * 2.5f)) | |
rayList.Add(ray); | |
} | |
ray = new Ray(transform.position, -this.transform.up); | |
if (!Physics.SphereCast(ray, radious, out hit, distance)) | |
{ | |
if (InCoverObj(transform.position + ray.direction * 2.5f)) | |
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<MazeMakerGrowInObj>().Grow(); | |
} | |
} | |
} | |
void GoAhead(Vector3 direction) | |
{ | |
GameObject copySpaceCube1 = Object.Instantiate(roadObject) as GameObject; | |
copySpaceCube1.transform.position = this.transform.position + direction * 1.0f; | |
Destroy(copySpaceCube1.GetComponent<MazeMakerGrowInObj>()); | |
GameObject copySpaceCube2 = Object.Instantiate(roadObject) as GameObject; | |
copySpaceCube2.transform.position = this.transform.position + direction * 2.0f; | |
copySpaceCube2.GetComponent<MazeMakerGrowInObj>().preRoadObject = this.gameObject; | |
copySpaceCube2.GetComponent<MazeMakerGrowInObj>().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