From b93bc32d446c980be0e30fe5171de25b5180d727 Mon Sep 17 00:00:00 2001 From: Red Date: Fri, 16 Feb 2024 12:06:18 +0100 Subject: [PATCH] moved component array --- src/EntitySystem.zig | 36 +++++----------------------- src/EntitySystemTypes.zig | 49 ++++++++++++++++++++++++++++++++++++--- src/main.zig | 2 -- 3 files changed, 52 insertions(+), 35 deletions(-) diff --git a/src/EntitySystem.zig b/src/EntitySystem.zig index 7ae12d7..c26e81c 100644 --- a/src/EntitySystem.zig +++ b/src/EntitySystem.zig @@ -6,7 +6,7 @@ const Etyp = @import("EntitySystemTypes.zig"); const EntityIDs: std.TailQueue(u32) = init: { var temp = std.TailQueue(u32); var i = 0; - while(i < Etyp.MAXEntitys){ + while (i < Etyp.MAXEntitys) { temp.append(i); i += 1; } @@ -19,8 +19,7 @@ const Signatures: [Etyp.MAXEntitys]Etyp.Signature = undefined; //Entity System pub fn CreateEntity() !Etyp.entity { - if(EntityAmount >= Etyp.MAXEntitys) - { + if (EntityAmount >= Etyp.MAXEntitys) { return error{}; } EntityAmount += 1; @@ -41,31 +40,8 @@ pub fn GetSignature(entity: Etyp.entity) Etyp.signature { return Signatures[entity]; } -const ComponentArray = struct { - Components: [Etyp.MAXEntitys]type, - Entity2Array: [usize]EntityIDs, - Array2Entity: [EntityIDs]usize, - sizeM: usize, +//Component Manager - pub fn AddComponent(self: ComponentArray, entity: Etyp.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: Etyp.entity) void{ - var remidx: usize = self.Entity2Array[entity]; - var lastidx: usize = self.sizeM-1; - self.Components[remidx] = self.Components[lastidx]; - - var lastentity: Etyp.entity = self.Array2Entity[lastidx]; - self.Entity2Array[lastentity] = remidx; - self.Array2Entity[remidx] = lastentity; - - self.Entity2Array[entity] = null; - self.Array2Entity[lastentity] = null; - - self.sizeM -= 1; - } -}; \ No newline at end of file +pub fn RegisterComponent() void { + +} \ No newline at end of file diff --git a/src/EntitySystemTypes.zig b/src/EntitySystemTypes.zig index 62c6484..a212abf 100644 --- a/src/EntitySystemTypes.zig +++ b/src/EntitySystemTypes.zig @@ -8,16 +8,59 @@ pub const MAXEntitys: u32 = 1000; pub const MAXComponents: u32 = 32; //Id Types -pub const entity = usize; +pub const Entity = usize; -pub const component = usize; +pub const Component = usize; pub const Signature = std.bit_set.IntegerBitSet(MAXComponents); +//Component array type + +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]; + + var lastentity: entity = self.Array2Entity[lastidx]; + self.Entity2Array[lastentity] = remidx; + self.Array2Entity[remidx] = lastentity; + + self.Entity2Array[entity] = null; + self.Array2Entity[lastentity] = null; + + 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; + } + } + } +}; + + //Components pub const Transform = struct { pos: rl.Vector2, rot: f16, scale: rl.Vector2, -}; \ No newline at end of file +}; diff --git a/src/main.zig b/src/main.zig index 612cfc9..fbd042a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -12,14 +12,12 @@ pub fn main() anyerror!void { //rl.setTargetFPS(60); - while (!rl.windowShouldClose()) { try update(); try draw(); } } pub fn update() !void{ - } pub fn draw() !void{ rl.beginDrawing();