-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
90 lines (76 loc) · 1.92 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
import { Point, Rect, pointInRect } from "./shape.js";
const my_id = Math.random();
let shared;
window.preload = () => {
partyConnect("wss://deepstream-server-1.herokuapp.com", "drag_1", "main");
shared = partyLoadShared("shared");
};
window.setup = () => {
createCanvas(400, 400);
noStroke();
if (partyIsHost()) {
shared.sprites = [];
shared.sprites.push(initSprite(new Rect(10, 10, 100, 100), "#ffff66"));
shared.sprites.push(initSprite(new Rect(30, 30, 100, 100), "#ff66ff"));
shared.sprites.push(initSprite(new Rect(50, 50, 100, 100), "#66ffff"));
}
};
window.draw = () => {
background("#cc6666");
shared.sprites.forEach(stepSprite);
shared.sprites.forEach(drawSprite);
};
window.mousePressed = () => {
for (const s of shared.sprites.slice().reverse()) {
if (mousePressedSprite(s)) break;
}
};
window.mouseReleased = () => {
for (const s of shared.sprites.slice().reverse()) {
if (mouseReleasedSprite(s)) break;
}
};
function initSprite(rect = new Rect(), color = "red") {
const s = {};
s.rect = rect;
s.color = color;
return s;
}
function stepSprite(s) {
if (s.inDrag && s.owner === my_id) {
s.rect.l = mouseX + s.dragOffset.x;
s.rect.t = mouseY + s.dragOffset.y;
}
}
function drawSprite(s) {
push();
fill(s.color);
noStroke();
if (s.inDrag) {
strokeWeight(3);
stroke("black");
}
rect(s.rect.l, s.rect.t, s.rect.w, s.rect.h);
pop();
}
function mousePressedSprite(s) {
if (!s.inDrag && pointInRect(new Point(mouseX, mouseY), s.rect)) {
// begin drag
s.inDrag = true;
s.owner = my_id;
s.dragOffset = new Point(s.rect.l - mouseX, s.rect.t - mouseY);
// move to top
const i = shared.sprites.indexOf(s);
shared.sprites.splice(i, 1);
shared.sprites.push(s);
return true;
}
return false;
}
function mouseReleasedSprite(s) {
if (s.owner === my_id) {
s.inDrag = false;
s.owner = null;
}
return false;
}