Skip to content

Commit 6d5ff7b

Browse files
committed
Fix flagging, unflagging and opening
1 parent 8c79dd3 commit 6d5ff7b

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

src/CellSprite.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ type MyTexture = PIXI.Texture;
1111

1212
type NeighborCount = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
1313

14-
// todo rename to neighborcount
15-
function numberToValueNumber(value: number): NeighborCount {
14+
export function numberToNeighborCount(
15+
value: number | null,
16+
): NeighborCount | null {
17+
if (value === null) return value;
1618
assert(value % 1 === 0);
1719
assert(value >= 0);
1820
assert(value <= 8);
@@ -21,17 +23,17 @@ function numberToValueNumber(value: number): NeighborCount {
2123
}
2224

2325
export class CellSprite {
24-
private value: NeighborCount | null;
26+
public neighborCount: NeighborCount | null;
2527
private back: PIXI.Sprite;
2628
private front: PIXI.Sprite;
2729

2830
public constructor(
2931
cell: Cell,
30-
value: number | null,
32+
neighborCount: NeighborCount | null,
3133
parent: PIXI.Container,
3234
playAnimation: boolean,
3335
) {
34-
this.value = value === null ? null : numberToValueNumber(value);
36+
this.neighborCount = neighborCount;
3537
const cellTexture = this.getCellTexture(cell);
3638
this.back = new PIXI.Sprite(cellTexture.back);
3739
this.front = new PIXI.Sprite(cellTexture.front);
@@ -50,8 +52,6 @@ export class CellSprite {
5052
this.front.y = y;
5153
this.back.x = x;
5254
this.back.y = y;
53-
this.back.name = "bg";
54-
this.front.name = "fg";
5555
this.back.zIndex = 1;
5656
this.front.zIndex = 2;
5757
parent.addChild(this.back, this.front);
@@ -80,20 +80,19 @@ export class CellSprite {
8080
back: MyTexture;
8181
front: MyTexture;
8282
} {
83-
// todo create a getter, maybe call load every time
8483
const textures = getTextures();
8584

86-
let back;
87-
let front;
85+
let back: PIXI.Texture;
86+
let front: PIXI.Texture;
8887

8988
if (cell.isOpen) {
9089
back = textures.open;
9190
if (cell.isMine) front = textures.mineWrong;
92-
else if (this.value !== null && this.value > 0)
93-
front = textures[this.value as 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8];
91+
else if (this.neighborCount !== null && this.neighborCount > 0)
92+
front = textures[this.neighborCount as 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8];
9493
else front = PIXI.Texture.EMPTY;
9594
} else {
96-
back = textures.closed;
95+
back = PIXI.Texture.EMPTY;
9796
front = cell.isFlagged ? textures.flag : PIXI.Texture.EMPTY;
9897
}
9998

src/FieldRenderer.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import * as PIXI from "pixi.js";
66
import { getTextures } from "./Textures.js";
77
import { Controls } from "./Controls";
8-
import { CellSprite } from "./CellSprite";
8+
import { CellSprite, numberToNeighborCount } from "./CellSprite";
99
import { Cell } from "./Cell.js";
1010
import { Field } from "./Field.js";
1111
import { FieldPersistence } from "./FieldPersistence.js";
1212

13-
// todo bug: when i flag a safe cell, then unflag, then open, then the cell looks like it is a 0.
13+
// todo bug: when i open a mine, and the mine has only a single neighbor which is a closed mine, then right pressing the mine flags the
1414
type CellSprites = Record<
1515
number,
1616
Record<number, CellSprite | undefined> | undefined
@@ -46,13 +46,17 @@ export class FieldRenderer extends PIXI.Application {
4646
this.cellSprites[cell.y] = {};
4747
}
4848
let cellSprite = this.cellSprites[cell.y]![cell.x];
49+
const neighborCount = numberToNeighborCount(
50+
this.field.value(cell.x, cell.y),
51+
);
52+
4953
if (cellSprite) {
54+
cellSprite.neighborCount = neighborCount;
5055
cellSprite.update(cell);
5156
} else {
52-
const value = this.field.value(cell.x, cell.y);
5357
cellSprite = new CellSprite(
5458
cell,
55-
value,
59+
neighborCount,
5660
this.fieldContainer,
5761
playAnimation,
5862
);
@@ -71,12 +75,11 @@ export class FieldRenderer extends PIXI.Application {
7175

7276
// todo inline
7377
private setup(): void {
74-
// todo migrate away from tilingsprite
75-
const background = new PIXI.TilingSprite(
76-
getTextures().closed,
77-
this.renderer?.width ?? window.innerWidth,
78-
this.renderer?.height ?? window.innerHeight,
79-
);
78+
const background = new PIXI.TilingSprite({
79+
texture: getTextures().closed,
80+
width: this.renderer?.width ?? window.innerWidth,
81+
height: this.renderer?.height ?? window.innerHeight,
82+
});
8083

8184
window.addEventListener("resize", () => {
8285
const width = window.innerWidth;

0 commit comments

Comments
 (0)