-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
239 lines (201 loc) · 6.1 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
var createGame = require('voxel-engine')
var highlight = require('voxel-highlight')
var player = require('voxel-player')
var walk = require('voxel-walk')
var texturePath = require('painterly-textures')(__dirname)
var voxel = require('voxel')
var convert = require('./lib/voxel-degrees')
var extend = require('extend')
var geojson = require("./data/map")
window.geojson = geojson
// Code for America Offices
var origin = {
lat: 37.775589,
lng: -122.413912
}
origin = {
lat: 37.7885423,
lng: -122.3960285
}
var padding = {
residential: 2,
secondary: 6
}
var world = {}
window.world = world
module.exports = function(opts, setup) {
var position = convert.degreesToPosition(origin.lat, origin.lng)
position = [24171358, 0, -2855713]
function connectNodes (nodeA, nodeB) {
// Convert nodes from degrees to meters
var posA = convert.degreesToPosition(nodeA[1], nodeA[0])
var posB = convert.degreesToPosition(nodeB[1], nodeB[0])
// Calculate intermediary nodes using y = mx + b
var m = (posA[2] - posB[2]) / (posA[0] - posB[0])
var b = (posA[2] - (posA[0] * m))
// Set xmin and xmax
var xmin, xmax
(posA[0] < posB[0]) ? (xmin = posA[0], xmax = posB[0]) : (xmin = posB[0], xmax = posA[0])
// Set index for each intermediary node
for (var x = xmin; x <= xmax; x++) {
var yPadding = 0
var zPadding = 0
var z = Math.round(m*x+b)
var y = 0
if (properties.highway) {
zPadding = padding[properties.highway]
}
if (properties.building) {
yPadding = 5
}
for (var zn = (z - zPadding); zn <= (z + zPadding); zn++) {
for (var yn = (y - yPadding); yn <= (y + yPadding); yn++){
setIndex( [x, yn, zn] )
}
}
}
}
function setIndex (position) {
world[ position.join("|") ] = properties
}
function processNode (node) {
setIndex( convert.degreesToPosition(node[1], node[0]) )
}
function processNodes (nodes) {
if (!(nodes[0] < nodes[nodes.length])) nodes = nodes.reverse()
for (var i = 0; i < nodes.length - 1; i++) {
connectNodes(nodes[i], nodes[i+1])
}
}
function processNodeSets (nodeSets) {
for (var i = 0; i < nodeSets.length; i++) {
processNodes( nodeSets[i] )
}
}
for (var i = 0; i < geojson.features.length; i++) {
var feature = geojson.features[i]
var properties = feature.properties
var geometry = feature.geometry
switch (geometry.type) {
case "Point" :
processNode(geometry.coordinates)
break
case "LineString" :
processNodes(geometry.coordinates)
break
case "Polygon" :
processNodeSets(geometry.coordinates)
break
}
}
// Setup new game
setup = setup || defaultSetup
var defaults = {
generate: function (x, y, z) {
x = x + position[0], y = y + position[1], z = z + position[2]
// Calculate index to look up metadata
var index = [x,y,z].join("|")
data = world[index]
// If data exists return data; else return grass
if (data) {
if (data.building == "yes") {
return 3
} else {
return 2
}
} else {
if (y != 0) return 0
return 1
}
},
chunkSize: 10,
chunkDistance: 2,
materials: [
['grass', 'dirt', 'grass_dirt'],
'obsidian',
'brick',
'grass',
'plank'
],
texturePath: texturePath,
startingPosition: position,
worldOrigin: position,
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)
avatar.pov('third')
setup(game, avatar)
return game
}
function displayMetaData (data) {
$element = document.querySelector("#metadata");
$element.style["padding"] = "10px 5px";
$element.style["height"] = "auto";
$element.style["opacity"] = 0.5;
$element.innerHTML = "<h1>"+data+"</h1>";
}
function hideMetaData () {
$element = document.querySelector("#metadata");
$element.style["padding"] = 0;
$element.style["height"] = 0;
$element.style["opacity"] = 0;
}
function relativePosition (position) {
var origin = game.startingPosition,
x = Math.round(origin[0] + position[0]),
y = Math.round(origin[1] + position[1]),
z = Math.round(origin[2] + position[2])
return [x,y,z]
}
function defaultSetup(game, avatar) {
var target = game.controls.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;
data = world[ relativePosition(voxelPos).join("|") ]
if (data) displayMetaData(data.name)
});
hl.on('remove', function (voxelPos) {
blockPosErase = null
hideMetaData()
})
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()
})
}