From 4fff81f82d8c5ef82c0d246d962517c368566eca Mon Sep 17 00:00:00 2001 From: RedStealthDev Date: Sat, 10 Feb 2024 18:48:33 +0100 Subject: [PATCH] started work on a ECS --- .gitignore | 18 ++++++++++ build.zig | 38 ++++++++++++++++++++ build.zig.zon | 11 ++++++ raylib-zig | 1 + src/EntitySystem.zig | 71 ++++++++++++++++++++++++++++++++++++++ src/EntitySystemTypes.zig | 23 ++++++++++++ src/Images/Wolf.png | Bin 0 -> 258 bytes src/main.zig | 30 ++++++++++++++++ 8 files changed, 192 insertions(+) create mode 100644 .gitignore create mode 100644 build.zig create mode 100644 build.zig.zon create mode 160000 raylib-zig create mode 100644 src/EntitySystem.zig create mode 100644 src/EntitySystemTypes.zig create mode 100644 src/Images/Wolf.png create mode 100644 src/main.zig diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0b4957c --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# This file is for zig-specific build artifacts. +# If you have OS-specific or editor-specific files to ignore, +# such as *.swp or .DS_Store, put those in your global +# ~/.gitignore and put this in your ~/.gitconfig: +# +# [core] +# excludesfile = ~/.gitignore +# +# Cheers! +# -andrewrk + +zig-cache/ +zig-out/ +/release/ +/debug/ +/build/ +/build-*/ +/docgen_tmp/ \ No newline at end of file diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..a82b735 --- /dev/null +++ b/build.zig @@ -0,0 +1,38 @@ +const std = @import("std"); +const rl = @import("raylib-zig/build.zig"); + +pub fn build(b: *std.Build) !void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + var raylib = rl.getModule(b, "raylib-zig"); + var raylib_math = rl.math.getModule(b, "raylib-zig"); + //web exports are completely separate + if (target.getOsTag() == .emscripten) { + const exe_lib = rl.compileForEmscripten(b, "zig-raylib-game-test", "src/main.zig", target, optimize); + exe_lib.addModule("raylib", raylib); + exe_lib.addModule("raylib-math", raylib_math); + const raylib_artifact = rl.getRaylib(b, target, optimize); + // Note that raylib itself is not actually added to the exe_lib output file, so it also needs to be linked with emscripten. + exe_lib.linkLibrary(raylib_artifact); + const link_step = try rl.linkWithEmscripten(b, &[_]*std.Build.Step.Compile{ exe_lib, raylib_artifact }); + b.getInstallStep().dependOn(&link_step.step); + const run_step = try rl.emscriptenRunStep(b); + run_step.step.dependOn(&link_step.step); + const run_option = b.step("run", "Run zig-raylib-game-test"); + run_option.dependOn(&run_step.step); + return; + } + + const exe = b.addExecutable(.{ .name = "zig-raylib-game-test", .root_source_file = .{ .path = "src/main.zig" }, .optimize = optimize, .target = target }); + + rl.link(b, exe, target, optimize); + exe.addModule("raylib", raylib); + exe.addModule("raylib-math", raylib_math); + + const run_cmd = b.addRunArtifact(exe); + const run_step = b.step("run", "Run zig-raylib-game-test"); + run_step.dependOn(&run_cmd.step); + + b.installArtifact(exe); +} + diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..b641ee2 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,11 @@ +.{ + .name = "raylib-zig", + .version = "0.1.1", + .dependencies = .{ + .raylib = .{ + .url = "https://github.com/raysan5/raylib/archive/5.0.tar.gz", + .hash = "1220c28847ca8e8756734ae84355802b764c9d9cf4de057dbc6fc2b15c56e726f27b", + }, + }, + .paths = .{""}, +} diff --git a/raylib-zig b/raylib-zig new file mode 160000 index 0000000..7f73ddf --- /dev/null +++ b/raylib-zig @@ -0,0 +1 @@ +Subproject commit 7f73ddfa720b7fbda680b2846ba3d7f0ec9f8741 diff --git a/src/EntitySystem.zig b/src/EntitySystem.zig new file mode 100644 index 0000000..7ae12d7 --- /dev/null +++ b/src/EntitySystem.zig @@ -0,0 +1,71 @@ +const std = @import("std"); +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 EntityAmount: u32 = 0; + +const Signatures: [Etyp.MAXEntitys]Etyp.Signature = undefined; + +//Entity System + +pub fn CreateEntity() !Etyp.entity { + if(EntityAmount >= Etyp.MAXEntitys) + { + return error{}; + } + EntityAmount += 1; + return EntityIDs.popFirst(); +} + +pub fn DestroyEntity(entity: Etyp.entity) void { + Signatures[entity] = null; + EntityIDs.append(entity); + EntityAmount -= 1; +} + +pub fn SetSignature(entity: Etyp.entity, signature: Etyp.Signature) void { + Signatures[entity] = signature; +} + +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, + + 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 diff --git a/src/EntitySystemTypes.zig b/src/EntitySystemTypes.zig new file mode 100644 index 0000000..62c6484 --- /dev/null +++ b/src/EntitySystemTypes.zig @@ -0,0 +1,23 @@ +const std = @import("std"); +const rl = @import("raylib"); + +//Max Vars + +pub const MAXEntitys: u32 = 1000; + +pub const MAXComponents: u32 = 32; + +//Id Types +pub const entity = usize; + +pub const component = usize; + +pub const Signature = std.bit_set.IntegerBitSet(MAXComponents); + +//Components + +pub const Transform = struct { + pos: rl.Vector2, + rot: f16, + scale: rl.Vector2, +}; \ No newline at end of file diff --git a/src/Images/Wolf.png b/src/Images/Wolf.png new file mode 100644 index 0000000000000000000000000000000000000000..07f1163602c168c86fdc2f1e10135433a80d7508 GIT binary patch literal 258 zcmeAS@N?(olHy`uVBq!ia0vp^3P3Et!3HGD8EPYe6k~CayA#8@b22Z1oJF24jv*eM zZzmh_H!BFZ&G%J*@VrdXlxOwTns