-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
142 lines (122 loc) · 2.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Add states isAlive, name and canvas Location.
// Add methods kill(), getLocation()?, setName() and moveLocation()
// creates keys array
// adds event listenters to body to for keydown and keyup
// checks code of keyevent and adds false or true to keys array
let keys = [];
document.body.addEventListener('keydown', function (event) {
keys[event.code] = true;
});
document.body.addEventListener('keyup', function (event) {
keys[event.code] = false;
});
class Player {
constructor(
width,
height,
color,
x,
y,
speed,
velX,
velY,
friction,
gravity
) {
this.width = width;
this.height = height;
this.color = color;
this.x = x;
this.y = y;
this.speed = speed;
this.velX = velX;
this.velY = velY;
/////jump and friction and gravity
this.jump = false;
this.friction = friction;
this.gravity = gravity;
}
makePlayer() {
let ctx = document.querySelector('canvas').getContext('2d'); // comment out later?
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
update() {
// up arrow
if (keys.ArrowUp) {
//38if(this.jump=false){ }
if (!this.jump) {
this.jump = true;
this.velY = -this.speed * 2;
}
}
// right arrow
if (keys.ArrowRight) {
if (this.velX < this.speed) {
this.velX++;
}
}
// left arrow
if (keys.ArrowLeft) {
if (this.velX > -this.speed) {
this.velX--;
}
}
this.velX *= this.friction;
this.velY += this.gravity;
//moves player side to side
this.x += this.velX;
//moves player up and down?
this.y += this.velY;
// makes boundaries
if (this.x >= 480 - this.width) {
this.x = 480 - this.width;
} else if (this.x <= 0) {
this.x = 0;
}
if (this.y >= 270 - this.height - 70) {
//console.log("too high");
this.y = 270 - this.height - 70;
this.jump = false;
}
let ctx = document.querySelector('canvas').getContext('2d');
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
// how we clear previous drawings?
//console.log(keys);
}
}
// OBSTACLE EXTENSION
class Obstacle extends Player {
constructor(
width,
height,
color,
x,
y,
speed,
velX,
velY,
friction,
gravity
) {
super(width, height, color, x, y, speed, velX, velY, friction, gravity);
}
moveObstacle() {
//if at far left, move far right
if (this.x - this.width / 4 <= 0) {
this.velX = 6;
}
if (this.x + this.width >= 480) {
this.velX = -6;
}
//if far right, move far left
}
updateObstacle() {
//moves player side to side
this.x += this.velX;
let ctx = document.querySelector('canvas').getContext('2d');
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}