OndrejNepozitek / Edgar-Unity

Unity Procedural Level Generator
https://ondrejnepozitek.github.io/Edgar-Unity/docs/introduction
MIT License
817 stars 70 forks source link

Are there any ways to get size(or bounds) of the rooms? (or the room's doors position) #45

Closed longtran2904 closed 4 years ago

longtran2904 commented 4 years ago

I have a question: How to get the size of each generated rooms? In my game, i want to know the which rooms the player's at so the way to do it is just take the room position and the size of the room and compare it to the player position. I know how to get the room position but what about the size? Or it will be easier if you can show me how to get the doors position?

OndrejNepozitek commented 4 years ago

Hey! Thanks for the question. Unfortunately, the documentation is not yet complete, so this feature is not documented.

To get information about a room, you need to get an instance of the RoomInstance class. There are at least two ways to do that:

  1. You can create a post process task and register it in the generator. Each post process task receives an instance of the GeneratedLevel class on which you can call the GetRoomInstances() method to get information about all the rooms. You can look at the Example1PostProcess class to see how that's done. It's also in the documentation.
  2. After a layout is generated, each generated room game object (located in Root game object -> Room template instances) gets a RoomInfo component which holds the corresponding RoomInstance. So if you can get the game object that corresponds to the room in which you're insterested, you can get the RoomInstance from there.

Each RoomInstance contains useful information about the room.

If you're interested in the size of the room, you can use the OutlinePolygon property. This class has a public List<Vector2Int> GetOutlinePoints() method which you can use to compute the bounds of the room.

The RoomInstance class also contains information about all the doors of the room in the List<DoorInstance> Doors property. For each room connected to your room, there is one instance of the DoorInstance class. From this instance, you can easily see to which room is the room connected (ConnectedRoomInstance property) or get the information about the position of the door (DoorLine property). The DoorLine contains all the points of the door. So if you want to get door's position, you can use doorInstance.DoorLine.From or doorInstance.DoorLine.To. If your doors are of length 1, From equals to To.

Both RoomInstance and 'DoorInstance` classes should be documented in code. (I'd give you links to the source code, but Github seems to have some problems now).

Let me know if it works.

longtran2904 commented 4 years ago

Thank you for your reply. I've already knew the first way in the example but i haven't knew about the RoomInfo so i will try it out. I have to say that this is the best 2d generator plugin ever and i really need this for my game. So thanks for upload this.

All the best.

OndrejNepozitek commented 4 years ago

I'm glad you like it. If you have any screenshots of your game to share (now or after you finish it) , I'd love to see how people use this plugin in their games.

longtran2904 commented 4 years ago

A small question: In the list that GetOutlinePoints() return what is the order of the conner?

OndrejNepozitek commented 4 years ago

They should always be in the clockwise order. If you tell me what you're trying to accomplish I can maybe point you in the right direction.