create and collect voxel debris from exploded voxels
npm install voxel-debriscreate and collect voxel debris from exploded voxels
Note: use my build of voxel-engine
until the game.THREE = THREE patch lands in the version on npm.
`` js
var createEngine = require('voxel-engine')
var game = createEngine({
generate: function(x, y, z) {
var d = Math.sqrt(xx + yy + z*z);
var r = Math.floor(Math.random() * 4) + 1;
if (y < -10) return 0;
if (y < 4) return r (xx + zz <= 4040);
return r * (d <= 20);
},
texturePath: './',
materials: [ 'dirt', 'grass', 'crate', 'brick' ]
});
game.appendTo('#container');
var explode = require('../')(game, {
yield: function (value) {
return {
dirt: 10,
grass: 4,
crate: 2,
brick: 3
}[game.materials[value-1]];
}
});
explode.on('collect', function (item) {
console.log(game.materials[item.value - 1]);
});
game.on('mousedown', function (pos) {
if (erase) {
explode(pos);
game.setBlock(pos, 0);
}
else {
game.createBlock(pos, 1);
}
});
window.addEventListener('keydown', ctrlToggle);
window.addEventListener('keyup', ctrlToggle);
var erase = true
function ctrlToggle (ev) { erase = !ev.ctrlKey }
game.requestPointerLock('canvas');
`
` js`
var voxelDebris = require('voxel-debris')
Create an explosion function from a
voxel-engine
instance and some options.
If opts.limit(item) is specified, when it returns true, item will not be
collected by the player when they pass within the collision radius.
If opts.yield(value) is specified, its return value will be how many debrisvalue
items are created for the voxel data .
opts.expire.start and opts.expire.end control how long debris should persistopts.expire
in the game world in milliseconds. A timeout will be chosen uniformly randomly
between start and end.
When is a number, its value will be used for both start and end.
Explode the block at the THREE.Vector3 position pos if the data at pos is
non-zero.
When a debris item passes within the collision radius, the 'collect' event
fires with the item object before being removed from the scene.
With npm do:
``
npm install voxel-debris
Then use browserify to require('voxel-debris')`.
MIT