GithubHelp home page GithubHelp logo

“能力”组件 about ecs HOT 2 CLOSED

shangdibaozi avatar shangdibaozi commented on September 17, 2024
“能力”组件

from ecs.

Comments (2)

shangdibaozi avatar shangdibaozi commented on September 17, 2024

仔细想想也不需要由ecs插件来提供这种功能。完全由用户自己来做这不麻烦。
用户自己定义一个“能力”组件添加到实体身上,或者写个系统来动态给实体添加“能力”组件。

from ecs.

shangdibaozi avatar shangdibaozi commented on September 17, 2024
@ecs.register('Node ')
class NodeComponent extends ecs.IComponent {
    val: Node;
    reset() {
        this.val= null;
    }
}

@ecs.register('Movement')
class MovementComponent extends ecs.IComponent {
    speed: number = 1;
    heading: Vec2 = v2(0, 0);
    pos: Vec2 = v2(0, 0);
    
    move(dt: number) {
        this.pos.x += this.speed * this.heading.x;
        this.pos.y += this.speed * this.heading.y;
    }

    reset() {
    }
}

@ecs.register('Render')
class RenderComponent extends ecs.IComponent {
    show(dt: number) {
        let node = this.ent.Node.val;
        let moveComp = this.ent.Move;
        moveComp.move(dt);
        node.setPosition(moveComp.pos);
    }
    reset() {
    }
}

class AddRenderComponentSys extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.Matcher {
        return ecs.allOf(NodeComponent, MovementComponent).excludeOf(RenderComponent);
    }

    entityEnter(entities: ecs.Entity[]) {
        entities.forEach(e => e.add(RenderComponent));
    }
}

class RemoveRenderComponentSys extends ecs.ComblockSystem implements ecs.IEntityRemoveSystem {
    filter(): ecs.Matcher {
        return ecs.allOf(NodeComponent, MovementComponent, RenderComponent);
    }

    entityRemove(entities: ecs.Entity[]) {
        entities.forEach(e => e.remove(RenderComponent));
    }
}

from ecs.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.