|
|
|
@ -3,59 +3,127 @@ const Etyp = @import("EntitySystemTypes.zig");
|
|
|
|
|
|
|
|
|
|
//Variables Entity
|
|
|
|
|
|
|
|
|
|
const EntityIDs: std.TailQueue(u32) = init: {
|
|
|
|
|
var temp = std.TailQueue(u32);
|
|
|
|
|
var i = 0;
|
|
|
|
|
while (i < Etyp.MAXEntitys) {
|
|
|
|
|
temp.append(i);
|
|
|
|
|
i += 1;
|
|
|
|
|
}
|
|
|
|
|
break :init temp;
|
|
|
|
|
};
|
|
|
|
|
var EntityIDs: std.ArrayList(usize) = undefined;
|
|
|
|
|
var EntityAmount: u32 = 0;
|
|
|
|
|
|
|
|
|
|
const Signatures: [Etyp.MAXEntitys]Etyp.Signature = undefined;
|
|
|
|
|
var Signatures: std.ArrayList(Etyp.Signature) = undefined;
|
|
|
|
|
|
|
|
|
|
//Entity System
|
|
|
|
|
|
|
|
|
|
pub fn CreateEntity() !Etyp.entity {
|
|
|
|
|
fn CreateEntitysub() !Etyp.Entity {
|
|
|
|
|
if (EntityAmount >= Etyp.MAXEntitys) {
|
|
|
|
|
return error{};
|
|
|
|
|
return error.GenericError;
|
|
|
|
|
}
|
|
|
|
|
EntityAmount += 1;
|
|
|
|
|
return EntityIDs.popFirst();
|
|
|
|
|
return EntityIDs.pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn DestroyEntity(entity: Etyp.entity) void {
|
|
|
|
|
Signatures[entity] = null;
|
|
|
|
|
fn DestroyEntitysub(entity: Etyp.Entity) void {
|
|
|
|
|
Signatures.items[entity] = null;
|
|
|
|
|
EntityIDs.append(entity);
|
|
|
|
|
EntityAmount -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn SetSignature(entity: Etyp.entity, signature: Etyp.Signature) void {
|
|
|
|
|
Signatures[entity] = signature;
|
|
|
|
|
fn SetSignature(entity: Etyp.Entity, signature: Etyp.Signature) void {
|
|
|
|
|
Signatures.items[entity] = signature;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn GetSignature(entity: Etyp.entity) Etyp.signature {
|
|
|
|
|
return Signatures[entity];
|
|
|
|
|
fn GetSignature(entity: Etyp.Entity) Etyp.Signature {
|
|
|
|
|
return Signatures.items[entity];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Component Manager variables
|
|
|
|
|
|
|
|
|
|
const ComponentTypeList: std.StringArrayHashMap(Etyp.Component) = std.StringArrayHashMap(Etyp.Component);
|
|
|
|
|
var ComponentTypeList: std.StringArrayHashMap(Etyp.Component) = std.StringArrayHashMap(Etyp.Component).init(std.heap.page_allocator);
|
|
|
|
|
|
|
|
|
|
const ComponentArrays: std.StringArrayHashMap(Etyp.ComponentArray) = std.StringArrayHashMap(Etyp.ComponentArray);
|
|
|
|
|
var ComponentArrays = undefined;
|
|
|
|
|
var String2CompArr: std.StringArrayHashMap(usize) = std.StringArrayHashMap(usize).init(std.heap.page_allocator);
|
|
|
|
|
|
|
|
|
|
var NextComponent: Etyp.Component = 0;
|
|
|
|
|
|
|
|
|
|
//component manager
|
|
|
|
|
|
|
|
|
|
pub fn RegisterComponent(in: type) void {
|
|
|
|
|
fn RegisterComponentsub(comptime in: anytype) !void {
|
|
|
|
|
var name = @typeName(in);
|
|
|
|
|
ComponentTypeList.put(name, NextComponent);
|
|
|
|
|
ComponentArrays.put(name, ComponentArrays{});
|
|
|
|
|
try ComponentTypeList.put(name, NextComponent);
|
|
|
|
|
ComponentArrays = Etyp.ComponentArray(in).init();
|
|
|
|
|
try String2CompArr.put(name, ComponentArrays.len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn GetComponentArray(comptime in: type) type {
|
|
|
|
|
return ComponentArrays[@typeName(in)]();
|
|
|
|
|
}
|
|
|
|
|
fn GetComponentTypesub(comptime in: type) Etyp.Component{
|
|
|
|
|
return ComponentTypeList[@typeName(in)];
|
|
|
|
|
}
|
|
|
|
|
fn AddComponentsub(Entity: Etyp.Entity, comptime Component: type) void{
|
|
|
|
|
GetComponentArray(Component).AddComponent(Entity, Component);
|
|
|
|
|
}
|
|
|
|
|
fn RemoveComponentsub(Entity: Etyp.Entity, comptime in: anytype) void{
|
|
|
|
|
GetComponentArray(in).Removedata(Entity);
|
|
|
|
|
}
|
|
|
|
|
fn GetComponentsub(Entity: Etyp.Entity, comptime in: type) type{
|
|
|
|
|
return GetComponentArray(in).GetData(Entity);
|
|
|
|
|
}
|
|
|
|
|
fn EntityDestroyed(Entity: Etyp.Entity) void{
|
|
|
|
|
for(ComponentArrays.values())|val|{
|
|
|
|
|
val.EntityDestroyed(Entity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//THE MASTER
|
|
|
|
|
pub fn Init() !void{
|
|
|
|
|
EntityIDs = init: {
|
|
|
|
|
var temp = try std.ArrayList(usize).initCapacity(std.heap.page_allocator, Etyp.MAXEntitys);
|
|
|
|
|
var i = Etyp.MAXEntitys;
|
|
|
|
|
while (i > 0) {
|
|
|
|
|
temp.appendAssumeCapacity(i);
|
|
|
|
|
i -= 1;
|
|
|
|
|
}
|
|
|
|
|
break :init temp;
|
|
|
|
|
};
|
|
|
|
|
Signatures = try std.ArrayList(Etyp.Signature).initCapacity(std.heap.page_allocator, Etyp.MAXEntitys);
|
|
|
|
|
}
|
|
|
|
|
pub fn CreateEntity() !Etyp.Entity{
|
|
|
|
|
return try CreateEntitysub();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn GetComponentArray(in: type) Etyp.ComponentArray {
|
|
|
|
|
return ComponentArrays[@typeName(in)];
|
|
|
|
|
pub fn DestroyEntity(Entity: Etyp.entity) void{
|
|
|
|
|
DestroyEntitysub(Entity);
|
|
|
|
|
EntityDestroyed(Entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn RegisterComponent(comptime in: anytype) !void{
|
|
|
|
|
try RegisterComponentsub(in);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn AddComponent(Entity: Etyp.Entity, comptime in: anytype) void{
|
|
|
|
|
AddComponentsub(Entity, in);
|
|
|
|
|
var sig = GetSignature(Entity);
|
|
|
|
|
sig.setValue(GetComponentTypesub(in), true);
|
|
|
|
|
SetSignature(Entity, sig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn RemoveComponent(Entity: Etyp.Entity, comptime in: anytype) void{
|
|
|
|
|
RemoveComponentsub(Entity, in);
|
|
|
|
|
var sig = GetSignature(Entity);
|
|
|
|
|
sig.setValue(GetComponentTypesub(in), false);
|
|
|
|
|
SetSignature(Entity, sig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn GetComponent(Entity: Etyp.Entity, comptime in: anytype) @TypeOf(in){
|
|
|
|
|
return GetComponentsub(Entity, in);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn GetComponentType(comptime in: anytype) Etyp.Component{
|
|
|
|
|
return GetComponentTypesub(in);
|
|
|
|
|
}
|
|
|
|
|
pub fn GetEntitys(Signature: Etyp.Signature) []Etyp.Entity{
|
|
|
|
|
var sel = std.ArrayList(Etyp.Entity).init(std.heap.page_allocator);
|
|
|
|
|
for(Signatures.items, 0..)|val, i|{
|
|
|
|
|
if(val.intersectWith(Signature) == Signature){
|
|
|
|
|
try sel.append(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return sel.items;
|
|
|
|
|
}
|