3
3
*/
4
4
5
5
import * as PIXI from "pixi.js" ;
6
- import { getTextures , loadTextures } from "./Textures.js" ;
6
+ import { getTextures } from "./Textures.js" ;
7
7
import { Controls } from "./Controls" ;
8
8
import { CellSprite } from "./CellSprite" ;
9
9
import { Cell } from "./Cell.js" ;
@@ -16,25 +16,28 @@ type CellSprites = Record<
16
16
> ;
17
17
18
18
export class FieldRenderer extends PIXI . Application {
19
- private field : Field ;
19
+ private fieldContainer = new PIXI . Container ( {
20
+ isRenderGroup : true ,
21
+ interactiveChildren : false ,
22
+ } ) ;
23
+
24
+ private clickHandler = new PIXI . Container ( ) ;
20
25
21
26
public cellSprites : CellSprites = { } ;
22
27
23
28
public constructor (
24
- field : Field ,
29
+ private field : Field ,
25
30
updateScore : ( input : Field ) => void ,
26
- fieldPersistence : FieldPersistence ,
31
+ private fieldPersistence : FieldPersistence ,
27
32
) {
28
33
super ( ) ;
29
- this . field = field ;
30
34
31
35
field . on ( "cellChanged" , ( cell ) => {
32
36
this . updateCell ( cell , true ) ;
33
37
updateScore ( field ) ;
34
38
} ) ;
35
39
36
- // todo use async, move to before fieldRenderer initialisation.
37
- loadTextures ( ) . then ( ( ) => this . setup ( field , fieldPersistence ) ) ;
40
+ this . setup ( ) ;
38
41
}
39
42
40
43
public updateCell ( cell : Cell , playAnimation : boolean ) {
@@ -46,7 +49,12 @@ export class FieldRenderer extends PIXI.Application {
46
49
cellSprite . update ( cell ) ;
47
50
} else {
48
51
const value = this . field . value ( cell . x , cell . y ) ;
49
- cellSprite = new CellSprite ( cell , value , fieldContainer , playAnimation ) ;
52
+ cellSprite = new CellSprite (
53
+ cell ,
54
+ value ,
55
+ this . fieldContainer ,
56
+ playAnimation ,
57
+ ) ;
50
58
this . cellSprites [ cell . y ] ! [ cell . x ] = cellSprite ;
51
59
}
52
60
}
@@ -61,63 +69,37 @@ export class FieldRenderer extends PIXI.Application {
61
69
// todo: bug when i reset the game to reset the camera to 0,0, then the cell at 0,0 flashes with the "zoom out to spawn" animation every time the camera moves a pixel.
62
70
63
71
// todo inline
64
- private setup (
65
- // todo get from textures directly
66
-
67
- field : Field ,
68
- fieldPersistence : FieldPersistence ,
69
- ) : void {
70
- const closedTexture = getTextures ( ) . closed ;
71
-
72
+ private setup ( ) : void {
72
73
// todo migrate away from tilingsprite
73
74
const background = new PIXI . TilingSprite (
74
- closedTexture ,
75
- app . renderer . width ,
76
- app . renderer . height ,
75
+ getTextures ( ) . closed ,
76
+ this . renderer ? .width ?? window . innerWidth ,
77
+ this . renderer ? .height ?? window . innerHeight ,
77
78
) ;
78
79
79
- window . addEventListener ( "resize" , function ( _event ) {
80
- function resize ( width : number , height : number ) {
81
- app . renderer . resize ( width , height ) ;
82
- background . width = app . renderer . width ;
83
- background . height = app . renderer . height ;
84
- }
80
+ window . addEventListener ( "resize" , ( ) => {
81
+ const width = window . innerWidth ;
82
+ const height = window . innerHeight ;
85
83
86
- resize ( window . innerWidth , window . innerHeight ) ;
87
- } ) ;
84
+ this . renderer . resize ( width , height ) ;
88
85
89
- // const width = minesTextures.closed?.width;
86
+ background . width = width ;
87
+ background . height = height ;
88
+ } ) ;
90
89
91
- // todo this is deprecated?
92
90
background . name = "bg" ;
93
- fieldContainer . name = "fg" ;
91
+ this . fieldContainer . name = "fg" ;
94
92
95
- clickHandler . addChildAt ( background , 0 ) ;
96
- clickHandler . addChildAt ( fieldContainer , 1 ) ;
93
+ this . clickHandler . addChildAt ( background , 0 ) ;
94
+ this . clickHandler . addChildAt ( this . fieldContainer , 1 ) ;
95
+ this . clickHandler . eventMode = "static" ;
97
96
98
- new Controls ( clickHandler , field , fieldPersistence ) ;
97
+ this . stage . addChild ( this . clickHandler ) ;
99
98
100
99
this . updateAllCells ( ) ;
101
100
}
102
- }
103
101
104
- const app = new PIXI . Application ( ) ;
105
- const fieldContainer = new PIXI . Container ( {
106
- isRenderGroup : true ,
107
- interactiveChildren : false ,
108
- } ) ;
109
- const clickHandler = new PIXI . Container ( ) ;
110
-
111
- ( async ( ) => {
112
- await app . init ( {
113
- resizeTo : window ,
114
- backgroundColor : 0x0f0f0f ,
115
- } ) ;
116
-
117
- document . body . appendChild ( app . canvas ) ;
118
-
119
- PIXI . TextureSource . defaultOptions . scaleMode = "nearest" ;
120
-
121
- clickHandler . eventMode = "static" ;
122
- app . stage . addChild ( clickHandler ) ;
123
- } ) ( ) ;
102
+ public setupAfterCanvasReady ( ) {
103
+ new Controls ( this . clickHandler , this . field , this . fieldPersistence ) ;
104
+ }
105
+ }
0 commit comments