Skip to content

Commit

Permalink
✨ ct.res.groups and group field in rooms and templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmoMyzrailGorynych committed Oct 3, 2022
1 parent daed484 commit 8d6c1a3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/data/ct.release/res.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
sounds: {},
textures: {},
skeletons: {},
groups: [/*@groups@*/][0],
/**
* Loads and executes a script by its URL
* @param {string} url The URL of the script file, with its extension.
Expand Down
29 changes: 29 additions & 0 deletions src/node_requires/exporter/groups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const flattenGroups = (project: IProject): Record<string, Record<string, string>> => {
const out: Record<string, Record<string, string>> = {};
let thing: keyof typeof project.groups;
for (thing in project.groups) {
out[thing] = {
'-1': 'ungrouped'
};
for (const group of project.groups[thing]) {
out[thing][group.uid] = group.name;
}
}
return out;
};
export const getGroups = (project: IProject): Record<string, Record<string, string[]>> => {
const out: Record<string, Record<string, string[]>> = {};
let thing: keyof typeof project.groups;
const flattened = flattenGroups(project);
for (thing in project.groups) {
out[thing] = {};
for (const groupId in flattened[thing]) {
out[thing][flattened[thing][groupId]] = [];
}
for (const asset of project[thing]) {
const groupName = asset.group ? flattened[thing][asset.group] : 'ungrouped';
out[thing][groupName].push(('name' in asset) ? asset.name : asset.typefaceName);
}
}
return out;
};
4 changes: 3 additions & 1 deletion src/node_requires/exporter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {stringifyContent} = require('./content');
import {bundleFonts, bakeBitmapFonts} from './fonts';
const {bakeFavicons} = require('./icons');
const {getUnwrappedExtends, getCleanKey} = require('./utils');
import {getGroups} from './groups';

const ifMatcher = (varName: string, symbol = '@') => new RegExp(`/\\* ?if +${symbol}${varName}${symbol} ?\\*/([\\s\\S]*)(?:/\\* ?else +${symbol}${varName}${symbol} ?\\*/([\\s\\S]*?))?/\\* ?endif +${symbol}${varName}${symbol} ?\\*/`, 'g');
const varMatcher = (varName: string, symbol = '@') => new RegExp(`/\\* ?${symbol}${varName}${symbol} ?\\*/`, 'g');
Expand Down Expand Up @@ -363,7 +364,8 @@ const exportCtProject = async (
tiledImages,
bitmapFonts,
dbSkeletons: skeletons.skeletonsDB,
sounds
sounds,
groups: JSON.stringify(getGroups(project))
}, injections);
buffer += '\n';

Expand Down
3 changes: 3 additions & 0 deletions src/node_requires/exporter/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const glob = require('./../glob');
const {getUnwrappedExtends} = require('./utils');
import {getBaseScripts} from './scriptableProcessor';
import {getTextureFromId} from '../resources/textures';
import {flattenGroups} from './groups';

const getStartingRoom = (proj: IProject): IRoom => {
let [startroom] = proj.rooms; // picks the first room by default
Expand Down Expand Up @@ -70,6 +71,7 @@ type ExportedBg = {

// eslint-disable-next-line max-lines-per-function
const stringifyRooms = (proj: IProject): IScriptablesFragment => {
const groups = flattenGroups(proj).rooms;
let roomsCode = '';
let rootRoomOnCreate = '';
let rootRoomOnStep = '';
Expand Down Expand Up @@ -142,6 +144,7 @@ const stringifyRooms = (proj: IProject): IScriptablesFragment => {
roomsCode += `
ct.rooms.templates['${r.name}'] = {
name: '${r.name}',
group: '${groups[r.group ? r.group.replace(/'/g, '\\\'') : -1]}',
width: ${r.width},
height: ${r.height},` +
/* JSON.parse is faster at loading big objects */`
Expand Down
5 changes: 4 additions & 1 deletion src/node_requires/exporter/templates.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const {getTextureFromId} = require('../resources/textures');
const {getUnwrappedExtends} = require('./utils');

import {flattenGroups} from './groups';

import {getBaseScripts} from './scriptableProcessor';

interface IBlankTexture {
Expand All @@ -25,7 +27,7 @@ const getTextureInfo = (blankTextures: IBlankTexture[], template: ITemplate) =>
};

const stringifyTemplates = function (proj: IProject): IScriptablesFragment {
/* Stringify templates */
const groups = flattenGroups(proj).templates;
let templates = '';
let rootRoomOnCreate = '';
let rootRoomOnStep = '';
Expand All @@ -52,6 +54,7 @@ ct.templates.templates["${template.name}"] = {
animationFPS: ${template.animationFPS ?? 60},
playAnimationOnStart: ${Boolean(template.playAnimationOnStart)},
loopAnimation: ${Boolean(template.loopAnimation)},
group: "${groups[template.group ? template.group.replace(/'/g, '\\\'') : -1]}",
${textureInfo}
onStep: function () {
${scripts.thisOnStep}
Expand Down

0 comments on commit 8d6c1a3

Please sign in to comment.