From 2de74dfa360604d2595f1ebecdc5682247a9aab7 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Fri, 17 Jul 2026 02:41:44 +0300 Subject: [PATCH] add tree station spawning and removing --- src/app.zig | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/app.zig b/src/app.zig index 1ef6869..83a7878 100644 --- a/src/app.zig +++ b/src/app.zig @@ -224,6 +224,7 @@ const Upgrades = struct { minion_carry_capacity: u32 = 1, minion_walk: MinionWalkStats = .{}, + tree_station_count: u32 = 0, tree_health: u32 = 10, tree_respawn_duration: Nanoseconds = std.time.ns_per_s, tree_wood_amount: u32 = 1, @@ -309,8 +310,6 @@ pub fn init(self: *App, plt: Platform.Init) !?u8 { .upgrades = .{} }; - self.spawnWoodStation(-100); - self.spawnWoodStation(-150); self.spawnBurnStation(100); self.spawnBurnStation(150); @@ -729,6 +728,17 @@ fn removeMinion(self: *App, minion_id: EntityId) void { self.entities.removeAssumeExists(minion_id); } +fn removeWoodStation(self: *App, id: EntityId) void { + const wood_station = self.entities.get(id) orelse return; + assert(wood_station.type == .wood_station); + + if (wood_station.station_owned_tree) |tree_id| { + self.entities.removeAssumeExists(tree_id); + } + + self.entities.removeAssumeExists(id); +} + fn getBurnMultiplier(self: *App) u32 { const stack_size = self.countEternalFlameStackSize(); @@ -786,6 +796,41 @@ pub fn frame(self: *App, plt: Platform.Frame) !void { } } + { // Spawn in or delete wood stations + var station_count: u32 = 0; + { + var wood_station_iter = self.initEntityIteratorByType(.wood_station); + while (wood_station_iter.next()) |_| { + station_count += 1; + } + } + + const wood_station_first_pos = -100; + const wood_station_step = -50; + + if (station_count > self.upgrades.tree_station_count) { + for (self.upgrades.tree_station_count..station_count) |i| { + + // Remove the last wood station that was added + var wood_station_iter = self.initEntityIteratorByType(.wood_station); + while (wood_station_iter.next()) |wood_station_id| { + const wood_station = self.entities.getAssumeExists(wood_station_id); + const i_f32: f32 = @floatFromInt(i); + const expected_x = wood_station_first_pos + wood_station_step * i_f32; + if (wood_station.pos.x == expected_x) { + self.removeWoodStation(wood_station_id); + break; + } + } + } + } else if (station_count < self.upgrades.tree_station_count) { + for (station_count..self.upgrades.tree_station_count) |i| { + const i_f32: f32 = @floatFromInt(i); + self.spawnWoodStation(wood_station_first_pos + wood_station_step * i_f32); + } + } + } + { // Spawn trees at wood station on a cooldown var wood_station_iter = self.initEntityIteratorByType(.wood_station); while (wood_station_iter.next()) |wood_station_id| { @@ -1164,6 +1209,7 @@ pub fn frame(self: *App, plt: Platform.Frame) !void { ImGUI.separator(); _ = ImGUI.text("Tree upgrades", .{}); + _ = ImGUI.inputU32("station_count", &self.upgrades.tree_station_count); _ = ImGUI.inputU32("health", &self.upgrades.tree_health); _ = ImGUI.inputU32("wood_amount", &self.upgrades.tree_wood_amount); _ = ImGUI.inputDuration("respawn_duration", &self.upgrades.tree_respawn_duration);