@ -6,6 +6,7 @@ const util = @import("./util.zig");
const Lookup = @import ( " ./main.zig " ) . Lookup ;
const Lookup = @import ( " ./main.zig " ) . Lookup ;
const Entity = @import ( " ./entity.zig " ) . Entity ;
const Entity = @import ( " ./entity.zig " ) . Entity ;
const EntityConfig = @import ( " ./entity.zig " ) . EntityConfig ;
const Iter = @import ( " ./iter.zig " ) . Iter ;
const Iter = @import ( " ./iter.zig " ) . Iter ;
pub fn World ( comptime ctx : anytype ) type {
pub fn World ( comptime ctx : anytype ) type {
@ -50,19 +51,21 @@ pub fn World(comptime ctx: anytype) type {
return lookupAlive ( self , Lookup ( ctx , T ) . id ) ;
return lookupAlive ( self , Lookup ( ctx , T ) . id ) ;
}
}
/ / TODO : Replace these functions with an entity builder interface .
/ / / Creates a new ` Entity ` in this ` World ` .
/ / /
pub fn entity (
/ / / The ` config ` argument may be used to specify the ` name ` and
self : * Self ,
/ / / ` symbol ` used to create the entity with . If an entity with the
comptime add : [ ] const type ,
/ / / given name already exists , that one is modified instead .
) Entity ( ctx ) {
/ / /
var desc = std . mem . zeroes ( c . ecs_entity_desc_t ) ;
/ / / The ` add ` argument is a tuple that specifies the ` Id ` s the entity
if ( add . len > c . FLECS_ID_DESC_MAX )
/ / / is created with initially . For example :
@compileLog ( " add.len > FLECS_ID_DESC_MAX " ) ;
/ / / ` . { Position , Velocity , . { ChildOf , parent } } `
inline for ( add , 0 . . ) | T , i |
/ / /
desc . add [ i ] = Lookup ( ctx , T ) . id ;
/ / / When you add components , they are default - initialized . To set
const result = c . ecs_entity_init ( self . * . raw , & desc ) ;
/ / / their values you ' ll need to use one of the setter functions on the
return Entity ( ctx ) . fromRaw ( self , result ) ;
/ / / ` Entity ` returned from this function .
pub fn entity ( self : * Self , config : EntityConfig , add : anytype ) Entity ( ctx ) {
return Entity ( ctx ) . new ( self , config , add ) ;
}
}
pub fn component (
pub fn component (
@ -70,16 +73,11 @@ pub fn World(comptime ctx: anytype) type {
comptime T : type ,
comptime T : type ,
) Entity ( ctx ) {
) Entity ( ctx ) {
const name = util . simpleTypeName ( T ) ;
const name = util . simpleTypeName ( T ) ;
const entDesc = std . mem . zeroInit ( c . ecs_entity_desc_t , . {
const desc = std . mem . zeroInit ( c . ecs_component_desc_t , . {
. name = name ,
. entity = self . entity ( . { . name = name , . symbol = name , . use_low_id = true } , . { } ) . raw ,
. symbol = name ,
. use_low_id = true ,
} ) ;
const compDesc = std . mem . zeroInit ( c . ecs_component_desc_t , . {
. entity = c . ecs_entity_init ( self . raw , & entDesc ) ,
. type = . { . size = @sizeOf ( T ) , . alignment = @alignOf ( T ) } ,
. type = . { . size = @sizeOf ( T ) , . alignment = @alignOf ( T ) } ,
} ) ;
} ) ;
Lookup ( ctx , T ) . id = c . ecs_component_init ( self . raw , & compD esc) ;
Lookup ( ctx , T ) . id = c . ecs_component_init ( self . raw , & desc ) ;
return Entity ( ctx ) . fromRaw ( self , Lookup ( ctx , T ) . id ) ;
return Entity ( ctx ) . fromRaw ( self , Lookup ( ctx , T ) . id ) ;
}
}
@ -87,23 +85,25 @@ pub fn World(comptime ctx: anytype) type {
self : * Self ,
self : * Self ,
name : [ : 0 ] const u8 ,
name : [ : 0 ] const u8 ,
callback : SystemCallback ,
callback : SystemCallback ,
phase : ? Entity ( ctx ) ,
phase : anytype ,
expr : [ : 0 ] const u8 ,
expr : [ : 0 ] const u8 ,
) Entity ( ctx ) {
) Entity ( ctx ) {
var context = SystemCallbackContext . init ( self , callback ) ;
var context = SystemCallbackContext . init ( self , callback ) ;
var entDesc = std . mem . zeroes ( c . ecs_entity_desc_t ) ;
entDesc . name = name ;
const p = util . anyToEntity ( ctx , phase ) ;
if ( phase ) | p | {
const e = if ( p ! = 0 )
entDesc . add [ 0 ] = c . ecs_pair ( c . EcsDependsOn , p . raw ) ;
self . entity ( . { . name = name } , . { . { c . EcsDependsOn , p } , p } )
entDesc . add [ 1 ] = p . raw ;
else
}
self . entity ( . { . name = name } , . { } ) ;
var sysDesc = std . mem . zeroes ( c . ecs_system_desc_t ) ;
sysDesc . entity = c . ecs_entity_init ( self . raw , & entDesc ) ;
var desc = std . mem . zeroes ( c . ecs_system_desc_t ) ;
sysDesc . query . filter . expr = expr ;
desc . entity = e . raw ;
sysDesc . callback = & SystemCallbackContext . invoke ;
desc . query . filter . expr = expr ;
sysDesc . binding_ctx = context ;
desc . callback = & SystemCallbackContext . invoke ;
sysDesc . binding_ctx_free = & SystemCallbackContext . free ;
desc . binding_ctx = context ;
const result = c . ecs_system_init ( self . raw , & sysDesc ) ;
desc . binding_ctx_free = & SystemCallbackContext . free ;
const result = c . ecs_system_init ( self . raw , & desc ) ;
return Entity ( ctx ) . fromRaw ( self , result ) ;
return Entity ( ctx ) . fromRaw ( self , result ) ;
}
}