- Home /
How to detect if player made building is air-tight(2D)
So, I am making a 2D block based game. I need to be able to tell if a custom, player made building is completely air-tight (So pretty much detecting if it is a completed house, with no holes outside). I had an idea how to do this, but I don't think it will work. Here was my first Idea-
So I was going to make an empty game object that would float around the inside of the building with ray casts on all sides checking to make sure there is a wall on all sides. When the game object hit a wall, it would go another way. If it found that there was a wall missing, it wouldn't be air-tight. Problem is, this probably isn't the best way to do this, and I don't know how I would spawn it in the building in the first place(How I would be able to check if one needs to be spawned).
So, I don't really know how to approach this. Steering me in the right direction would help me out a lot. If there is another post on this or a tutorial you know that could help me, I would appreciate if you could comment it.
Bonus: What would be a good way to get all the air blocks in the room (once it detects the room is air-tight) into a group? Any ideas for that would be greatly appreciated.
Thanks! Looked it up and read about it, just thought of how I might do it! Really helped.
Answer by Cherno · May 16, 2014 at 02:20 AM
It depends how your grid system works. It would be easiest if you have a 2-dimensional int array that takes xy coordinates to set the contents of this coordinate, like 0 for nothing, 1 for wall, 2 for door or whatever. Then you can just iterate through all positions and check each one's neighbors, so that when an "in-house" coordinate has a neighbouring "not-in-house" block, you know that it's not airtight.
If you are using actual gameobjects for each grid space, you can either do the same, or use four raycasts for each in-house space.
First option seems the best, Ill have to change it around a bit but seems its like a good idea, didn't think of it. Also, you mention checking a neighbouring block, what would be the best way to do that?
public int xSize = 10;
public int ySize = 10;
public bool airtight;
public int[,] fillData;//0 = nothing, 1 = inside house, 2 = outside house, 3 = ...
void checkIfAirtight() {
airtight = true;
for(int i = 0; i < xSize; i++) {
for(int y = 0; y < ySize; y++) {
if(x - 1 == 2 || x + 1 == 2 || y - 1 == 2 || y + 1 == 2) {
airtight = false;
}
}
}
if( airtight == true) {
Print("House is airtight!");
}
else {
Print("House is not airtight!");
}
}
Your answer
Follow this Question
Related Questions
Making the players head face toward the location of the mouse? 1 Answer
Mouse Click to keyCode 1 Answer
Create clickable GameObjects (JS) 1 Answer
Evac city outdated? 0 Answers
Creating a Shadow tail for a 2D Character when walking 1 Answer