GithubHelp home page GithubHelp logo

luaecs's Introduction

LuaECS

Unity ECS框架Entities的Lua实现
对实现细节有兴趣的可以看我对UnityECS的框架源码分析:https://blog.csdn.net/yudianxia/column/info/31641
Lua版本的话基本上和Unity实现是一样的,除了数据的存放方式,在早期版本试过直接开辟一段内存然后把组件的数据逐字段地写入,读取时计算下该字段的指针偏移就可以了,但lua和c的交互消耗比连续存放数据带来的优化更大,所以就放弃了该方案,现在改成直接存放table了。

用例

local ECS = require "ECS"
--ComponentType
ECS.TypeManager.RegisterType("ECS.CustomCom", {x=0, y="", z=false})

--Entity
local entityMgr = ECS.World.Active:GetOrCreateManager(ECS.EntityManager.Name)
local archetype = entityMgr:CreateArchetype({"ECS.CustomCom"})
local entity = entityMgr:CreateEntityByArcheType(archetype)
entityMgr:SetComponentData(entity, "ECS.CustomCom", {x=1.1, y="hello", z=true, tbl={a=1,b=false}})
local comp_data = entityMgr:GetComponentData(entity, "ECS.CustomCom")

--ComponentSystem
local TestInjectSystem = ECS.BaseClass(ECS.ComponentSystem)
ECS.TypeManager.RegisterScriptMgr("TestInjectSystem", TestInjectSystem)
function TestInjectSystem:OnCreateManager(  )
	ECS.ComponentSystem.OnCreateManager(self)
	self.group = self:GetComponentGroup({"ECS.CustomCom"})
end
function TestInjectSystem:OnUpdate(  )
	local comps = self.group:ToComponentDataArray("ECS.CustomCom")
	for i=1,comps.Length do
		local comp = comps[i]
		if comp.tbl.b then
			do some thing...
		end
	end
end

测试

可以在windows或linux上运行测试用例,Lua5.2以上都是可以的 :
lua ./Tests/test_all.lua -v

Todo

)增加ShardComponent
)支持多线程?
)System根据UpdateBefore,After等排序

luaecs's People

Contributors

liuhaopen avatar

Watchers

James Cloos avatar

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.