working on the entity systemw

master
Red 10 months ago
parent c5ca08a610
commit 380dc7d68e

@ -3,59 +3,127 @@ const Etyp = @import("EntitySystemTypes.zig");
//Variables Entity //Variables Entity
const EntityIDs: std.TailQueue(u32) = init: { var EntityIDs: std.ArrayList(usize) = undefined;
var temp = std.TailQueue(u32);
var i = 0;
while (i < Etyp.MAXEntitys) {
temp.append(i);
i += 1;
}
break :init temp;
};
var EntityAmount: u32 = 0; var EntityAmount: u32 = 0;
const Signatures: [Etyp.MAXEntitys]Etyp.Signature = undefined; var Signatures: std.ArrayList(Etyp.Signature) = undefined;
//Entity System //Entity System
pub fn CreateEntity() !Etyp.entity { fn CreateEntitysub() !Etyp.Entity {
if (EntityAmount >= Etyp.MAXEntitys) { if (EntityAmount >= Etyp.MAXEntitys) {
return error{}; return error.GenericError;
} }
EntityAmount += 1; EntityAmount += 1;
return EntityIDs.popFirst(); return EntityIDs.pop();
} }
pub fn DestroyEntity(entity: Etyp.entity) void { fn DestroyEntitysub(entity: Etyp.Entity) void {
Signatures[entity] = null; Signatures.items[entity] = null;
EntityIDs.append(entity); EntityIDs.append(entity);
EntityAmount -= 1; EntityAmount -= 1;
} }
pub fn SetSignature(entity: Etyp.entity, signature: Etyp.Signature) void { fn SetSignature(entity: Etyp.Entity, signature: Etyp.Signature) void {
Signatures[entity] = signature; Signatures.items[entity] = signature;
} }
pub fn GetSignature(entity: Etyp.entity) Etyp.signature { fn GetSignature(entity: Etyp.Entity) Etyp.Signature {
return Signatures[entity]; return Signatures.items[entity];
} }
//Component Manager variables //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; var NextComponent: Etyp.Component = 0;
//component manager //component manager
pub fn RegisterComponent(in: type) void { fn RegisterComponentsub(comptime in: anytype) !void {
var name = @typeName(in); var name = @typeName(in);
ComponentTypeList.put(name, NextComponent); try ComponentTypeList.put(name, NextComponent);
ComponentArrays.put(name, ComponentArrays{}); 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);
} }
fn GetComponentArray(in: type) Etyp.ComponentArray { pub fn RemoveComponent(Entity: Etyp.Entity, comptime in: anytype) void{
return ComponentArrays[@typeName(in)]; 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;
} }

@ -16,35 +16,37 @@ pub const Signature = std.bit_set.IntegerBitSet(MAXComponents);
//Component array type //Component array type
pub const ComponentArray = struct { pub fn ComponentArray (comptime T: type) type {
Components: [MAXEntitys]type, return struct {
Entity2Array: [usize]Entity, Components: [MAXEntitys]T,
Array2Entity: [Entity]usize, Entity2Array: std.AutoArrayHashMap(Entity, usize),
Array2Entity: std.AutoArrayHashMap(usize, Entity),
sizeM: usize, sizeM: usize,
const Self = @This();
pub fn AddComponent(self: ComponentArray, entity: Entity, comp: type) void { pub fn AddComponent(entity: Entity, comp: T) void {
var newidx: usize = self.sizeM; var newidx: usize = Self.sizeM;
self.Entity2Array[entity] = newidx; Self.Entity2Array[entity] = newidx;
self.Entity2Array[newidx] = entity; Self.Entity2Array[newidx] = entity;
self.Components[newidx] = comp; Self.Components[newidx] = comp;
self.sizeM += 1; Self.sizeM += 1;
} }
pub fn Removedata(self: ComponentArray, entity: Entity) void { pub fn Removedata(entity: Entity) void {
var remidx: usize = self.Entity2Array[entity]; var remidx: usize = Self.Entity2Array[entity];
var lastidx: usize = self.sizeM - 1; var lastidx: usize = Self.sizeM - 1;
self.Components[remidx] = self.Components[lastidx]; Self.Components[remidx] = Self.Components[lastidx];
var lastentity: entity = self.Array2Entity[lastidx]; var lastentity: entity = Self.Array2Entity[lastidx];
self.Entity2Array[lastentity] = remidx; Self.Entity2Array[lastentity] = remidx;
self.Array2Entity[remidx] = lastentity; Self.Array2Entity[remidx] = lastentity;
self.Entity2Array[entity] = null; Self.Entity2Array[entity] = null;
self.Array2Entity[lastentity] = null; Self.Array2Entity[lastentity] = null;
self.sizeM -= 1; Self.sizeM -= 1;
} }
pub fn GetData(self: ComponentArray, entity: Entity) type { pub fn GetData(entity: Entity) type {
return self.Components[self.Entity2Array[entity]]; return Self.Components[Self.Entity2Array[entity]];
} }
pub fn EntityDestroyed(self: ComponentArray, entity: Entity) void { pub fn EntityDestroyed(self: ComponentArray, entity: Entity) void {
for(self.Array2Entity) |val|{ for(self.Array2Entity) |val|{
@ -54,7 +56,16 @@ pub const ComponentArray = struct {
} }
} }
} }
}; 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 //Components
@ -62,5 +73,13 @@ pub const ComponentArray = struct {
pub const Transform = struct { pub const Transform = struct {
pos: rl.Vector2, pos: rl.Vector2,
rot: f16, rot: f16,
scale: rl.Vector2, };
//Systems
pub const Systems = struct {
const Self = @This();
pub fn Gravity() void{
}
}; };

@ -1,6 +1,7 @@
const rl = @import("raylib"); const rl = @import("raylib");
const std = @import("std"); const std = @import("std");
const EnS = @import("EntitySystem.zig"); const EnS = @import("EntitySystem.zig");
const Etyp = @import("EntitySystemTypes.zig");
pub fn main() anyerror!void { pub fn main() anyerror!void {
const screenWidth = 1280; const screenWidth = 1280;
@ -8,21 +9,34 @@ pub fn main() anyerror!void {
rl.initWindow(screenWidth, screenHeight, "AHHHH"); rl.initWindow(screenWidth, screenHeight, "AHHHH");
defer rl.closeWindow(); 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); //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()) { while (!rl.windowShouldClose()) {
try update(); Etyp.Systems.Gravity();
try draw();
}
}
pub fn update() !void{
}
pub fn draw() !void{
rl.beginDrawing(); rl.beginDrawing();
defer rl.endDrawing(); 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.clearBackground(rl.Color.black);
rl.drawFPS(10, 10); rl.drawFPS(10, 10);
}
} }

Loading…
Cancel
Save