Noah2610 / deathfloor

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

Entity config inheritance chain #75

Closed Noah2610 closed 4 years ago

Noah2610 commented 4 years ago

Implement a way for an entity config to inherit other entity configs.

Example

Abstract Walker entity config

Can walk left (variant "WalkLeft") and right (variant "WalkRight"). Turns around when hitting a wall or detecting a ledge. This entity should be considered "abstract", which means it shouldn't be used as a standalone entity, instead this entity config should only used by inheriting it in other entity configs.

(
    components: (
        ledge_detector: (
            corners: [
                (
                    corner:      BottomLeft,
                    if_touching: Bottom,
                    size: (1.0, 1.0),
                    offset: (-4.0, 0.0),
                ),
                (
                    corner:      BottomRight,
                    if_touching: Bottom,
                    size: (1.0, 1.0),
                    offset: (-4.0, 0.0),
                ),
            ],
            collides_with: ["Solid"],
        ),
    ),

    variants: {
        "WalkLeft": (
            components: (
                walker: (
                    x: -500.0,
                ),
            ),
            events: {
                OnLedgeDetect(BottomLeft, Bottom):
                    EntityAction(SwitchVariant("WalkRight")),
                OnCollision(And([
                        IsTag("Solid"),
                        IsState(Enter),
                        IsSide(Left),
                    ])): EntityAction(SwitchVariant("WalkRight")),
            },
        ),
        "WalkRight": (
            components: (
                walker: (
                    x: 500.0,
                ),
            ),
            events: {
                OnLedgeDetect(BottomRight, Bottom):
                    EntityAction(SwitchVariant("WalkLeft")),
                OnCollision(And([
                        IsTag("Solid"),
                        IsState(Enter),
                        IsSide(Right),
                    ])): EntityAction(SwitchVariant("WalkLeft")),
            },
        ),
    },
)

Example Jumper entity config

Inherits Walker and jumps in regular intervals.

(
    // Inherit abstract `Walker` entity config.
    inherits: ["Walker"],

    events: {
        Interval(2000): MoveAction(Jump(y: 500.0)),
    },
)
Noah2610 commented 4 years ago

See technical implementation details in this comment (should add this to the docs): https://github.com/Noah2610/deathfloor/commit/ef175d8d65b139ed6a05664848f0f3c66149dcfc#diff-91cf08512e6aa443c1cd1e13bcbe6dd3R23-R56

Noah2610 commented 4 years ago

Working, closing for now. Docs could use some love tho.