-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.js
73 lines (60 loc) · 1.89 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
70
71
72
73
"use strict";
const W = 87;
const A = 65;
const S = 83;
const D = 68;
let targetx;
let targety;
function Player(velocity, position, radius, color, easing) {
this.velocity = velocity;
this.position = position;
this.radius = radius;
this.color = color;
this.easing = easing;
this.canShoot = true;
}
Player.prototype.move = function (p) {
if (p.keyIsDown(p.UP_ARROW) || p.keyIsDown(W)) {
targety = this.position.y - this.velocity.y;
}
if (p.keyIsDown(p.DOWN_ARROW) || p.keyIsDown(S)) {
//player.position.y = player.position.y + player.velocity.y;
targety = this.position.y + this.velocity.y;
}
if (p.keyIsDown(p.LEFT_ARROW) || p.keyIsDown(A)) {
//player.position.x = player.position.x - player.velocity.x;
targetx = this.position.x - this.velocity.x;
}
if (p.keyIsDown(p.RIGHT_ARROW) || p.keyIsDown(D)) {
//player.position.x = player.position.x + player.velocity.x;
targetx = this.position.x + this.velocity.x;
}
let dy = targety - this.position.y;
let dx = targetx - this.position.x;
if (p.abs(dy) > 1) {
this.position.y += dy * this.easing;
}
if (p.abs(dx) > 1) {
this.position.x += dx * this.easing;
}
this.clamp(p);
this.drawPlayer(p);
}
Player.prototype.clamp = function (p) {
if (this.position.x <= 0 + this.radius / 2) {
this.position.x = 0 + this.radius / 2;
}
if (this.position.y <= 0 + this.radius / 2) {
this.position.y = 0 + this.radius / 2;
}
if (this.position.x > p.width - this.radius / 2) {
this.position.x = p.width - this.radius / 2;
}
if (this.position.y > p.height - this.radius / 2) {
this.position.y = p.height - this.radius / 2;
}
}
Player.prototype.drawPlayer = function (p) {
p.fill(this.color);
p.ellipse(this.position.x, this.position.y, this.radius, this.radius);
}