-
Notifications
You must be signed in to change notification settings - Fork 64
/
index.js
82 lines (68 loc) · 2.38 KB
/
index.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
var createGame = require('voxel-engine')
var highlight = require('voxel-highlight')
var player = require('voxel-player')
var voxel = require('voxel')
var extend = require('extend')
var fly = require('voxel-fly')
var walk = require('voxel-walk')
module.exports = function(opts, setup) {
setup = setup || defaultSetup
var defaults = {
generate: voxel.generator['Valley'],
chunkDistance: 2,
materials: ['#fff', '#000'],
materialFlatColor: true,
worldOrigin: [0, 0, 0],
controls: { discreteFire: true }
}
opts = extend({}, defaults, opts || {})
// setup the game and add some trees
var game = createGame(opts)
var container = opts.container || document.body
window.game = game // for debugging
game.appendTo(container)
if (game.notCapable()) return game
var createPlayer = player(game)
// create the player from a minecraft skin file and tell the
// game to use it as the main player
var avatar = createPlayer(opts.playerSkin || 'player.png')
avatar.possess()
avatar.yaw.position.set(2, 14, 4)
setup(game, avatar)
return game
}
function defaultSetup(game, avatar) {
var makeFly = fly(game)
var target = game.controls.target()
game.flyer = makeFly(target)
// highlight blocks when you look at them, hold <Ctrl> for block placement
var blockPosPlace, blockPosErase
var hl = game.highlighter = highlight(game, { color: 0xff0000 })
hl.on('highlight', function (voxelPos) { blockPosErase = voxelPos })
hl.on('remove', function (voxelPos) { blockPosErase = null })
hl.on('highlight-adjacent', function (voxelPos) { blockPosPlace = voxelPos })
hl.on('remove-adjacent', function (voxelPos) { blockPosPlace = null })
// toggle between first and third person modes
window.addEventListener('keydown', function (ev) {
if (ev.keyCode === 'R'.charCodeAt(0)) avatar.toggle()
})
// block interaction stuff, uses highlight data
var currentMaterial = 1
game.on('fire', function (target, state) {
var position = blockPosPlace
if (position) {
game.createBlock(position, currentMaterial)
}
else {
position = blockPosErase
if (position) game.setBlock(position, 0)
}
})
game.on('tick', function() {
walk.render(target.playerSkin)
var vx = Math.abs(target.velocity.x)
var vz = Math.abs(target.velocity.z)
if (vx > 0.001 || vz > 0.001) walk.stopWalking()
else walk.startWalking()
})
}