@@ -53,25 +53,35 @@ export class FieldItems implements IStepper, ILoader {
5353 #items: Set < IItem > = new Set ( )
5454 #deactivateScope: RectScope
5555 #gridSet = new GridSet < IItem > ( )
56+ #idSet = new Set < string > ( )
5657
5758 constructor ( scope : RectScope ) {
5859 this . #deactivateScope = scope
5960 }
6061
6162 checkDeactivate ( i : number , j : number ) {
6263 this . #deactivateScope. setCenter ( i * CELL_SIZE , j * CELL_SIZE )
63- ; [ ...this . #items. values ( ) ]
64+ let c = 0
65+ this . #items. values ( )
6466 . filter ( ( item ) => ! this . #deactivateScope. overlaps ( item ) )
6567 . forEach ( ( item ) => {
66- console . log ( "deactivating item" , item . id )
68+ c ++
6769 this . #deactivate( item . i , item . j , item . id )
6870 } )
71+ if ( c > 0 ) {
72+ console . log ( `deactivating ${ c } items` )
73+ }
6974 signal . itemsCount . update ( this . #items. size )
7075 }
7176
77+ has ( id : string ) {
78+ return this . #idSet. has ( id )
79+ }
80+
7281 add ( item : IItem ) {
7382 this . #items. add ( item )
7483 this . #gridSet. add ( item . i , item . j , item )
84+ this . #idSet. add ( item . id )
7585 signal . itemsCount . update ( this . #items. size )
7686 }
7787
@@ -106,6 +116,7 @@ export class FieldItems implements IStepper, ILoader {
106116 }
107117 this . #items. delete ( item )
108118 this . #gridSet. delete ( i , j , item )
119+ this . #idSet. delete ( id )
109120 signal . itemsCount . update ( this . #items. size )
110121 return item
111122 }
@@ -127,14 +138,15 @@ export class FieldItems implements IStepper, ILoader {
127138
128139 async loadAssets ( options : LoadOptions ) : Promise < void > {
129140 await Promise . all (
130- [ ... this . #items]
141+ this . #items. values ( )
131142 . filter ( ( item ) => ! item . assetsReady )
132- . map ( ( item ) => item . loadAssets ( options ) ) ,
143+ . map ( ( item ) => item . loadAssets ( options ) )
144+ . toArray ( ) ,
133145 )
134146 }
135147
136148 get assetsReady ( ) : boolean {
137- return [ ... this . #items] . every ( ( x ) => x . assetsReady )
149+ return this . #items. values ( ) . every ( ( x ) => x . assetsReady )
138150 }
139151
140152 iter ( ) {
@@ -153,12 +165,16 @@ export class FieldProps implements IStepper, ILoader {
153165
154166 checkDeactivate ( i : number , j : number ) {
155167 this . #deactivateScope. setCenter ( i * CELL_SIZE , j * CELL_SIZE )
156- ; [ ...this . #props. values ( ) ]
168+ let c = 0
169+ this . #props. values ( )
157170 . filter ( ( obj ) => ! this . #deactivateScope. overlaps ( obj ) )
158171 . forEach ( ( obj ) => {
159- console . log ( "deactivating object" , obj . id )
172+ c ++
160173 this . remove ( obj . i , obj . j )
161174 } )
175+ if ( c > 0 ) {
176+ console . log ( `deactivating ${ c } objects` )
177+ }
162178 signal . propsCount . update ( this . #props. size )
163179 }
164180
@@ -306,17 +322,21 @@ export class FieldActors implements IStepper, ILoader {
306322 this . #deactivateScope. setCenter ( i * CELL_SIZE , j * CELL_SIZE )
307323
308324 const actors = [ ] as IActor [ ]
325+ let c = 0
309326 for ( const actor of this . #actors) {
310327 if ( this . #deactivateScope. overlaps ( actor ) ) {
311328 actors . push ( actor )
312329 continue
313330 }
314- console . log ( "deactivating actor" , actor . id )
331+ c ++
315332 this . #idSet. delete ( actor . id )
316333 this . #coordCountMap. decrement ( actor . physicalGridKey )
317334 }
318335 this . #actors = actors
319336 signal . actorsCount . update ( this . #actors. length )
337+ if ( c > 0 ) {
338+ console . log ( `deactivating ${ c } actors` )
339+ }
320340 }
321341
322342 iter ( ) {
@@ -590,9 +610,7 @@ export class Field implements IField {
590610 this . #addBlock( new FieldBlock ( map ) )
591611 }
592612 const initialLoad = ! this . #initialBlocksLoaded
593- await Promise . all (
594- [ ...this . #getChunks( i , j ) ] . map ( ( c ) => c . render ( initialLoad ) ) ,
595- )
613+ await Promise . all ( this . #getChunks( i , j ) . map ( ( c ) => c . render ( initialLoad ) ) )
596614 if ( ! this . #initialBlocksLoaded) {
597615 this . #initialBlocksLoaded = true
598616 this . checkActivate (
@@ -627,23 +645,30 @@ export class Field implements IField {
627645 this . #activateScope. setCenter ( i * CELL_SIZE , j * CELL_SIZE )
628646
629647 const chunks = [ ...this . #getChunks( i , j ) ]
630- const newCharSpawns = chunks . flatMap ( ( c ) => c . getCharacterSpawns ( ) )
648+ const newActorSpawns = chunks . values ( )
649+ . flatMap ( ( c ) => c . getCharacterSpawns ( ) )
631650 . filter ( ( spawn ) => ! this . #actors. has ( spawn . id ) ) // isn't spawned yet
632651 . filter ( ( spawn ) => initialLoad || ! viewScope . overlaps ( spawn ) ) // not in view
633652 . filter ( ( spawn ) => this . #activateScope. overlaps ( spawn ) ) // in activate scope
653+ . toArray ( )
634654
635- const newItemSpawns = chunks . flatMap ( ( c ) => c . getItemSpawns ( ) )
655+ const newItemSpawns = chunks . values ( )
656+ . flatMap ( ( c ) => c . getItemSpawns ( ) )
636657 . filter ( ( spawn ) => ! this . #items. isCollected ( spawn . id ) ) // isn't collected yet
658+ . filter ( ( spawn ) => ! this . #items. has ( spawn . id ) ) // item is already on the field
637659 . filter ( ( spawn ) => initialLoad || ! viewScope . overlaps ( spawn ) ) // not in view
638660 . filter ( ( spawn ) => this . #activateScope. overlaps ( spawn ) ) // in activate scope
661+ . toArray ( )
639662
640- const newObjectSpawns = chunks . flatMap ( ( c ) => c . getPropSpawns ( ) )
663+ const newPropSpawns = chunks . values ( )
664+ . flatMap ( ( c ) => c . getPropSpawns ( ) )
641665 . filter ( ( spawn ) => initialLoad || ! viewScope . overlaps ( spawn ) ) // not in view
642666 . filter ( ( spawn ) => this . #activateScope. overlaps ( spawn ) ) // in activate scope
667+ . toArray ( )
643668
644- if ( newCharSpawns . length > 0 ) {
669+ if ( newActorSpawns . length > 0 ) {
645670 let i = 0
646- for ( const spawn of newCharSpawns ) {
671+ for ( const spawn of newActorSpawns ) {
647672 i ++
648673 this . #actors. add ( Actor . fromSpawn ( spawn ) )
649674 }
@@ -681,9 +706,9 @@ export class Field implements IField {
681706 await this . #items. loadAssets ( { loadImage } )
682707 }
683708
684- if ( newObjectSpawns . length > 0 ) {
709+ if ( newPropSpawns . length > 0 ) {
685710 let i = 0
686- for ( const spawn of newObjectSpawns ) {
711+ for ( const spawn of newPropSpawns ) {
687712 if ( this . #props. get ( spawn . i , spawn . j ) ) {
688713 // The space is already occupied by some other object
689714 continue
0 commit comments