diff options
Diffstat (limited to 'src/Ball.ts')
| -rw-r--r-- | src/Ball.ts | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/Ball.ts b/src/Ball.ts index a8dc912..e2c04f1 100644 --- a/src/Ball.ts +++ b/src/Ball.ts @@ -1,5 +1,6 @@ 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; @@ -11,15 +12,46 @@ export default class Ball { this.y = y; this.color = color; } - static generateNewBalls(): Array<Ball> { + + 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), getRandomEl(COLORS)]; - } while (out.filter(b => b.x === x && b.y === y).length !== 0) + [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; + } +} |
