/
index.js
382 lines (339 loc) · 10 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Play Rock Paper Scissors online
let titleScreen = true;
const signs = ["rock", "paper", "scissors"];
const colors = { player1: "#A44", player2: "#379", observer: "gray" };
const buttons = [];
const imageLib = {}; // object to hold loaded images
let shared;
/**
* here are the properties in `shared`
*
* shared.player1Signs - randomized order of the signs
* e.g. ["rock", "paper", "scissors"]
*
* shared.player2Signs - randomized order of the signs
* e.g. ["rock", "paper", "scissors]
*
* shared.player1Sign - currently selected sign of player1
* "rock" || "paper" || "scissors" || undefined
*
* shared.player2Sign - currently selected sign of player2 || undefined
* "rock" || "paper" || "scissors" || undefined
*
* shared.gameState - high level switch of what stage of the game we are in
* "play" || "result" || "outro"
*
* shared.winner - the last winner, only valid if gameState === "result"
* "player1" || "player2" || "draw"
*/
let my, guests;
/**
* here are the properties in `my` and guests[] objects
*
* x - the x position of the cursor
* y - the y position of the cursor
* show - whether to show the cursor
* role - "player1" || "player2" || "observer"
*
*/
function preload() {
imageLib.blue_blue = loadImage(`./assets/blue_blue.png`);
imageLib.blue_wins = loadImage(`./assets/blue_wins.png`);
imageLib.blue_hand_rock = loadImage(`./assets/blue_hand_rock.png`);
imageLib.blue_hand_paper = loadImage(`./assets/blue_hand_paper.png`);
imageLib.blue_hand_scissors = loadImage(`./assets/blue_hand_scissors.png`);
imageLib.blue_question = loadImage(`./assets/blue_question.png`);
imageLib.red_red = loadImage(`./assets/red_red.png`);
imageLib.red_wins = loadImage(`./assets/red_wins.png`);
imageLib.red_hand_rock = loadImage(`./assets/red_hand_rock.png`);
imageLib.red_hand_paper = loadImage(`./assets/red_hand_paper.png`);
imageLib.red_hand_scissors = loadImage(`./assets/red_hand_scissors.png`);
imageLib.red_question = loadImage(`./assets/red_question.png`);
imageLib.title_scissors = loadImage(`./assets/title_scissors.png`);
imageLib.title_paper = loadImage(`./assets/title_paper.png`);
imageLib.title_rock = loadImage(`./assets/title_rock.png`);
imageLib.draw = loadImage(`./assets/draw.png`);
// connect to the party server
partyConnect("wss://demoserver.p5party.org", "hello_party");
shared = partyLoadShared("shared", {});
my = partyLoadMyShared({ x: 100, y: 100, show: false, role: "observer" });
guests = partyLoadGuestShareds("guests");
}
function setup() {
createCanvas(640, 480);
noStroke();
cursor("none");
// left side
buttons.push(
new Button({
x: width * 0.25,
y: height * 0.25,
radius: 50,
visible: true,
sign: "rock",
controller: "player1",
})
);
buttons.push(
new Button({
x: width * 0.15,
y: height * 0.5,
radius: 50,
visible: true,
sign: "paper",
controller: "player1",
})
);
buttons.push(
new Button({
x: width * 0.25,
y: height * 0.75,
radius: 50,
visible: true,
sign: "scissors",
controller: "player1",
})
);
// right side
buttons.push(
new Button({
x: width * 0.75,
y: height * 0.25,
radius: 50,
visible: true,
sign: "rock",
controller: "player2",
})
);
buttons.push(
new Button({
x: width * 0.85,
y: height * 0.5,
radius: 50,
visible: true,
sign: "paper",
controller: "player2",
})
);
buttons.push(
new Button({
x: width * 0.75,
y: height * 0.75,
radius: 50,
visible: true,
sign: "scissors",
controller: "player2",
})
);
if (partyIsHost()) startPlay();
}
function mouseMoved() {
if (pointInRect(mouseX, mouseY, 0, 0, width, height)) {
my.x = mouseX;
my.y = mouseY;
my.show = true;
} else {
my.show = false;
}
}
function draw() {
// update
stepHost();
updateRoles();
syncUI();
// draw
if (titleScreen) {
drawTitleScreen();
} else {
drawGame();
}
drawGuests();
}
function drawTitleScreen() {
background("white");
wobbleImage(imageLib.title_rock, width * 0.4, height * 0.4, 1, {
phase: 0,
amplitude: 5,
period: 1,
});
wobbleImage(imageLib.title_paper, width * 0.5, height * 0.5, 1, {
phase: 0.33,
amplitude: 5,
period: 0.8,
});
wobbleImage(imageLib.title_scissors, width * 0.5, height * 0.7, 1, {
phase: 0.66,
amplitude: 5,
period: 0.9,
});
}
function drawGame() {
background("white");
buttons.forEach((b) => b.draw());
if (shared.gameState === "result") drawResult();
}
function drawResult() {
if (shared.winner === "player1") {
wobbleImage(imageLib.red_red, width * 0.5, height * 0.4);
wobbleImage(imageLib.red_wins, width * 0.5, height * 0.6);
}
if (shared.winner === "player2") {
wobbleImage(imageLib.blue_blue, width * 0.5, height * 0.4);
wobbleImage(imageLib.blue_wins, width * 0.5, height * 0.6);
}
if (shared.winner === "draw") {
wobbleImage(imageLib.draw, width * 0.5, height * 0.5);
}
}
function drawGuests() {
for (let guest of guests) {
if (!guest.show) continue;
push();
translate(guest.x, guest.y);
stroke(0, 0, 0, 50);
strokeWeight(5);
triangle(0, 0, 16, 16, 0, 22);
stroke("white");
strokeWeight(2);
fill(colors[guest.role]);
triangle(0, 0, 16, 16, 0, 22);
pop();
}
}
function stepHost() {
if (!partyIsHost()) return;
if (
shared.gameState === "play" &&
shared.player1Sign !== undefined &&
shared.player2Sign !== undefined
) {
startResult();
setTimeout(() => (shared.gameState = "outro"), 2000);
setTimeout(startPlay, 3000);
}
}
function updateRoles() {
// if there isn't a player1
if (!guests.find((p) => p.role === "player1")) {
// find the first observer
const o = guests.find((p) => p.role === "observer");
// if thats me, take the role
if (o === my) o.role = "player1";
}
if (!guests.find((p) => p.role === "player2")) {
const o = guests.find((p) => p.role === "observer");
if (o === my) o.role = "player2";
}
}
function startPlay() {
shared.player1Signs = shuffle(signs);
shared.player2Signs = shuffle(signs);
shared.player1Sign = undefined;
shared.player2Sign = undefined;
shared.gameState = "play";
}
function startResult() {
shared.gameState = "result";
shared.winner = getWinner(shared.player1Sign, shared.player2Sign);
}
function syncUI() {
// sync the randomized sign order
buttons[0].sign = shared.player1Signs[0];
buttons[1].sign = shared.player1Signs[1];
buttons[2].sign = shared.player1Signs[2];
buttons[3].sign = shared.player2Signs[0];
buttons[4].sign = shared.player2Signs[1];
buttons[5].sign = shared.player2Signs[2];
// hide buttons when selection made
buttons[0].visible =
shared.player1Sign === undefined || shared.player1Sign === buttons[0].sign;
buttons[1].visible =
shared.player1Sign === undefined || shared.player1Sign === buttons[1].sign;
buttons[2].visible =
shared.player1Sign === undefined || shared.player1Sign === buttons[2].sign;
buttons[3].visible =
shared.player2Sign === undefined || shared.player2Sign === buttons[3].sign;
buttons[4].visible =
shared.player2Sign === undefined || shared.player2Sign === buttons[4].sign;
buttons[5].visible =
shared.player2Sign === undefined || shared.player2Sign === buttons[5].sign;
}
function mousePressed() {
if (titleScreen) {
titleScreen = false;
return;
}
buttons.forEach((b) => b.mousePressed());
}
function getWinner(player1Sign, player2Sign) {
if (player1Sign === player2Sign) return "draw";
if (player1Sign === "rock" && player2Sign === "scissors") return "player1";
if (player1Sign === "rock" && player2Sign === "paper") return "player2";
if (player1Sign === "paper" && player2Sign === "rock") return "player1";
if (player1Sign === "paper" && player2Sign === "scissors") return "player2";
if (player1Sign === "scissors" && player2Sign === "paper") return "player1";
if (player1Sign === "scissors" && player2Sign === "rock") return "player2";
}
class Button {
constructor({ x, y, radius, visible, sign, controller }) {
this.x = x;
this.y = y;
this.radius = radius;
this.visible = visible;
this.sign = sign;
this.controller = controller;
this.transitionScale = 0;
}
draw() {
const secret = my.role !== this.controller;
const hover = !secret && dist(this.x, this.y, mouseX, mouseY) < this.radius;
const colorName = this.controller === "player1" ? "red" : "blue";
let i;
if (secret && shared.gameState === "play") {
i = imageLib[`${colorName}_question`];
} else {
i = imageLib[`${colorName}_hand_${this.sign}`];
}
if (!this.visible || shared.gameState === "outro") {
this.transitionScale = lerp(this.transitionScale, 0.0, 0.3);
wobbleImage(i, this.x, this.y, this.transitionScale, { amplitude: 10 });
} else if (hover || shared.gameState === "result") {
this.transitionScale = lerp(this.transitionScale, 1.4, 0.1);
wobbleImage(i, this.x, this.y, this.transitionScale, { amplitude: 10 });
} else {
this.transitionScale = lerp(this.transitionScale, 1, 0.1);
wobbleImage(i, this.x, this.y, this.transitionScale, { amplitude: 2 });
}
}
mousePressed() {
if (my.role != this.controller) return;
if (dist(this.x, this.y, mouseX, mouseY) > this.radius) return;
if (this.controller === "player1") {
shared.player1Sign = this.sign;
}
if (this.controller === "player2") {
shared.player2Sign = this.sign;
}
}
}
function pointInRect(x, y, rx, ry, rw, rh) {
return x >= rx && x <= rx + rw && y >= ry && y <= ry + rh;
}
function wobbleImage(i, x = 0, y = 0, s = 1, settings = {}) {
settings = Object.assign(
{
period: 1,
amplitude: 10,
phase: 0,
},
settings
);
push();
imageMode(CENTER);
translate(x, y);
scale(s);
const a = ((millis() / 1000 + settings.phase) * PI) / settings.period;
rotate(radians(sin(a) * settings.amplitude));
image(i, 0, 0);
pop();
}