-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpep.js
More file actions
126 lines (100 loc) · 3.35 KB
/
pep.js
File metadata and controls
126 lines (100 loc) · 3.35 KB
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
// Use Window.localStorage to save and retrieve paintings on a per-browser basis - p5.js provides wrapper functions for this
//// holds an array of all paintingID currently saved; a JSON representation of each painting will be saved its ID as a retrieval key
let savedPaintings = [];
let painting;
let bubbleWand;
let nextColor;
let titleCard;
let titleCardDismissed = false;
let unlockedColors = [];
let sonar;
async function preload() {
titleCard = loadImage('./assets/titlecard-paint.png');
sonar = new BreathingSonarJS();
await sonar.init();
}
function setup() {
createCanvas(windowWidth, windowHeight);
// retrieve the list of existing paintings
savedPaintings = JSON.parse(getItem('savedPaintings'));
if (savedPaintings == null) {
savedPaintings = [];
storeItem('savedPaintings', JSON.stringify(savedPaintings));
}
// retrieve the paintingID to load (assigned by gallery page)
// if unset or value=0, create a new painting
let id = getURLParams().paintingid;
if (id == null || id == 0) {
painting = Painting.create();
} else {
painting = Painting.load(id);
}
// Populate list of unlocked colors to use in painting from saved player `stats` object
// ...if it isn't found, fallback to painting with grey.
let stats = getItem('stats');
if (stats != null) {
unlockedColors = JSON.parse(stats).colors.map(c => color(c.rgb));
} else {
unlockedColors.push(color('rgb(128, 128, 128)'));
}
bubbleWand = new BubbleWand();
nextColor = random(unlockedColors);
}
function draw() {
clear();
background(255);
if (!titleCardDismissed) {
image(titleCard, 0, 0, width, height);
return
}
noCursor();
if (mouseIsPressed && mouseY > 0.9 * height && isForcefulBreathing()) {
bubbleWand.absorb(deltaTime);
} else if (mouseIsPressed && painting.bubbles.length > 0 && isForcefulBreathing()) {
bubbleWand.emit(deltaTime);
let heading = createVector((mouseX-pmouseX)/width, (mouseY-pmouseY)/height).div(5);
if (heading.mag() > 0.001) {
painting.blow(mouseX/width, mouseY/height, deltaTime, heading);
} else {
painting.blow(mouseX/width, mouseY/height, deltaTime);
}
}
painting.update(deltaTime);
painting.draw();
push();
stroke(0);
fill(255, 150);
rectMode(CORNER);
rect(0, 0.9*height, width, 0.1*height);
strokeWeight(0.1);
fill(0);
text('Recharge bubble wand here...', 5, 0.95*height);
text('Press space bar to simulate inhale or exhale.\nUse the mouse to position wand and hold leftmouse button to interact once breathing.\nOn canvas exhale, over recharge bar inhale.', width - 500, 20);
pop();
bubbleWand.draw(mouseX, mouseY);
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function mousePressed() {
if (mouseY < 0.9 * height && isForcefulBreathing()) {
painting.addBubbleGroup(nextColor);
}
}
function mouseReleased() {
// i.e., only when above bubble recharge zone
if (mouseY < 0.9 * height) {
nextColor = random(unlockedColors);
painting.save();
// schedule another save 2.5 sec from now, to capture painting once all animations have finished...
setTimeout(() => painting.save(), 2500);
}
}
function keyPressed() {
if (keyCode == ENTER || keyCode == RETURN) {
titleCardDismissed = true;
}
}
function isForcefulBreathing() {
return sonar.isForcefulBreathing || (keyIsPressed && key == ' ');
}