Skip to content

Commit

Permalink
🐛 Fix nested copies not being removed from appended/prepended rooms w…
Browse files Browse the repository at this point in the history
…hen a user calls `rooms.remove` on them.
  • Loading branch information
CosmoMyzrailGorynych committed Jun 29, 2024
1 parent 9abbfd8 commit 04acd47
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
35 changes: 1 addition & 34 deletions src/ct.release/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import resM from 'res';
import roomsM, {Room} from './rooms';
import soundsM from 'sounds';
import stylesM from 'styles';
import templatesM, {BasicCopy} from './templates';
import templatesM, {BasicCopy, killRecursive} from './templates';
import tilemapsM, {Tilemap} from './tilemaps';
import timerM from './timer';
import {scriptsLib as scriptsM} from './scripts';
Expand Down Expand Up @@ -186,39 +186,6 @@ export let pixiApp: pixiMod.Application;

let loading: Promise<void>;
{
const killRecursive = (copy: (BasicCopy & pixiMod.DisplayObject) | Background) => {
copy.kill = true;
if (templatesM.isCopy(copy) && (copy as BasicCopy).onDestroy) {
templatesM.onDestroy.apply(copy);
(copy as BasicCopy).onDestroy.apply(copy);
}
if (copy.children) {
for (const child of copy.children) {
if (templatesM.isCopy(child)) {
killRecursive(child as (BasicCopy & pixiMod.DisplayObject)); // bruh
}
}
}
const stackIndex = stack.indexOf(copy);
if (stackIndex !== -1) {
stack.splice(stackIndex, 1);
}
if (templatesM.isCopy(copy) && (copy as BasicCopy).template) {
if ((copy as BasicCopy).template) {
const {template} = (copy as BasicCopy);
if (template) {
const templatelistIndex = templatesM
.list[template]
.indexOf((copy as BasicCopy));
if (templatelistIndex !== -1) {
templatesM.list[template]
.splice(templatelistIndex, 1);
}
}
}
}
deadPool.push(copy);
};
const manageCamera = () => {
cameraM.update(uM.timeUi);
cameraM.manageStage();
Expand Down
4 changes: 2 additions & 2 deletions src/ct.release/rooms.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import uLib from './u';
import backgrounds, {Background} from './backgrounds';
import templatesLib, {BasicCopy} from './templates';
import templatesLib, {BasicCopy, killRecursive} from './templates';
import {Tilemap} from './tilemaps';
import mainCamera from './camera';
import {copyTypeSymbol, deadPool, pixiApp, stack, forceDestroy} from '.';
Expand Down Expand Up @@ -429,7 +429,7 @@ const roomsLib = {
pixiApp.stage.removeChild(room);
for (const copy of room.children) {
if (copyTypeSymbol in copy) {
(copy as BasicCopy).kill = true;
killRecursive(copy as BasicCopy);
}
}
room.onLeave();
Expand Down
36 changes: 35 additions & 1 deletion src/ct.release/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Background} from './backgrounds';
import {Tilemap} from './tilemaps';
import roomsLib, {Room} from './rooms';
import {runBehaviors} from './behaviors';
import {copyTypeSymbol, stack} from '.';
import {copyTypeSymbol, stack, deadPool} from '.';
import uLib from './u';

import type * as pixiMod from 'pixi.js';
Expand Down Expand Up @@ -424,6 +424,40 @@ export const makeCopy = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
};

export const killRecursive = (copy: (BasicCopy & pixiMod.DisplayObject) | Background) => {
copy.kill = true;
if (templatesLib.isCopy(copy) && (copy as BasicCopy).onDestroy) {
templatesLib.onDestroy.apply(copy);
(copy as BasicCopy).onDestroy.apply(copy);
}
if (copy.children) {
for (const child of copy.children) {
if (templatesLib.isCopy(child)) {
killRecursive(child as (BasicCopy & pixiMod.DisplayObject)); // bruh
}
}
}
const stackIndex = stack.indexOf(copy);
if (stackIndex !== -1) {
stack.splice(stackIndex, 1);
}
if (templatesLib.isCopy(copy) && (copy as BasicCopy).template) {
if ((copy as BasicCopy).template) {
const {template} = (copy as BasicCopy);
if (template) {
const templatelistIndex = templatesLib
.list[template]
.indexOf((copy as BasicCopy));
if (templatelistIndex !== -1) {
templatesLib.list[template]
.splice(templatelistIndex, 1);
}
}
}
}
deadPool.push(copy);
};

const onCreateModifier = function () {
/*!%oncreate%*/
};
Expand Down

0 comments on commit 04acd47

Please sign in to comment.