mirror of
				https://github.com/Ukendio/jecs.git
				synced 2025-11-03 18:39:19 +00:00 
			
		
		
		
	Add backwards relation to edges
	
		
			
	
		
	
	
		
	
		
			Some checks are pending
		
		
	
	
	
				
					
				
			
		
			Some checks are pending
		
		
	
	
This commit is contained in:
		
							parent
							
								
									c67dfcbd24
								
							
						
					
					
						commit
						f6731069aa
					
				
					 2 changed files with 49 additions and 7 deletions
				
			
		
							
								
								
									
										14
									
								
								jecs.luau
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								jecs.luau
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -1052,14 +1052,15 @@ local function archetype_destroy(world: World, archetype: Archetype)
 | 
			
		|||
 | 
			
		||||
	local component_index = world.component_index
 | 
			
		||||
	local archetype_edges = world.archetype_edges
 | 
			
		||||
 | 
			
		||||
	for id, edge in archetype_edges[archetype.id] do
 | 
			
		||||
		archetype_edges[edge.id][id] = nil
 | 
			
		||||
	local edges = archetype_edges[archetype.id]
 | 
			
		||||
	for id, node in edges do
 | 
			
		||||
		archetype_edges[node.id][id] = nil
 | 
			
		||||
		edges[id] = nil
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	local archetype_id = archetype.id
 | 
			
		||||
	world.archetypes[archetype_id] = nil :: any
 | 
			
		||||
	world.archetype_index[archetype.type] = nil :: any
 | 
			
		||||
	-- world.archetypes[archetype_id] = nil :: any
 | 
			
		||||
	-- world.archetype_index[archetype.type] = nil :: any
 | 
			
		||||
	local columns_map = archetype.columns_map
 | 
			
		||||
 | 
			
		||||
	for id in columns_map do
 | 
			
		||||
| 
						 | 
				
			
			@ -2330,6 +2331,8 @@ local function world_new()
 | 
			
		|||
			else
 | 
			
		||||
				if to.dead then
 | 
			
		||||
					archetype_register(world, to)
 | 
			
		||||
					edge[id] = to
 | 
			
		||||
					archetype_edges[to.id][id] = src
 | 
			
		||||
					to.dead = false
 | 
			
		||||
				end
 | 
			
		||||
				idr = component_index[id]
 | 
			
		||||
| 
						 | 
				
			
			@ -2539,6 +2542,7 @@ local function world_new()
 | 
			
		|||
						idr = component_index[id]
 | 
			
		||||
					end
 | 
			
		||||
					edge[id] = to
 | 
			
		||||
					archetype_edges[to.id][id] = src
 | 
			
		||||
				else
 | 
			
		||||
					idr = component_index[id]
 | 
			
		||||
				end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -24,8 +24,46 @@ type Id<T=unknown> = jecs.Id<T>
 | 
			
		|||
local entity_visualiser = require("@tools/entity_visualiser")
 | 
			
		||||
local dwi = entity_visualiser.stringify
 | 
			
		||||
 | 
			
		||||
FOCUS()
 | 
			
		||||
TEST("", function()
 | 
			
		||||
TEST("repeated pairs", function()
 | 
			
		||||
	local pair = jecs.pair
 | 
			
		||||
	local world = jecs.world()
 | 
			
		||||
	local rel = world:component() -- Does not error if this is just a tag
 | 
			
		||||
 | 
			
		||||
	-- Does not happen if we delete manually instead of using this
 | 
			
		||||
	world:add(rel, pair(jecs.OnDeleteTarget, jecs.Delete))
 | 
			
		||||
 | 
			
		||||
	local t1 = world:entity()
 | 
			
		||||
 | 
			
		||||
	local p1 = pair(rel, t1)
 | 
			
		||||
 | 
			
		||||
	local e1 = world:entity()
 | 
			
		||||
 | 
			
		||||
	world:set(e1, p1, true)
 | 
			
		||||
 | 
			
		||||
	CHECK(world:get(e1, p1))
 | 
			
		||||
	CHECK(world:each(p1)() == e1)
 | 
			
		||||
 | 
			
		||||
	world:delete(t1)
 | 
			
		||||
 | 
			
		||||
	local t2 = world:entity()
 | 
			
		||||
	local p2 = pair(rel, t2)
 | 
			
		||||
 | 
			
		||||
	local e2 = world:entity()
 | 
			
		||||
 | 
			
		||||
	print("-----")
 | 
			
		||||
	world:set(e2, p2, true)
 | 
			
		||||
 | 
			
		||||
	CHECK(world:get(e2, p2))
 | 
			
		||||
	CHECK(p1 == p2)
 | 
			
		||||
	local count = 0
 | 
			
		||||
	CHECK(world:has(e2, p2))
 | 
			
		||||
	for _ in world:query(p2) do
 | 
			
		||||
		count += 1
 | 
			
		||||
	end
 | 
			
		||||
	CHECK(count == 1)
 | 
			
		||||
	CHECK(world:each(p2)() == e2) -- Fails
 | 
			
		||||
end)
 | 
			
		||||
TEST("repro", function()
 | 
			
		||||
	local world = jecs.world()
 | 
			
		||||
	local data = world:component()
 | 
			
		||||
	local relation = world:component()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue