Skip to content

Commit 59718d9

Browse files
committed
chore: modify deactivate scope
1 parent 12d921d commit 59718d9

2 files changed

Lines changed: 42 additions & 20 deletions

File tree

game/field.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@ import { CellDefinition, loadCatalog } from "../model/catalog.ts"
2727
export class FieldItems implements IStepper, ILoader {
2828
#items: Set<IItem> = new Set()
2929
#coordMap = {} as Record<string, IItem>
30-
#activateScope: RectScope
30+
#deactivateScope: RectScope
3131

3232
constructor(scope: RectScope) {
33-
this.#activateScope = scope
33+
this.#deactivateScope = scope
3434
}
3535

3636
checkDeactivate(i: number, j: number) {
37-
this.#activateScope.setCenter(CELL_SIZE * i, CELL_SIZE * j)
37+
this.#deactivateScope.setCenter(i * CELL_SIZE, j * CELL_SIZE)
38+
3839
this.#items.values()
39-
.filter((item) => !this.#activateScope.overlaps(item))
40+
.filter((item) => !this.#deactivateScope.overlaps(item))
4041
.forEach((item) => {
4142
console.log("deactivating item", item.id)
4243
this.#deactivate(item.i, item.j)
@@ -106,16 +107,17 @@ export class FieldItems implements IStepper, ILoader {
106107
export class FieldProps implements IStepper, ILoader {
107108
#props: Set<IProp> = new Set()
108109
#coordMap = {} as Record<string, IProp>
109-
#activateScope: RectScope
110+
#deactivateScope: RectScope
110111

111112
constructor(scope: RectScope) {
112-
this.#activateScope = scope
113+
this.#deactivateScope = scope
113114
}
114115

115116
checkDeactivate(i: number, j: number) {
116-
this.#activateScope.setCenter(CELL_SIZE * i, CELL_SIZE * j)
117+
this.#deactivateScope.setCenter(i * CELL_SIZE, j * CELL_SIZE)
118+
117119
this.#props.values()
118-
.filter((obj) => !this.#activateScope.overlaps(obj))
120+
.filter((obj) => !this.#deactivateScope.overlaps(obj))
119121
.forEach((obj) => {
120122
console.log("deactivating object", obj.id)
121123
this.remove(obj.i, obj.j)
@@ -207,7 +209,7 @@ class CoordCountMap {
207209
export class FieldActors implements IStepper, ILoader {
208210
#actors: IActor[] = []
209211
#coordCountMap = new CoordCountMap()
210-
#activateScope: RectScope
212+
#deactivateScope: RectScope
211213
#idSet: Set<string> = new Set()
212214

213215
/**
@@ -219,8 +221,8 @@ export class FieldActors implements IStepper, ILoader {
219221
return this.#coordCountMap.get(`${i}.${j}`) > 0
220222
}
221223

222-
constructor(chars: IActor[] = [], activateScope: RectScope) {
223-
this.#activateScope = activateScope
224+
constructor(chars: IActor[] = [], deactivateScope: RectScope) {
225+
this.#deactivateScope = deactivateScope
224226
for (const actor of chars) {
225227
this.add(actor)
226228
}
@@ -264,10 +266,11 @@ export class FieldActors implements IStepper, ILoader {
264266
}
265267

266268
checkDeactivate(i: number, j: number) {
267-
this.#activateScope.setCenter(CELL_SIZE * i, CELL_SIZE * j)
269+
this.#deactivateScope.setCenter(i * CELL_SIZE, j * CELL_SIZE)
270+
268271
const actors = [] as IActor[]
269272
for (const actor of this.#actors) {
270-
if (this.#activateScope.overlaps(actor)) {
273+
if (this.#deactivateScope.overlaps(actor)) {
271274
actors.push(actor)
272275
continue
273276
}
@@ -384,6 +387,7 @@ export class Field implements IField {
384387
#loadScope = new BlockLoadScope()
385388
#unloadScope = new BlockUnloadScope()
386389
#activateScope: RectScope
390+
#deactivateScope: RectScope
387391
#mapLoader = new BlockMapLoader(new URL("map/", location.href).href)
388392
#initialBlocksLoaded = false
389393
#initialActivateReady = false
@@ -394,12 +398,17 @@ export class Field implements IField {
394398

395399
#time = 0
396400

397-
constructor(el: HTMLElement, activateScope: RectScope) {
401+
constructor(
402+
el: HTMLElement,
403+
activateScope: RectScope,
404+
deactivateScope: RectScope,
405+
) {
398406
this.#el = el
399407
this.#activateScope = activateScope
400-
this.#actors = new FieldActors([], activateScope)
401-
this.#items = new FieldItems(activateScope)
402-
this.#props = new FieldProps(activateScope)
408+
this.#deactivateScope = deactivateScope
409+
this.#actors = new FieldActors([], deactivateScope)
410+
this.#items = new FieldItems(deactivateScope)
411+
this.#props = new FieldProps(deactivateScope)
403412
}
404413

405414
get actors() {
@@ -531,6 +540,8 @@ export class Field implements IField {
531540
initialLoad?: boolean
532541
},
533542
) {
543+
this.#activateScope.setCenter(i * CELL_SIZE, j * CELL_SIZE)
544+
534545
const chunks = [...this.#getChunks(i, j)]
535546
const newCharSpawns = chunks.flatMap((c) => c.getCharacterSpawns())
536547
.filter((spawn) => !this.#actors.has(spawn.id)) // isn't spawned yet

game/game-screen.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ class ActivateScope extends RectScope {
3232
}
3333
}
3434

35+
class DeactivateScope extends RectScope {
36+
static MARGIN = 22 * CELL_SIZE
37+
constructor(screenSize: number) {
38+
super(
39+
screenSize + DeactivateScope.MARGIN,
40+
screenSize + DeactivateScope.MARGIN,
41+
)
42+
}
43+
}
44+
3545
const kimiDef = {
3646
type: "main",
3747
main: "main",
@@ -74,13 +84,14 @@ export function GameScreen({ el, query }: Context) {
7484
const charLayer = new DrawLayer(charCanvas, viewScope, { enableNoise: true })
7585

7686
const activateScope = new ActivateScope(screenSize)
87+
const deactivateScope = new DeactivateScope(screenSize)
7788

78-
const field = new Field(query(".field")!, activateScope)
89+
const field = new Field(query(".field")!, activateScope, deactivateScope)
7990
field.actors.add(me)
8091

8192
signal.centerGrid10.subscribe(({ i, j }) => {
82-
field.checkBlockLoad(i, j, viewScope)
83-
field.checkBlockUnload(i, j)
93+
field.checkBlockLoad(me.i, me.j, viewScope)
94+
field.checkBlockUnload(me.i, me.j)
8495
})
8596

8697
signal.centerPixel.subscribe(({ x, y }) => {

0 commit comments

Comments
 (0)