From 380dc7d68ee459ee4d3ee5b55dee7271af2053ce Mon Sep 17 00:00:00 2001 From: Red Date: Mon, 26 Feb 2024 08:43:28 +0100 Subject: [PATCH] working on the entity systemw --- src/EntitySystem.zig | 120 +++++++++++++++++++++++++++++--------- src/EntitySystemTypes.zig | 89 +++++++++++++++++----------- src/main.zig | 34 +++++++---- 3 files changed, 172 insertions(+), 71 deletions(-) diff --git a/src/EntitySystem.zig b/src/EntitySystem.zig index e369b3c..74f0603 100644 --- a/src/EntitySystem.zig +++ b/src/EntitySystem.zig @@ -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; } \ No newline at end of file diff --git a/src/EntitySystemTypes.zig b/src/EntitySystemTypes.zig index ee99e91..9cd253a 100644 --- a/src/EntitySystemTypes.zig +++ b/src/EntitySystemTypes.zig @@ -16,45 +16,56 @@ pub const Signature = std.bit_set.IntegerBitSet(MAXComponents); //Component array type -pub const ComponentArray = struct { - Components: [MAXEntitys]type, - Entity2Array: [usize]Entity, - Array2Entity: [Entity]usize, - sizeM: usize, - - pub fn AddComponent(self: ComponentArray, entity: Entity, comp: type) void { - var newidx: usize = self.sizeM; - self.Entity2Array[entity] = newidx; - self.Entity2Array[newidx] = entity; - self.Components[newidx] = comp; - self.sizeM += 1; - } - pub fn Removedata(self: ComponentArray, entity: Entity) void { - var remidx: usize = self.Entity2Array[entity]; - var lastidx: usize = self.sizeM - 1; - self.Components[remidx] = self.Components[lastidx]; +pub fn ComponentArray (comptime T: type) type { + return struct { + Components: [MAXEntitys]T, + Entity2Array: std.AutoArrayHashMap(Entity, usize), + Array2Entity: std.AutoArrayHashMap(usize, Entity), + sizeM: usize, + const Self = @This(); - var lastentity: entity = self.Array2Entity[lastidx]; - self.Entity2Array[lastentity] = remidx; - self.Array2Entity[remidx] = lastentity; + pub fn AddComponent(entity: Entity, comp: T) void { + var newidx: usize = Self.sizeM; + Self.Entity2Array[entity] = newidx; + Self.Entity2Array[newidx] = entity; + Self.Components[newidx] = comp; + Self.sizeM += 1; + } + pub fn Removedata(entity: Entity) void { + var remidx: usize = Self.Entity2Array[entity]; + var lastidx: usize = Self.sizeM - 1; + Self.Components[remidx] = Self.Components[lastidx]; - self.Entity2Array[entity] = null; - self.Array2Entity[lastentity] = null; + var lastentity: entity = Self.Array2Entity[lastidx]; + Self.Entity2Array[lastentity] = remidx; + Self.Array2Entity[remidx] = lastentity; - self.sizeM -= 1; - } - pub fn GetData(self: ComponentArray, entity: Entity) type { - return self.Components[self.Entity2Array[entity]]; - } - pub fn EntityDestroyed(self: ComponentArray, entity: Entity) void { - for(self.Array2Entity) |val|{ - if(val == entity){ - self.Removedata(entity); - return; + Self.Entity2Array[entity] = null; + Self.Array2Entity[lastentity] = null; + + Self.sizeM -= 1; + } + pub fn GetData(entity: Entity) type { + return Self.Components[Self.Entity2Array[entity]]; + } + pub fn EntityDestroyed(self: ComponentArray, entity: Entity) void { + for(self.Array2Entity) |val|{ + if(val == entity){ + self.Removedata(entity); + return; + } } } - } -}; + pub fn init() Self{ + return .{ + .Components = undefined, + .Entity2Array = std.AutoArrayHashMap(Entity, usize).init(std.heap.page_allocator), + .Array2Entity = std.AutoArrayHashMap(usize, Entity).init(std.heap.page_allocator), + .sizeM = 0 + }; + } + }; +} //Components @@ -62,5 +73,13 @@ pub const ComponentArray = struct { pub const Transform = struct { pos: rl.Vector2, rot: f16, - scale: rl.Vector2, }; + +//Systems + +pub const Systems = struct { + const Self = @This(); + pub fn Gravity() void{ + + } +}; \ No newline at end of file diff --git a/src/main.zig b/src/main.zig index fbd042a..03b245f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,6 +1,7 @@ const rl = @import("raylib"); const std = @import("std"); const EnS = @import("EntitySystem.zig"); +const Etyp = @import("EntitySystemTypes.zig"); pub fn main() anyerror!void { const screenWidth = 1280; @@ -8,21 +9,34 @@ pub fn main() anyerror!void { rl.initWindow(screenWidth, screenHeight, "AHHHH"); defer rl.closeWindow(); + var alloc = std.heap.ArenaAllocator.init(std.heap.page_allocator); + var ptr = try alloc.allocator().create(Etyp.Transform); + ptr.* = Etyp.Transform{.pos = rl.Vector2.init(0, 0), .rot = 0}; //rl.setTargetFPS(60); + try EnS.Init(); + try EnS.RegisterComponent(Etyp.Transform); + + + + var player = try EnS.CreateEntity(); + EnS.AddComponent(player, Etyp.Transform{.pos = rl.Vector2.init(0, 0), .rot = 0}); + while (!rl.windowShouldClose()) { - try update(); - try draw(); + Etyp.Systems.Gravity(); + + + + rl.beginDrawing(); + defer rl.endDrawing(); + var pos = EnS.GetComponent(player, Etyp.Transform).pos; + + rl.drawRectangle(pos.x, pos.y, 5, 5, rl.Color.gold); + + rl.clearBackground(rl.Color.black); + rl.drawFPS(10, 10); } } -pub fn update() !void{ -} -pub fn draw() !void{ - rl.beginDrawing(); - defer rl.endDrawing(); - rl.clearBackground(rl.Color.black); - rl.drawFPS(10, 10); -}