Refactored Level Components out for reuse and renamed it to TileMap.

Refactored highlighting a single tile out.
This commit is contained in:
Sander Schobers 2023-06-04 18:23:40 +02:00
parent 0d9e0c4828
commit e66908ca71
8 changed files with 80 additions and 73 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
zig-cache
zig-out
allegro.path
**/*~

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.6 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

@ -1,3 +1,4 @@
pub const Animation = @import("game/animation.zig").Animation;
pub const Game = @import("game/game.zig").Game;
pub const Level = @import("game/level.zig").Level;
pub const TileMap = @import("game/tile_map.zig").TileMap;

View File

@ -1,4 +1,5 @@
const std = @import("std");
const allegro = @import("allegro");
const engine = @import("../engine.zig");
const Animation = @import("animation.zig").Animation;
const Renderer = @import("../renderer.zig").Renderer;
@ -10,6 +11,8 @@ pub const Game = struct {
Right,
};
pub const TileSize = 32;
level: Level,
health: i64 = 4,
@ -58,6 +61,14 @@ pub const Game = struct {
self.renderer.drawSpriteFrameV(spriteName, frame, view.x, view.y);
}
pub fn debugHighlightTile(self: Game, position: engine.PointF) void {
const renderer = self.renderer;
const topLeft = self.tileP(position);
const topLeftScreen = renderer.viewport.viewToScreenP(topLeft);
const tileSizeScreen = TileSize * renderer.viewport.scale;
renderer.textures.get("opaque").?.drawTintedScaled(allegro.mapRgba(127, 127, 127, 127), topLeftScreen.x, topLeftScreen.y, tileSizeScreen, tileSizeScreen);
}
pub fn moveCharacter(self: *Game, distance: f32) void {
self.playerPosition.x += distance;
self.isPlayerWalking = true;

View File

@ -1,71 +1,8 @@
const std = @import("std");
const engine = @import("../engine.zig");
const TileMap = @import("tile_map.zig").TileMap;
pub const Level = struct {
pub fn Column(comptime Value: type) type {
return struct {
const Self = @This();
values: std.AutoHashMap(i64, Value),
pub fn init(allocator: std.mem.Allocator) Self {
return Self{
.values = std.AutoHashMap(i64, Value).init(allocator),
};
}
pub fn deinit(self: Self) void {
self.values.deinit();
}
pub fn get(self: Self, row: i64) ?Value {
return self.values.get(row);
}
pub fn set(self: *Self, row: i64, value: Value) !void {
try self.values.put(row, value);
}
};
}
pub fn Components(comptime Value: type) type {
return struct {
const Self = @This();
allocator: std.mem.Allocator,
columns: std.ArrayList(Column(Value)),
pub fn init(allocator: std.mem.Allocator) Self {
return Self{
.allocator = allocator,
.columns = std.ArrayList(Column(Value)).init(allocator),
};
}
pub fn deinit(self: Self) void {
for (self.columns.items) |item| {
item.deinit();
}
self.columns.deinit();
}
pub fn column(self: Self, i: i64) *Column(Value) {
return &self.columns.items[@intCast(usize, i)];
}
fn ensureColumns(self: *Self, n: usize) !void {
while (self.columns.items.len < n) {
try self.columns.append(Column(Value).init(self.allocator));
}
}
pub fn set(self: *Self, c: i64, r: i64, value: Value) !void {
try self.ensureColumns(@intCast(usize, c) + 1);
try self.column(c).set(r, value);
}
};
}
pub const Tile = enum {
Grass,
};
@ -75,8 +12,8 @@ pub const Level = struct {
};
character: engine.Point,
tiles: Components(Tile),
collectables: Components(Collectable),
tiles: TileMap(Tile),
collectables: TileMap(Collectable),
pub fn load(allocator: std.mem.Allocator, path: []const u8) !Level {
const path_ = try std.fs.realpathAlloc(allocator, path);
@ -96,8 +33,8 @@ pub const Level = struct {
_ = n;
var character: ?engine.Point = null;
var tiles = Components(Tile).init(allocator);
var collectables = Components(Collectable).init(allocator);
var tiles = TileMap(Tile).init(allocator);
var collectables = TileMap(Collectable).init(allocator);
var lines = std.mem.split(u8, data, "\n");
var y: i64 = 0;

61
src/game/tile_map.zig Normal file
View File

@ -0,0 +1,61 @@
const std = @import("std");
pub fn TileMap(comptime Value: type) type {
return struct {
const Self = @This();
pub const Column = struct {
values: std.AutoHashMap(i64, Value),
pub fn init(allocator: std.mem.Allocator) Column {
return Column{
.values = std.AutoHashMap(i64, Value).init(allocator),
};
}
pub fn deinit(self: Column) void {
self.values.deinit();
}
pub fn get(self: Column, row: i64) ?Value {
return self.values.get(row);
}
pub fn set(self: *Column, row: i64, value: Value) !void {
try self.values.put(row, value);
}
};
allocator: std.mem.Allocator,
columns: std.ArrayList(Column),
pub fn init(allocator: std.mem.Allocator) Self {
return Self{
.allocator = allocator,
.columns = std.ArrayList(Column).init(allocator),
};
}
pub fn deinit(self: Self) void {
for (self.columns.items) |item| {
item.deinit();
}
self.columns.deinit();
}
pub fn column(self: Self, i: i64) *Column {
return &self.columns.items[@intCast(usize, i)];
}
pub fn ensureColumns(self: *Self, n: usize) !void {
while (self.columns.items.len < n) {
try self.columns.append(Column.init(self.allocator));
}
}
pub fn set(self: *Self, c: i64, r: i64, value: Value) !void {
try self.ensureColumns(@intCast(usize, c) + 1);
try self.column(c).set(r, value);
}
};
}

View File

@ -108,11 +108,7 @@ pub const GameScene = struct {
if (ctx.showDebug) {
ctx.renderer.printTextV("debug", ctx.palette.background.text, 0.01, 0.1, Renderer.TextAlignment.Left, "Character: ({d}, {d})", .{ self.game.playerPosition.x, self.game.playerPosition.y });
ctx.renderer.printTextV("debug", ctx.palette.background.text, 0.01, 0.15, Renderer.TextAlignment.Left, "Tiles: ({d}, {d}) -> ({d}, {d})", .{ tileBounds.min.x, tileBounds.min.y, tileBounds.max.x, tileBounds.max.y });
// const characterTopLeft = self.game.tileP(self.game.playerPosition);
// const characterTopLeftScreen = renderer.viewport.viewToScreenP(characterTopLeft);
// const tileWidthScreen = 32 * viewport.scale;
// renderer.textures.get("opaque").?.drawTintedScaled(allegro.mapRgba(127, 127, 127, 127), characterTopLeftScreen.x, characterTopLeftScreen.y, tileWidthScreen, tileWidthScreen);
// self.game.debugHighlightTile(self.game.playerPosition);
}
}
};