examples: add initial ecs-app

This is a temporary application to begin iterating on high-level ECS applications.

Eventually, this will be removed - for now it's just here so we can see how this API
looks today and improve it.

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-07-04 22:19:35 -07:00 committed by Stephen Gutekanst
parent 6ec27861b4
commit d3b03901fb
4 changed files with 96 additions and 1 deletions

54
examples/ecs-app/main.zig Normal file
View file

@ -0,0 +1,54 @@
// Experimental ECS app example. Not yet ready for actual use.
const std = @import("std");
const mach = @import("mach");
const gpu = mach.gpu;
const ecs = mach.ecs;
// TODO: rename *ecs.World to *engine.Engine or something
const renderer = @import("renderer.zig");
const physics2d = @import("physics2d.zig");
// Define all the modules in our application. Modules can have components, systems, state,
// and/or global values in them. They can also send and receive messages to coordinate
// with each-other.
//
// Single-word module names (`.mach`, `.renderer`, etc.) are reserved for the application itself.
//
// Modules that come from libraries must be prefixed (e.g. `.bullet_physics`, `.ziglibs_box2d`)
// similar to GitHub repositories, to avoid conflicts with one another. Note that modules themselves
// will interact with the ECS using e.g. `.getComponent(.bullet_physics, .location)` internally and
// so cannot be renamed here.
// TODO: just make this a list so one cannot even think renaming is possible here
const modules = ecs.Modules(.{
.mach = mach.module,
.renderer = renderer.module,
.physics2d = physics2d.module,
});
// Our Mach app, which tells Mach where to find our modules and init entry point.
pub const App = mach.App(modules, init);
pub fn init(engine: *ecs.World(modules)) !void {
// The Mach .core is where we set window options, etc.
const core = engine.get(.mach, .core);
try core.setOptions(.{ .title = "Hello, ECS!" });
// We can get the GPU device:
const device = engine.get(.mach, .device);
_ = device; // TODO: actually show off using the GPU device
// We can create entities, and set components on them. Note that components live in a module
// namespace, so we set the `.renderer, .location` component which is different than the
// `.physics2d, .location` component.
// TODO: cut out the `.entities.` in this API to make it more brief
const player = try engine.entities.new();
try engine.entities.setComponent(player, .renderer, .location, .{ .x = 0, .y = 0, .z = 0 });
try engine.entities.setComponent(player, .physics2d, .location, .{ .x = 0, .y = 0 });
_ = player;
// TODO: there could be an entities wrapper to interact with a single namespace so you don't
// have to pass it in as a parameter always?
}

View file

@ -0,0 +1,26 @@
const mach = @import("mach");
const ecs = mach.ecs;
const std = @import("std");
pub const Message = ecs.Messages(.{
.tick = void,
});
pub const module = ecs.Module(.{
.components = .{
.location = Vec2,
.rotation = Vec2,
.velocity = Vec2,
},
.messages = Message,
.update = update,
});
pub const Vec2 = struct { x: f32, y: f32 };
fn update(msg: Message) void {
switch (msg) {
// TODO: implement queries, ability to set components, etc.
.tick => std.debug.print("\nphysics tick!\n", .{}),
}
}

View file

@ -0,0 +1,13 @@
const mach = @import("mach");
const ecs = mach.ecs;
pub const module = ecs.Module(.{
.components = .{
.location = Vec3,
.rotation = Vec3,
},
// TODO: there would be systems that we register here. Functions that iterate over entities
// with renderer components like `.geometry` and render them for example!
});
pub const Vec3 = struct { x: f32, y: f32, z: f32 };