You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.7 KiB

const std = @import("std");
const Etyp = @import("EntitySystemTypes.zig");
//Variables Entity
var EntityIDs: std.ArrayList(usize) = undefined;
var EntityAmount: u32 = 0;
var Signatures: std.ArrayList(Etyp.Signature) = undefined;
//Entity System
fn CreateEntitysub() !Etyp.Entity {
if (EntityAmount >= Etyp.MAXEntitys) {
return error.GenericError;
}
EntityAmount += 1;
return EntityIDs.pop();
}
fn DestroyEntitysub(entity: Etyp.Entity) void {
Signatures.items[entity] = null;
EntityIDs.append(entity);
EntityAmount -= 1;
}
fn SetSignature(entity: Etyp.Entity, signature: Etyp.Signature) void {
Signatures.items[entity] = signature;
}
fn GetSignature(entity: Etyp.Entity) Etyp.Signature {
return Signatures.items[entity];
}
//Component Manager variables
var ComponentTypeList: std.StringArrayHashMap(Etyp.Component) = std.StringArrayHashMap(Etyp.Component).init(std.heap.page_allocator);
var ComponentArrays = undefined;
var String2CompArr: std.StringArrayHashMap(usize) = std.StringArrayHashMap(usize).init(std.heap.page_allocator);
var NextComponent: Etyp.Component = 0;
//component manager
fn RegisterComponentsub(comptime in: anytype) !void {
var name = @typeName(in);
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();
}
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;
}