Overv / WebCraft

Minecraft clone written in Javascript.
zlib License
386 stars 179 forks source link

Fluid Elements #30

Open andrewgrider opened 6 years ago

andrewgrider commented 6 years ago

You can't walk through fluid elements, you just stand on them, and they expand till they cover the map or till they hit another block alt text

p3rlphr33k commented 2 years ago

I have made adjustments to this recently when I started working on this again. I realize this is an old issue but i thought I would share.

I have added a stepping system into fluids to track increments and prevent flooding. You can adjust the maxStep variable to how many steps/size increases you want to allow water to spread.

in physics.js replace fluids section with this:

` // Fluids if ( step % 5 == 0 ) { // Newly spawned fluid blocks are stored so that those aren't // updated in the same step, creating a simulation avalanche. var newFluidBlocks = {}; var maxStep = 5; for ( var x = 0; x < world.sx; x++ ) { for ( var y = 0; y < world.sy; y++ ) { for ( var z = 0; z < world.sz; z++ ) { var material = blocks[x][y][z];

                if ( material.fluid && newFluidBlocks[x+","+y+","+z] == null && material.step < maxStep)
                {
                    console.log('spread step: '+material.step);
                    if ( x > 0 && blocks[x-1][y][z] == BLOCK.AIR ) {
                        world.setBlock( x - 1, y, z, material );
                        newFluidBlocks[(x-1)+","+y+","+z] = true;
                    }
                    if ( x < world.sx - 1 && blocks[x+1][y][z] == BLOCK.AIR ) {
                        world.setBlock( x + 1, y, z, material );
                        newFluidBlocks[(x+1)+","+y+","+z] = true;
                    }
                    if ( y > 0 && blocks[x][y-1][z] == BLOCK.AIR ) {
                        world.setBlock( x, y - 1, z, material );
                        newFluidBlocks[x+","+(y-1)+","+z] = true;
                    }
                    if ( y < world.sy - 1 && blocks[x][y+1][z] == BLOCK.AIR ) {
                        world.setBlock( x, y + 1, z, material );
                        newFluidBlocks[x+","+(y+1)+","+z] = true;
                    }

                    material.step++;
                }
            }
        }
    } 
}`

In block.js update BLOCK.WATER, BLOCK.LAVA, or any other liquids you might have so they have a step option:

` // Water BLOCK.WATER = { id: 19, strike: 1, spawnable: true, breakable: false, transparent: true, selflit: false, gravity: true, fluid: true, step: 1, fire: false, texture: function( world, lightmap, lit, x, y, z, dir ) { return [ 13/16, 12/16, 14/16, 13/16 ]; },

icon: "water.png"

}; `