-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
118 lines (107 loc) · 2.87 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
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
class Player {
constructor() {
this.velocity = new createVector(0, 0);
this.location = new createVector(0, 0);
this.acceleration = new createVector(0, 0);
this.size = 70;
this.speed = 0.15;
this.maxSpeed = 10;
this.turnRate = 2;
this.attackSpeed = 5; // bullet per second
this.bulletSystem = new BulletSystem();
this.secSinceLastFire = 0;
}
run() {
this.draw();
this.move();
this.edges();
this.interaction();
this.appylyFriction();
this.bulletSystem.run();
this.secSinceLastFire += deltaTime / 100;
}
move() {
this.velocity.add(this.acceleration);
this.location.add(this.velocity);
this.acceleration.mult(0);
this.velocity.limit(this.maxSpeed);
}
draw() {
stroke(0);
this.setGunDirection();
ellipse(this.location.x, this.location.y, this.size, this.size);
}
setGunDirection() {
push();
let s = screenPosition(this.location);
let angle = Math.atan2(mouseY - s.y, mouseX - s.x);
translate(this.location.x, this.location.y);
rotate(angle);
fill("black");
rectMode(CENTER);
rect(this.size / 2, 0, this.size * 0.9, 20);
pop();
}
interaction() {
if (keyIsDown(87)) {
// w
this.applyForce(createVector(0, -this.speed));
}
if (keyIsDown(65)) {
// a
this.applyForce(createVector(-this.speed, 0));
}
if (keyIsDown(83)) {
// s
this.applyForce(createVector(0, this.speed));
}
if (keyIsDown(68)) {
// d
this.applyForce(createVector(this.speed, 0));
}
if (mouseIsPressed) {
// space
this.fire();
}
}
edges() {
if (this.location.x - this.size / 2 < -playBox.width / 2) {
this.location.x = -playBox.width / 2 + this.size / 2;
this.velocity.x = 0;
} else if (this.location.x + this.size / 2 > playBox.width / 2) {
this.location.x = playBox.width / 2 - this.size / 2;
this.velocity.x = 0;
} else if (this.location.y - this.size / 2 < -playBox.height / 2) {
this.location.y = -playBox.height / 2 + this.size / 2;
this.velocity.y = 0;
} else if (this.location.y + this.size / 2 > playBox.height / 2) {
this.location.y = playBox.height / 2 - this.size / 2;
this.velocity.y = 0;
}
}
applyForce(f) {
this.acceleration.add(f);
}
appylyFriction() {
let friction = this.velocity.copy();
friction.mult(-1);
friction.normalize();
friction.mult(0.1);
this.applyForce(friction);
}
fire() {
if (this.secSinceLastFire * (this.attackSpeed / 10) > 1) {
this.secSinceLastFire = 0;
let mouseDir = createVector(mouseX, mouseY).sub(
screenPosition(this.location)
);
mouseDir.setMag(this.size);
this.bulletSystem.fire(
this.location.x,
this.location.y,
mouseDir,
p5.Vector.add(this.location, mouseDir)
);
}
}
}