tree lerped health bar
This commit is contained in:
parent
df7bbf087e
commit
096a082ac7
59
src/app.zig
59
src/app.zig
@ -180,6 +180,7 @@ const Entity = struct {
|
||||
minion_wander_cooldown: Nanoseconds = 0,
|
||||
|
||||
tree_health: u32 = 0,
|
||||
tree_visual_health: f32 = 0,
|
||||
tree_max_health: u32 = 0,
|
||||
|
||||
station_spawn_tree_cooldown: Nanoseconds = 0,
|
||||
@ -717,6 +718,7 @@ fn spawnTree(self: *App, station_id: EntityId) void {
|
||||
if (self.spawnEntity(Entity{
|
||||
.type = .tree,
|
||||
.tree_health = health,
|
||||
.tree_visual_health = @floatFromInt(health),
|
||||
.tree_max_health = health,
|
||||
.seed = rng.int(u64),
|
||||
|
||||
@ -1015,12 +1017,6 @@ fn minionLogic(self: *App, plt: Platform.Frame, minion_id: EntityId) !void {
|
||||
tree.tree_health = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (tree.tree_health == 0) {
|
||||
self.unsetEntityTarget(minion_id);
|
||||
self.destroyTree(tree_id);
|
||||
minion.minion_state = .wander;
|
||||
}
|
||||
} else {
|
||||
walk_goal = tree.pos;
|
||||
}
|
||||
@ -1467,17 +1463,22 @@ fn showDebug(self: *App, plt: Platform.Frame) !void {
|
||||
}
|
||||
}
|
||||
|
||||
fn drawTree(self: *App, seed: u64, pos: Vec2) void {
|
||||
fn drawTree(self: *App, seed: u64, pos: Vec2, health_percent: f32) void {
|
||||
var prng = Prng.init(seed);
|
||||
const rng = prng.random();
|
||||
|
||||
Gfx.transformPush();
|
||||
defer Gfx.transformPop();
|
||||
|
||||
Gfx.transformTranslate(pos.sub(.init(terrain_tile_size.x/2, terrain_tile_size.y)));
|
||||
Gfx.transformScale(terrain_tile_size);
|
||||
Gfx.transformTranslate(pos);
|
||||
Gfx.transformTranslate(.init((rng.float(f32)-0.5) * terrain_tile_size.x, 0));
|
||||
|
||||
Gfx.transformTranslate(.init(rng.float(f32)-0.5, 0));
|
||||
{
|
||||
Gfx.transformPush();
|
||||
defer Gfx.transformPop();
|
||||
|
||||
Gfx.transformTranslate(.init(-terrain_tile_size.x/2, -terrain_tile_size.y));
|
||||
Gfx.transformScale(terrain_tile_size);
|
||||
|
||||
const tree_middle = &[_]Gfx.Sprite.Id{
|
||||
self.terrain_sheet.get(125, 41),
|
||||
@ -1527,6 +1528,12 @@ fn drawTree(self: *App, seed: u64, pos: Vec2) void {
|
||||
Gfx.drawSprite(corner_leaves, .init(2, -1), .init(-1, -1), .white);
|
||||
}
|
||||
|
||||
if (health_percent < 1) {
|
||||
drawProgressBar(1, .init(0, 20), .init(terrain_tile_size.x, 10), .black);
|
||||
drawProgressBar(health_percent, .init(0, 20), .init(terrain_tile_size.x, 10), .white);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn frame(self: *App, plt: Platform.Frame) !void {
|
||||
const input = plt.input;
|
||||
_ = input; // autofix
|
||||
@ -1553,7 +1560,6 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
||||
Gfx.transformTranslate(self.camera_pos.multiplyScalar(-1));
|
||||
|
||||
Gfx.setClearColor(.rgb(40, 40, 40));
|
||||
Gfx.drawLine(.init(-4000, 0), .init(4000, 0), .white, 5);
|
||||
|
||||
{ // Spawn in or delete minions based on self.upgrades.minion_count
|
||||
var minion_count: u32 = 0;
|
||||
@ -1585,7 +1591,7 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
||||
}
|
||||
}
|
||||
|
||||
const wood_station_first_pos = -100;
|
||||
const wood_station_first_pos = -200;
|
||||
const wood_station_step = -70;
|
||||
|
||||
if (station_count > self.upgrades.tree_station_count) {
|
||||
@ -1849,10 +1855,22 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
||||
while (tree_iter.next()) |id| {
|
||||
const e = self.entities.getAssumeExists(id);
|
||||
|
||||
self.drawTree(e.seed, e.pos);
|
||||
e.tree_visual_health = std.math.lerp(
|
||||
e.tree_visual_health,
|
||||
@as(f32, @floatFromInt(e.tree_health)),
|
||||
0.05,
|
||||
);
|
||||
|
||||
const progress = @as(f32, @floatFromInt(e.tree_health)) / @as(f32, @floatFromInt(e.tree_max_health));
|
||||
drawProgressBar(progress, e.pos.add(.init(0, 20)), .init(40, 10), .rgb(0, 100, 20));
|
||||
const health_percent = e.tree_visual_health / @as(f32, @floatFromInt(e.tree_max_health));
|
||||
self.drawTree(
|
||||
e.seed,
|
||||
e.pos,
|
||||
health_percent
|
||||
);
|
||||
|
||||
if (e.tree_visual_health < 0.1) {
|
||||
self.destroyTree(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1917,11 +1935,6 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
||||
pos.x -= character_size.x/2;
|
||||
}
|
||||
|
||||
Gfx.drawRectangle(
|
||||
e.pos.sub(.init(5, 5)),
|
||||
.init(10, 10),
|
||||
.white
|
||||
);
|
||||
Gfx.drawSprite(sprite, pos, size, .white);
|
||||
|
||||
if (e.type == .minion and e.minion_state == .burn_wood and e.minion_throw_cooldown > 0) {
|
||||
@ -1931,12 +1944,6 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
||||
|
||||
if (e.type == .burn_station) {
|
||||
Gfx.drawRectangle(e.pos.sub(.init(5, 5)), .init(10, 10), .rgb(200, 20, 20));
|
||||
} else if (e.type == .minion) {
|
||||
Gfx.drawText(self.font, .{
|
||||
.pos = e.pos.sub(.init(20, 50)),
|
||||
.height = 16,
|
||||
.text = try std.fmt.allocPrint(plt.arena, "{}", .{e.minion_state})
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user