• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TrivialRoo · Jun 20, 2015 at 03:16 AM · c#proceduralgenerationmodelingdungeon

How would I change my procedurally generated map change cells if it detects cells are around it?

Hi, so I have two models, an "intersection" piece, and a "straight line" piece. The straight line piece has walls.

I'm having trouble figuring out how to change the tiles into different tiles if the "cell" or tile detects other tiles already near it.

Here's my script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Instance : MonoBehaviour {
 
     public LevelCell cellPrefab;
     private LevelCell[,] cells;
 
     public IntVector2 size;
 
     public LevelPassage[] passages;
 
     public LevelCell GetCell (IntVector2 coordinates) {
         return cells[coordinates.x, coordinates.z];
     }
 
     public void Generate() {
         cells = new LevelCell[size.x, size.z];
         List<LevelCell> activeCells = new List<LevelCell>();
         DoFirstGenerationStep(activeCells);
         while (activeCells.Count > 0) {
             DoNextGenerationStep(activeCells);
         }
 
         if (this.transform.childCount <= 5) {
             Generate ();
         }
     }
 
     private void DoFirstGenerationStep (List<LevelCell> activeCells) {
         activeCells.Add (CreateCell (RandomCoordinates));
     }
 
     private void DoNextGenerationStep (List<LevelCell> activeCells) {
         int currentIndex = activeCells.Count - 1;
         LevelCell currentCell = activeCells[currentIndex];
         if (currentCell.IsFullyInitialized) {
             activeCells.RemoveAt (currentIndex);
             return;
         }
         LevelDirection direction = currentCell.RandomUninitializedDirection;
         IntVector2 coordinates = currentCell.coordinates + direction.ToIntVector2();
         if (ContainsCoordinates(coordinates) && GetCell(coordinates) == null) {
             LevelCell neighbor = GetCell(coordinates);
             if (neighbor == null) {
                 neighbor = CreateCell (coordinates);
                 CreatePassage(currentCell, neighbor, direction, 0);
                 activeCells.Add (neighbor);
             }
             else {
                 CreatePassage (currentCell, neighbor, direction, 1);
                 activeCells.RemoveAt (currentIndex);
             }
         }
         else {
             activeCells.RemoveAt (currentIndex);
         }
     }
 
     private void CreatePassage(LevelCell cells, LevelCell otherCell, LevelDirection direction, int tileID) {
         LevelPassage passage = Instantiate (passages[tileID]) as LevelPassage;
         passage.Initialize (cells, otherCell, direction);
         if (otherCell != null) {
             passage = Instantiate (passages[1]) as LevelPassage;
             passage.Initialize(otherCell, cells, direction.GetOpposite());
         }
     }
 
     public IntVector2 RandomCoordinates {
         get {
             return new IntVector2(Random.Range (0, size.x), Random.Range (0, size.z));
         }
     }
 
     public bool ContainsCoordinates(IntVector2 coordinate) {
         return coordinate.x >= 0 && coordinate.x < size.x && coordinate.z >= 0 && coordinate.z < size.z;
     }
 
     private LevelCell CreateCell(IntVector2 coordinates) {
         LevelCell newCell = Instantiate (cellPrefab) as LevelCell;
         cells[coordinates.x, coordinates.z] = newCell;
         newCell.coordinates = coordinates;
         newCell.name = "Maze Cell " + coordinates.x + ", " + coordinates.z;
         newCell.transform.parent = transform;
         newCell.transform.localPosition = new Vector3(coordinates.x - size.x * 0.5f + 0.5f, 0f, coordinates.z - size.z * 0.5f + 0.5f);
         return newCell;
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Procedurally Generated Cube Mesh 3 Answers

Drunkard Walk Algorithm C# Question 0 Answers

Multiple Cars not working 1 Answer

Help in understanding Mesh Generation 1 Answer

BSP Tree for dungeons generation, HELP! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges