简介

libs/ecs 这是一个 Typescript 语言版的Entity-Component-System框架架。

使用说明

创建实体

ecs.getEntity<ecs.Entity>(ecs.Entity);

组件

自定义组件必须继承ecs.Comp,并且需要使用ecs.register注册组件

@ecs.register('Hello')
export class HelloComponent extends ecs.Comp {
    info: string;
    data: number;

    // 组件被回收前会调用这个方法reset() {
        this.info = '';
        this.data = 0;
    }
}

ecs.register功能

实体

为了能利用Typescript类型提示机制,在使用实体时候需要用户自己继承ecs.Entity。

ecs.register('HelloEntity')
export class HelloEntity extends ecs.Entity {
    Hello: HelloComponent; // 这里的Hello要和ecs.register中填入的参数一致
}
// 添加实体
entity.addChild(ecs.Entity);

// 移除实体
entity.removeChild(ecs.Entity);
entity.add(HelloComponent); // 添加组件时会优先从组件缓存池中获取无用的组件对象,如果没有才会新创建一个组件对象
let compObj = new HelloComponent();
entity.add(compObj)
entity.remove(HelloComponent); // 组件对象会从实体身上移除放入组件缓存池中
entity.remove(HelloComponent, false)
  • 获得组件对象
entity.Hello; // 见上方自定义实体操作

entity.get(HelloComponent);
entity.has(HelloComponent);

!!entity.Hello;
entity.destroy() // 销毁实体时会先删除实体身上的所有组件,然后将实体放入实体缓存池中

实体筛选

目前提供了四种类型筛选能力,但是这四种筛选能力可以组合从而提供更强大的筛选功能

使用方式

ecs.allOf(AComponent, BComponent, CComponent);
ecs.anyOf(AComponent, BComponent);
  • 表示拥有某些组件,并且不包含某些组件
// 不包含CComponent或者DComponent
ecs.allOf(AComponent, BComponent).excludeOf(CComponent, DComponent);

// 不同时包含CComponent和DComponent
ecs.allOf(AComponent, BComponent).excludeOf(CComponent).excludeOf(DComponent);

直接查询并获得实体

ecs.query(ecs.allOf(Comp1, Comp2))

系统

怎么使用

1、声明组件

@ecs.register('Node')
export class NodeComponent extends ecs.Comp {
    val: cc.Node = null;

    reset() {
        this.val = null;
    }
}

@ecs.reigster('Move')
export class MoveComponent extends ecs.Comp {
    heading: cc.Vec2 = cc.v2();
    speed: number = 0;

    reset() {
        this.heading.x = 0;
        this.heading.y = 0;
        this.speed = 0;
    }
}

@ecs.register('Transform')
export class TransformComponent extends ecs.Comp {
    position: cc.Vec2 = cc.v2();
    angle: number;
    reset() {
    
    }
}

export class AvatarEntity extends ecs.Entity {
    Node: NodeComponent;
    Move: MoveComponent;
    Transform: TransformComponent;
}

2、创建系统

export class RoomSystem extends ecs.RootSystem {
    constructor() {
        super();
        this.add(new MoveSystem());
        this.add(new RenderSystem());
    }
}

export class MoveSystem extends ecs.ComblockSystem<AvatarEntity&gt; implements ecs.IEntityEnterSystem, ecs.ISystemUpdate {
    init() {
    
    }

    filter(): ecs.IMatcher {
        return ecs.allOf(MoveComponent, TransformComponent);
    }

     // 实体第一次进入MoveSystem会进入此方法
    entityEnter(e: AvatarEntity) {
        e.Move.speed = 100;
    }
    
    // 每帧都会更新
    update(e: AvatarEntity) {
        let moveComp = e.Move;                      // e.get(MoveComponent);
        lel position = e.Transform.position;
        
        position.x += moveComp.heading.x * moveComp.speed * this.dt;
        position.y += moveComp.heading.y * moveComp.speed * this.dt;
        
        e.Transform.angle = cc.misc.lerp(e.Transform.angle, Math.atan2(moveComp.speed.y, moveComp.speed.x) * cc.macro.DEG, dt);
    }
}

export class RenderSystem extends ecs.ComblockSystem<AvatarEntity> implements ecs.IEntityEnterSystem, ecs.IEntityRemoveSystem, ecs.ISystemUpdate {
    filter(): ecs.IMatcher {
        return ecs.allOf(NodeComponent, TransformComponent);
    }
    
    // 实体第一次进入MoveSystem会进入此方法
    entityEnter(e: AvatarEntity) {
        e.Node.val.active = true;
    }
    
    entityRemove(e: AvatarEntity) {
       
    }
    
    update(e: AvatarEntity) {
        e.Node.val.setPosition(e.Transform.position);
        e.Node.val.angle = e.Transform.angle;
    }
}

3、驱动ecs框架

const { ccclass, property } = cc._decorator;
@ccclass
export class GameControllerBehaviour extends Component {
    rootSystem: RootSystem = null;

    onLoad() {
        this.rootSystem = new RootSystem();
        this.rootSystem.init();
    }
    
    createAvatar(node: cc.Node) {
        let entity = ecs.createEntityWithComps<AvatarEntity>(NodeComponent, TransformComponent, MoveComponent);
        entity.Node.val = node;
    }

    update(dt: number) {
        this.rootSystem.execute(dt);
    }
}

和Cocos Creator的组件混合使用

创建基类

import { Component, _decorator } from "cc";
import { ecs } from "../../../Libs/ECS";
const { ccclass, property } = _decorator;

@ccclass('CCComp')
export abstract class CCComp extends Component implements ecs.IComp {
    static tid: number = -1;
    static compName: string;

    canRecycle: boolean;
    ent: ecs.Entity;

    onLoad() {
        this.ent = ecs.createEntity();
        this.ent.add(this);    
    }

    abstract reset(): void;
}

创建ecs组件并且赋予序列化功能,这样就能在Cocos Creator的“属性检查器”上修改参数

import { _decorator, toDegree, v3, Node, Vec3 } from "cc";
import { ecs } from "../../../Libs/ECS";
const { ccclass, property } = _decorator;

let outV3 = v3();
@ccclass('MovementComponent')
@ecs.register('Movement')
export class MovementComponent extends CCComp {
    pos: Vec3 = v3();
    angle: number = 0;
    speed: number = 0;

    @property
    acceleration: number = 0;

    @property
    private _maxSpeed: number = 0;
    @property
    set maxSpeed(val: number) {
        this._maxSpeed = val;
    }
    get maxSpeed() {
        return this._maxSpeed;
    }

    @property
    heading: Vec3 = v3();
    
    @property
    targetHeading: Vec3 = v3();

    reset() {

    }

    update(dt: number) {
        if(!Vec3.equals(this.heading, this.targetHeading, 0.01)) {
            Vec3.subtract(outV3, this.targetHeading, this.heading);
            outV3.multiplyScalar(0.025);
            this.heading.add(outV3);
            this.heading.normalize();
            this.angle = toDegree(Math.atan2(this.heading.y, this.heading.x)) - 90;
        }
        
        this.speed = Math.min(this.speed + this.acceleration * dt, this._maxSpeed);

        this.pos.add3f(this.heading.x * this.speed * dt, this.heading.y * this.speed * dt, 0);
    }

    calcAngle() {
        this.angle = toDegree(Math.atan2(this.heading.y, this.heading.x)) - 90;
        return this.angle;
    }
}

创建面向Cocos Creator的组件

import { Component, _decorator } from "cc";
const { ccclass, property } = _decorator;
@ccclass('Player')
@ecs.register('Player', false)
export class Player extends CCComp {
    @property({
        type: MovementComponent
    })
    movement: MovementComponent;

    onLoad() {
        super.onLoad();

        // 添加MovementComponent组件对象
        this.ent.add(this.movement);
    }
}

调试

添加如下代码

windows['ecs'] = ecs;

原文地址:https://blog.csdn.net/weixin_39324642/article/details/127248441

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_49774.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注