Simple Air/Contamination flow system design

So i’m looking to create a system where the rooms in my game (topdown 3D if that matters) have a certain amount of oxygen and possibility of contamination (such as fire or gas) and i wanted to allow for this to spread as needed.

Since my entire game is rooms and corridors my though is to group each rooms static elements (The prefabs the make the walls and floor) under a game object called Room. This would then have a script that manages the amount of air in the room and the contamination. it also keeps track of doors that link to this room. And those doors have a script that will notify when the door is opened or closed.

This way each room knows how much air it has, and which rooms are opened to it, and as such it can work out if the air should flow out of the room, or into the room. My though was to have each room act as a provider. So it would only handle airflow out of the room (as such each rooms airflow in would be handled by either a provider such as an air vent, or by other rooms)

The only issue I can see with this approach is that each room would have it’s own script for managing this and as such tweaking things such as flow rate could become a pain. Is this something that should be more of a singleton that handles the airflow for all rooms. Or do i have the right idea making each room manage it’s own airflow?

Hopefully I’ve explained myself well enough. And i appreciate any suggestions.

I think your approach sounds fine. If you need some global variables, such as an overall flow rate, you can store those in statics, or in some singleton object that serves just that purpose.

Hey JoeStrout.

Yeah I think thats the route i’m going to take using a singleton “Manager” that handles base flow rates ect. this also then gives me the ability to have certain specific rooms have multipliers to the flow rate.

Another quick question if it’s not too much outside of the original. I’m trying to work out the best way to keep track of the room my player is currently inside. I originally planned to use triggers wrapping the entire room but that seems like it would be a pain to manage and very possibly a resource hog (I must admit i have no idea how expensive colliders as triggers are), I can imagine having dozens of rooms, plus every corridor having at least one trigger would be a pain

My next idea was to have a trigger on the doorway since the doors know which rooms they are connected too and doing a simple check if currentRoom == linkedRoom1 currentRoom = linkRoom2 and the reverse. however i then realised that the player could enter the doorway and hence the trigger, and then turn around and reenter the same room. which would leave my script with the wrong current room. I’m not entirely sure how to work that into a system generic enough that i could track the direction the player left the trigger and tie it to the linked room without having to adjust every single script for orientations.

Thanks.

Yeah, triggers on the doorways is just asking for trouble.

Triggers on the rooms is a pretty decent idea, though.

The other thing you could do is draw a map of the level, with a unique color for each room; then look up the pixel color at the map position corresponding to the player. But that’s a lot of work to go to… the triggers are probably easier.

Thanks for your help Joe. I ended up having a trigger at each entrance to each room which contains a reference to the room. That became a prefab with a little debug warning if i forget to set the room. So it takes a few extra seconds to setup each room but not a big issue. That trigger than let’s the player know which room he’s in.

Thanks again

1 Like