mirror of
				https://github.com/Ukendio/jecs.git
				synced 2025-11-04 02:49:18 +00:00 
			
		
		
		
	Allow nil index in world:target
This commit is contained in:
		
							parent
							
								
									68729446d0
								
							
						
					
					
						commit
						c1e11cf844
					
				
					 3 changed files with 34 additions and 1 deletions
				
			
		
							
								
								
									
										10
									
								
								src/index.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										10
									
								
								src/index.d.ts
									
									
									
									
										vendored
									
									
								
							| 
						 | 
					@ -95,11 +95,21 @@ export class World {
 | 
				
			||||||
     * Gets the target of a relationship. For example, when a user calls
 | 
					     * Gets the target of a relationship. For example, when a user calls
 | 
				
			||||||
     * `world.target(entity, ChildOf(parent))`, you will obtain the parent entity.
 | 
					     * `world.target(entity, ChildOf(parent))`, you will obtain the parent entity.
 | 
				
			||||||
     * @param entity Entity
 | 
					     * @param entity Entity
 | 
				
			||||||
 | 
					     * @param index Target index
 | 
				
			||||||
     * @param relation The Relationship
 | 
					     * @param relation The Relationship
 | 
				
			||||||
     * @returns The Parent Entity if it exists
 | 
					     * @returns The Parent Entity if it exists
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    target(entity: Entity, relation: Entity, index: number): Entity | undefined;
 | 
					    target(entity: Entity, relation: Entity, index: number): Entity | undefined;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Gets the target of a relationship. For example, when a user calls
 | 
				
			||||||
 | 
					     * `world.target(entity, ChildOf(parent))`, you will obtain the parent entity.
 | 
				
			||||||
 | 
					     * @param entity Entity
 | 
				
			||||||
 | 
					     * @param relation The Relationship
 | 
				
			||||||
 | 
					     * @returns The Parent Entity if it exists
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    target(entity: Entity, relation: Entity): Entity | undefined;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Clears an entity from the world.
 | 
					     * Clears an entity from the world.
 | 
				
			||||||
     * @param entity Entity to be cleared
 | 
					     * @param entity Entity to be cleared
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -389,7 +389,8 @@ local function world_has(world: World, entity: number, ...: i53): boolean
 | 
				
			||||||
	return true
 | 
						return true
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local function world_target(world: World, entity: i53, relation: i24, index: number): i24?
 | 
					local function world_target(world: World, entity: i53, relation: i24, index_opt: number?): i24?
 | 
				
			||||||
 | 
						local index = if index_opt then index_opt else 0
 | 
				
			||||||
	local record = world.entityIndex.sparse[entity]
 | 
						local record = world.entityIndex.sparse[entity]
 | 
				
			||||||
	local archetype = record.archetype
 | 
						local archetype = record.archetype
 | 
				
			||||||
	if not archetype then
 | 
						if not archetype then
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1012,6 +1012,28 @@ TEST("world:target", function()
 | 
				
			||||||
        CHECK(world:target(e, C, 1) == nil)
 | 
					        CHECK(world:target(e, C, 1) == nil)
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    do CASE "infer index when unspecified"
 | 
				
			||||||
 | 
					        local world = world_new()
 | 
				
			||||||
 | 
					        local A = world:component()
 | 
				
			||||||
 | 
					        local B = world:component()
 | 
				
			||||||
 | 
					        local C = world:component()
 | 
				
			||||||
 | 
					        local D = world:component()
 | 
				
			||||||
 | 
					        local e = world:entity()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        world:add(e, pair(A, B))
 | 
				
			||||||
 | 
					        world:add(e, pair(A, C))
 | 
				
			||||||
 | 
					        world:add(e, pair(B, C))
 | 
				
			||||||
 | 
					        world:add(e, pair(B, D))
 | 
				
			||||||
 | 
					        world:add(e, pair(C, D))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        CHECK(world:target(e, A) == B)
 | 
				
			||||||
 | 
					        CHECK(world:target(e, A, 1) == C)
 | 
				
			||||||
 | 
					        CHECK(world:target(e, B) == C)
 | 
				
			||||||
 | 
					        CHECK(world:target(e, B, 1) == D)
 | 
				
			||||||
 | 
					        CHECK(world:target(e, C) == D)
 | 
				
			||||||
 | 
					        CHECK(world:target(e, C, 1) == nil)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    do CASE "loop until no target"
 | 
					    do CASE "loop until no target"
 | 
				
			||||||
        local world = world_new()
 | 
					        local world = world_new()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue