Skip to content

Commit

Permalink
Merge branch 'game-shell-voxel' into ndarray
Browse files Browse the repository at this point in the history
Conflicts:
	.gitignore
	LICENSE
	README.md
	package.json

https://github.com/deathcap/game-shell-voxel
  • Loading branch information
deathcap committed Apr 17, 2014
2 parents 635b3a1 + 18a1112 commit 474ad64
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 4 deletions.
18 changes: 16 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
node_modules
dist
dist
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

npm-debug.log
node_modules/*
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<<<<<<< HEAD
Copyright (c) 2013, Max Ogden
All rights reserved.

Expand All @@ -7,3 +8,27 @@ Redistributions of source code must retain the above copyright notice, this list
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=======

The MIT License (MIT)

Copyright (c) 2013 Mikola Lysenko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
>>>>>>> game-shell-voxel
28 changes: 28 additions & 0 deletions demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

var createShell = require('./');
var createGUI = require('dat-gui');

require('voxel-plugins-ui');
require('kb-bindings-ui');
require('voxel-drop');
require('voxel-keys');
require('voxel-artpacks');
require('voxel-wireframe');
require('./lib/blocks.js');
require('./lib/terrain.js');

createShell({require: require, pluginOpts:
{
//'voxel-stitch': {debug: true},
'voxel-plugins-ui': {gui: new createGUI.GUI()},
'kb-bindings-ui': {},
'voxel-drop': {},
'voxel-keys': {},
'voxel-artpacks': {},
'voxel-wireframe': {},
'./lib/blocks.js': {},
'./lib/terrain.js': {},
}
});

27 changes: 27 additions & 0 deletions lib/blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

// sample blocks for demo.js TODO: move to voxel-land as a proper plugin, refactor with lib/examples.js terrain gen

module.exports = function(game, opts) {
return new BlocksPlugin(game, opts);
};
module.exports.pluginInfo = {
loadAfter: ['voxel-registry']
};

function BlocksPlugin(game, opts) {
var registry = game.plugins.get('voxel-registry');

registry.registerBlock('dirt', {texture: 'dirt'})
registry.registerBlock('stone', {texture: 'stone'})
registry.registerBlock('cobblestone', {texture: 'cobblestone'})
registry.registerBlock('lava', {texture: 'lava_still', transparent: true})
registry.registerBlock('water', {texture: 'water_flow', transparent: true})
registry.registerBlock('glass', {texture: 'glass', transparent: true})
registry.registerBlock('oreDiamond', {texture: 'diamond_ore'})
registry.registerBlock('grass', {texture: ['grass_top', 'dirt', 'grass_side']})
registry.registerBlock('wool', {texture: ['wool_colored_gray', 'wool_colored_white', 'wool_colored_green', 'wool_colored_light_blue', 'wool_colored_red', 'wool_colored_yellow']})
registry.registerBlock('logOak', {texture:['log_oak_top', 'log_oak_top', 'log_oak']})
registry.registerBlock('leavesOak', {texture: 'leaves_oak', transparent: true})
}

122 changes: 122 additions & 0 deletions lib/terrain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
"use strict"

var ndarray = require("ndarray")
var fill = require("ndarray-fill")

// TODO: refactor with voxel-land
module.exports = function(game, opts) {
return new TerrainPlugin(game, opts);
};
module.exports.pluginInfo = {
loadAfter: ['voxel-registry', 'voxel-mesher']
};

function TerrainPlugin(game, opts) {
this.shell = game.shell;

this.registry = game.plugins.get('voxel-registry');
if (!this.registry) throw new Error('lib/terrain requires voxel-registry plugin');

this.mesher = game.plugins.get('voxel-mesher');
if (!this.mesher) throw new Error('lib/terrain requires voxel-mesher plugin');

this.enable();
};

TerrainPlugin.prototype.enable = function() {
// once the game is about to initialize, gather all registered materials and build voxels
// TODO: this doesn't really have to be done on gl-init, only when the blocks are ready. refactor with lib/blocks.js
this.shell.on('gl-init', this.onInit = this.addVoxels.bind(this));
};

TerrainPlugin.prototype.disable = function() {
this.shell.removeListener('gl-init', this.onInit);
};

// bit in voxel array to indicate voxel is opaque (transparent if not set)
var OPAQUE = 1<<15;

TerrainPlugin.prototype.addVoxels = function() {
var terrainMaterials = {};
for (var blockName in this.registry.blockName2Index) {
var blockIndex = this.registry.blockName2Index[blockName];
if (this.registry.getProp(blockName, 'transparent')) {
terrainMaterials[blockName] = blockIndex - 1 // TODO: remove -1 offset? leave 0 as 'air' (unused) in texture arrays?
} else {
terrainMaterials[blockName] = OPAQUE|(blockIndex - 1) // TODO: separate arrays? https://github.com/mikolalysenko/ao-mesher/issues/2
}
}

// add voxels
this.mesher.addVoxelArray(createTerrain(terrainMaterials));
};

var createTerrain = function(materials) {
var size = [33,33,33]
var result = ndarray(new Int32Array(size[0]*size[1]*size[2]), size)
//Fill ndarray with function
fill(result, function(i,j,k) {
//Terrain

if (i===30 && j===30 && k===30) {
// face texture test
return materials.wool
}

if(i <=1 || i>=31 || j <= 1 || j >= 31 || k <= 1 || k >= 31) {
return 0 // air
}

// tree
if (i===7 && k===8 && 21<j && j<31) {
return materials.logOak
}
var x = i - 7
var y = j - 28
var z = k - 8
if (x*x + y*y + z*z < (Math.random()+1.0) * 5.0) {
return materials.leavesOak
}

// glass structure
x = i - 20
y = j - 28
z = k - 8
if (x*x + y*y + z*z < 5.0) {
return materials.glass
}

// lake
x = i - 5
y = j - 24
z = k - 19
if (x*x + y*y + z*z < 3.0) {
return materials.water
}

// rolling hills
var h0 = 3.0 * Math.sin(Math.PI * i / 12.0 - Math.PI * k * 0.1) + 22
if(j > h0+1) {
return 0 // air
}
// grassy surface with dirt
if(h0 <= j) {
return materials.grass
}
var h1 = 2.0 * Math.sin(Math.PI * i * 0.25 - Math.PI * k * 0.3) + 20
if(h1 <= j) {
return materials.dirt
}

// stone with ores
if(4 < j) {
return Math.random() < 0.1 ?
materials.oreDiamond :
materials.stone
}
return materials.lava
})
return result
};


72 changes: 72 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"use strict"

var createShell = require("gl-now")

var createPlugins = require('voxel-plugins')
require('voxel-registry')
require('voxel-stitch')
require('voxel-shader')
require('voxel-mesher')
require('game-shell-fps-camera')

var BUILTIN_PLUGIN_OPTS = {
'voxel-registry': {},
'voxel-stitch': {},
'voxel-shader': {},
'voxel-mesher': {},
'game-shell-fps-camera': {},
};

var game = {};
global.game = game; // for debugging

var main = function(opts) {
opts = opts || {};
opts.clearColor = opts.clearColor || [0.75, 0.8, 0.9, 1.0]
opts.pointerLock = opts.pointerLock !== undefined ? opts.pointerLock : true;

var shell = createShell(opts);

game.isClient = true;
game.shell = shell;

var plugins = createPlugins(game, {require: function(name) {
// we provide the built-in plugins ourselves; otherwise check caller's require, if any
// TODO: allow caller to override built-ins? better way to do this?
if (name in BUILTIN_PLUGIN_OPTS) {
return require(name);
} else {
return opts.require ? opts.require(name) : require(name);
}
}});

var pluginOpts = opts.pluginOpts || {};

for (var name in BUILTIN_PLUGIN_OPTS) {
opts.pluginOpts[name] = opts.pluginOpts[name] || BUILTIN_PLUGIN_OPTS[name];
}

for (var name in pluginOpts) {
plugins.add(name, pluginOpts[name]);
}
plugins.loadAll();

shell.on("gl-init", function() {
var gl = shell.gl

// since the plugins are loaded before gl-init, the <canvas> element will be
// below other UI widgets in the DOM tree, so by default the z-order will cause
// the canvas to cover the other widgets - to allow plugins to cover the canvas,
// we lower the z-index of the canvas below
shell.canvas.style.zIndex = '-1';
})

shell.on("gl-error", function(err) {
document.body.appendChild(document.createTextNode('Fatal WebGL error: ' + err))
})

return shell
}

module.exports = main

14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@
"contributors": [
"Max Ogden <[email protected]> (https://github.com/maxogden)",
"Kumavis <[email protected]> (https://github.com/kumavis)",
"Deathcap <[email protected]> (https://github.com/deathcap)"
"Deathcap <[email protected]> (https://github.com/deathcap)",
"Mikola Lysenko <[email protected]> (https://github.com/mikolalysenko/)"
],
"dependencies": {
"game-shell-voxel": "git://github.com/deathcap/game-shell-voxel.git",
"voxel-mesher": "^0.6.0",
"voxel-shader": "^0.8.0",
"gl-now": "^1.3.0",
"ndarray": "^1.0.10",
"ndarray-fill": "^0.1.0",
"brfs": "^1.0.1",
"game-shell-fps-camera": "^0.2.0",
"voxel-stitch": "^0.6.0",
"voxel-registry": "^0.3.0",
"voxel-plugins": "^0.3.0",
"voxel": "git://github.com/deathcap/voxel#ndarray",
"voxel-raycast": "0.2.1",
"voxel-control": "git://github.com/deathcap/voxel-control.git#44540136f963b7aa0dda1363b837540e6b6bcb1e",
Expand Down

0 comments on commit 474ad64

Please sign in to comment.