-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Place.js
143 lines (119 loc) · 4.16 KB
/
Place.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
// Silly r/place for Brick Hill. - made by lykaspars4
// Before
if (!Game.serverSettings.rplace)
return;
// Variables
const rplaceServerSettings = Game.serverSettings.rplace;
const rplace = {};
rplace.timeouts = {}; // Let's make it fair. First, I will use userId instead of netId, and second, I will NOT reset it when player left the game. userId - [timeout, halt]
rplace.colors = rplaceServerSettings.colors || {
"White (0%)": "#FFFFFF",
"20%": "#C4C4C4",
"Grey (40%)": "#888888",
"60%": "#555555",
"80%": "#222222",
"Black (100%)": "#000000",
"Bright Pink": "#FFA7D1",
"Pink": "#EC08EC",
"Dark Pink": "#820080",
"Dark Red": "#6b0000",
"Red": "#E50000",
"Bright Red": "#FF3904",
"Orange": "#E59500",
"Yellow": "#ESD900",
"Pale": "#FFDFCC",
"Dark Pale": "#A06A42",
"Brown": "#633C1F",
"Bright Green": "#94E044",
"Green": "#51E119",
"Dark Green": "#02BE01",
"Darker Green": "#006600",
"Cyan": "#36BAFF",
"Bright Blue": "#0083C7",
"Blue": "#044BFF",
"Dark Blue": "#0000EA"
};
rplace.timeout = rplaceServerSettings.timeout || 30;
rplace.offsetX = rplaceServerSettings.offsetX || 0;
rplace.offsetY = rplaceServerSettings.offsetY || 0;
rplace.width = rplaceServerSettings.width || 50;
rplace.height = rplaceServerSettings.height || 50;
rplace.pixelSize = rplaceServerSettings.pixelSize || 2;
// Functions
function getColorName(colorSelection) {
return Object.getOwnPropertyNames(rplace.colors)[colorSelection];
}
function getColor(colorSelection) {
return Object.values(rplace.colors)[colorSelection];
}
function objectLength(object) {
return Object.keys(object).length;
}
function createPixel(x, y) {
let pixelBrick = new Brick(new Vector3(x * rplace.pixelSize, y * rplace.pixelSize, 0), new Vector3(rplace.pixelSize, rplace.pixelSize, 1));
Game.newBrick(pixelBrick);
pixelBrick.setClickable(true);
// console.log(pixelBrick.color);
pixelBrick.clicked((player, secure) => {
if (rplace.timeouts[player.userId][0] > 0 || !secure)
return;
pixelBrick.setColor(getColor(player.colorSelection));
rplace.timeouts[player.userId][0] = rplace.timeout;
});
}
function renderToPlayer(player) {
player.bottomPrint(`[${getColor(player.colorSelection)}]${getColorName(player.colorSelection)} [#FFFFFF]| ${rplace.timeouts[player.userId][0]}`);
}
function createCanvas(offsetx, offsety, width, height) {
for (let x = 0; x<width; x++) {
for (let y = 0; y<height; y++) {
createPixel(offsetx + x, offsety + y);
}
}
}
function timeoutTick() {
Object.keys(rplace.timeouts).forEach(timeoutUser => {
// console.log(`${timeoutUser}, ${rplace.timeouts}`);
let timeout = rplace.timeouts[timeoutUser];
if (timeout[0] > 0 && !timeout[1]) {
timeout[0] -= 1;
}
});
}
function cycleColor(player, color) {
player.colorSelection += color;
if (player.colorSelection > objectLength(rplace.colors) - 1) {
player.colorSelection = 0;
} else if (player.colorSelection < 0) {
player.colorSelection = objectLength(rplace.colors) - 1;
}
console.log(player.colorSelection);
}
// Callbacks
Game.on("playerJoin", (player) => {
player.on("initialSpawn", () => {
player.colorSelection = 0;
player.renderTick = setInterval(function() { renderToPlayer(player); }, 10);
if (!rplace.timeouts[player.userId]) {
rplace.timeouts[player.userId] = [rplace.timeout, false];
} else {
rplace.timeouts[player.userId][1] = false;
}
player.keypress((key) => {
if (key == "q") {
cycleColor(player, -1);
} else if (key == "e") {
cycleColor(player, 1);
}
});
});
});
Game.on("playerLeave", (player) => {
if (player.renderTick)
clearInterval(player.renderTick);
if (rplace.timeouts[player.userId])
rplace.timeouts[player.userId][1] = true; // Put on halt!
});
// After
rplace.timeoutTick = setInterval(timeoutTick, 1000);
createCanvas(rplace.offsetX, rplace.offsetY, rplace.width, rplace.height);