class Cart {
constructor (id, row, col, symbol) {
this.id = id;
this.alive = true;
this.intersection = 0;
this.row = row;
this.col = col;
this.direction = {
'>': math.complex('1'),
'<': math.complex('-1'),
'^': math.complex('i'),
'v': math.complex('-i')
}[symbol];
}
rotate (direction) {
if (direction === 'R') {
this.direction = math.multiply(this.direction, math.complex('-i'));
} else if (direction === 'L') {
this.direction = math.multiply(this.direction, math.complex('i'));
}
}
move () {
if (this.direction.re === 0) {
this.row -= this.direction.im;
} else {
this.col += this.direction.re;
}
if (TRACK[this.row][this.col] === '/') {
(this.direction.re === 0) ? this.rotate('R') : this.rotate('L');
} else if (TRACK[this.row][this.col] === '\\') {
(this.direction.re === 0) ? this.rotate('L') : this.rotate('R');
} else if (TRACK[this.row][this.col] === '+') {
if (this.intersection === 0) {
this.rotate('L');
} else if (this.intersection === 2) {
this.rotate('R');
}
this.intersection = (this.intersection + 1) % 3;
}
}
static collides (a, b) {
return (a.id !== b.id &&
a.alive && b.alive &&
a.row === b.row &&
a.col === b.col);
}
}