@@ -23,6 +23,7 @@ import {
2323 DemoGameSettings ,
2424 GameInitializer ,
2525} from "./GameInitializer" ;
26+ import { SourceGrid } from "../grid/SourceGrid" ;
2627
2728export type GameSettings = {
2829 serializedJSON ?: string ;
@@ -67,6 +68,7 @@ export const GameState_S = zod.object({
6768 continued : zod . boolean ( ) ,
6869 tileStack : TileStackWithSlots_S ,
6970 tiles : TileSet_S ,
71+ sourceGrid : zod . optional ( zod . unknown ( ) ) ,
7072} ) ;
7173export type GameState_S = zod . infer < typeof GameState_S > ;
7274
@@ -114,28 +116,37 @@ export class Game extends EventTarget {
114116
115117 this . settings = settings ;
116118
117- this . grid = new Grid ( this . settings . atlas ) ;
118- if ( settings . rules ) this . grid . rules = settings . rules . create ( ) ;
119- this . scorer = ( settings . scorer || ConnectedSegmentScorer ) . create ( ) ;
120-
119+ let state = null ;
120+ let sourceGrid = undefined ;
121121 if ( restoreState ) {
122- // restore previous game state
123122 try {
124- const state = GameState_S . parse ( restoreState ) ;
125- this . points = state . points ;
126- this . continued = state . continued ;
127- this . tileStack = TileStackWithSlots . restore (
128- state . tileStack ,
129- this . grid . atlas . shapes ,
130- ) ;
131- this . grid . restoreTiles ( state . tiles ) ;
132- return ;
123+ state = GameState_S . parse ( restoreState ) ;
124+ if ( state . sourceGrid && this . settings . atlas . sourceGrid ) {
125+ sourceGrid = this . settings . atlas . sourceGrid . restore (
126+ state . sourceGrid ,
127+ ) ;
128+ }
133129 } catch ( err ) {
134130 console . log ( err ) ;
135131 // continue with default initialization
136132 }
137133 }
138134
135+ this . grid = new Grid ( this . settings . atlas , undefined , sourceGrid ) ;
136+ if ( settings . rules ) this . grid . rules = settings . rules . create ( ) ;
137+ this . scorer = ( settings . scorer || ConnectedSegmentScorer ) . create ( ) ;
138+
139+ if ( state ) {
140+ this . points = state . points ;
141+ this . continued = state . continued ;
142+ this . tileStack = TileStackWithSlots . restore (
143+ state . tileStack ,
144+ this . grid . atlas . shapes ,
145+ ) ;
146+ this . grid . restoreTiles ( state . tiles ) ;
147+ return ;
148+ }
149+
139150 this . points = 0 ;
140151 this . continued = false ;
141152
@@ -182,6 +193,7 @@ export class Game extends EventTarget {
182193 continued : this . continued ,
183194 tileStack : this . tileStack . save ( this . grid . atlas . shapes ) ,
184195 tiles : this . grid . saveTilesAndPlaceholders ( ) ,
196+ sourceGrid : this . grid . sourceGrid ?. save ( ) ,
185197 } ;
186198 }
187199
0 commit comments