From 6b08c641e9ad1f3b1c7ecc626efc0ee34d3d6925 Mon Sep 17 00:00:00 2001 From: Maksymilian Jopek Date: Sun, 27 Mar 2022 23:06:05 +0200 Subject: v0.1.0 - Game without ending --- src/Ball.ts | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'src/Ball.ts') 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 { + + 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 { const out = []; 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; + } +} -- cgit v1.3.1