Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Grid2 : MonoBehaviour
  6. {
  7.     public GameObject prefab;
  8.     public GameObject prefab2;
  9.     GameObject addedCell;
  10.  
  11.  
  12.  
  13.     void Start()
  14.     {
  15.         List<Vector3> path = Grid.gridpath; //lista ścieżki
  16.         Debug.Log(prefab.transform.localScale);
  17.         List<Grid> cells = Grid.gridCells; //lista siatki
  18.  
  19.         for (int i = 0; i < Grid.mapSizeX; i++) //stworzenie listy siatki
  20.         {
  21.             for (int j = 0; j < Grid.mapSizeY; j++)
  22.             {
  23.                 Vector3 position = new Vector3(i * prefab.transform.localScale.x, 0f, j * prefab.transform.localScale.y);
  24.                 cells.Add(new Grid(position, (GameObject)Instantiate(prefab, position, Quaternion.identity, transform.parent = transform)));
  25.             }
  26.         }
  27.  
  28.  
  29.  
  30.         Vector3 currentpos = cells[0].pos;
  31.         path.Add(currentpos);
  32.         cells[0].visited = true;
  33.  
  34.         for (int i = 0; i < 150; i++)
  35.         {
  36.  
  37.             Vector3 temppos = currentpos;
  38.             int index = cells.FindIndex(f => f.pos == currentpos);
  39.             // 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);
  40.             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);
  41.  
  42.             if (posMovs.Count == 0)
  43.             {
  44.                 do
  45.                 {
  46.                     Debug.Log("//////////////////////////////////////");
  47.                     Debug.Log(path.Count);
  48.                     Debug.Log("stuck my currpos: " + currentpos);
  49.                     currentpos = cells[index].myParent;
  50.                     Debug.Log("stuck my currpos(after myparent): " + currentpos);
  51.                     Debug.Log("//////////////////////////////////////");
  52.                     index = cells.FindIndex(f => f.pos == currentpos);
  53.                    // 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);
  54.                     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);
  55.  
  56.                 } while (posMovs.Count == 0);
  57.             }
  58.            /* int k = Random.Range(0, posMovs.Count);
  59.             currentpos = posMovs[k].pos; */
  60.             currentpos = posMovs[Random.Range(0, posMovs.Count)].pos;
  61.             index = cells.FindIndex(f => f.pos == currentpos);
  62.                 cells[index].myParent = temppos;
  63.                 cells[index].visited = true;
  64.             path.Add(currentpos);
  65.  
  66.             posMovs.Clear();
  67.  
  68.          }
  69.  
  70.         foreach (Vector3 p in path)
  71.         {
  72.             Instantiate(prefab2, p + new Vector3(0, 7f, 0), Quaternion.identity);
  73.         }
  74.  
  75.  
  76.     }
  77.         void Update()
  78.     {
  79.  
  80.     }
  81.  
  82.  
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement