/
weight.js
62 lines (55 loc) · 1.44 KB
/
weight.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
function Weight(mass, charge, x, y) {
this.isFixed = false;
this.isSolid = true;
this.type = ":weight";
this.mass = mass;
this.charge = charge;
this.x = x;
this.y = y;
this.xVel = 0;
this.yVel = 0;
this.height = 20;
this.width = 20;
this.hitbox = new Hitbox(this, 0, 0, this.width, this.height);
}
Weight.prototype.getCenter = function() {
return {
x: this.x + this.width / 2,
y: this.y + this.height / 2
};
}
Weight.prototype.move = function() {
this.moveX();
this.moveY();
}
Weight.prototype.moveX = function() {
if ((this.xVel < 0 && !this.hitbox.isLeftColliding) ||
(this.xVel > 0 && !this.hitbox.isRightColliding)) {
this.x += this.xVel;
} else {
this.xVel = 0;
}
}
Weight.prototype.moveY = function() {
if ((this.yVel < 0 && !this.hitbox.isTopColliding) ||
(this.yVel > 0 && !this.hitbox.isBottomColliding)) {
this.y += this.yVel;
} else {
this.yVel = 0;
}
}
Weight.prototype.draw = function() {
drawRect(this.x, this.y, this.width, this.height);
var center = this.getCenter();
if (this.charge > 0) {
drawPlus(center.x, center.y, 7);
} else if (this.charge < 0) {
drawMinus(center.x, center.y, 7);
}
}
Weight.prototype.xAccelerate = function(accelAmount) {
this.xVel += accelAmount;
}
Weight.prototype.yAccelerate = function(accelAmount) {
this.yVel += accelAmount;
}