-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
201 lines (188 loc) · 4.55 KB
/
sketch.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
/* TO DO
* Improve physics
* improve the way 3 balls or more interract, we get these weird balls teleporting,
when a ball push one ball, this second ball should be able to push the third one with being
squeezed between the two. when pushing a ball out of another, we should check there is not a third one there…
--> try using a boolean alreadyMoved
*implement pushing balls without velocity (they just move and stop instantly)
*implement fixed balls
* remake super attraction (like in the video)
*improve alignment on the page (aligned right, should be left)
*implement ball spawner
*/
var balls = [];
var gravityValue = 0.2;
var spawnBalls = false;
var notifications = new Notifications();
var options =
{
Option: function(name, value, shortcut)
{
this.name = name;
this.value = value;
this.shortcut = shortcut;
this.texte = createElement("span",this.name + " : " + this.shortcut + "<br />");
this.texte.parent("options");
this.texte.id(this.name);
},
toggle: function(shortcut)
{
for (var o in this)
{
if (shortcut == this[o].shortcut)
{
this[o].value = !this[o].value;
this.updateColor(this[o]);
notifications.addText("Toggled "+ this[o].name);
}
}
},
updateColor: function(optionToChange)
{
if (optionToChange.value)
{
optionToChange.texte.style("fontWeight","bold");
optionToChange.texte.style("color","green");
}
else
{
optionToChange.texte.style("fontWeight","normal");
optionToChange.texte.style("color","red");
}
},
initiate: function()
{
this.randomSpeed = new this.Option("Random velocity", false,"R");
this.oneBall = new this.Option("Spawn one ball", true,"O");
this.gravity = new this.Option("Gravity", false,"G");
this.collision = new this.Option("Collision", true,"C");
this.unstuck = new this.Option("Unstuck balls", true,"U");
this.paintBackground = new this.Option("Paint mode",false,"P");
this.wall = new this.Option("Wall Collision", true,"W");
this.blow = new this.Option("Mouse blow", false,"B");
this.attraction = new this.Option("Mouse Attraction", false,"A");
this.superAttraction = new this.Option("Super attraction", false,"A");
for (var o in this)
{
if (this[o].texte) {
this[o].texte.mouseClicked(this.toggle(this[o].shortcut));
}
if (this[o].value)
{
this.updateColor(this[o]);
}
}
},
reset: function()
{
for (var o in this)
{
if (this[o].value)
{
this[o].value = false;
this.updateColor(this[o]);
}
}
}
};
function setup()
{
var canvas = createCanvas(1024,768);
canvas.parent("#game");
background(70,20,150);
options.initiate();
}
function draw()
{
if (!options.paintBackground.value)
{
background(70,20,150);
}
if (spawnBalls)
{
balls.push(new Ball(mouseX,mouseY,balls.length));
if (options.oneBall.value)
{
spawnBalls = false;
}
}
for (var i = balls.length - 1; i >= 0 ; i--) //Walls, bounce on the wall and/or remove out of bound balls (checking the array backward so that when an object is deleted from it, the next in the list isn’t skip (it’s automatically put in the deleted index))
{
if (options.wall.value) // Walls are activated
{
if (balls[i].isOutOfBound()) //remove balls that are going up, we need to do it just once !
{
balls.splice(i,1);
}
else
{
balls[i].bounce();
}
}
else if (balls[i].isOutOfBound()) //balls exit the world
{
balls.splice(i,1);
}
}
for (var i = 0; i < balls.length; i++) //Moves, tests collisions, displays balls.
{
if (options.attraction.value)
{
balls[i].attraction();
}
if (options.blow.value)
{
balls[i].blown();
}
if (options.collision.value) // collision detection
{
var ballsToCheck = [];
for (var j = i+1; j < balls.length; j++)
{
if (balls[i].doesIntersect(balls[j])) // WORK IN PROGRESS
{
balls[i].bumpsInto(balls[j]);
}
}
}
balls[i].move();
balls[i].display();
}
for (var i = 0; i < balls.length; i++)
{
balls[i].gotUnstuck = false;
}
notifications.display();
}
function keyPressed()
{
//console.log("key pressed=" + String.fromCharCode(keyCode) + "=" + keyCode);
if (String.fromCharCode(keyCode) == " ")
{
balls.splice(0,balls.length);
background(70,20,150);
notifications.addText("Removed all balls");
}
if (keyCode == 27)
{
console.log(keyCode);
notifications.addText("Reset");
options.reset();
}
else
{
options.toggle(String.fromCharCode(keyCode));
}
}
function mousePressed()
{
spawnBalls = true;
}
function mouseReleased()
{
spawnBalls = false;
for (var i = 0; i < balls.length; i++)
{
balls[i].release();
}
}