aboutsummaryrefslogtreecommitdiffstats
path: root/src/Ball.ts
diff options
context:
space:
mode:
authorMaksymilian Jopek <maks@jopek.eu>2022-03-27 23:06:05 +0200
committerMaksymilian Jopek <maks@jopek.eu>2022-03-27 23:06:05 +0200
commit6b08c641e9ad1f3b1c7ecc626efc0ee34d3d6925 (patch)
tree52a77c928e527125c752fdc2fec5d1809ea43818 /src/Ball.ts
parentab750d05a2eb580067e51c26032ff24358834d0b (diff)
downloadboules-6b08c641e9ad1f3b1c7ecc626efc0ee34d3d6925.tar.gz
boules-6b08c641e9ad1f3b1c7ecc626efc0ee34d3d6925.tar.zst
boules-6b08c641e9ad1f3b1c7ecc626efc0ee34d3d6925.zip
v0.1.0 - Game without ending
Diffstat (limited to 'src/Ball.ts')
-rw-r--r--src/Ball.ts38
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;
+ }
+}