-
Notifications
You must be signed in to change notification settings - Fork 13
/
racer.js
113 lines (95 loc) · 2.12 KB
/
racer.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
/*global Car,TO_RADIANS,drawRotatedImage */
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
ctxW = canvas.width,
ctxH = canvas.height,
player = new Car(),
track = new Image(),
trackHit = new Image(),
elPX = document.getElementById('px'),
elPY = document.getElementById('py')
;
track.src = "track.png";
trackHit.src = "track-hit.png";
// collision
var hit = new HitMap(trackHit);
// Keyboard Variables
var key = {
UP: 38,
DOWN: 40,
LEFT: 37,
RIGHT: 39
};
var keys = {
38: false,
40: false,
37: false,
39: false
};
function speedXY (rotation, speed) {
return {
x: Math.sin(rotation * TO_RADIANS) * speed,
y: Math.cos(rotation * TO_RADIANS) * speed * -1,
};
}
var x=10,y=10;
function step (car) {
if (car.code === 'player'){
// constantly decrease speed
if (!car.isMoving()){
car.speed = 0;
} else {
car.speed *= car.speedDecay;
}
// keys movements
if (keys[key.UP]) { car.accelerate(); }
if (keys[key.DOWN]){ car.decelerate(); }
if (keys[key.LEFT]){ car.steerLeft(); }
if (keys[key.RIGHT]){car.steerRight(); }
var speedAxis = speedXY(car.rotation, car.speed);
car.x += speedAxis.x;
car.y += speedAxis.y;
// collisions
if (car.collisions.left.isHit(hit)){
car.steerRight();
car.decelerate(1);
}
if (car.collisions.right.isHit(hit)){
car.steerLeft();
car.decelerate(1);
}
if (car.collisions.top.isHit(hit)){
car.decelerate(1);
}
if (car.collisions.bottom.isHit(hit)){
car.decelerate(1);
}
// info
elPX.innerHTML = Math.floor(car.x);
elPY.innerHTML = Math.floor(car.y);
}
}
function draw (car) {
context.clearRect(0,0,ctxW,ctxH);
context.drawImage(track, 0, 0);
drawRotatedImage(car.img, car.x, car.y, car.rotation);
}
// Keyboard event listeners
$(window).keydown(function(e){
if (keys[e.keyCode] !== 'undefined'){
keys[e.keyCode] = true;
// e.preventDefault();
}
});
$(window).keyup(function(e){
if (keys[e.keyCode] !== 'undefined'){
keys[e.keyCode] = false;
// e.preventDefault();
}
});
function frame () {
step(player);
draw(player);
window.requestAnimationFrame(frame);
}
frame();