aboutsummaryrefslogtreecommitdiffstats
path: root/src/Ball.ts
blob: e2c04f1066635790bd05be2010a9b52045934476 (plain) (blame)
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
import { START_NO_OF_BALLS, MAP_WIDTH, MAP_HEIGHT, COLORS } from "./consts";
import { getRandomInt, getRandomEl } from "./utils";
import { getTd } from "./ui";

export default class Ball {
  x: number;
  y: number;
  color: string;

  constructor(x: number, y: number, color: string) {
    this.x = x;
    this.y = y;
    this.color = color;
  }

  get td() {
    return getTd(this.x, this.y);
  }
  isEqual(b: Ball) {
    if (!b) throw Error("b is undeifneds")
    if (!this) throw Error("this is undeifneds")
    return this.x === b.x && this.y === b.y;
  }

  static generateNewBalls(currentBalls: Ball[]): Array<Ball> {
    const out = <Ball[]>[];
    for (let i = 0; i < START_NO_OF_BALLS; i++) {
      let x: number, y: number, color: string;
      do {
        [x, y, color] = [getRandomInt(0, MAP_HEIGHT), getRandomInt(0, MAP_WIDTH), Nexts.pop()];
      } while (
        out.find(b => b.x === x && b.y === y) ||
        currentBalls.find(b => b.x === x && b.y === y)
      )
      out.push(new Ball(x, y, color));
    }
    return out;
  }
}

export class Nexts {
  static els = Array.from(document.querySelectorAll("#next div")) as HTMLDivElement[]
  static setEls() {
    for (const el of this.els) {
      if (el.style.backgroundColor === "") {
        el.style.backgroundColor = getRandomEl(COLORS);
      }
    }
  }
  static pop() {
    const out = this.els[0].style.backgroundColor;
    this.els[0].style.backgroundColor = this.els[1].style.backgroundColor;
    this.els[1].style.backgroundColor = this.els[2].style.backgroundColor
    this.els[2].style.backgroundColor = getRandomEl(COLORS)
    return out;
  }
}