Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Grid2 : MonoBehaviour
- {
- public GameObject prefab;
- public GameObject prefab2;
- GameObject addedCell;
- void Start()
- {
- List<Vector3> path = Grid.gridpath; //lista ścieżki
- Debug.Log(prefab.transform.localScale);
- List<Grid> cells = Grid.gridCells; //lista siatki
- for (int i = 0; i < Grid.mapSizeX; i++) //stworzenie listy siatki
- {
- for (int j = 0; j < Grid.mapSizeY; j++)
- {
- Vector3 position = new Vector3(i * prefab.transform.localScale.x, 0f, j * prefab.transform.localScale.y);
- cells.Add(new Grid(position, (GameObject)Instantiate(prefab, position, Quaternion.identity, transform.parent = transform)));
- }
- }
- Vector3 currentpos = cells[0].pos;
- path.Add(currentpos);
- cells[0].visited = true;
- for (int i = 0; i < 150; i++)
- {
- Vector3 temppos = currentpos;
- int index = cells.FindIndex(f => f.pos == currentpos);
- // List<Grid> posMovs = cells.FindAll(f => (f.pos == cells[index].pos + Vector3.forward * cells[index].cellPrefab.transform.localScale.z || f.pos == cells[index].pos + Vector3.back * cells[index].cellPrefab.transform.localScale.z || f.pos == cells[index].pos + Vector3.left * cells[index].cellPrefab.transform.localScale.x || f.pos == cells[index].pos + Vector3.right * cells[index].cellPrefab.transform.localScale.x) && f.visited == false);
- List<Grid> posMovs = cells.FindAll(f => (f.pos == cells[index].pos + Vector3.forward * 2 * cells[index].cellPrefab.transform.localScale.z || f.pos == cells[index].pos + Vector3.back * 2 * cells[index].cellPrefab.transform.localScale.z || f.pos == cells[index].pos + Vector3.left * 2 * cells[index].cellPrefab.transform.localScale.x || f.pos == cells[index].pos + Vector3.right * 2 * cells[index].cellPrefab.transform.localScale.x) && f.visited == false);
- if (posMovs.Count == 0)
- {
- do
- {
- Debug.Log("//////////////////////////////////////");
- Debug.Log(path.Count);
- Debug.Log("stuck my currpos: " + currentpos);
- currentpos = cells[index].myParent;
- Debug.Log("stuck my currpos(after myparent): " + currentpos);
- Debug.Log("//////////////////////////////////////");
- index = cells.FindIndex(f => f.pos == currentpos);
- // posMovs = cells.FindAll(f => (f.pos == cells[index].pos + Vector3.forward * cells[index].cellPrefab.transform.localScale.z || f.pos == cells[index].pos + Vector3.back * cells[index].cellPrefab.transform.localScale.z || f.pos == cells[index].pos + Vector3.left * cells[index].cellPrefab.transform.localScale.x || f.pos == cells[index].pos + Vector3.right * cells[index].cellPrefab.transform.localScale.x) && f.visited == false);
- posMovs = cells.FindAll(f => (f.pos == cells[index].pos + Vector3.forward * 2 * cells[index].cellPrefab.transform.localScale.z || f.pos == cells[index].pos + Vector3.back * 2 * cells[index].cellPrefab.transform.localScale.z || f.pos == cells[index].pos + Vector3.left * 2 * cells[index].cellPrefab.transform.localScale.x || f.pos == cells[index].pos + Vector3.right * 2 * cells[index].cellPrefab.transform.localScale.x) && f.visited == false);
- } while (posMovs.Count == 0);
- }
- /* int k = Random.Range(0, posMovs.Count);
- currentpos = posMovs[k].pos; */
- currentpos = posMovs[Random.Range(0, posMovs.Count)].pos;
- index = cells.FindIndex(f => f.pos == currentpos);
- cells[index].myParent = temppos;
- cells[index].visited = true;
- path.Add(currentpos);
- posMovs.Clear();
- }
- foreach (Vector3 p in path)
- {
- Instantiate(prefab2, p + new Vector3(0, 7f, 0), Quaternion.identity);
- }
- }
- void Update()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement