mirror of
				https://github.com/Ukendio/jecs.git
				synced 2025-11-04 02:49:18 +00:00 
			
		
		
		
	Define behaviour for world:entity(id)
	
		
			
	
		
	
	
		
	
		
			Some checks are pending
		
		
	
	
	
				
					
				
			
		
			Some checks are pending
		
		
	
	
This commit is contained in:
		
							parent
							
								
									fd6883cfed
								
							
						
					
					
						commit
						4bb7e64edd
					
				
					 4 changed files with 20 additions and 17 deletions
				
			
		| 
						 | 
					@ -1,16 +1,16 @@
 | 
				
			||||||
local jecs = require("@jecs")
 | 
					local jecs = require("@jecs")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Observer<T...> = {
 | 
					type Observer = {
 | 
				
			||||||
	callback: (jecs.Entity) -> (),
 | 
						callback: (jecs.Entity) -> (),
 | 
				
			||||||
	query: jecs.Query<T...>,
 | 
						query: jecs.Query<...jecs.Id>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export type PatchedWorld = jecs.World & {
 | 
					export type PatchedWorld = jecs.World & {
 | 
				
			||||||
	added: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
 | 
						added: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (),
 | 
				
			||||||
	removed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id) -> ()) -> () -> (),
 | 
						removed: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id) -> ()) -> () -> (),
 | 
				
			||||||
	changed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
 | 
						changed: <T>(PatchedWorld, jecs.Id<T>, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (),
 | 
				
			||||||
	observer: (PatchedWorld, Observer<any>) -> (),
 | 
						observer: (PatchedWorld, Observer) -> (),
 | 
				
			||||||
	monitor: (PatchedWorld, Observer<any>) -> (),
 | 
						monitor: (PatchedWorld, Observer) -> (),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local function observers_new(world, description)
 | 
					local function observers_new(world, description)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										10
									
								
								jecs.luau
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								jecs.luau
									
									
									
									
									
								
							| 
						 | 
					@ -780,7 +780,11 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			local any = dense_array[dense]
 | 
								local any = dense_array[dense]
 | 
				
			||||||
			if dense <= alive_count then
 | 
								if dense <= alive_count then
 | 
				
			||||||
				return any
 | 
									if any ~= entity then
 | 
				
			||||||
 | 
										error("Entity ID is already in use with a different generation")
 | 
				
			||||||
 | 
									else
 | 
				
			||||||
 | 
										return entity
 | 
				
			||||||
 | 
									end
 | 
				
			||||||
			end
 | 
								end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			local e_swap = dense_array[dense]
 | 
								local e_swap = dense_array[dense]
 | 
				
			||||||
| 
						 | 
					@ -790,9 +794,9 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53
 | 
				
			||||||
			r_swap.dense = dense
 | 
								r_swap.dense = dense
 | 
				
			||||||
			r.dense = alive_count
 | 
								r.dense = alive_count
 | 
				
			||||||
			dense_array[dense] = e_swap
 | 
								dense_array[dense] = e_swap
 | 
				
			||||||
			dense_array[alive_count] = any
 | 
								dense_array[alive_count] = entity
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			return any
 | 
								return entity
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
			for i = max_id + 1, index do
 | 
								for i = max_id + 1, index do
 | 
				
			||||||
				sparse_array[i]	= { dense = i } :: ecs_record_t
 | 
									sparse_array[i]	= { dense = i } :: ecs_record_t
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,7 +23,6 @@ TEST("addons/observers", function()
 | 
				
			||||||
		world:set(world:entity(), A, true)
 | 
							world:set(world:entity(), A, true)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		CHECK(count == 3)
 | 
							CHECK(count == 3)
 | 
				
			||||||
		print(count)
 | 
					 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	do CASE "Ensure ordering between signals and observers"
 | 
						do CASE "Ensure ordering between signals and observers"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -672,7 +672,7 @@ TEST("world:range()", function()
 | 
				
			||||||
		local e2v1 = world:entity(399)
 | 
							local e2v1 = world:entity(399)
 | 
				
			||||||
		CHECK(world:contains(e2v1))
 | 
							CHECK(world:contains(e2v1))
 | 
				
			||||||
		CHECK(ECS_ID(e2v1) == 399)
 | 
							CHECK(ECS_ID(e2v1) == 399)
 | 
				
			||||||
		CHECK(ECS_GENERATION(e2v1) == 1)
 | 
							CHECK(ECS_GENERATION(e2v1) == 0)
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	do CASE "over range start"
 | 
						do CASE "over range start"
 | 
				
			||||||
| 
						 | 
					@ -685,12 +685,12 @@ TEST("world:range()", function()
 | 
				
			||||||
		local e2v1 = world:entity(405)
 | 
							local e2v1 = world:entity(405)
 | 
				
			||||||
		CHECK(world:contains(e2v1))
 | 
							CHECK(world:contains(e2v1))
 | 
				
			||||||
		CHECK(ECS_ID(e2v1) == 405)
 | 
							CHECK(ECS_ID(e2v1) == 405)
 | 
				
			||||||
		CHECK(ECS_GENERATION(e2v1) == 1)
 | 
							CHECK(ECS_GENERATION(e2v1) == 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		do
 | 
							world:delete(e2v1)
 | 
				
			||||||
			local _e2v1 = world:entity(405)
 | 
							local e2v2 = world:entity(e2v1)
 | 
				
			||||||
			CHECK(_e2v1 == e2v1)
 | 
							CHECK(ECS_ID(e2v2) == 405)
 | 
				
			||||||
		end
 | 
							CHECK(ECS_GENERATION(e2v2) == 0)
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
end)
 | 
					end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue