-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
69 lines (58 loc) · 1.34 KB
/
player.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
function Player() {
this._x = 30;
this._y = 30;
this._vx = 0;
this._vy = 0;
this._radius = 15;
this._moveDistance = 5;
this._moveTime = 100;
this._avatar = Game.world.grantAvatar(this._x, this._y, this._radius);
this.startLoop = function() {
var player = this;
this._loop = setInterval(function() {
player.move();
}, this._moveTime);
}
this.stopLoop = function() {
clearInterval(this._loop);
}
this.left = function() {
return this._x - this._radius;
}
this.right = function() {
return this._x + this._radius;
}
this.top = function() {
return this._y - this._radius;
}
this.bottom = function() {
return this._y + this._radius;
}
this.signal = function(event) {
$(this._avatar.node).trigger('player:' + event, this);
}
this.move = function() {
this._x += this._vx;
this._y += this._vy;
this._avatar.animate({ cx: this._x, cy: this._y }, this._moveTime);
this.signal('move');
}
this.moveRight = function() {
this._vx = this._moveDistance;
}
this.moveDown = function() {
this._vy = this._moveDistance;
}
this.moveLeft = function() {
this._vx = -this._moveDistance;
}
this.moveUp = function() {
this._vy = -this._moveDistance;
}
this.stopX = function() {
this._vx = 0;
}
this.stopY = function() {
this._vy = 0;
}
};