Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: strict .use() #579

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Replaced the Separating Axis Theorem (SAT) with the "Gilbert–Johnson–Keerthi"
(`GJK`) distance algorithm.
- Changed default behaviour of `kaplay({ tagsAsComponents: false })` to `false`.
- Now if you pass a nullish value to `.use()` it throws an error

## [3001.0.7] - TBD

### Changed

- Now you cannot pass parameters that are not a component or string to `.use()`.
Otherwise it will throw an error

## [3001.0.6] "Santa Events" - 2024-12-27

Expand Down
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
31 changes: 18 additions & 13 deletions src/game/make.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ export function make<T>(comps: CompList<T> = []): GameObj<T> {
_parent.children.splice(index, 1);
}
_parent = p;
if (p)
if (p) {
p.children.push(this as GameObj);
}
},

setParent(p: GameObj, opt: SetParentOpt) {
Expand Down Expand Up @@ -248,10 +249,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 @@ -261,14 +267,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 @@ -302,15 +307,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 @@ -322,9 +327,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 @@ -350,7 +350,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,
}),
]);
Loading