Noah2610 / deathfloor

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

Entity config variables #85

Closed Noah2610 closed 3 years ago

Noah2610 commented 3 years ago

Add gettable and settable variables / state to entity config.

Example config:

(
    // Initialize variables.
    // Every variable must be initialized before it can be used.
    variables: {
        "is_shooting": false,
        "health": 0,
        "some_future_value": null,
        "name": "MyEntity",

        // It would be cool if we could initialize values
        // with a value from the object's props.
        // This should error if the object didn't receive that prop.
        "prop_z": Prop("z"),
    },

    // Variables can then be accessed in event/actions.
    // Can be read to use in `If` expressions.
    // Can NOT (yet) be used in action fields.
    events: {
        // Get a variable value with `GetVar` expression.
        Interval(500): If((
            if: Equal(
                GetVar("is_shooting"),
                true,
            ),
            then: Echo("Is shooting!"),
        )),

        // Set a variable value with `SetVar` action.
        // The value to set is an expression.
        OnKeyDown(PlayerShoot): SetVar("is_shooting", true),
        OnKeyUp(PlayerShoot): SetVar("is_shooting", false),

        // Update the "health" variable with the `Health` expression.
        Interval(100): SetVar("health", Health()),
    },
)
Noah2610 commented 3 years ago

Working entity config variables on master.

Example config:

(
    components: [
        // This component has to be given before variables can be used.
        VariableRegister({
            // Can optionally set default variables here.
            // If not set, all variables will default to `Null`,
            // when used the first time.
            "my_variable": Null,
            "my_number": 4.20,
            "some_boolean": false,
        }),
    ],

    events: {
        // `If`, `Echo`, and some other actions now take a new `Expression` value.
        // I still need to document this properly.
        //
        // The `Var(variable_name)` expression returns the given variable value.
        // This can be used here to pass the expression value of the variable to
        // the `Echo` action to print that value to the terminal.
        Init: Echo(Var("my_number")),

        Interval(1000): Group([
            Echo(Var("some_boolean")),

            // This new `VariableAction` action currently only has the `Set` action.
            // It takes two arguments, the variable name as a string and the value
            // to set the variable to as an expression.
            VariableAction(Set(
                "some_boolean",
                // Expressions are powerful.
                // They can be literal values (strings, numbers, booleans, and null),
                // they can be operations (`Not`, `And`, `Or`, or mathematical operations like `Add`, `Sub`, etc.),
                // or they can evaluate to a value from an entity's component or variable (`Var`, `Health`, `Velocity(Axis)`, etc.).
                Not(Var("some_boolean")),
            )),
        ]),
    },
)

See documentation comments and available syntax for expressions: https://github.com/Noah2610/deathfloor/blob/c266806818403a942498514c03546a663e9867f9/src/expression/mod.rs#L1 https://github.com/Noah2610/deathfloor/blob/c266806818403a942498514c03546a663e9867f9/src/expression/value.rs#L4 https://github.com/Noah2610/deathfloor/blob/c266806818403a942498514c03546a663e9867f9/src/expression/operation.rs#L5 https://github.com/Noah2610/deathfloor/blob/c266806818403a942498514c03546a663e9867f9/src/expression/component_value.rs#L8