-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
192 lines (174 loc) · 6.65 KB
/
index.html
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<style>
*{
padding: 0;
margin: 0;
}
body {
font-family: 'Roboto', sans-serif;
}
#canvas {
background: #ddd;
}
#info {
min-width: 150px;
position: fixed;
top: 0;
right: 0;
background: rgba(0, 0, 0, 0.9);
color: white;
}
.info-item{
padding:7px 15px;
display: flex;
align-items: center;
justify-content: space-between;
}
.info-item:not(:last-child){
border-bottom: 1px solid rgba(255,255,255,.1);
}
.info-item-name{
margin-right:10px;
color:rgba(255, 255, 255, 0.7);
font-size: 13px;
}
.info-item-name::after{
content: ' :';
}
.info-item-value{
display: flex;
align-items: center;
}
.info-item-value::after{
margin-left:5px;
content: attr(data-unit);
font-size: 13px;
color:rgba(255,255,255,0.7);
}
</style>
</head>
<body>
<div id="info">
<div class="info-item">
<span class="info-item-name">Time</span>
<span class="info-item-value" data-info-key="time" data-unit="s">0</span>
</div>
<div class="info-item">
<span class="info-item-name">Displacement</span>
<span class="info-item-value" data-info-key="displacement" data-unit="px">0</span>
</div>
<div class="info-item">
<span class="info-item-name">Velocity</span>
<span class="info-item-value" data-info-key="velocity" data-unit="px/s">0</span>
</div>
<div class="info-item">
<span class="info-item-name">Distance</span>
<span class="info-item-value" data-info-key="distance" data-unit="px">0</span>
</div>
<div class="info-item">
<span class="info-item-name">Speed</span>
<span class="info-item-value" data-info-key="speed" data-unit="px/s">0</span>
</div>
<div class="info-item">
<span class="info-item-name">Instantaneous Velocity</span>
<span class="info-item-value" data-info-key="instantaneous-velocity" data-unit="px/s">0</span>
</div>
<div class="info-item">
<span class="info-item-name">Frame Rate</span>
<span class="info-item-value" data-info-key="fps" data-unit="fps">0</span>
</div>
</div>
<canvas id="canvas"></canvas>
<script src="src/utility.js"></script>
<script src="src/Game.js"></script>
<script src="src/Info.js"></script>
<script src="src/objects/GameObject.js"></script>
<script src="src/objects/Rectangle.js"></script>
<script src="src/objects/Button.js"></script>
<script src="src/objects/Car.js"></script>
<script src="src/objects/DisplacementLine.js"></script>
<script src="src/objects/Dot.js"></script>
<script src="src/objects/Speedometer.js"></script>
<script>
const info = new Info();
const canvas = document.getElementById('canvas');
canvas.width = innerWidth - 1;
canvas.height = innerHeight - 1;
const ctx = canvas.getContext('2d');
let startTime;
const startPos = [400, 400];
let gameObjects = [
new Car(startPos.slice()), new DisplacementLine(startPos, startPos), new Speedometer(),
new Button([35 + 60, canvas.height - 35 - 60], 'W'),
new Button([35, canvas.height - 35], 'A'),
new Button([35 + 60, canvas.height - 35], 'S'),
new Button([35 + 2 * 60, canvas.height - 35], 'D'),
];
const dotsStartIndexInGameObject = gameObjects.length;
const game = new Game();
game.register();
const assets = [
game.addAsset('car', 'src/assets/car.png')
];
game.registerKeyGroup('forward', ['w', 'W', 'ص', 'ArrowUp']);
game.registerKeyGroup('backward', ['s', 'S', 'س', 'ArrowDown']);
game.registerKeyGroup('turnRight', ['d', 'D', 'ی', 'ArrowRight']);
game.registerKeyGroup('turnLeft', ['a', 'A', 'ش', 'ArrowLeft']);
game.onKeyPress(['forward', 'backward'], function (key) {
if(!startTime) startTime = Date.now();
});
game.onKeyPress(['forward', 'backward', 'turnRight', 'turnLeft'], key => {
const mapKeyGroupToObjectIndex = {forward: 3, turnLeft: 4, backward: 5, turnRight: 6};
for(const [name, keys] of Object.entries(game.keys)){
if(!keys.includes(key)) continue;
gameObjects[mapKeyGroupToObjectIndex[name]].active = true;
break;
}
});
game.onKeyRelease(['forward', 'backward', 'turnRight', 'turnLeft'], key => {
const mapKeyGroupToObjectIndex = {forward: 3, turnLeft: 4, backward: 5, turnRight: 6};
for(const [name, keys] of Object.entries(game.keys)){
if(!keys.includes(key)) continue;
gameObjects[mapKeyGroupToObjectIndex[name]].active = false;
break;
}
})
game.loop = function(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (game.isKeyPressed('forward')) gameObjects[0].forward();
if (game.isKeyPressed('backward')) gameObjects[0].backward();
if (game.isKeyPressed('turnRight')) gameObjects[0].turn(5);
if (game.isKeyPressed('turnLeft')) gameObjects[0].turn(-5);
let deltaTime = Date.now() - startTime;
info.updateAttribute('time', (deltaTime / 1000).toFixed(1));
gameObjects.forEach(gameObject => {
gameObject.process().draw();
})
const displacement = euclideanDistance(gameObjects[0].pos, startPos);
const velocity = displacement / (deltaTime / 1000);
info.updateAttribute('displacement', Math.round(displacement));
info.updateAttribute('velocity', velocity.toFixed(1));
const distance = gameObjects.slice(dotsStartIndexInGameObject).reduce(({ distance, prevDot }, currentDot) => {
const newDistance = distance + euclideanDistance(prevDot.pos, currentDot.pos);
return { distance: newDistance, prevDot: currentDot };
}, { distance: 0, prevDot: gameObjects[1] }).distance;
info.updateAttribute('distance', Math.round(distance));
info.updateAttribute('speed', (distance / (deltaTime / 1000)).toFixed(1));
info.updateFPS(Math.round(game.fps));
info.updateAttribute('instantaneous-velocity', Math.round(gameObjects[0].instantaneousVelocity));
};
Promise.all(assets).then(() => {
game.start();
})
</script>
</body>
</html>