Пример #1
0
        private void GenerateMainPath(int mainPathLength)
        {
            mainPath = new List <Vector2i>();
            MapChunk curChunk = MapChunk.CreateMapChunk(startingRoom);

            Grid.SetChunk(new Vector2i(0, 0), curChunk, "Start");
            mainPath.Add(curChunk.FakePos);
            Side nextSide = null;
            bool stuck    = false;

            for (int i = 0; i < mainPathLength; i++)
            {
                List <Side> possibleSides = curChunk.GetAllOpenUnusedSides();   // Get All the possible sides (i.e: open and unused)
                nextSide = possibleSides[Random.Range(0, possibleSides.Count)]; // Pick a new possible side
                possibleSides.Remove(nextSide);
                Orientation    requiredOri = nextSide.Orient.GetOposite();
                MapChunkPrefab prefab      = null;
                while (Grid.GetAdjacentSidesLeadingToTile(nextSide.GetAdjacentPos(), requiredOri).Length > 2)
                {
                    if (possibleSides.Count == 0)
                    {
                        stuck = true;
                        break;
                    }
                    nextSide = possibleSides[Random.Range(0, possibleSides.Count)]; // Pick a new possible side
                    possibleSides.Remove(nextSide);
                    requiredOri = nextSide.Orient.GetOposite();
                }
                List <MapChunkPrefab> possiblePrefabs = GetAllRoomPrefabs(requiredOri, nextSide.Type, Grid.GetAdjacentSidesOccupied(nextSide.GetAdjacentPos(), requiredOri));
                prefab = possiblePrefabs[Random.Range(0, possiblePrefabs.Count)]; // Pick a new possible side
                possiblePrefabs.Remove(prefab);
                while (prefab.OpenCount < 2)
                {
                    if (possiblePrefabs.Count == 0)
                    {
                        stuck = true;
                        break;
                    }
                    prefab = possiblePrefabs[Random.Range(0, possiblePrefabs.Count)]; // Pick a new possible side
                    possiblePrefabs.Remove(prefab);
                }
                if (stuck)
                {
                    Debug.Log("Path Stuck!");
                    break;
                }
                curChunk = MapChunk.CreateMapChunk(prefab);
                Grid.SetChunk(nextSide.GetAdjacentPos(), curChunk, "Main");
                mainPath.Add(curChunk.FakePos);
            }
            nextSide = curChunk.GetRandomOpenUnusedSide();
            curChunk = MapChunk.CreateMapChunk(endingRoom);
            Grid.SetChunk(nextSide.GetAdjacentPos(), curChunk, "End");
            mainPath.Add(curChunk.FakePos);
        }
Пример #2
0
 public void OnPlayerChangedRoom(MapChunk nRoom)
 {
     if (!nRoom.gameObject.activeInHierarchy)
     {
         nRoom.gameObject.SetActive(true);
         foreach (MobSpawner spawner in nRoom.transform.GetComponentsInChildren <MobSpawner>())
         {
             spawner.SpawnMob(1f);
         }
     }
 }
Пример #3
0
        public static MapChunk CreateMapChunk(MapChunkPrefab prefab)
        {
            GameObject go       = Instantiate <GameObject>(prefab.prefab, MapManager.I.mapRoot);
            MapChunk   mapChunk = go.AddComponent <MapChunk>();

            mapChunk.Top    = new Side(mapChunk, Orientation.TOP, prefab.top);
            mapChunk.Left   = new Side(mapChunk, Orientation.LEFT, prefab.left);
            mapChunk.Bottom = new Side(mapChunk, Orientation.BOTTOM, prefab.bottom);
            mapChunk.Right  = new Side(mapChunk, Orientation.RIGHT, prefab.right);
            return(mapChunk);
        }
Пример #4
0
        public void SetChunk(Vector2i fakeGridPos, MapChunk chunk, string specialName)
        {
            Vector2i gridPos = FakeToRealGridPos(fakeGridPos);

            chunk.FakePos = fakeGridPos;
            if (chunks[gridPos.x, gridPos.y] != null)
            {
                GameObject.Destroy(chunks[gridPos.x, gridPos.y].gameObject);
            }
            chunks[gridPos.x, gridPos.y] = chunk;
            chunk.transform.position     = new Vector3(realChunkWidth * fakeGridPos.x, realChunkHeight * fakeGridPos.y, 0f);
            chunk.gameObject.SetActive(true);
            //chunk.name = "Chunk " + (index++);
            chunk.name = "C" + (index++) + "-";
            foreach (Orientation ori in chunk.GetAllOpenSidesOrientations())
            {
                chunk.name += ori.ToString()[0];
            }
            chunk.name += " " + specialName;
            //chunk.gameObject.SetActive(false);
            UpdateAdjacents(gridPos);
        }