1
\$\begingroup\$

I have a 2D infinite tile map game in which the player can place floors and walls to construct "buildings".

I'm trying to think of an efficient way to determine whether the player is inside a "complete" building, meaning it's fully enclosed by walls and a door.

Normally, the logic isn't difficult since it's easy to tell if the walls fully encircle an area - but in the case of an infinite map, the "building" may extend into unloaded areas of the game.

For example, if a player makes a complete rectangular building, 10x100 tiles long, only some of the chunks it covers will be loaded at once.

I have a few ideas:

  • If a player is near candidate walls, follow them and load all necessary chunks to check completeness. This would work, but could load a moderate number of unused chunks.
  • Cache a list of "complete" buildings as they're completed. So option #1 is applied only as the player builds the building. This would have to saved/loaded, and updated when integral walls/doors are removed, so it trades efficiency for a little overhead, which might be OK.
  • Using a different metric to determine "indoors". I can't think of one though.

Anyone have any other ideas? Has this ever been implemented somewhere else, something I can at least use as an example?

\$\endgroup\$
1
\$\begingroup\$

I'll try to extend your second option. Add a falg "indoor" on your tiles. When you finish a building set the "indoor" true on all tiles inside the building. When the player is on a "indoor" tile he is inside a building. If you need to know in wich buiding you are inside, make "indoor" a pointer to a building in your

list of "complete" buildings

.

\$\endgroup\$
  • \$\begingroup\$ That's an interesting solution. It requires the same determination logic as option 2, moves the caching to the tiles themselves, has an initial higher cost to "write" the info to all tiles "inside" (since it would cache the bool in all tiles, not just coordinates of the walls), yet is also cheaper to lookup every frame which is more important. Unless I find something better, I think I'll start with this. If something doesn't work it can easily be modified into the original option #2. \$\endgroup\$ – BotskoNet Jul 7 '17 at 14:27
  • \$\begingroup\$ @BotskoNet Please accept this answer (checkmark) if it helped you. \$\endgroup\$ – Engineer Feb 13 '18 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.