aboutsummaryrefslogtreecommitdiff
path: root/race/cars/car1ai.js
blob: 87e5920b49a6727231ca552022c216df094b1720 (plain)
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
aiorient = "right";
elem = 10;

function enableAI() {
    car1enableOOBChecker = false;
    setInterval(() => {
        if (aiorient === "top") {
            ai_up();
        } else if (aiorient === "right") {
            ai_right();
        } else if (aiorient === "bottom") {
            ai_down();
        } else if (aiorient === "left") {
            ai_left();
        }
    }, 100)
    setInterval(() => {
        carshb = document.getElementById("aibox-near").getBoundingClientRect();
        hitbox = document.getElementById('barrier').getBoundingClientRect();

        var overlap = !(carshb.right < hitbox.left ||
            carshb.left > hitbox.right ||
            carshb.bottom < hitbox.top ||
            carshb.top > hitbox.bottom)

        if (overlap) {
            aiorient = "right";
        }

        if (elem < 10) { elem++; }
        if (elem !== 10) { return; }

        walls = Array.from(document.getElementsByClassName("wall"));

        walls.forEach((wall) => {
            carshb = document.getElementById("aibox-far").getBoundingClientRect();
            hitbox = wall.getBoundingClientRect();

            var overlap = !(carshb.right < hitbox.left ||
                carshb.left > hitbox.right ||
                carshb.bottom < hitbox.top ||
                carshb.top > hitbox.bottom)

            carshb2 = document.getElementById("aibox-near").getBoundingClientRect();

            var overlap2 = !(carshb2.right < hitbox.left ||
                carshb2.left > hitbox.right ||
                carshb2.bottom < hitbox.top ||
                carshb2.top > hitbox.bottom)

            if (overlap2) {
                car1speed = 8;
            } else if (overlap) {
                car1speed = 9;
            } else {
                car1speed = 16;
            }

            carshb = document.getElementById("aibox-far").getBoundingClientRect();
            hitbox = wall.getBoundingClientRect();

            var overlap = !(carshb.right < hitbox.left ||
                carshb.left > hitbox.right ||
                carshb.bottom < hitbox.top ||
                carshb.top > hitbox.bottom)

            if (overlap) {
                if (aiorient === "top") {
                    aiorient = "right";
                    elem = 0;
                } else if (aiorient === "right") {
                    aiorient = "bottom";
                    elem = 0;
                } else if (aiorient === "bottom") {
                    aiorient = "left";
                    elem = 0;
                } else if (aiorient === "left") {
                    aiorient = "top";
                    elem = 0;
                }
            }

            carshb = document.getElementById("aibox-near").getBoundingClientRect();
            hitbox = document.getElementById('car0').getBoundingClientRect();

            var overlap = !(carshb.right < hitbox.left ||
                carshb.left > hitbox.right ||
                carshb.bottom < hitbox.top ||
                carshb.top > hitbox.bottom)

            if (overlap) {
                if (aiorient === "top") {
                    aiorient = "bottom";
                    elem = 0;
                } else if (aiorient === "right") {
                    aiorient = "left";
                    elem = 0;
                } else if (aiorient === "bottom") {
                    aiorient = "top";
                    elem = 0;
                } else if (aiorient === "left") {
                    aiorient = "right";
                    elem = 0;
                }
            }
        })
    }, 50)
}

function ai_up() {
    if (car1cspeed < car1speed) {
        car1cspeed = car1cspeed + 0.2;
    }
    if (document.getElementById("car1").style.transform !== "rotate(-90deg)") {
        document.getElementById("car1").style.transform = "rotate(-90deg)";
        car1collisionon = false;
        setTimeout(() => {
            car1collisionon = true;
        }, 500)
    }
}

function ai_down() {
    if (car1cspeed < car1speed) {
        car1cspeed = car1cspeed + 0.2;
    }
    if (document.getElementById("car1").style.transform !== "rotate(90deg)") {
        document.getElementById("car1").style.transform = "rotate(90deg)";
        car1collisionon = false;
        setTimeout(() => {
            car1collisionon = true;
        }, 500)
    }
}

function ai_left() {
    if (car1cspeed < car1speed) {
        car1cspeed = car1cspeed + 0.2;
    }
    if (document.getElementById("car1").style.transform !== "rotate(180deg)") {
        document.getElementById("car1").style.transform = "rotate(180deg)";
        car1collisionon = false;
        setTimeout(() => {
            car1collisionon = true;
        }, 500)
    }
}

function ai_right() {
    if (car1cspeed < car1speed) {
        car1cspeed = car1cspeed + 0.2;
    }
    if (document.getElementById("car1").style.transform !== "rotate(0deg)") {
        document.getElementById("car1").style.transform = "rotate(0deg)";
        car1collisionon = false;
        setTimeout(() => {
            car1collisionon = true;
        }, 500)
    }
}