-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.js
95 lines (80 loc) · 2.19 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var createGame = require('voxel-engine')
var toolbar = require('toolbar')
var url = require('url')
var createTouchControls = require('./')
var blockSelector = toolbar({el: '#tools'})
var chunkDistance = 2
var chunkSize = 32
var container = document.querySelector('#container')
window.game = createGame({
materials: ['grass', 'brick', 'dirt'],
startingPosition: [0, 500, 0]
})
game.controls.pitchObject.rotation.x = -1.5;
game.appendTo(container)
window.touchControls = createTouchControls()
game.controls.enabled = true
touchControls.pipe(game.controls)
touchControls.on('command', function(command, setting) {
game.controls.emit('command', command, setting)
})
var currentMaterial = 1
blockSelector.on('select', function(material) {
var idx = game.materials.indexOf(material)
if (idx > -1) currentMaterial = idx + 1
})
game.on('collision', function (item) {
game.removeItem(item)
})
THREE = game.THREE
function createDebris (pos, value) {
var mesh = new THREE.Mesh(
new THREE.CubeGeometry(4, 4, 4),
game.material
)
mesh.geometry.faces.forEach(function (face) {
face.materialIndex = value - 1
})
mesh.translateX(pos.x)
mesh.translateY(pos.y)
mesh.translateZ(pos.z)
return {
mesh: mesh,
size: 4,
collisionRadius: 22,
value: value
}
}
function explode (pos, value) {
if (!value) return
var item = createDebris(pos, value)
item.velocity = {
x: (Math.random() * 2 - 1) * 0.05,
y: (Math.random() * 2 - 1) * 0.05,
z: (Math.random() * 2 - 1) * 0.05,
}
game.addItem(item)
setTimeout(function (item) {
game.removeItem(item)
}, 15 * 1000 + Math.random() * 15 * 1000, item)
}
game.appendTo('#container')
game.on('mousedown', function (pos) {
var cid = game.voxels.chunkAtPosition(pos)
var vid = game.voxels.voxelAtPosition(pos)
if (erase) {
explode(pos, game.getBlock(pos))
game.setBlock(pos, 0)
} else {
game.createBlock(pos, currentMaterial)
}
})
var erase = true
window.addEventListener('keydown', function (ev) {
if (ev.keyCode === 'X'.charCodeAt(0)) {
erase = !erase
}
})
function ctrlToggle (ev) { erase = !ev.ctrlKey }
window.addEventListener('keyup', ctrlToggle)
window.addEventListener('keydown', ctrlToggle)