Created
September 22, 2017 19:24
-
-
Save brianantonelli/c4beb7c18e23d3a4b60f9c7210f09c94 to your computer and use it in GitHub Desktop.
POC of voxeljs with AWS
This file contains hidden or 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
const createGame = require('voxel-engine'), | |
highlight = require('voxel-highlight'), | |
player = require('voxel-player'), | |
voxel = require('voxel'), | |
fly = require('voxel-fly'), | |
walk = require('voxel-walk'), | |
label = require('voxel-label'), | |
AWS = require('aws-sdk'); | |
// setup engine | |
const game = createGame({ | |
generate: function(x, y, z) { | |
// return (Math.sqrt(x*x + y*y + z*z) > 100 || y*y > 10) ? 0 : (Math.random() * 3) + 1; | |
return y === 1 ? 1 : 0; | |
// if (x*x + y*y + z*z > 20*20) return 0; | |
// return Math.floor(Math.random() * 4) + 1; | |
}, | |
chunkDistance: 2, | |
textures: './textures', | |
worldOrigin: [0, 0, 0], | |
controls: { discreteFire: true } | |
}); | |
window.game = game; // for debugging | |
// attach engine to document container | |
const container = document.getElementById('container') || document.body; | |
game.appendTo(container); | |
// setup player avatar | |
const avatar = player(game)('player.png'); | |
avatar.possess(); | |
avatar.yaw.position.set(2, 14, 4); | |
// enable flying support | |
game.flyer = fly(game)(game.controls.target()); | |
// block highlighting | |
let blockPosPlace, blockPosErase; | |
const hl = game.highlighter = highlight(game, { color: 0xff0000 }); | |
hl.on('highlight', (voxelPos) => { blockPosErase = voxelPos }); | |
hl.on('remove', (voxelPos) => { blockPosErase = null }); | |
hl.on('highlight-adjacent', (voxelPos) => { blockPosPlace = voxelPos }); | |
hl.on('remove-adjacent', (voxelPos) => { blockPosPlace = null }); | |
// enable 1st person / 3rd person toggle | |
window.addEventListener('keydown', (ev) => { | |
if(ev.keyCode === 'R'.charCodeAt(0)) avatar.toggle(); | |
}); | |
// block creation/destruction | |
let currentMaterial = 2; | |
game.on('fire', (target, state) => { | |
let position = blockPosPlace | |
if(position){ | |
game.createBlock(position, currentMaterial); | |
console.log(position) | |
} | |
else{ | |
position = blockPosErase; | |
if(position) game.setBlock(position, 0); | |
} | |
}); | |
// walking animation | |
game.on('tick', () => { | |
const target = game.controls.target(); | |
walk.render(target.playerSkin) | |
const vx = Math.abs(target.velocity.x), | |
vz = Math.abs(target.velocity.z); | |
if(vx > 0.001 || vz > 0.001) walk.stopWalking(); | |
else walk.startWalking(); | |
}); | |
// sample item mesh | |
var mesh = new game.THREE.Mesh( | |
new game.THREE.CubeGeometry(5, 3, 1), // width, height, depth | |
game.materials.material | |
) | |
game.materials.paint(mesh, 'grass') | |
mesh.position.set(0, 3, -5) | |
var item = game.addItem({ | |
mesh: mesh, | |
size: 1, | |
velocity: { x: 0, y: 0, z: 0 } | |
}) | |
// build data center | |
for(let x=0; x<30; x++){ | |
for(let z=0; z<30; z++){ | |
for(let y=0; y<30; y++){ | |
// walls | |
if(z == 0 || z === 29 || x == 0 || x === 29){ | |
game.createBlock([x, y, z], 2); | |
} | |
// level | |
if(y % 5 === 0 && ((x < 10 || x > 12) || (z < 10 || z > 12))){ | |
game.createBlock([x,y,z], 3); | |
} | |
// roof | |
if(y === 29){ | |
game.createBlock([x,y,z], 2); | |
} | |
} | |
} | |
} | |
// cut out door | |
game.setBlock([0,2,3],0); | |
game.setBlock([0,2,4],0); | |
game.setBlock([0,3,3],0); | |
game.setBlock([0,3,4],0); | |
// AWS data | |
const awsParams = { | |
region: 'us-east-1', | |
accessKeyId: 'asdaf', | |
secretAccessKey: 'asdfasd', | |
sessionToken: 'asdfasdf' | |
}; | |
const eb = new AWS.ElasticBeanstalk(awsParams), | |
ec2 = new AWS.EC2(awsParams); | |
setTimeout(() => { | |
console.log('Querying EB...'); | |
eb.describeApplications({}, (err, data) => { | |
let i = 1; | |
data.Applications.forEach((app) => { | |
console.log('Adding block for: %s', app.ApplicationName); | |
game.createBlock([1, 2*(i++), -5], currentMaterial); | |
}); | |
}); | |
console.log('Querying EC2...'); | |
ec2.describeInstances({}, (err, data) => { | |
let i = 1; | |
console.log(data) | |
data.Reservations.forEach((res) => { | |
res.Instances.forEach((instance) => { | |
console.log('Adding block for :%s', instance.InstanceId); | |
game.createBlock([3, 2*(i++), -5], 3); | |
}); | |
}); | |
}); | |
}, 2000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment