代码拉取完成,页面将自动刷新
Context:Init()
比如:
实体拥有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:
self.group = Context:GetGroup(NewMatcher():AllOf(EMatcher.Test))
可以这样处理group匹配到的实体:
for key, entity in pairs(self.group:GetEntities()) do
-- do something
end
空数据组件:
return {}
带数据的组件
return {
speed = 22,
direction = {x = 0, y = 0, z = 0},
isMoving = true
}
创建完组件后在 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
继承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
继承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
在Update中调用Feature:Execute(dt)
创建Entity
local e = Context:CreateEntity()
e:AddTest("new test", { 523 }) -- 执行代码生成后可以使用添加某组件的扩展方法
销毁Entity
e:Destroy()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。