-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglass.js
126 lines (106 loc) · 3.77 KB
/
glass.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
'use strict';
var ItemPile = require('itempile');
var ucfirst = require('ucfirst');
module.exports = function(game, opts) {
return new GlassPlugin(game, opts);
};
module.exports.pluginInfo = {
loadAfter: ['voxel-registry', 'voxel-use', 'voxel-recipes']
};
function GlassPlugin(game, opts) {
this.registry = game.plugins.get('voxel-registry');
if (!this.registry) throw new Error('voxel-glass requires voxel-registry');
this.use = game.plugins.get('voxel-use');
if (!this.use) throw new Error('voxel-glass requires voxel-use');
this.recipes = game.plugins.get('voxel-recipes'); // optional
this.colors = opts.colors !== undefined ? opts.colors : ['black', 'blue', 'brown', 'cyan', 'gray', 'green', 'light_blue', 'lime', 'magenta', 'orange', 'pink', 'purple', 'red', 'silver', 'white', 'yellow'];
this.enable();
}
// Get X or Z depending on the player orientation
GlassPlugin.prototype.playerOrientation = function() {
var heading = Math.atan2(self.game.cameraVector()[0], self.game.cameraVector()[2]);
var dir;
if (Math.abs(heading) <= Math.PI / 4) { // 0 +/- 45 degrees // TODO: refactor with voxel-pumpkin, generic block/player orientation module?
return 'Z'; // north
} else if (Math.PI - Math.abs(heading) <= Math.PI / 4) { // +/-180 +/- 45
return 'Z'; // south
} else if (heading > 0) { // +90 +/- 45
return 'X'; // west
} else { // if (heading <= 0) { // -90 +/- 45
return 'X'; // east
}
};
GlassPlugin.prototype.enable = function() {
this.registry.registerBlock('glass', {texture: 'glass', transparent: true, hardness: 0.2, creativeTab: 'glass', harvestSound: 'random/glass1'});
for (var i = 0; i < this.colors.length; i += 1) {
this.registerPane(this.colors[i]); // TODO: use metablocks?
}
this.registerPane(''); // clear
if (this.recipes) {
this.recipes.registerPositional([['glass', 'glass', 'glass']], ['glassPane', 3]);
// TODO: dye recipes, harmonize with https://github.com/deathcap/voxel-wool (API?)
}
};
// Register an item and two blocks for a glass pane of the given color
GlassPlugin.prototype.registerPane = function(color) {
var colorName = ucfirst(color);
var texture = color !== '' ? ('glass_' + color) : 'glass';
// item
var self = this;
this.registry.registerItem('glassPane' + colorName, {
displayName: colorName + ' Glass Pane',
itemTexture: texture,
creativeTab: 'glass',
onUse: function(held, target) {
// place X or Z pane depending on facing
return self.use.useBlock(target, new ItemPile('glassPane' + self.playerOrientation() + colorName)) === undefined;
},
});
// oriented blocks
this.registry.registerBlock('glassPaneZ' + colorName, {
harvestSound: 'random/glass1',
creativeTab: false,
itemDrop: 'glassPane' + colorName,
displayName: colorName + ' Glass Pane Z',
itemTexture: texture, // flat, not 3D cube
texture: texture, // preload for model below
blockModel:
[{from: [0,0,7],
to: [16,16,2],
faceData: {
down: {},
up: {},
north: {},
south: {},
west: {},
east: {}
},
texture: texture, // for all faces. TODO: use glass_pane_top for narrow faces?
}],
});
// same as above but oriented along X axis
this.registry.registerBlock('glassPaneX' + colorName, {
harvestSound: 'random/glass1',
creativeTab: false,
itemDrop: 'glassPane' + colorName,
displayName: colorName + ' Glass Pane X',
itemTexture: texture,
texture: texture,
blockModel:
[{from: [7,0,0],
to: [2,16,16],
faceData: {
down: {},
up: {},
north: {},
south: {},
west: {},
east: {}
},
texture: texture,
}],
});
};
GlassPlugin.prototype.disable = function() {
// TODO: unregister blocks
};