1 Star 2 Fork 0

如此良人何 / ECS For Lua

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

ECS For Lua

Lua版轻量级ECS框架

输入图片说明

1、初始化 Context

Context:Init()

2、使用匹配器Matcher和过滤组Group

(Matcher)匹配器:用来定义一个实体匹配规则

比如:

  实体拥有Move组件:NewMatcher():AllOf(EMatcher.Move, EMatcher.Camp1)

  实体拥有Move组件但是不能有Stun组件:NewMatcher():AllOf(EMatcher.Move):Noneof(EMatcher.Stun)

支持规则:

AllOf(...):必须拥有组件

NoneOf(...):不包含组件
    
AnyOf(...):包含任意组件,和AllOf规则互斥

Added():组件添加时

Removed():组件移除时

Updated():组件更新数据时,需要使用实体上的UpdateXXX(...)方法才能触发

(Group)过滤组:用来管理匹配到规则的实体

获取一个group:

self.group = Context:GetGroup(NewMatcher():AllOf(EMatcher.Test))

可以这样处理group匹配到的实体:

for key, entity in pairs(self.group:GetEntities()) do
    -- do something
end

3、创建 Component:

空数据组件:

return {}

带数据的组件

return {
    speed = 22,
    direction = {x = 0, y = 0, z = 0},
    isMoving = true
}

4、生成代码

创建完组件后在 ECS\Framework\Generator.lua 的process表中加入组件代码的相对路径:

local process = {
    TestComponent = "ECS\\Game\\Test\\TestComponent.lua",
    MoveComponent = "ECS\\Game\\Move\\MoveComponent.lua",
    Camp1Component = "ECS\\Game\\Camp1Component.lua"
}

运行Generator.lua脚本,生成GameComponent类和Entity扩展方法

如果要删除旧组件,需要手动清空一下生成的组件代码,然后运行生成脚本,代码文件夹位置:ECS\Generated\Components

5、创建System

继承System.lua, 重写ctor和Execute方法

local _Base = require("ECS.Framework.System")
local MoveSystem = class("MoveSystem", _Base)

function MoveSystem:ctor()
    self.group = Context:GetGroup(NewMatcher():AllOf(EMatcher.Test))
end

function MoveSystem:Execute(dt)
    for key, entity in pairs(self.group:GetEntities()) do
        print("move system execute ", entity:HasTest())
    end
end

return MoveSystem

6、创建Feature

继承Feature.lua, 重写ctor方法, 在ctor方法中加入System

local _Base = require("ECS.Framework.Feature")
local FeatureUpdate = class("FeatureUpdate", _Base)

function FeatureUpdate:ctor()
    self.super:ctor()
    -- 添加system
    self:Add(require("ECS.Game.Move.MoveSystem"))
end

return FeatureUpdate

7、使用Feature

在Update中调用Feature:Execute(dt)

8、使用Entity

创建Entity

local e = Context:CreateEntity()
e:AddTest("new  test", { 523 })  -- 执行代码生成后可以使用添加某组件的扩展方法

销毁Entity

e:Destroy()

空文件

简介

一个lua版的ECS框架,模仿了Entitas的一些特性 展开 收起
Lua
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Lua
1
https://gitee.com/zhaige95/ecs-for-lua.git
git@gitee.com:zhaige95/ecs-for-lua.git
zhaige95
ecs-for-lua
ECS For Lua
master

搜索帮助