/
index_4.js
175 lines (150 loc) · 3.34 KB
/
index_4.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
const ROUND_LENGTH = 600; // duration in frames
let gameState = "waiting"; // "waiting" or "playing"
let roundStart;
let recording; // recording[frame] = {x, y}
let guests;
let me;
// {
// "x": 100,
// "y": 100,
// "color": "red",
// "history": [
// [ {x, y}, {x, y}, ... ],
// [ {x, y}, {x, y}, ... ],
// ]
// }
function preload() {
partyConnect("wss://demoserver.p5party.org", "ghosts");
me = partyLoadMyShared();
guests = partyLoadGuestShareds();
}
/////////////////////////////////////////////////
// controls
const controls = { up: false, down: false, left: false, right: false };
function keyPressed() {
if (gameState === "waiting") {
partyEmit("startRound");
}
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);
partySubscribe("startRound", startRound);
me.color = random([
"red",
"orange",
"yellow",
"green",
"blue",
"indigo",
"violet",
]);
me.x = 100;
me.y = 100;
me.history = [];
me.ready = true;
}
/////////////////////////////////////////////////
// game state
function endRound() {
console.log("end round");
// add this round's recording to the history
me.history.push(recording);
// change game state
gameState = "waiting";
}
function startRound() {
console.log("start round");
// not start frame
roundStart = frameCount;
// empty recording
recording = [];
// reset avatar
me.x = 100;
me.y = 100;
// change game state
gameState = "playing";
}
/////////////////////////////////////////////////
// game loop
function draw() {
if (gameState === "waiting") {
drawWaiting();
}
if (gameState === "playing") {
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();
}
}
function drawWaiting() {
background(220);
textAlign(CENTER, CENTER);
textSize(32);
fill(0);
text("Press Any Key To\nStart Round", width / 2, height / 2);
}
function drawGame() {
background(220);
// draw all the ghosts
for (const p of guests) {
if (!p.ready) continue;
for (const round of p.history) {
const current_frame = frameCount - roundStart;
if (!round[current_frame]) continue;
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();
}