Skip to content

Commit

Permalink
update: better signatures for Condition
Browse files Browse the repository at this point in the history
  • Loading branch information
helloyork committed Nov 29, 2024
1 parent 6160dd3 commit f4b1d2b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 60 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- Use displayable actions to reorder layers
- Better image preloading
- Scene config inheritance
- Use scene name to jump between two cross-referenced scenes
- Use the scene name to jump between two cross-referenced scenes

### _Incompatible Changes_

Expand All @@ -24,7 +24,8 @@
- Disable image auto initialize using image.config
- Quick image preloading only preloads images when needed
- Use `scene.inherit` to inherit scene config
- Use scene name to jump between two cross-referenced scenes
- Use the scene name to jump between two cross-referenced scenes
- Better signatures for `Condition`

### Updated

Expand Down
7 changes: 6 additions & 1 deletion src/game/nlcore/action/actionable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export class Actionable<
return null;
}

/**@internal */
/**
* @internal
* override this method can override the default behavior of chaining
*
* When converting a chain to actions, this method is called to convert the chain to actions
*/
public fromChained(chained: Proxied<GameElement, Chained<LogicAction.Actions>>): LogicAction.Actions[] {
return chained.getActions();
}
Expand Down
85 changes: 28 additions & 57 deletions src/game/nlcore/elements/condition.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import {deepMerge} from "@lib/util/data";
import {ContentNode, RenderableNode} from "@core/action/tree/actionTree";
import {LogicAction} from "@core/action/logicAction";
import {Actionable} from "@core/action/actionable";
import {GameState} from "@player/gameState";
import {Chained, ChainedActions, Proxied} from "@core/action/chain";
import {ScriptCtx} from "@core/elements/script";
import {StaticScriptWarning} from "@core/common/Utils";
import Actions = LogicAction.Actions;
import {ConditionAction} from "@core/action/actions/conditionAction";
import Actions = LogicAction.Actions;

/* eslint-disable @typescript-eslint/no-empty-object-type */
export type ConditionConfig = {};

interface LambdaCtx extends ScriptCtx {
}

type LambdaCtx = ScriptCtx;
type LambdaHandler<T = any> = (ctx: LambdaCtx) => T;

export class Lambda {
public static isLambda(value: any): value is Lambda {
return value instanceof Lambda && "handler" in value;
}

handler: LambdaHandler;

constructor(handler: LambdaHandler) {
Expand Down Expand Up @@ -57,10 +55,7 @@ export type ConditionData = {
}
};

export class Condition extends Actionable {
/**@internal */
static defaultConfig: ConditionConfig = {};

export class Condition<Closed extends true | false = false> extends Actionable {
/**@internal */
static getInitialState(): ConditionData {
return {
Expand All @@ -75,14 +70,15 @@ export class Condition extends Actionable {
};
}

/**
* @chainable
*/
public static If(
condition: Lambda | LambdaHandler<boolean>, action: ChainedActions
): Proxied<Condition, Chained<LogicAction.Actions>> {
return new Condition().If(condition, action);
return new Condition().createIfCondition(condition, action);
}

/**@internal */
readonly config: ConditionConfig;
/**@internal */
conditions: ConditionData = {
If: {
Expand All @@ -95,54 +91,25 @@ export class Condition extends Actionable {
}
};

constructor(config: ConditionConfig = {}) {
/**@internal */
private constructor() {
super();
this.config = deepMerge<ConditionConfig>(Condition.defaultConfig, config);
}

/**
* @chainable
*/
public If(
condition: Lambda | LambdaHandler<boolean>, action: ChainedActions
): Proxied<Condition, Chained<LogicAction.Actions>> {
// when IF condition already set
if (this.conditions.If.condition) {
throw new StaticScriptWarning("IF condition already set\nYou are trying to set multiple IF conditions for the same condition");
}

// when ELSE-IF condition already set
if (this.conditions.ElseIf.length) {
throw new StaticScriptWarning("ELSE-IF condition already set\nYou are trying to set an IF condition after an ELSE-IF condition");
}

// when ELSE condition already set
if (this.conditions.Else.action) {
throw new StaticScriptWarning("ELSE condition already set\nYou are trying to set an IF condition after an ELSE condition");
}
this.conditions.If.condition = condition instanceof Lambda ? condition : new Lambda(condition);
this.conditions.If.action = this.construct(Array.isArray(action) ? action : [action]);
return this.chain();
}

/**
* @chainable
*/
public ElseIf(
condition: Lambda | LambdaHandler<boolean>, action: ChainedActions
): Proxied<Condition, Chained<LogicAction.Actions>> {
// when there is no IF condition
if (!this.conditions.If.condition) {
throw new StaticScriptWarning("IF condition not set\nYou are trying to set an ELSE-IF condition without an IF condition");
}

condition: Closed extends false ? (Lambda | LambdaHandler<boolean>) : never,
action: Closed extends false ? ChainedActions : never
): Closed extends false ? Proxied<Condition, Chained<LogicAction.Actions>> : never {
// when ELSE condition already set
if (this.conditions.Else.action) {
throw new StaticScriptWarning("ELSE condition already set\nYou are trying to set an ELSE-IF condition after an ELSE condition");
}

this.conditions.ElseIf.push({
condition: condition instanceof Lambda ? condition : new Lambda(condition),
condition: Lambda.isLambda(condition) ? condition : new Lambda(condition),
action: this.construct(Array.isArray(action) ? action : [action])
});
return this.chain();
Expand All @@ -152,13 +119,8 @@ export class Condition extends Actionable {
* @chainable
*/
public Else(
action: ChainedActions
): Proxied<Condition, Chained<LogicAction.Actions>> {
// when there is no IF condition
if (!this.conditions.If.condition) {
throw new StaticScriptWarning("IF condition not set\nYou are trying to set an ELSE condition without an IF condition");
}

action: Closed extends false ? ChainedActions : never
): Closed extends false ? Proxied<Condition<true>, Chained<LogicAction.Actions>> : never {
// when ELSE condition already set
if (this.conditions.Else.action) {
throw new StaticScriptWarning("ELSE condition already set\nYou are trying to set multiple ELSE conditions for the same condition");
Expand Down Expand Up @@ -225,4 +187,13 @@ export class Condition extends Actionable {
(this.conditions.Else.action?.[0] || [])
]);
}

/**@internal */
private createIfCondition(
condition: Lambda | LambdaHandler<boolean>, action: ChainedActions
): Proxied<Condition, Chained<LogicAction.Actions>> {
this.conditions.If.condition = condition instanceof Lambda ? condition : new Lambda(condition);
this.conditions.If.action = this.construct(Array.isArray(action) ? action : [action]);
return this.chain();
}
}

0 comments on commit f4b1d2b

Please sign in to comment.