-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.js
97 lines (84 loc) · 2.82 KB
/
car.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
// construcitng car
class Car{
constructor(x,y,width,height){//atributes stored in the object
this.x = x;
this.y = y;
this.width = width;
this.height = height;//car rememeber its own height and width
//speed attribute
this.speed =0;
this.acceleration = 0.3;
this.maxSpeed = 5;
this.friction = 0.05;
this.angle = 0;
// controls
this.sensor= new Sensor(this);
this.controls= new Controls();
}
update(roadBoarders){
this.#move();
// for updating the sensor as we move
this.sensor.update(roadBoarders);
}
#move(){
if(this.controls.forward){
this.speed+= this.acceleration;//move up, y moves downward on computer
}
if (this.controls.reverse){
this.speed -= this.acceleration;
}
if(this.speed>this.maxSpeed){
this.speed=this.maxSpeed;
} //caping max speed, when going up
if(this.speed<-this.maxSpeed/2){
this.speed=-this.maxSpeed/2;
} //slowing down slowly
if(this.speed>0){
this.speed-= this.friction;
}
if(this.speed<0){
this.speed+= this.friction;
}
if(Math.abs(this.speed)<this.friction){
this.speed =0;
}
if (this.speed!=0){
const flip= this.speed>0?1:-1;
if(this.controls.left){
this.angle+=0.05*flip;
}
if(this.controls.right){
this.angle-= 0.05*flip;
}
}
// if(this.controls.right){
// this.angle-=0.03;
// // this.x += 2; change to rotation, to make it more realistic and have the same speed condition defined previously
// }
// if (this.controls.left){
// this.angle+=0.03;
// // this.x -= 2;, change to rotation
// }
this.x -= Math.sin(this.angle)*this.speed;
this.y -= Math.cos(this.angle)*this.speed;
}
draw(ctx){//draw is a method
// making rotation
ctx.save(); //save the current state of the canvas(context)
ctx.translate(this.x,this.y);//move the canvas to the center of the car
ctx.rotate(-this.angle);//because we want to rotate it counter clockwise
ctx.beginPath();
ctx.rect(
-this.width/2,
-this.height/2,
this.width,
this.height,
);
ctx.fill();
// ctx.fillStyle = "red";
// ctx.fillRect(this.x,this.y,this.width,this.height);
ctx.restore();//restore the canvas to the state before the rotation
// draw the sensor, car has responsibilty to draw it's own sensor
this.sensor.draw(ctx);
}
}