mirror of
				https://github.com/Ukendio/jecs.git
				synced 2025-11-04 02:49:18 +00:00 
			
		
		
		
	Add options to lifetime tracker
	
		
			
	
		
	
	
		
	
		
			Some checks are pending
		
		
	
	
	
				
					
				
			
		
			Some checks are pending
		
		
	
	
This commit is contained in:
		
							parent
							
								
									0e4f40ced7
								
							
						
					
					
						commit
						b26fc39fce
					
				
					 4 changed files with 50 additions and 37 deletions
				
			
		| 
						 | 
					@ -2512,6 +2512,8 @@ return {
 | 
				
			||||||
	ECS_GENERATION = ECS_GENERATION,
 | 
						ECS_GENERATION = ECS_GENERATION,
 | 
				
			||||||
	ECS_ID_IS_WILDCARD = ECS_ID_IS_WILDCARD,
 | 
						ECS_ID_IS_WILDCARD = ECS_ID_IS_WILDCARD,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ECS_ID_DELETE = ECS_ID_DELETE,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	IS_PAIR = ECS_IS_PAIR,
 | 
						IS_PAIR = ECS_IS_PAIR,
 | 
				
			||||||
	pair_first = ecs_pair_first,
 | 
						pair_first = ecs_pair_first,
 | 
				
			||||||
	pair_second = ecs_pair_second,
 | 
						pair_second = ecs_pair_second,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,23 +2,24 @@ local jecs = require("@jecs")
 | 
				
			||||||
local pair = jecs.pair
 | 
					local pair = jecs.pair
 | 
				
			||||||
local ChildOf = jecs.ChildOf
 | 
					local ChildOf = jecs.ChildOf
 | 
				
			||||||
local lifetime_tracker_add = require("@tools/lifetime_tracker")
 | 
					local lifetime_tracker_add = require("@tools/lifetime_tracker")
 | 
				
			||||||
local world = lifetime_tracker_add(jecs.world())
 | 
					local pe = require("@tools/entity_visualiser").prettify
 | 
				
			||||||
world:print_snapshot()
 | 
					local world = lifetime_tracker_add(jecs.world(), {padding_enabled=false})
 | 
				
			||||||
 | 
					local FriendsWith = world:component()
 | 
				
			||||||
 | 
					local _1 = world:print_snapshot()
 | 
				
			||||||
local e1 = world:entity()
 | 
					local e1 = world:entity()
 | 
				
			||||||
local e2 = world:entity()
 | 
					local e2 = world:entity()
 | 
				
			||||||
world:delete(e2)
 | 
					world:delete(e2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
world:print_snapshot()
 | 
					local _2 = world:print_snapshot()
 | 
				
			||||||
local e3 = world:entity()
 | 
					local e3 = world:entity()
 | 
				
			||||||
world:add(e3, pair(ChildOf, e1))
 | 
					world:add(e3, pair(ChildOf, e1))
 | 
				
			||||||
local e4 = world:entity()
 | 
					local e4 = world:entity()
 | 
				
			||||||
world:add(e4, pair(ChildOf, e4))
 | 
					world:add(e4, pair(FriendsWith, e3))
 | 
				
			||||||
world:print_snapshot()
 | 
					local _3 = world:print_snapshot()
 | 
				
			||||||
world:delete(e1)
 | 
					world:delete(e1)
 | 
				
			||||||
world:delete(e2)
 | 
					 | 
				
			||||||
world:delete(e3)
 | 
					world:delete(e3)
 | 
				
			||||||
world:print_snapshot()
 | 
					local _4 = world:print_snapshot()
 | 
				
			||||||
world:print_entity_index()
 | 
					world:print_entity_index()
 | 
				
			||||||
world:entity()
 | 
					world:entity()
 | 
				
			||||||
world:entity()
 | 
					world:entity()
 | 
				
			||||||
world:print_snapshot()
 | 
					local _5 = world:print_snapshot()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,5 +39,5 @@ end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
return {
 | 
					return {
 | 
				
			||||||
	components = components,
 | 
						components = components,
 | 
				
			||||||
	prettify = pe
 | 
						prettify = pe,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,7 +4,9 @@ local ECS_ID = jecs.ECS_ID
 | 
				
			||||||
local __ = jecs.Wildcard
 | 
					local __ = jecs.Wildcard
 | 
				
			||||||
local pair = jecs.pair
 | 
					local pair = jecs.pair
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local pe = require("@tools/entity_visualiser").prettify
 | 
					local prettify = require("@tools/entity_visualiser").prettify
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					local pe = prettify
 | 
				
			||||||
local ansi = require("@tools/ansi")
 | 
					local ansi = require("@tools/ansi")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function print_centered_entity(entity, width: number)
 | 
					function print_centered_entity(entity, width: number)
 | 
				
			||||||
| 
						 | 
					@ -24,8 +26,14 @@ end
 | 
				
			||||||
local function name(world, e)
 | 
					local function name(world, e)
 | 
				
			||||||
	return world:get(world, e, jecs.Name) or pe(e)
 | 
						return world:get(world, e, jecs.Name) or pe(e)
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					local padding_enabled = false
 | 
				
			||||||
 | 
					local function pad()
 | 
				
			||||||
 | 
						if padding_enabled then
 | 
				
			||||||
 | 
							print("")
 | 
				
			||||||
 | 
						end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local function lifetime_tracker_add(world: jecs.World)
 | 
					local function lifetime_tracker_add(world: jecs.World, opt)
 | 
				
			||||||
	local entity_index = world.entity_index
 | 
						local entity_index = world.entity_index
 | 
				
			||||||
	local dense_array = entity_index.dense_array
 | 
						local dense_array = entity_index.dense_array
 | 
				
			||||||
	local component_index = world.component_index
 | 
						local component_index = world.component_index
 | 
				
			||||||
| 
						 | 
					@ -34,6 +42,7 @@ local function lifetime_tracker_add(world: jecs.World)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	local w = setmetatable({}, { __index = world })
 | 
						local w = setmetatable({}, { __index = world })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						padding_enabled = opt.padding_enabled
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	local world_entity = world.entity
 | 
						local world_entity = world.entity
 | 
				
			||||||
	w.entity = function(self)
 | 
						w.entity = function(self)
 | 
				
			||||||
| 
						 | 
					@ -44,6 +53,7 @@ local function lifetime_tracker_add(world: jecs.World)
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
			print(`*created {pe(e)}`)
 | 
								print(`*created {pe(e)}`)
 | 
				
			||||||
		end
 | 
							end
 | 
				
			||||||
 | 
							pad()
 | 
				
			||||||
		return e
 | 
							return e
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
	w.print_entity_index = function(self)
 | 
						w.print_entity_index = function(self)
 | 
				
			||||||
| 
						 | 
					@ -70,6 +80,7 @@ local function lifetime_tracker_add(world: jecs.World)
 | 
				
			||||||
				print(sep)
 | 
									print(sep)
 | 
				
			||||||
			end
 | 
								end
 | 
				
			||||||
		end
 | 
							end
 | 
				
			||||||
 | 
							pad()
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
	local timelines = {}
 | 
						local timelines = {}
 | 
				
			||||||
	w.print_snapshot = function(self)
 | 
						w.print_snapshot = function(self)
 | 
				
			||||||
| 
						 | 
					@ -143,6 +154,7 @@ local function lifetime_tracker_add(world: jecs.World)
 | 
				
			||||||
			print(row)
 | 
								print(row)
 | 
				
			||||||
		end
 | 
							end
 | 
				
			||||||
		print("-------------------------------------------------------------------")
 | 
							print("-------------------------------------------------------------------")
 | 
				
			||||||
 | 
							pad()
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
	local world_add = world.add
 | 
						local world_add = world.add
 | 
				
			||||||
	local relations = {}
 | 
						local relations = {}
 | 
				
			||||||
| 
						 | 
					@ -152,49 +164,47 @@ local function lifetime_tracker_add(world: jecs.World)
 | 
				
			||||||
			local relation = jecs.pair_first(world, component)
 | 
								local relation = jecs.pair_first(world, component)
 | 
				
			||||||
			local target = jecs.pair_second(world, component)
 | 
								local target = jecs.pair_second(world, component)
 | 
				
			||||||
			print(`*added ({pe(relation)}, {pe(target)}) to {pe(entity)}`)
 | 
								print(`*added ({pe(relation)}, {pe(target)}) to {pe(entity)}`)
 | 
				
			||||||
			local rs = relations[relation]
 | 
								pad()
 | 
				
			||||||
			if not rs then
 | 
					 | 
				
			||||||
				local flags = component_index[component].flags
 | 
					 | 
				
			||||||
				rs = {
 | 
					 | 
				
			||||||
					fragmenting = bit32.band(flags, 0b0000_0010) ~= 0
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				relations[relation] = rs
 | 
					 | 
				
			||||||
			end
 | 
					 | 
				
			||||||
			local ts = rs[target]
 | 
					 | 
				
			||||||
			if not ts then
 | 
					 | 
				
			||||||
				ts = {}
 | 
					 | 
				
			||||||
				rs[target] = ts
 | 
					 | 
				
			||||||
			end
 | 
					 | 
				
			||||||
			table.insert(ts, entity)
 | 
					 | 
				
			||||||
		end
 | 
							end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	local world_delete = world.delete
 | 
						local world_delete = world.delete
 | 
				
			||||||
	w.delete = function(self, e)
 | 
						w.delete = function(self, e)
 | 
				
			||||||
 | 
							world_delete(world, e)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		local idr_t = component_index[pair(__, e)]
 | 
							local idr_t = component_index[pair(__, e)]
 | 
				
			||||||
		if idr_t then
 | 
							if idr_t then
 | 
				
			||||||
			for relation, ts in relations do
 | 
								for archetype_id in idr_t.cache do
 | 
				
			||||||
				local targets = ts[e]
 | 
									local archetype = world.archetypes[archetype_id]
 | 
				
			||||||
				if not targets then
 | 
									for _, id in archetype.types do
 | 
				
			||||||
 | 
										if not jecs.IS_PAIR(id) then
 | 
				
			||||||
						continue
 | 
											continue
 | 
				
			||||||
					end
 | 
										end
 | 
				
			||||||
				for _, target in targets do
 | 
										local object = jecs.pair_second(world, id)
 | 
				
			||||||
					if type(target) == "boolean" then
 | 
										if object ~= e then
 | 
				
			||||||
						continue
 | 
											continue
 | 
				
			||||||
					end
 | 
										end
 | 
				
			||||||
					if targets.fragmenting then
 | 
										local id_record = component_index[id]
 | 
				
			||||||
						print(`*deleted dependant {pe(target)}`)
 | 
										local flags = id_record.flags
 | 
				
			||||||
 | 
										local flags_delete_mask: number = bit32.band(flags, jecs.ECS_ID_DELETE)
 | 
				
			||||||
 | 
										if flags_delete_mask ~= 0 then
 | 
				
			||||||
 | 
											for _, entity in archetype.entities do
 | 
				
			||||||
 | 
												print(`*deleted dependant {pe(entity)} of {pe(e)}`)
 | 
				
			||||||
 | 
												pad()
 | 
				
			||||||
 | 
											end
 | 
				
			||||||
 | 
											break
 | 
				
			||||||
					else
 | 
										else
 | 
				
			||||||
						local t = pe(target)
 | 
											for _, entity in archetype.entities do
 | 
				
			||||||
						print(`*removed dependency {ansi.red("(*, ")}{t}{ansi.green(")")} from {pe(e)}`)
 | 
												print(`*removed dependency ({pe(jecs.pair_first(world, id))}, {pe(object)}) from {pe(entity)}`)
 | 
				
			||||||
 | 
											end
 | 
				
			||||||
					end
 | 
										end
 | 
				
			||||||
				end
 | 
									end
 | 
				
			||||||
			end
 | 
								end
 | 
				
			||||||
		end
 | 
							end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		world_delete(world, e)
 | 
					 | 
				
			||||||
		print(`*deleted {pe(e)}`)
 | 
							print(`*deleted {pe(e)}`)
 | 
				
			||||||
 | 
							pad()
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
	return w
 | 
						return w
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue