-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
64 lines (47 loc) · 1.91 KB
/
lib.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
function moveWall()
{
var getWallX = document.getElementById('wall').offsetLeft;
var getWallY = document.getElementById('wall').offsetTop;
var getAIX = document.getElementById('sensor_2').offsetLeft+500;
var getAIY = document.getElementById('ai').offsetTop;
var successRate = Math.floor((avoided/(avoided+crash)*100));
document.getElementById('topDebug').innerHTML = " ["+c+"]<br> Wall ("+getWallX+","+getWallY+")<br> AI ("+getAIX+","+getAIY+")<br> Walls: "+walls+" Avoided: "+avoided+" Crash: "+crash+" Success rate: "+successRate+"%";
if(getWallX <= 0)
{
walls++;
}else{
getWallX = getWallX-20;
document.getElementById('wall').style.left = getWallX+'px';
}
}
// move car up or down, make sure it stays inbound
function moveCar(direction)
{
//auto correct
if(aiPos < 50){aiPos = 50;}
if(aiPos > 200){aiPos = 200;}
if(direction == 'down')
{
aiPos=aiPos+10;
}else{
aiPos=aiPos-10;
}
document.getElementById('ai').style.marginTop = aiPos+"px";
}
function checkCollision()
{
var getWallX = document.getElementById('wall').offsetLeft;
var getAIX = document.getElementById('sensor_2').offsetLeft+500;
var getWallY = document.getElementById('wall').offsetTop+100;
var getAIY = document.getElementById('ai').offsetTop;
if(getWallX < getAIX && getAIY >= getWallY-100 && getAIY < getWallY || getWallX < getAIX && getWallY-100 > getAIY && getWallY-100 < getAIY+50)
{
//moveCar('down');
document.getElementById('sensor_2').style.backgroundColor = 'red';
//decide if the car was hit
if(getWallX < 100){crash++;}
}else{
document.getElementById('sensor_2').style.backgroundColor = 'white';
if(getWallX < 100){avoided++;}
}
}