I have been trying to come up with a way to have a fluid spread out in a 2d top down game. I came up with several different ways but kept on running into concurrency issues and the biggest issue of it wanting to make a bazzillion water tiles over a 6 x 6 area. I have a room where the wall tiles are surrounding a 4 x 4 block of floor tiles which are represented in another z level. There is one water tile with a density of 100 that should spread out and diminish which is located on the same z level as the walls. The latest one I feel should work but still it ends up creating thousands of objects. What I’m doing here is just going ahead and adding the new water objects into the fluid objects array. Then getting the index of the ones that won’t be moved to the bigger list, then removing them. I hate coming for help but I spent maybe 6 -10 hours setting up for fluids and probably 5 of it on the problem coming up with all kinds of ways of doing it. For some reason the integer array i’s size is getting into the thousands for reason, I have no idea since i should be at max 8.
public void flow(Objects fluid, ArrayList<Objects> worldObjects)
{
fluidObjects.add(new Fluids(fluid.x - 1, fluid.y - 1, fluid.z, fluid.id, (fluid.density / 2) / 8));
fluidObjects.add(new Fluids(fluid.x, fluid.y - 1, fluid.z, fluid.id, (fluid.density / 2) / 8));
fluidObjects.add(new Fluids(fluid.x + 1, fluid.y - 1, fluid.z, fluid.id, (fluid.density / 2) / 8));
fluidObjects.add(new Fluids(fluid.x - 1, fluid.y, fluid.z, fluid.id, (fluid.density / 2) / 8));
fluidObjects.add(new Fluids(fluid.x + 1, fluid.y, fluid.z, fluid.id, (fluid.density / 2) / 8));
fluidObjects.add(new Fluids(fluid.x - 1, fluid.y + 1, fluid.z, fluid.id, (fluid.density / 2) / 8));
fluidObjects.add(new Fluids(fluid.x, fluid.y + 1, fluid.z, fluid.id, (fluid.density / 2) / 8));
fluidObjects.add(new Fluids(fluid.x + 1, fluid.y + 1, fluid.z, fluid.id, (fluid.density / 2) / 8));
fluid.density = fluid.density - (fluid.density / 2);
ArrayList<Integer> i = new ArrayList<Integer>();
int count = 0;
for(Objects obj: worldObjects) // Goes through all objects in 10 x 10 world
{
for(Objects fObj: fluidObjects) // 8 new water objects
{
if((fObj.x == obj.x && fObj.y == obj.y && fObj.z == obj.z) &&
(obj.stopsFluid == true || fObj.id == obj.id || fObj.density < 5)) // Tests to see if any should be deleted do to, walls other water
{
i.add(count); // Adds the objects index into a integer array list for later
}
count++;
}
} count = 0;
for(Integer j: i) // Goes through the integer index array list
{
fluidObjects.remove(j); // Removes the fluid from the 8 that shouldn't be
}
i.clear(); // Clears the other int array list.
objects.addAll(fluidObjects); // Adds the working fluids from the 8 to a list that gets stuck into the master list
fluidObjects.clear(); // in the other class.
}