-
Notifications
You must be signed in to change notification settings - Fork 10
/
item.js
64 lines (58 loc) · 2.03 KB
/
item.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
/*
* Copyright (c) 2013, Javier Ayres
*
* This file is part of Lona.
*
* Lona is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Lona is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Lona. If not, see <http://www.gnu.org/licenses/>.
*/
function Item() {
this.x = null;
this.y = null;
this.draw = function() {
if (this.x == null) {
var position = [Math.random() * SCREEN_WIDTH, Math.random() * SCREEN_HEIGHT];
var isValid = true;
var distanceF1 = Math.sqrt(Math.pow(position[0] - FOCI_X, 2) + Math.pow(position[1] - FOCUS_1_Y, 2));
var distanceF2 = Math.sqrt(Math.pow(position[0] - FOCI_X, 2) + Math.pow(position[1] - FOCUS_2_Y, 2));
if (distanceF1 + distanceF2 < SCREEN_HEIGHT - LINEAR_WIDTH) {
for (var i = 0; i < centers.length && isValid; i++) {
isValid = !centers[i].collide(position);
}
} else {
isValid = false;
}
if (isValid) {
this.x = position[0];
this.y = position[1];
this.actualDraw();
}
} else {
this.actualDraw();
}
}
this.actualDraw = function() {
ctx.beginPath();
ctx.arc(this.x, this.y, LINEAR_WIDTH / 2, 0, Math.PI * 2, false);
ctx.fill();
}
this.reset = function() {
this.x = this.y = null;
}
this.isDrawn = function() {
return this.x != null;
}
this.getPosition = function() {
return [this.x, this.y];
}
}