From 2ebc85fc683079f4f352fe8a100375ad74889aca Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Fri, 1 Jul 2022 23:26:55 -0400 Subject: [PATCH] try getting a map type working Signed-off-by: Xe Iaso --- src/main.zig | 22 +++---- src/maps/dungeon.zig | 17 ++--- src/maps/overworld.zig | 15 +++-- src/sh0rk.zig | 26 ++++++++ src/tools/genmap.js | 140 ++++++++++++++++++++--------------------- 5 files changed, 124 insertions(+), 96 deletions(-) diff --git a/src/main.zig b/src/main.zig index 9bdcd0d..3a134bf 100644 --- a/src/main.zig +++ b/src/main.zig @@ -11,12 +11,12 @@ const Direction = sh0rk.Direction; const Point = sh0rk.Point; const Rect = sh0rk.Rect; -const map = @import("./maps/overworld.zig"); +const map = @import("./maps/overworld.zig").map; var frame_count: u32 = 0; var mara_direction: Direction = Direction.Right; var mara_frame: bool = false; -var mara_box: Rect = Rect{.base = map.start_point, .width = 16, .height = 16}; +var mara_box: Rect = Rect{.base = map.start, .width = 16, .height = 16}; var mara_speed: u16 = 0; var screen = Rect{.base = Point{.x = 0, .y = 0}, .width = 160, .height = 160}; @@ -30,10 +30,10 @@ var done: bool = false; var camera: [400]u9 = []u9{} ** 400; -var state: sh0rk.State = .Title; +var state: sh0rk.State = .Gameplay; export fn start() void { - palette.tamtam(); + palette.mist(); } fn bonk() void { @@ -78,14 +78,14 @@ fn drawMap() !void { while (row < screen_height) { defer row += 1; - var tile = map.data[col * map.width + row]; - var tileX = tile % map.ts_width; - var tileY = tile / map.ts_width; + var tile = map.tiles[col * map.width + row]; + var tileX = tile % map.tileset.width(); + var tileY = tile / map.tileset.height(); - // if (!done) { - // var buf = fmt.bufPrint(&textBuf, "{},{}: {}: {},{}", .{col, row, tile, tileX, tileY}) catch unreachable; - // w4.trace(buf); - // } + if (!done) { + var buf = fmt.bufPrint(&textBuf, "{},{}: {}: {},{}", .{col, row, tile, tileX, tileY}) catch unreachable; + w4.trace(buf); + } w4.blitSub( &sprites.kenney_rpg, diff --git a/src/maps/dungeon.zig b/src/maps/dungeon.zig index 1117da2..f353003 100644 --- a/src/maps/dungeon.zig +++ b/src/maps/dungeon.zig @@ -7,14 +7,10 @@ const Tileset = sh0rk.Tileset; const Trigger = sh0rk.Trigger; const Rect = sh0rk.Rect; -pub const width = 20; -pub const height = 20; - -pub const tileset: Tileset = .Dungeon; pub const ts_width = 14; pub const ts_height = 13; -pub const data = [400]u9{ +pub const data = [_]u9{ 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, @@ -65,8 +61,15 @@ pub const coll = [_]Rect{ Rect{.base = Point{.x = 112, .y = 98}, .width = 15, .height = 14}, }; -pub const start_point = Point{ .x = 40, .y = 73 }; - pub const triggers = [_]Trigger{ }; +pub const map = sh0rk.Map{ + .width = 20, + .height = 20, + .tileset = .Dungeon, + .tiles = data[0..data.len], + .coll = coll[0..coll.len], + .triggers = triggers[0..triggers.len], + .start = Point{ .x = 40, .y = 73 }, +}; diff --git a/src/maps/overworld.zig b/src/maps/overworld.zig index 5f330f1..b4f2796 100644 --- a/src/maps/overworld.zig +++ b/src/maps/overworld.zig @@ -7,10 +7,6 @@ const Tileset = sh0rk.Tileset; const Trigger = sh0rk.Trigger; const Rect = sh0rk.Rect; -pub const width = 20; -pub const height = 20; - -pub const tileset: Tileset = .Rpg; pub const ts_width = 34; pub const ts_height = 16; @@ -82,5 +78,12 @@ pub const triggers = [_]Trigger{ Trigger{.aura = Rect{.base = Point{.x = 96, .y = 103}, .width = 7, .height = 16}, .direction = Direction.Up, .dialogue = "Moar ded."}, // gravestone 2 }; -pub const start_point = Point{ .x = 32, .y = 32 }; - +pub const map = sh0rk.Map{ + .width = 20, + .height = 20, + .tileset = .Rpg, + .tiles = data[0..data.len], + .coll = coll[0..coll.len], + .triggers = triggers[0..triggers.len], + .start = Point{ .x = 32, .y = 32 }, +}; diff --git a/src/sh0rk.zig b/src/sh0rk.zig index 6074a1f..6152cf3 100644 --- a/src/sh0rk.zig +++ b/src/sh0rk.zig @@ -60,10 +60,36 @@ pub const Tileset = enum(u2) { Cave, Rpg, Dungeon, + + pub fn width(this: @This()) u9 { + switch (this) { + .Cave => return 28, + .Rpg => return 34, + .Dungeon => return 14, + } + } + + pub fn height(this: @This()) u9 { + switch (this) { + .Cave => return 10, + .Rpg => return 16, + .Dungeon => return 13, + } + } }; pub const State = enum { Title, StoryDump, Gameplay, +}; + +pub const Map = struct { + width: u8, + height: u8, + tileset: Tileset = .Rpg, + tiles: []const u9, + coll: []const Rect, + triggers: []const Trigger, + start: Point, }; \ No newline at end of file diff --git a/src/tools/genmap.js b/src/tools/genmap.js index 5cf69e6..b4b6a90 100644 --- a/src/tools/genmap.js +++ b/src/tools/genmap.js @@ -1,3 +1,58 @@ +const genMap = (tiles) => { + out += `pub const data = [_]u9{\n `; + tiles.data.forEach((tile, index) => { + tile -= 1; + out += `${tile},`; + if (index !== 0 && index % 10 === 0) { + out += "\n "; + } else { + out += " "; + } + }); + out += "\n};\n\n"; +}; + +const genColl = coll => { + out += `pub const coll = [_]Rect{\n`; + coll.objects.forEach(obj => { + const x = Math.round(obj.x); + const y = Math.round(obj.y); + const width = Math.round(obj.width); + const height = Math.round(obj.height); + let comment = ""; + if (obj.name !== "") { + comment = ` // ${obj.name}`; + } + out += ` Rect{.base = Point{.x = ${x}, .y = ${y}}, .width = ${width}, .height = ${height}},${comment}\n`; + }); + out += "};\n\n"; +}; + +const genAct = act => { + out += `pub const triggers = [_]Trigger{\n`; + act.objects.forEach(obj => { + const x = Math.round(obj.x); + const y = Math.round(obj.y); + const width = Math.round(obj.width); + const height = Math.round(obj.height); + let direction = ""; + let dialogue = ""; + obj.properties.forEach(prop => { + if (prop.name === "direction") { + direction = prop.value; + } + if (prop.name === "dialogue") { + dialogue = prop.value; + } + }); + if (direction === "" || dialogue === "") { + throw new Error("Missing direction or dialogue"); + } + out += ` Trigger{.aura = Rect{.base = Point{.x = ${x}, .y = ${y}}, .width = ${width}, .height = ${height}}, .direction = Direction.${direction}, .dialogue = "${dialogue}"}, // ${obj.name}\n`; + }); + out += "};\n\n"; +}; + if (Deno.args.length !== 2) { console.log("Usage: ./genmap.js "); Deno.exit(1); @@ -21,9 +76,6 @@ out += `const Tileset = sh0rk.Tileset;\n`; out += `const Trigger = sh0rk.Trigger;\n`; out += `const Rect = sh0rk.Rect;\n\n`; -out += `pub const width = ${map.width};\n`; -out += `pub const height = ${map.height};\n\n`; - let tiles = map.layers[0]; let tilesheet = { @@ -57,77 +109,9 @@ map.tilesets.forEach(tset => { } }); -out += `pub const tileset: Tileset = .${tilesheet.name};\n`; out += `pub const ts_width = ${tilesheet.width};\n`; out += `pub const ts_height = ${tilesheet.height};\n\n`; -const genMap = (tiles) => { - out += `pub const data = [${tiles.data.length}]u9{\n `; - - tiles.data.forEach((tile, index) => { - tile -= 1; - out += `${tile},`; - if (index !== 0 && index % 10 === 0) { - out += "\n "; - } else { - out += " "; - } - }); - - out += "\n};\n\n"; -}; - -const genColl = coll => { - out += `pub const coll = [_]Rect{\n`; - - coll.objects.forEach(obj => { - const x = Math.round(obj.x); - const y = Math.round(obj.y); - const width = Math.round(obj.width); - const height = Math.round(obj.height); - - let comment = ""; - if (obj.name !== "") { - comment = ` // ${obj.name}`; - } - - out += ` Rect{.base = Point{.x = ${x}, .y = ${y}}, .width = ${width}, .height = ${height}},${comment}\n`; - }); - - out += "};\n\n"; -}; - -const genAct = act => { - out += `pub const triggers = [_]Trigger{\n`; - - act.objects.forEach(obj => { - const x = Math.round(obj.x); - const y = Math.round(obj.y); - const width = Math.round(obj.width); - const height = Math.round(obj.height); - let direction = ""; - let dialogue = ""; - - obj.properties.forEach(prop => { - if (prop.name === "direction") { - direction = prop.value; - } - - if (prop.name === "dialogue") { - dialogue = prop.value; - } - }); - - if (direction === "" || dialogue === "") { - throw new Error("Missing direction or dialogue"); - } - - out += ` Trigger{.aura = Rect{.base = Point{.x = ${x}, .y = ${y}}, .width = ${width}, .height = ${height}}, .direction = Direction.${direction}, .dialogue = "${dialogue}"}, // ${obj.name}\n`; - }); - - out += "};\n\n"; -}; - map.layers.forEach(layer => { if (layer.name == "map") { genMap(layer); @@ -140,14 +124,26 @@ map.layers.forEach(layer => { if (layer.name == "act") { genAct(layer); } +}); +out += `pub const map = sh0rk.Map{\n`; +out += ` .width = ${map.width},\n`; +out += ` .height = ${map.height},\n`; +out += ` .tileset = .${tilesheet.name},\n`; +out += ` .tiles = data[0..data.len],\n`; +out += ` .coll = coll[0..coll.len],\n`; +out += ` .triggers = triggers[0..triggers.len],\n`; + +map.layers.forEach(layer => { if (layer.name == "spc") { layer.objects.forEach(obj => { if (obj.name === "mara_start") { - out += `pub const start_point = Point{ .x = ${Math.round(obj.x)}, .y = ${Math.round(obj.y)} };\n\n`; + out += ` .start = Point{ .x = ${Math.round(obj.x)}, .y = ${Math.round(obj.y)} },\n`; } }); } }); +out += `};\n`; + await Deno.writeTextFile(outFname, out);