all: use new mach.Entity.Mod query API
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
541ce9e7c0
commit
bd655828e3
13 changed files with 213 additions and 265 deletions
|
|
@ -157,27 +157,29 @@ fn tick(
|
|||
|
||||
// Query all the entities that have the .follower tag indicating they should follow the player.
|
||||
// TODO(important): better querying API
|
||||
var archetypes_iter = core.__entities.queryDeprecated(.{ .all = &.{
|
||||
.{ .app = &.{.follower} },
|
||||
} });
|
||||
while (archetypes_iter.next()) |archetype| {
|
||||
// Iterate the ID and position of each entity
|
||||
const ids = archetype.slice(.entities, .id);
|
||||
const positions = archetype.slice(.renderer, .position);
|
||||
for (ids, positions) |id, position| {
|
||||
|
||||
// Iterate the ID and position of each follower entity
|
||||
var q = try entities.query(.{
|
||||
.ids = mach.Entities.Mod.read(.id),
|
||||
.followers = Mod.read(.follower),
|
||||
.positions = Renderer.Mod.read(.position),
|
||||
});
|
||||
while (q.next()) |v| {
|
||||
for (v.ids, v.positions) |id, position| {
|
||||
// Nested query to find all the other follower entities that we should move away from.
|
||||
// We will avoid all other follower entities if we're too close to them.
|
||||
// This is not very efficient, but it works!
|
||||
const close_dist = 1.0 / 15.0;
|
||||
var avoidance = Vec3.splat(0);
|
||||
var avoidance_div: f32 = 1.0;
|
||||
var archetypes_iter_2 = core.__entities.queryDeprecated(.{ .all = &.{
|
||||
.{ .app = &.{.follower} },
|
||||
} });
|
||||
while (archetypes_iter_2.next()) |archetype_2| {
|
||||
const other_ids = archetype_2.slice(.entities, .id);
|
||||
const other_positions = archetype_2.slice(.renderer, .position);
|
||||
for (other_ids, other_positions) |other_id, other_position| {
|
||||
|
||||
var q2 = try entities.query(.{
|
||||
.ids = mach.Entities.Mod.read(.id),
|
||||
.followers = Mod.read(.follower),
|
||||
.positions = Renderer.Mod.read(.position),
|
||||
});
|
||||
while (q2.next()) |v2| {
|
||||
for (v2.ids, v2.positions) |other_id, other_position| {
|
||||
if (id == other_id) continue;
|
||||
if (position.dist(&other_position) < close_dist) {
|
||||
avoidance = avoidance.sub(&position.dir(&other_position, 0.0000001));
|
||||
|
|
|
|||
|
|
@ -124,6 +124,7 @@ fn deinit(
|
|||
}
|
||||
|
||||
fn renderFrame(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
renderer: *Mod,
|
||||
) !void {
|
||||
|
|
@ -138,17 +139,13 @@ fn renderFrame(
|
|||
defer encoder.release();
|
||||
|
||||
// Update uniform buffer
|
||||
var archetypes_iter = core.__entities.queryDeprecated(.{ .all = &.{
|
||||
.{ .renderer = &.{ .position, .scale } },
|
||||
} });
|
||||
var num_entities: usize = 0;
|
||||
while (archetypes_iter.next()) |archetype| {
|
||||
const ids = archetype.slice(.entities, .id);
|
||||
const positions = archetype.slice(.renderer, .position);
|
||||
const scales = archetype.slice(.renderer, .scale);
|
||||
for (ids, positions, scales) |id, position, scale| {
|
||||
_ = id;
|
||||
|
||||
var q = try entities.query(.{
|
||||
.positions = Mod.read(.position),
|
||||
.scales = Mod.read(.scale),
|
||||
});
|
||||
while (q.next()) |v| {
|
||||
for (v.positions, v.scales) |position, scale| {
|
||||
const ubo = UniformBufferObject{
|
||||
.offset = position,
|
||||
.scale = scale,
|
||||
|
|
|
|||
|
|
@ -165,18 +165,17 @@ fn tick(
|
|||
const delta_time = game.state().timer.lap();
|
||||
|
||||
// Animate entities
|
||||
var archetypes_iter = core.__entities.queryDeprecated(.{ .all = &.{
|
||||
.{ .mach_gfx_sprite = &.{.transform} },
|
||||
} });
|
||||
while (archetypes_iter.next()) |archetype| {
|
||||
const ids = archetype.slice(.entities, .id);
|
||||
const transforms = archetype.slice(.mach_gfx_sprite, .transform);
|
||||
for (ids, transforms) |id, *old_transform| {
|
||||
var location = old_transform.translation();
|
||||
var q = try entities.query(.{
|
||||
.ids = mach.Entities.Mod.read(.id),
|
||||
.transforms = gfx.Sprite.Mod.write(.transform),
|
||||
});
|
||||
while (q.next()) |v| {
|
||||
for (v.ids, v.transforms) |id, *entity_transform| {
|
||||
var location = entity_transform.translation();
|
||||
// TODO: formatting
|
||||
// TODO(Core)
|
||||
if (location.x() < -@as(f32, @floatFromInt(mach.core.size().width)) / 1.5 or location.x() > @as(f32, @floatFromInt(mach.core.size().width)) / 1.5 or location.y() < -@as(f32, @floatFromInt(mach.core.size().height)) / 1.5 or location.y() > @as(f32, @floatFromInt(mach.core.size().height)) / 1.5) {
|
||||
try core.__entities.remove(id);
|
||||
try entities.remove(id);
|
||||
game.state().sprites -= 1;
|
||||
continue;
|
||||
}
|
||||
|
|
@ -186,10 +185,7 @@ fn tick(
|
|||
transform = transform.mul(&Mat4x4.translate(location));
|
||||
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * game.state().time));
|
||||
transform = transform.mul(&Mat4x4.scale(Vec3.splat(@max(math.cos(game.state().time / 2.0), 0.2))));
|
||||
|
||||
// TODO: .set() API is substantially slower due to internals
|
||||
// try sprite.set(id, .transform, transform);
|
||||
old_transform.* = transform;
|
||||
entity_transform.* = transform;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -70,14 +70,12 @@ fn audioStateChange(
|
|||
app: *Mod,
|
||||
) !void {
|
||||
// Find audio entities that are no longer playing
|
||||
var archetypes_iter = audio.__entities.queryDeprecated(.{ .all = &.{
|
||||
.{ .mach_audio = &.{.playing} },
|
||||
} });
|
||||
while (archetypes_iter.next()) |archetype| {
|
||||
for (
|
||||
archetype.slice(.entities, .id),
|
||||
archetype.slice(.mach_audio, .playing),
|
||||
) |id, playing| {
|
||||
var q = try entities.query(.{
|
||||
.ids = mach.Entities.Mod.read(.id),
|
||||
.playings = mach.Audio.Mod.read(.playing),
|
||||
});
|
||||
while (q.next()) |v| {
|
||||
for (v.ids, v.playings) |id, playing| {
|
||||
if (playing) continue;
|
||||
|
||||
if (app.get(id, .play_after)) |frequency| {
|
||||
|
|
|
|||
|
|
@ -85,12 +85,12 @@ fn audioStateChange(
|
|||
app: *Mod,
|
||||
) !void {
|
||||
// Find audio entities that are no longer playing
|
||||
var archetypes_iter = audio.__entities.queryDeprecated(.{ .all = &.{.{ .mach_audio = &.{.playing} }} });
|
||||
while (archetypes_iter.next()) |archetype| {
|
||||
for (
|
||||
archetype.slice(.entities, .id),
|
||||
archetype.slice(.mach_audio, .playing),
|
||||
) |id, playing| {
|
||||
var q = try entities.query(.{
|
||||
.ids = mach.Entities.Mod.read(.id),
|
||||
.playings = mach.Audio.Mod.read(.playing),
|
||||
});
|
||||
while (q.next()) |v| {
|
||||
for (v.ids, v.playings) |id, playing| {
|
||||
if (playing) continue;
|
||||
|
||||
if (app.get(id, .is_bgm)) |_| {
|
||||
|
|
|
|||
|
|
@ -158,26 +158,20 @@ fn tick(
|
|||
const delta_time = game.state().timer.lap();
|
||||
|
||||
// Rotate entities
|
||||
var archetypes_iter = core.__entities.queryDeprecated(.{ .all = &.{
|
||||
.{ .mach_gfx_sprite = &.{.transform} },
|
||||
} });
|
||||
while (archetypes_iter.next()) |archetype| {
|
||||
const ids = archetype.slice(.entities, .id);
|
||||
const transforms = archetype.slice(.mach_gfx_sprite, .transform);
|
||||
for (ids, transforms) |id, *old_transform| {
|
||||
_ = id;
|
||||
const location = old_transform.*.translation();
|
||||
// var transform = old_transform.mul(&Mat4x4.translate(-location));
|
||||
var q = try entities.query(.{
|
||||
.transforms = gfx.Sprite.Mod.write(.transform),
|
||||
});
|
||||
while (q.next()) |v| {
|
||||
for (v.transforms) |*entity_transform| {
|
||||
const location = entity_transform.*.translation();
|
||||
// var transform = entity_transform.mul(&Mat4x4.translate(-location));
|
||||
// transform = mat.rotateZ(0.3 * delta_time).mul(&transform);
|
||||
// transform = transform.mul(&Mat4x4.translate(location));
|
||||
var transform = Mat4x4.ident;
|
||||
transform = transform.mul(&Mat4x4.translate(location));
|
||||
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * game.state().time));
|
||||
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(game.state().time / 2.0), 0.5)));
|
||||
|
||||
// TODO: .set() API is substantially slower due to internals
|
||||
// try sprite.set(id, .transform, transform);
|
||||
old_transform.* = transform;
|
||||
entity_transform.* = transform;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -201,15 +201,12 @@ fn tick(
|
|||
const delta_time = game.state().timer.lap();
|
||||
|
||||
// Rotate entities
|
||||
var archetypes_iter = core.__entities.queryDeprecated(.{ .all = &.{
|
||||
.{ .mach_gfx_text = &.{.transform} },
|
||||
} });
|
||||
while (archetypes_iter.next()) |archetype| {
|
||||
const ids = archetype.slice(.entities, .id);
|
||||
const transforms = archetype.slice(.mach_gfx_text, .transform);
|
||||
for (ids, transforms) |id, *old_transform| {
|
||||
_ = id;
|
||||
const location = old_transform.*.translation();
|
||||
var q = try entities.query(.{
|
||||
.transforms = gfx.Text.Mod.write(.transform),
|
||||
});
|
||||
while (q.next()) |v| {
|
||||
for (v.transforms) |*entity_transform| {
|
||||
const location = entity_transform.*.translation();
|
||||
// var transform = old_transform.mul(&Mat4x4.translate(-location));
|
||||
// transform = mat.rotateZ(0.3 * delta_time).mul(&transform);
|
||||
// transform = transform.mul(&Mat4x4.translate(location));
|
||||
|
|
@ -217,10 +214,7 @@ fn tick(
|
|||
transform = transform.mul(&Mat4x4.translate(location));
|
||||
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * game.state().time));
|
||||
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(game.state().time / 2.0), 0.5)));
|
||||
|
||||
// TODO: .set() API is substantially slower due to internals
|
||||
// try text.set(id, .transform, transform);
|
||||
old_transform.* = transform;
|
||||
entity_transform.* = transform;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -268,7 +262,11 @@ fn tick(
|
|||
game.state().time += delta_time;
|
||||
}
|
||||
|
||||
fn endFrame(game: *Mod, text: *gfx.Text.Mod, core: *mach.Core.Mod) !void {
|
||||
fn endFrame(
|
||||
entities: *mach.Entities.Mod,
|
||||
game: *Mod,
|
||||
core: *mach.Core.Mod,
|
||||
) !void {
|
||||
// Finish render pass
|
||||
game.state().frame_render_pass.end();
|
||||
const label = @tagName(name) ++ ".tick";
|
||||
|
|
@ -285,14 +283,11 @@ fn endFrame(game: *Mod, text: *gfx.Text.Mod, core: *mach.Core.Mod) !void {
|
|||
// Gather some text rendering stats
|
||||
var num_texts: u32 = 0;
|
||||
var num_glyphs: usize = 0;
|
||||
var archetypes_iter = text.__entities.queryDeprecated(.{ .all = &.{
|
||||
.{ .mach_gfx_text = &.{
|
||||
.built,
|
||||
} },
|
||||
} });
|
||||
while (archetypes_iter.next()) |archetype| {
|
||||
const builts = archetype.slice(.mach_gfx_text, .built);
|
||||
for (builts) |built| {
|
||||
var q = try entities.query(.{
|
||||
.built_pipelines = gfx.Text.Mod.read(.built),
|
||||
});
|
||||
while (q.next()) |v| {
|
||||
for (v.built_pipelines) |built| {
|
||||
num_texts += 1;
|
||||
num_glyphs += built.glyphs.items.len;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue