summaryrefslogtreecommitdiff
path: root/honey/ecs-systems.lua
blob: a511b3d28187e4b29416c0a4dcb3858010369717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
local ecs = require 'honey.ecs'


local module = {}
setmetatable(module, {__index=_G})
setfenv(1, module)



--===== transform cascading =====--

local function recursiveComputeTransform(entity)
	if entity._transformComputed then 
		return entity._transform 
	end
	if entity.parent == false then
		entity._transformComputed = true
		entity._transform = entity.transform
		return entity.transform
	end

	entity._transformComputed = true
	local parentTransform = recursiveComputeTransform(entity.parent)
	entity._transform = parentTransform * entity.transform
	return entity._transform
end

-- update transforms
transformCascade = {
	filter=ecs.Filter.AND{"transform", "parent"},
	preUpdate=function(entity)
		entity._transform = nil
		entity._transformComputed = false
	end,
	update=function(entity, dt)
		recursiveComputeTransform(entity)
	end,
	priority=98,
}


--===== rendering =====--

function renderCam(camera, priority)
	local priority = priority or 99
	return {
		filter=ecs.Filter.AND{"mesh", "shader", "transform"},
		update=function(entity, dt)
			entity.shader:use()
			entity.shader:configure{
				matrix={
					model=entity._transform,
					view=camera.view,
					projection=camera.projection,
				},
			}
			entity.mesh:drawElements()
		end,
		priority=priority,
	}
end

--===== update functions =====--

update = {
	filter=ecs.Filter.AND{"update"},
	update=function(entity, dt)
		entity.update(entity, dt)
	end,
	priority=50,
}



return module