module: injected mach.Entity.Mod for global entity operations
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
cb6bdd7eca
commit
65e2168b9f
11 changed files with 94 additions and 56 deletions
|
|
@ -48,6 +48,7 @@ fn init(
|
|||
// These are injected dependencies - as long as these modules were registered in the top-level
|
||||
// of the program we can have these types injected here, letting us work with other modules in
|
||||
// our program seamlessly and with a type-safe API:
|
||||
entity: *mach.Entity.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
renderer: *Renderer.Mod,
|
||||
game: *Mod,
|
||||
|
|
@ -55,7 +56,7 @@ fn init(
|
|||
renderer.send(.init, .{});
|
||||
|
||||
// Create our player entity.
|
||||
const player = try core.newEntity();
|
||||
const player = try entity.new();
|
||||
|
||||
// Give our player entity a .renderer.position and .renderer.scale component. Note that these
|
||||
// are defined by the Renderer module, so we use `renderer: *Renderer.Mod` to interact with
|
||||
|
|
@ -81,6 +82,7 @@ fn init(
|
|||
|
||||
// TODO(important): remove need for returning an error here
|
||||
fn tick(
|
||||
entity: *mach.Entity.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
renderer: *Renderer.Mod,
|
||||
game: *Mod,
|
||||
|
|
@ -134,7 +136,7 @@ fn tick(
|
|||
_ = game.state().spawn_timer.lap(); // Reset the timer
|
||||
for (0..5) |_| {
|
||||
// Spawn a new entity at the same position as the player, but smaller in scale.
|
||||
const new_entity = try core.newEntity();
|
||||
const new_entity = try entity.new();
|
||||
try renderer.set(new_entity, .position, player_pos);
|
||||
try renderer.set(new_entity, .scale, 1.0 / 6.0);
|
||||
|
||||
|
|
|
|||
|
|
@ -60,6 +60,7 @@ fn init(core: *mach.Core.Mod, sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs:
|
|||
}
|
||||
|
||||
fn afterInit(
|
||||
entity: *mach.Entity.Mod,
|
||||
sprite: *gfx.Sprite.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
glyphs: *Glyphs.Mod,
|
||||
|
|
@ -67,7 +68,7 @@ fn afterInit(
|
|||
) !void {
|
||||
// Create a sprite rendering pipeline
|
||||
const texture = glyphs.state().texture;
|
||||
const pipeline = try sprite_pipeline.newEntity();
|
||||
const pipeline = try entity.new();
|
||||
try sprite_pipeline.set(pipeline, .texture, texture);
|
||||
sprite_pipeline.send(.update, .{});
|
||||
|
||||
|
|
@ -76,7 +77,7 @@ fn afterInit(
|
|||
// type than the `.physics2d` module's `.location` component if you desire.
|
||||
|
||||
const r = glyphs.state().regions.get('?').?;
|
||||
const player = try sprite.newEntity();
|
||||
const player = try entity.new();
|
||||
try sprite.set(player, .transform, Mat4x4.translate(vec3(-0.02, 0, 0)));
|
||||
try sprite.set(player, .pipeline, pipeline);
|
||||
try sprite.set(player, .size, vec2(@floatFromInt(r.width), @floatFromInt(r.height)));
|
||||
|
|
@ -97,6 +98,7 @@ fn afterInit(
|
|||
}
|
||||
|
||||
fn tick(
|
||||
entity: *mach.Entity.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
sprite: *gfx.Sprite.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
|
|
@ -150,7 +152,7 @@ fn tick(
|
|||
const rand_index = game.state().rand.random().intRangeAtMost(usize, 0, glyphs.state().regions.count() - 1);
|
||||
const r = glyphs.state().regions.entries.get(rand_index).value;
|
||||
|
||||
const new_entity = try core.newEntity();
|
||||
const new_entity = try entity.new();
|
||||
try sprite.set(new_entity, .transform, Mat4x4.translate(new_pos).mul(&Mat4x4.scaleScalar(0.3)));
|
||||
try sprite.set(new_entity, .size, vec2(@floatFromInt(r.width), @floatFromInt(r.height)));
|
||||
try sprite.set(new_entity, .uv_transform, Mat3x3.translate(vec2(@floatFromInt(r.x), @floatFromInt(r.y))));
|
||||
|
|
|
|||
|
|
@ -65,6 +65,7 @@ fn deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void {
|
|||
}
|
||||
|
||||
fn audioStateChange(
|
||||
entity: *mach.Entity.Mod,
|
||||
audio: *mach.Audio.Mod,
|
||||
app: *Mod,
|
||||
) !void {
|
||||
|
|
@ -81,20 +82,21 @@ fn audioStateChange(
|
|||
|
||||
if (app.get(id, .play_after)) |frequency| {
|
||||
// Play a new sound
|
||||
const entity = try audio.newEntity();
|
||||
try audio.set(entity, .samples, try fillTone(audio, frequency));
|
||||
try audio.set(entity, .channels, @intCast(audio.state().player.channels().len));
|
||||
try audio.set(entity, .playing, true);
|
||||
try audio.set(entity, .index, 0);
|
||||
const e = try entity.new();
|
||||
try audio.set(e, .samples, try fillTone(audio, frequency));
|
||||
try audio.set(e, .channels, @intCast(audio.state().player.channels().len));
|
||||
try audio.set(e, .playing, true);
|
||||
try audio.set(e, .index, 0);
|
||||
}
|
||||
|
||||
// Remove the entity for the old sound
|
||||
try audio.removeEntity(id);
|
||||
try entity.remove(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn tick(
|
||||
entity: *mach.Entity.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
audio: *mach.Audio.Mod,
|
||||
app: *Mod,
|
||||
|
|
@ -121,16 +123,16 @@ fn tick(
|
|||
// Piano keys
|
||||
else => {
|
||||
// Play a new sound
|
||||
const entity = try audio.newEntity();
|
||||
try audio.set(entity, .samples, try fillTone(audio, keyToFrequency(ev.key)));
|
||||
try audio.set(entity, .channels, @intCast(audio.state().player.channels().len));
|
||||
try audio.set(entity, .playing, true);
|
||||
try audio.set(entity, .index, 0);
|
||||
const e = try entity.new();
|
||||
try audio.set(e, .samples, try fillTone(audio, keyToFrequency(ev.key)));
|
||||
try audio.set(e, .channels, @intCast(audio.state().player.channels().len));
|
||||
try audio.set(e, .playing, true);
|
||||
try audio.set(e, .index, 0);
|
||||
|
||||
if (app.state().ghost_key_mode) {
|
||||
// After that sound plays, we'll chain on another sound that is one semi-tone higher.
|
||||
const one_semi_tone_higher = keyToFrequency(ev.key) * math.pow(f32, 2.0, (1.0 / 12.0));
|
||||
try app.set(entity, .play_after, one_semi_tone_higher);
|
||||
try app.set(e, .play_after, one_semi_tone_higher);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,12 @@ pub const components = .{
|
|||
|
||||
sfx: Opus,
|
||||
|
||||
fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) !void {
|
||||
fn init(
|
||||
entity: *mach.Entity.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
audio: *mach.Audio.Mod,
|
||||
app: *Mod,
|
||||
) !void {
|
||||
audio.send(.init, .{});
|
||||
app.send(.after_init, .{});
|
||||
|
||||
|
|
@ -48,7 +53,7 @@ fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) !void {
|
|||
// Initialize module state
|
||||
app.init(.{ .sfx = sfx });
|
||||
|
||||
const bgm_entity = try audio.newEntity();
|
||||
const bgm_entity = try entity.new();
|
||||
try app.set(bgm_entity, .is_bgm, {});
|
||||
try audio.set(bgm_entity, .samples, bgm.samples);
|
||||
try audio.set(bgm_entity, .channels, bgm.channels);
|
||||
|
|
@ -75,6 +80,7 @@ fn deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void {
|
|||
}
|
||||
|
||||
fn audioStateChange(
|
||||
entity: *mach.Entity.Mod,
|
||||
audio: *mach.Audio.Mod,
|
||||
app: *Mod,
|
||||
) !void {
|
||||
|
|
@ -93,13 +99,14 @@ fn audioStateChange(
|
|||
try audio.set(id, .playing, true);
|
||||
} else {
|
||||
// Remove the entity for the old sound
|
||||
try audio.removeEntity(id);
|
||||
try entity.remove(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn tick(
|
||||
entity: *mach.Entity.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
audio: *mach.Audio.Mod,
|
||||
app: *Mod,
|
||||
|
|
@ -121,11 +128,11 @@ fn tick(
|
|||
},
|
||||
else => {
|
||||
// Play a new SFX
|
||||
const entity = try audio.newEntity();
|
||||
try audio.set(entity, .samples, app.state().sfx.samples);
|
||||
try audio.set(entity, .channels, app.state().sfx.channels);
|
||||
try audio.set(entity, .index, 0);
|
||||
try audio.set(entity, .playing, true);
|
||||
const e = try entity.new();
|
||||
try audio.set(e, .samples, app.state().sfx.samples);
|
||||
try audio.set(e, .channels, app.state().sfx.channels);
|
||||
try audio.set(e, .index, 0);
|
||||
try audio.set(e, .playing, true);
|
||||
},
|
||||
},
|
||||
.close => core.send(.exit, .{}),
|
||||
|
|
|
|||
|
|
@ -52,6 +52,7 @@ fn deinit(
|
|||
}
|
||||
|
||||
fn init(
|
||||
entity: *mach.Entity.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
sprite: *gfx.Sprite.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
|
|
@ -65,12 +66,12 @@ fn init(
|
|||
|
||||
// Create a sprite rendering pipeline
|
||||
const allocator = gpa.allocator();
|
||||
const pipeline = try core.newEntity();
|
||||
const pipeline = try entity.new();
|
||||
try sprite_pipeline.set(pipeline, .texture, try loadTexture(core, allocator));
|
||||
sprite_pipeline.send(.update, .{});
|
||||
|
||||
// Create our player sprite
|
||||
const player = try core.newEntity();
|
||||
const player = try entity.new();
|
||||
try sprite.set(player, .transform, Mat4x4.translate(vec3(-0.02, 0, 0)));
|
||||
try sprite.set(player, .size, vec2(32, 32));
|
||||
try sprite.set(player, .uv_transform, Mat3x3.translate(vec2(0, 0)));
|
||||
|
|
@ -94,6 +95,7 @@ fn init(
|
|||
}
|
||||
|
||||
fn tick(
|
||||
entity: *mach.Entity.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
sprite: *gfx.Sprite.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
|
|
@ -143,7 +145,7 @@ fn tick(
|
|||
new_pos.v[0] += game.state().rand.random().floatNorm(f32) * 25;
|
||||
new_pos.v[1] += game.state().rand.random().floatNorm(f32) * 25;
|
||||
|
||||
const new_entity = try core.newEntity();
|
||||
const new_entity = try entity.new();
|
||||
try sprite.set(new_entity, .transform, Mat4x4.translate(new_pos).mul(&Mat4x4.scale(Vec3.splat(0.3))));
|
||||
try sprite.set(new_entity, .size, vec2(32, 32));
|
||||
try sprite.set(new_entity, .uv_transform, Mat3x3.translate(vec2(0, 0)));
|
||||
|
|
|
|||
|
|
@ -63,6 +63,7 @@ fn deinit(
|
|||
}
|
||||
|
||||
fn init(
|
||||
entity: *mach.Entity.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
text: *gfx.Text.Mod,
|
||||
text_pipeline: *gfx.TextPipeline.Mod,
|
||||
|
|
@ -73,21 +74,21 @@ fn init(
|
|||
|
||||
// TODO: a better way to initialize entities with default values
|
||||
// TODO(text): most of these style options are not respected yet.
|
||||
const style1 = try core.newEntity();
|
||||
const style1 = try entity.new();
|
||||
try text_style.set(style1, .font_name, "Roboto Medium"); // TODO
|
||||
try text_style.set(style1, .font_size, 48 * gfx.px_per_pt); // 48pt
|
||||
try text_style.set(style1, .font_weight, gfx.font_weight_normal);
|
||||
try text_style.set(style1, .italic, false);
|
||||
try text_style.set(style1, .color, vec4(0.6, 1.0, 0.6, 1.0));
|
||||
|
||||
const style2 = try core.newEntity();
|
||||
const style2 = try entity.new();
|
||||
try text_style.set(style2, .font_name, "Roboto Medium"); // TODO
|
||||
try text_style.set(style2, .font_size, 48 * gfx.px_per_pt); // 48pt
|
||||
try text_style.set(style2, .font_weight, gfx.font_weight_normal);
|
||||
try text_style.set(style2, .italic, true);
|
||||
try text_style.set(style2, .color, vec4(0.6, 1.0, 0.6, 1.0));
|
||||
|
||||
const style3 = try core.newEntity();
|
||||
const style3 = try entity.new();
|
||||
try text_style.set(style3, .font_name, "Roboto Medium"); // TODO
|
||||
try text_style.set(style3, .font_size, 48 * gfx.px_per_pt); // 48pt
|
||||
try text_style.set(style3, .font_weight, gfx.font_weight_bold);
|
||||
|
|
@ -95,12 +96,12 @@ fn init(
|
|||
try text_style.set(style3, .color, vec4(0.6, 1.0, 0.6, 1.0));
|
||||
|
||||
// Create a text rendering pipeline
|
||||
const pipeline = try core.newEntity();
|
||||
const pipeline = try entity.new();
|
||||
try text_pipeline.set(pipeline, .is_pipeline, {});
|
||||
text_pipeline.send(.update, .{});
|
||||
|
||||
// Create some text
|
||||
const player = try core.newEntity();
|
||||
const player = try entity.new();
|
||||
try text.set(player, .pipeline, pipeline);
|
||||
try text.set(player, .transform, Mat4x4.scaleScalar(upscale).mul(&Mat4x4.translate(vec3(0, 0, 0))));
|
||||
|
||||
|
|
@ -132,6 +133,7 @@ fn init(
|
|||
}
|
||||
|
||||
fn tick(
|
||||
entity: *mach.Entity.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
text: *gfx.Text.Mod,
|
||||
text_pipeline: *gfx.TextPipeline.Mod,
|
||||
|
|
@ -181,7 +183,7 @@ fn tick(
|
|||
new_pos.v[0] += game.state().rand.random().floatNorm(f32) * 50;
|
||||
new_pos.v[1] += game.state().rand.random().floatNorm(f32) * 50;
|
||||
|
||||
const new_entity = try core.newEntity();
|
||||
const new_entity = try entity.new();
|
||||
try text.set(new_entity, .pipeline, game.state().pipeline);
|
||||
try text.set(new_entity, .transform, Mat4x4.scaleScalar(upscale).mul(&Mat4x4.translate(new_pos)));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue