-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'game-shell-voxel' into ndarray
Conflicts: .gitignore LICENSE README.md package.json https://github.com/deathcap/game-shell-voxel
- Loading branch information
Showing
7 changed files
with
302 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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': {}, | ||
} | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|