implement advancing through levels
This commit is contained in:
parent
a6bf95c570
commit
6f21915c9a
61
src/app.zig
61
src/app.zig
@ -141,6 +141,11 @@ const World = struct {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *World, gpa: Allocator) void {
|
||||
self.tilemap.deinit(gpa);
|
||||
self.entities.deinit(gpa);
|
||||
}
|
||||
|
||||
fn spawnEntity(self: *World, opts: Entity) Entity.Id {
|
||||
var e = opts;
|
||||
e.pos_x = -1;
|
||||
@ -219,6 +224,7 @@ const Level = struct {
|
||||
};
|
||||
|
||||
world: World,
|
||||
current_level: u32 = 0,
|
||||
levels: std.ArrayList(Level),
|
||||
|
||||
roboto_font: Font,
|
||||
@ -306,7 +312,7 @@ pub fn init(self: *App, plt: Platform.Init) !?u8 {
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn loadLevels(self: *App, gpa: Allocator, assets: *Platform.Assets) !std.ArrayList(Level) {
|
||||
fn loadLevels(self: *App, gpa: Allocator, assets: *Platform.Assets) !std.ArrayList(Level) {
|
||||
var xml_parser = try Tiled.xml.Parser.initCapacity(gpa, .{});
|
||||
defer xml_parser.deinit(gpa);
|
||||
|
||||
@ -370,7 +376,7 @@ pub fn loadLevels(self: *App, gpa: Allocator, assets: *Platform.Assets) !std.Arr
|
||||
return levels;
|
||||
}
|
||||
|
||||
pub fn loadLevel(
|
||||
fn loadLevel(
|
||||
self: *App,
|
||||
gpa: Allocator,
|
||||
tilemap: Tiled.Tilemap,
|
||||
@ -458,6 +464,10 @@ pub fn loadLevel(
|
||||
return level;
|
||||
}
|
||||
|
||||
fn updateWorld(world: *World) void {
|
||||
_ = world; // autofix
|
||||
}
|
||||
|
||||
pub fn frame(self: *App, plt: Platform.Frame) !void {
|
||||
const input = plt.input;
|
||||
|
||||
@ -490,16 +500,8 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
||||
canvas.push();
|
||||
defer canvas.pop(clear_color);
|
||||
|
||||
for (0..self.world.tilemap.height) |y| {
|
||||
for (0..self.world.tilemap.width) |x| {
|
||||
const tile = self.world.tilemap.get(x, y);
|
||||
|
||||
if (tile.ground_sprite) |sprite| {
|
||||
Gfx.drawSprite(sprite, Vec2.initFromInt(usize, x, y).multiply(tile_size), tile_size, .white);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var goal_count: u32 = 0;
|
||||
var box_at_goal_count: u32 = 0;
|
||||
{
|
||||
var entity_iter = self.world.entities.iterator();
|
||||
while (entity_iter.next()) |entity_id| {
|
||||
@ -540,12 +542,31 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
||||
}
|
||||
|
||||
entity.sprite = frames[entity.player_frame_index];
|
||||
} else if (entity.type == .goal_marker) {
|
||||
goal_count += 1;
|
||||
|
||||
const tile = self.world.getTile(entity.pos_x, entity.pos_y).?;
|
||||
if (self.world.entities.get(tile.entities_by_layer.get(.surface))) |above_entity| {
|
||||
if (above_entity.type == .box) {
|
||||
box_at_goal_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
entity.visual_pos = .initFromInt(i32, entity.pos_x, entity.pos_y);
|
||||
}
|
||||
}
|
||||
|
||||
for (0..self.world.tilemap.height) |y| {
|
||||
for (0..self.world.tilemap.width) |x| {
|
||||
const tile = self.world.tilemap.get(x, y);
|
||||
|
||||
if (tile.ground_sprite) |sprite| {
|
||||
Gfx.drawSprite(sprite, Vec2.initFromInt(usize, x, y).multiply(tile_size), tile_size, .white);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline for (.{ Entity.Layer.ground, Entity.Layer.surface }) |layer| {
|
||||
var entity_iter = self.world.entities.iterator();
|
||||
while (entity_iter.next()) |entity_id| {
|
||||
@ -565,23 +586,21 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
||||
}
|
||||
}
|
||||
|
||||
// self.roboto_font.drawText(.{
|
||||
// .text = try std.fmt.allocPrint(plt.frame, "Hello {:.0}!", .{plt.time()}),
|
||||
// .pos = .init(0, 0),
|
||||
// .height = 32,
|
||||
// });
|
||||
if (goal_count > 0 and goal_count == box_at_goal_count and self.current_level+1 < self.levels.items.len) {
|
||||
self.current_level += 1;
|
||||
self.world.deinit(plt.gpa);
|
||||
self.world = try self.levels.items[self.current_level].world.clone(plt.gpa);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deinit(self: *App, plt: Platform.Deinit) void {
|
||||
self.roboto_font.deinit(plt.gpa);
|
||||
Gfx.deinitSpritesheet(self.roboto_font.spritesheet);
|
||||
|
||||
self.world.tilemap.deinit(plt.gpa);
|
||||
self.world.entities.slots.deinit(plt.gpa);
|
||||
self.world.deinit(plt.gpa);
|
||||
|
||||
for (self.levels.items) |*level| {
|
||||
level.world.entities.deinit(plt.gpa);
|
||||
level.world.tilemap.deinit(plt.gpa);
|
||||
level.world.deinit(plt.gpa);
|
||||
}
|
||||
self.levels.deinit(plt.gpa);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user