Skip to content

Commit

Permalink
Merge pull request #61 from NarraLeaf/dev_nomen
Browse files Browse the repository at this point in the history
narraleaf-react-0.2.3
  • Loading branch information
helloyork authored Dec 27, 2024
2 parents 019e00d + cbb0606 commit 70d5fb4
Show file tree
Hide file tree
Showing 57 changed files with 319 additions and 146 deletions.
19 changes: 18 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# Changelog

## [0.2.1]
## [0.2.3]

### _Feature_

- Use `usePreference` to manage preference easier

### Added

- `image.setTags`
- `image.setPosition`
- Utility component: `Full`
- `usePreference` hook

### Updated

- `Player` component now doesn't require a `story` prop

## [0.2.2] - 2024/12/02

### _Feature_

Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"husky": "^9.1.6",
"jest": "^27.0.6",
"postcss-loader": "^8.1.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"rimraf": "^6.0.1",
"style-loader": "^4.0.0",
"tailwindcss": "^3.4.11",
Expand All @@ -56,8 +56,9 @@
"webpack-dev-server": "^5.1.0"
},
"peerDependencies": {
"react": "^17.0.0 || ^18.0.0",
"react-dom": "^17.0.0 || ^18.0.0"
"@emotion/is-prop-valid": "*",
"react": "^18.0.0 || ^19.0.0",
"react-dom": "^18.0.0 || ^19.0.0"
},
"files": [
"dist"
Expand All @@ -78,5 +79,6 @@
"framer-motion": "^11.11.9",
"howler": "^2.2.4",
"prop-types": "^15.8.1"
}
},
"packageManager": "[email protected]+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447"
}
26 changes: 14 additions & 12 deletions src/game/nlcore/action/actions/controlAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ export class ControlAction<T extends typeof ControlActionTypes[keyof typeof Cont
}
}

public async executeSingleAction(state: GameState, action: LogicAction.Actions) {
public executeSingleAction(state: GameState, action: LogicAction.Actions): Promise<ContentNode | null> {
const next = state.game.getLiveGame().executeAction(state, action);
if (Awaitable.isAwaitable<CalledActionResult, CalledActionResult>(next)) {
const {node} = await new Promise<CalledActionResult>((r) => {
next.then((_) => r(next.result as any));
return new Promise<ContentNode | null>((r) => {
next.then((_) => {
state.logger.debug("Control - Next Action (single)", next);
r(next.result?.node || null);
});
});
return node;
} else {
return next;
return Promise.resolve(next?.contentNode || null);
}
}

Expand Down Expand Up @@ -96,14 +98,14 @@ export class ControlAction<T extends typeof ControlActionTypes[keyof typeof Cont
return awaitable;
} else if (this.type === ControlActionTypes.all) {
const awaitable = new Awaitable<CalledActionResult, CalledActionResult>(v => v);
(async () => {
await Promise.all(content.map(action => this.executeSingleAction(state, action)));
awaitable.resolve({
type: this.type,
node: this.contentNode.getChild()
Promise.all(content.map(action => this.executeSingleAction(state, action)))
.then(() => {
awaitable.resolve({
type: this.type,
node: this.contentNode.getChild()
});
state.stage.next();
});
state.stage.next();
})();
return awaitable;
} else if (this.type === ControlActionTypes.allAsync) {
(async () => {
Expand Down
6 changes: 6 additions & 0 deletions src/game/nlcore/common/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class RGBColor {
}
}

/**@internal */
export class Utils {
static RGBColor = RGBColor;

Expand Down Expand Up @@ -118,6 +119,7 @@ export class Utils {
}
}

/**@internal */
export class UseError<T = Record<string, any>> extends Error {
static isUseError(error: any): error is UseError {
return error instanceof UseError;
Expand All @@ -132,6 +134,7 @@ export class UseError<T = Record<string, any>> extends Error {
}
}

/**@internal */
export class StaticScriptWarning extends UseError<{
stack?: string;
info?: any;
Expand All @@ -150,6 +153,7 @@ type ImageState = {
usedExternalSrc: boolean;
};

/**@internal */
export class StaticChecker {
private readonly scene: Scene;

Expand Down Expand Up @@ -257,6 +261,7 @@ export class StaticChecker {
}
}

/**@internal */
export class RuntimeScriptError extends Error {
static toMessage(msg: string | string[], trace?: Action | Action[]) {
const messages: string[] = [];
Expand All @@ -282,6 +287,7 @@ export class RuntimeScriptError extends Error {
}
}

/**@internal */
export class RuntimeGameError extends Error {
constructor(message: string) {
super(message);
Expand Down
2 changes: 2 additions & 0 deletions src/game/nlcore/common/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const Image: ImageConstructor = function <T extends TagGroupDefinition | null>(
config.tag as TagDefinitions<T> | undefined
);
} as unknown as ImageConstructor;
const AbstractImage = ImageClass;

export {
Character,
Expand All @@ -78,4 +79,5 @@ export {
Text,
Pause,
Persistent,
AbstractImage,
};
2 changes: 2 additions & 0 deletions src/game/nlcore/elements/character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import {Sentence, SentencePrompt, SentenceUserConfig, SingleWord} from "@core/el
import {CharacterAction} from "@core/action/actions/characterAction";

export type CharacterConfig = {} & Color;
/**@internal */
export type CharacterStateData = {
name: string;
};
/**@internal */
export type CharacterState = {
name: string;
};
Expand Down
1 change: 1 addition & 0 deletions src/game/nlcore/elements/character/pause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type PauseConfig = {
duration?: number;
};

/**@internal */
export type PausingShortcut = typeof Pause;
export type Pausing = Pause | PausingShortcut;

Expand Down
8 changes: 8 additions & 0 deletions src/game/nlcore/elements/character/sentence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,37 @@ import {Color, Font} from "@core/types";
import type {ScriptCtx} from "@core/elements/script";
import {Pause, Pausing} from "@core/elements/character/pause";

/**@internal */
export type SentenceConfig = {
pause?: boolean | number;
voice: Sound | null;
character: Character | null;
voiceId: string | number | null;
} & Color & Font;

/**@internal */
export type SentenceDataRaw = {
state: SentenceState;
};
/**@internal */
export type SentenceState = {
display: boolean;
};
export type SentenceUserConfig = Partial<Omit<SentenceConfig, "voice"> & {
voice: Sound | string | null | undefined
}>;
/**@internal */
export type DynamicWord = (ctx: ScriptCtx) => DynamicWordResult;
/**@internal */
export type DynamicWordResult = string | Word | Pausing | (string | Word | Pausing)[];
/**@internal */
export type StaticWord<T extends string | DynamicWord | Pausing = string | DynamicWord | Pausing> =
string
| Pausing
| Word<T>;
/**@internal */
export type SingleWord = StaticWord | DynamicWord;
/**@internal */
export type SentencePrompt = SingleWord[] | SingleWord;

export class Sentence {
Expand Down
1 change: 1 addition & 0 deletions src/game/nlcore/elements/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class Lambda<T = any> {
}
}

/**@internal */
export type ConditionData = {
If: {
condition: Lambda | null;
Expand Down
1 change: 1 addition & 0 deletions src/game/nlcore/elements/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Chained, ChainedActions, Proxied} from "@core/action/chain";
import {ControlAction} from "@core/action/actions/controlAction";


/**@internal */
type ChainedControl = Proxied<Control, Chained<LogicAction.Actions>>;

export class Control extends Actionable {
Expand Down
2 changes: 2 additions & 0 deletions src/game/nlcore/elements/displayable/displayable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import {Chained, Proxied} from "@core/action/chain";
import {LogicAction} from "@core/action/logicAction";
import {ContentNode} from "@core/action/tree/actionTree";

/**@internal */
export type DisplayableEventTypes = {
"event:displayable.applyTransition": [ITransition];
"event:displayable.applyTransform": [Transform];
"event:displayable.init": [];
};

/**@internal */
export abstract class Displayable<
StateData extends Record<string, any>,
Self extends Actionable
Expand Down
61 changes: 45 additions & 16 deletions src/game/nlcore/elements/displayable/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {Control} from "@core/elements/control";
import {ImageAction} from "@core/action/actions/imageAction";
import {Displayable, DisplayableEventTypes} from "@core/elements/displayable/displayable";

/**@internal */
export type ImageConfig = {
display: boolean;
/**@internal */
Expand All @@ -42,10 +43,12 @@ export type ImageConfig = {
autoInit: boolean;
} & CommonDisplayable;

/**@internal */
export type ImageDataRaw = {
state: Record<string, any>;
};

/**@internal */
export type ImageEventTypes = {
"event:wearable.create": [Image];
} & DisplayableEventTypes;
Expand All @@ -54,7 +57,9 @@ export type TagDefinitions<T extends TagGroupDefinition | null> =
groups: T;
defaults: SelectElementFromEach<T>;
} : never;
/**@internal */
export type TagGroupDefinition = string[][];
/**@internal */
export type TagSrcResolver<T extends TagGroupDefinition> = (...tags: SelectElementFromEach<T>) => string;
export type RichImageUserConfig<T extends TagGroupDefinition | null> = ImageConfig & {
/**@internal */
Expand All @@ -70,8 +75,8 @@ export type RichImageUserConfig<T extends TagGroupDefinition | null> = ImageConf
tag: TagDefinitions<T>;
}
: never);
export type RichImageConfig<T extends TagGroupDefinition | null> = RichImageUserConfig<T> & {};
export type StaticRichConfig = RichImageUserConfig<TagGroupDefinition | null>;
type RichImageConfig<T extends TagGroupDefinition | null> = RichImageUserConfig<T> & {};
type StaticRichConfig = RichImageUserConfig<TagGroupDefinition | null>;


export class Image<
Expand Down Expand Up @@ -332,20 +337,16 @@ export class Image<
* @chainable
*/
public applyTransform(transform: Transform<TransformDefinitions.ImageTransformProps>): Proxied<Image, Chained<LogicAction.Actions>> {
return this.combineActions(new Control(), chain => {
const action = new ImageAction<typeof ImageAction.ActionTypes.applyTransform>(
chain,
ImageAction.ActionTypes.applyTransform,
new ContentNode().setContent([
void 0,
transform.copy(),
getCallStack()
])
);
return chain
.chain(action)
.chain(this._flush());
});
const chain = this.chain();
return chain.chain(new ImageAction<typeof ImageAction.ActionTypes.applyTransform>(
chain,
ImageAction.ActionTypes.applyTransform,
new ContentNode().setContent([
void 0,
transform.copy(),
getCallStack()
])
));
}

/**
Expand Down Expand Up @@ -426,6 +427,34 @@ export class Image<
});
}

/**
* Alia of {@link Image.setAppearance}
* @chainable
*/
public setTags(
tags: Tags extends TagGroupDefinition ? FlexibleTuple<SelectElementFromEach<Tags>> : string[],
transition?: IImageTransition
): Proxied<Image, Chained<LogicAction.Actions>> {
return this.setAppearance(tags, transition);
}

/**
* Set Image Position
* @chainable
*/
public setPosition(
position: TransformDefinitions.ImageTransformProps["position"],
duration?: number,
easing?: TransformDefinitions.EasingDefinition
): Proxied<Image, Chained<LogicAction.Actions>> {
return this.applyTransform(new Transform<TransformDefinitions.ImageTransformProps>({
position,
}, {
duration,
ease: easing,
}));
}

/**
* Add a wearable to the image
* @param children - Wearable image or images
Expand Down
3 changes: 2 additions & 1 deletion src/game/nlcore/elements/displayable/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ export type TextConfig = {
text: string;
} & CommonDisplayable;

/**@internal */
export type TextDataRaw = {
state: Record<string, any>;
};

/**@internal */
export type TextEventTypes = {
"event:text.show": [Transform];
"event:text.hide": [Transform];
Expand Down
2 changes: 2 additions & 0 deletions src/game/nlcore/elements/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export type MenuChoice = {
prompt: SentencePrompt | Sentence;
};

/**@internal */
type ChainedAction = Proxied<GameElement, Chained<LogicAction.Actions>>;
/**@internal */
type ChainedActions = (ChainedAction | ChainedAction[] | Actions | Actions[])[];

export type Choice = {
Expand Down
Loading

0 comments on commit 70d5fb4

Please sign in to comment.