/
index_3.js
142 lines (120 loc) · 2.69 KB
/
index_3.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
let guests;
let me;
function preload() {
partyConnect("wss://demoserver.p5party.org", "ghosts");
me = partyLoadMyShared();
guests = partyLoadGuestShareds();
}
const ROUND_LENGTH = 600; //frames
let roundStart;
let recording; // recording[frame] = {x, y}
// const avatar_history = []; // avatar_history[round][frame] = {x, y}
/////////////////////////////////////////////////
// controls
const controls = { up: false, down: false, left: false, right: false };
function keyPressed() {
if (key === "w") {
controls.up = true;
} else if (key === "s") {
controls.down = true;
} else if (key === "a") {
controls.left = true;
} else if (key === "d") {
controls.right = true;
}
}
function keyReleased() {
if (key === "w") {
controls.up = false;
} else if (key === "s") {
controls.down = false;
} else if (key === "a") {
controls.left = false;
} else if (key === "d") {
controls.right = false;
}
}
/////////////////////////////////////////////////
// controls
function setup() {
createCanvas(400, 400);
me.color = random([
"red",
"orange",
"yellow",
"green",
"blue",
"indigo",
"violet",
]);
me.x = 100;
me.y = 100;
me.avatar_history = [];
me.ready = true;
startRound();
}
/////////////////////////////////////////////////
// game state
function endRound() {
console.log("end round");
// add this round's recording to the history
me.avatar_history.push(recording);
}
function startRound() {
roundStart = frameCount;
// empty recording
recording = [];
// reset avatar
me.x = 100;
me.y = 100;
}
/////////////////////////////////////////////////
// game loop
function draw() {
updateGame();
drawGame();
}
function updateGame() {
if (controls.up) me.y -= 5;
if (controls.down) me.y += 5;
if (controls.left) me.x -= 5;
if (controls.right) me.x += 5;
// record the current position
recording.push({ x: me.x, y: me.y });
if (frameCount - roundStart > ROUND_LENGTH) {
endRound();
startRound();
}
}
function drawGame() {
background(220);
// draw all the ghosts
for (const p of guests) {
if (!p.ready) continue;
for (const round of p.avatar_history) {
const current_frame = frameCount - roundStart;
drawAvatar(round[current_frame], p.color);
}
}
// draw all the avatars
for (const p of guests) {
if (!p.ready) continue;
drawAvatar(p, p.color);
}
// draw timer
drawTimer();
}
function drawTimer() {
push();
const timerWidth = map(frameCount - roundStart, 0, ROUND_LENGTH, width, 0);
fill(0);
rect(0, height - 10, timerWidth, 10);
pop();
}
function drawAvatar(a, c = "red") {
push();
fill(c);
noStroke();
ellipse(a.x, a.y, 20, 20);
pop();
}