Skip to content

Commit

Permalink
feat: strict .use() (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel committed Jan 15, 2025
1 parent 79bb53c commit 2bad279
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 73 deletions.
20 changes: 10 additions & 10 deletions src/assets/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export * from "./aseprite";
export * from "./asset";
export * from "./bitmapFont";
export * from "./font";
export * from "./pedit";
export * from "./shader";
export * from "./sound";
export * from "./sprite";
export * from "./spriteAtlas";
export * from "./utils";
export * from './aseprite';
export * from './asset';
export * from './bitmapFont';
export * from './font';
export * from './pedit';
export * from './shader';
export * from './sound';
export * from './sprite';
export * from './spriteAtlas';
export * from './utils';
30 changes: 15 additions & 15 deletions src/components/draw/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export * from "./circle";
export * from "./color";
export * from "./drawon";
export * from "./fadeIn";
export * from "./mask";
export * from "./opacity";
export * from "./outline";
export * from "./particles";
export * from "./polygon";
export * from "./raycast";
export * from "./rect";
export * from "./shader";
export * from "./sprite";
export * from "./text";
export * from "./uvquad";
export * from './circle';
export * from './color';
export * from './drawon';
export * from './fadeIn';
export * from './mask';
export * from './opacity';
export * from './outline';
export * from './particles';
export * from './polygon';
export * from './raycast';
export * from './rect';
export * from './shader';
export * from './sprite';
export * from './text';
export * from './uvquad';
2 changes: 1 addition & 1 deletion src/components/draw/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export function text(t: string, opt: TextCompOpt = {}): TextComp {
// TODO: shouldn't run when object / ancestor is paused
transform: obj.textTransform,
styles: obj.textStyles,
indentAll: opt.indentAll
indentAll: opt.indentAll,
}));

if (!opt.width) {
Expand Down
18 changes: 9 additions & 9 deletions src/components/misc/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export * from "./animate";
export * from "./boom";
export * from "./health";
export * from "./lifespan";
export * from "./named";
export * from "./state";
export * from "./stay";
export * from "./textInput";
export * from "./timer";
export * from './animate';
export * from './boom';
export * from './health';
export * from './lifespan';
export * from './named';
export * from './state';
export * from './stay';
export * from './textInput';
export * from './timer';
20 changes: 10 additions & 10 deletions src/components/transform/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export * from "./anchor";
export * from "./fixed";
export * from "./follow";
export * from "./layer";
export * from "./move";
export * from "./offscreen";
export * from "./pos";
export * from "./rotate";
export * from "./scale";
export * from "./z";
export * from './anchor';
export * from './fixed';
export * from './follow';
export * from './layer';
export * from './move';
export * from './offscreen';
export * from './pos';
export * from './rotate';
export * from './scale';
export * from './z';
28 changes: 16 additions & 12 deletions src/game/make.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,15 @@ export function make<T>(comps: CompList<T> = []): GameObj<T> {

// use a comp
use(this: GameObj, comp: Comp) {
// tag
if (typeof comp === "string") {
if (typeof comp == "string") {
// for use add(["tag"])
return tags.add(comp);
}
else if (!comp || typeof comp != "object") {
throw new Error(
`You can only pass a component or a string to .use(), you passed a "${typeof comp}"`,
);
}

let gc = [];

Expand All @@ -209,14 +214,13 @@ export function make<T>(comps: CompList<T> = []): GameObj<T> {
cleanups[comp.id] = [];
gc = cleanups[comp.id];
compStates.set(comp.id, comp);
// supporting tagsAsComponents
if (treatTagsAsComponents) tags.add(comp.id);
}
else {
anonymousCompStates.push(comp);
}

// add component id as tag
if (treatTagsAsComponents) tags.add(comp.id!);

for (const k in comp) {
if (COMP_DESC.has(k)) {
continue;
Expand Down Expand Up @@ -250,15 +254,15 @@ export function make<T>(comps: CompList<T> = []): GameObj<T> {
comp[k]?.();
onCurCompCleanup = null;
}
: comp[<keyof typeof comp> k];
gc.push(this.on(k, <any> func).cancel);
: comp[<keyof typeof comp>k];
gc.push(this.on(k, <any>func).cancel);
}
else {
if (this[k] === undefined) {
// assign comp fields to game obj
Object.defineProperty(this, k, {
get: () => comp[<keyof typeof comp> k],
set: (val) => comp[<keyof typeof comp> k] = val,
get: () => comp[<keyof typeof comp>k],
set: (val) => comp[<keyof typeof comp>k] = val,
configurable: true,
enumerable: true,
});
Expand All @@ -270,9 +274,9 @@ export function make<T>(comps: CompList<T> = []): GameObj<T> {
)?.id;
throw new Error(
`Duplicate component property: "${k}" while adding component "${comp.id}"`
+ (originalCompId
? ` (originally added by "${originalCompId}")`
: ""),
+ (originalCompId
? ` (originally added by "${originalCompId}")`
: ""),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/gfx/draw/drawText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export type DrawTextOpt = RenderProps & {
* If true, any (whitespace) indent on the first line of the paragraph
* will be copied to all of the lines for those parts that text-wrap.
*/
indentAll?: boolean
indentAll?: boolean;
};

/**
Expand Down Expand Up @@ -120,7 +120,7 @@ export interface CharTransform {
* will override, rather than compose with, the default styles given in {@link DrawTextOpt.transform} and by other
* components' styles.
*/
override?: boolean
override?: boolean;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/gfx/formatText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ export function formatText(opt: DrawTextOpt): FormattedText {
lastSpace = curLine.length;
lastSpaceWidth = curX;
}
if (opt.indentAll && paraIndentX === undefined && /\S/.test(ch)) {
if (
opt.indentAll && paraIndentX === undefined && /\S/.test(ch)
) {
paraIndentX = curX;
}

Expand Down
22 changes: 11 additions & 11 deletions tests/playtests/styleOverride.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ add([
},
styles: {
foo: {
color: RED
}
}
})
color: RED,
},
},
}),
]);

add([
Expand All @@ -23,10 +23,10 @@ add([
styles: {
foo: {
color: RED,
override: true
}
}
})
override: true,
},
},
}),
]);

add([
Expand All @@ -35,9 +35,9 @@ add([
styles: {
foo: {
color: RED,
override: true
}
}
override: true,
},
},
}),
color(WHITE.darken(200)),
]);
4 changes: 2 additions & 2 deletions tests/playtests/textwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const theText = `MAN PAGE
Very long description paragraph. Very long description paragraph. Very long description paragraph. Very long description paragraph. Very long description paragraph. Very long description paragraph. Very long description paragraph.
ANOTHER SECTION
Yet some more text here. Yet some more text here. Yet some more text here. Yet some more text here.`
Yet some more text here. Yet some more text here. Yet some more text here. Yet some more text here.`;

add([
pos(100, 100),
Expand All @@ -19,6 +19,6 @@ add([
text(theText, {
size: 16,
width: 17 * 16,
indentAll: true
indentAll: true,
}),
]);

0 comments on commit 2bad279

Please sign in to comment.