Noah2610 / deathfloor

Work-in-progress Mega Man inspired game.
MIT License
2 stars 0 forks source link

Entity config conditional actions (`if`) #72

Closed Noah2610 closed 4 years ago

Noah2610 commented 4 years ago

If action

An If action triggers actions if a condition succeeds.

IfElse action

Same as If, but has a fallback action (else).

Condition

A "condition" returns true or false. Conditions can be logical comparisons like equal, larger-than, etc., which take expressions as arguments, or conditions can be operations like and, or, etc., which would take more conditions as arguments.

Expression

An expression returns a value (primarily numbers). Possible expressions could include expressions that return the entity's health, their position, velocity, lifecycle state, current variant, collision, etc. (and in the future maybe entity config variables, if I ever add those).

Noah2610 commented 4 years ago

Syntax design ideas:

events: {
    // Simple `if` conditions that add actions if condition passes.
    Init: If(
        if: True,
        then: Echo("True!"),
    ),
    Init: If(
        if: Not(False),
        then: Echo("Not false!"),
    ),

    OnInteract: PlayerAction(If(
        if: LessThan(Health, Num(10.0)),
        then: HealthAction(Gain(5.0)),
    )),

    // If condition with fallback `else` action.
    OnCollision(/* ... */): If(
        if: LargerThan(Velocity(X), 10.0)),
        then: MoveAction(AddVelocity(x: -10.0)),
        else: MoveAction(SetVelocity(x: 0.0)),
    ),

    /*
     * DESIGNS I DON'T LIKE AND THAT SHOULDN'T BE IMPLEMENTED.
     * (At least not exactly like this.)
     */

    // With `else_if` field.
    // I don't like this design.
    Interval(500): If(
        if: LargerThan(Velocity(X), 0.0),
        then: Echo("Moving RIGHT!"),
        else_if: [
            (
                if: LessThan(Velocity(X), 0.0),
                then: Echo("Moving LEFT!"),
            ),
            (
                if: Equal(Velocity(X), 0.0),
                then: Echo("Standing still."),
            ),
        ],
        else: Echo("Default, shouldn't get here."),
    ),

    // If/else chain as its own conditional action.
    // I still don't like this. Isn't obvious enough that
    // this is an if/else-if chain, and not just multiple ifs.
    // Default/fallback `else` also doesn't really work here.
    Interval(1000): IfElseChain([
        (
            if: LargerThan(Velocity(X), 0.0),
            then: Echo("Moving RIGHT!"),
        ),
        (
            if: LessThan(Velocity(X), 0.0),
            then: Echo("Moving LEFT!"),
        ),
        (
            if: Equal(Velocity(X), 0.0),
            then: Echo("Standing still."),
        ),
    ]),

    // Conditional switch action.
    // This is probably not needed for a while.
    Interval(750): Switch(
        value: Health,
        cases: [
            (0.0, Echo("No health, dead.")),
            (10.0, Echo("Full health, definitely not dead.")),
        ],
    ),
},
Noah2610 commented 4 years ago

Expressions:

Noah2610 commented 4 years ago

This is good enough and working, closing for now. I'll add more condition expressions later when we need them.