Improve map format

master
Louis Pearson 2022-01-17 21:33:47 -07:00
parent 08d3efa3f4
commit f42ceadaca
7 changed files with 41 additions and 14 deletions

View File

@ -1,5 +1,6 @@
const std = @import("std");
const Vec2 = std.meta.Vector(2,i32);
const Wire = struct { p1: Vec2, p2: Vec2, a1: bool, a2: bool };
pub const solid: [400]u8 = .{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 1, 1, 1, 39, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 1, 1, 1, 50, 19, 19, 19, 19, 19, 19, 19, 19, 1, 1, 1, 39, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 50, 65, 19, 19, 19, 19, 19, 19, 19, 19, 19, 52, 1, 37, 1, 1, 39, 1, 1, 1, 1, 20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 36, 52, 1, 1, 1, 20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 50, 67, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 36, 52, 1, 1, 1, 20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 39, 1, 1, 1, 1, 20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 37, 1, 1, 39, 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0, 132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 163, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
pub const wire: [2][2]Vec2 = [_][2]Vec2{.{.{ 20, 20 },.{ 76, 20 },}, .{.{ 84, 19 },.{ 134, 30 },}, };
pub const wire: [2]Wire = [_]Wire{.{.a1 = true, .a2 = true,.p1 = Vec2{ 20, 20 },.p2 = Vec2{ 76, 20 },}, .{.a1 = true, .a2 = false,.p1 = Vec2{ 84, 19 },.p2 = Vec2{ 139, 20 },}, };

View File

@ -21,7 +21,7 @@
"name":"conduit",
"opacity":1,
"type":"tilelayer",
"visible":false,
"visible":true,
"width":20,
"x":0,
"y":0
@ -61,8 +61,14 @@
"y":0
},
{
"x":50.5,
"y":10.875
"x":55.5909090909091,
"y":0.147727272727273
}],
"properties":[
{
"name":"anchor2",
"type":"bool",
"value":false
}],
"rotation":0,
"type":"wire",

View File

@ -28,7 +28,7 @@
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
</data>
</layer>
<layer id="2" name="conduit" width="20" height="20" visible="0">
<layer id="2" name="conduit" width="20" height="20">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
@ -57,7 +57,10 @@
<polyline points="0,0 56,0"/>
</object>
<object id="6" type="wire" x="84.125" y="19.875">
<polyline points="0,0 50.5,10.875"/>
<properties>
<property name="anchor2" type="bool" value="false"/>
</properties>
<polyline points="0,0 55.5909,0.147727"/>
</object>
</objectgroup>
</map>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 648 B

After

Width:  |  Height:  |  Size: 658 B

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,12 @@
const std = @import("std");
const PropertyType = enum { @"bool" };
const Property = struct {
name: []const u8 = &.{},
@"type": PropertyType = .@"bool",
value: union(PropertyType) { @"bool": bool },
};
const Point = struct {
x: f64 = 0,
y: f64 = 0,
@ -10,8 +17,9 @@ const Object = struct {
id: u64 = 0,
name: []const u8,
polyline: []Point,
properties: []Property = &.{},
rotation: f64 = 0,
@"type": []const u8 = "",
@"type": enum { wire },
visible: bool = true,
width: u64 = 0,
x: f64 = 0,
@ -98,6 +106,7 @@ pub fn do() !void {
try outlist.appendSlice("const std = @import(\"std\");\n");
try outlist.appendSlice("const Vec2 = std.meta.Vector(2,i32);\n");
try outlist.appendSlice("const Wire = struct { p1: Vec2, p2: Vec2, a1: bool, a2: bool };\n");
var outbuffer: [4 * KB]u8 = undefined;
for (map.layers) |layer| {
@ -107,13 +116,21 @@ pub fn do() !void {
_ = try outlist.appendSlice(outcontent);
},
.objectgroup => {
var outcontent = try std.fmt.bufPrint(&outbuffer, "pub const {s}: [{}][2]Vec2 = [_][2]Vec2{{", .{ layer.name, layer.objects.len });
var outcontent = try std.fmt.bufPrint(&outbuffer, "pub const {s}: [{}]Wire = [_]Wire{{", .{ layer.name, layer.objects.len });
try outlist.appendSlice(outcontent);
for (layer.objects) |obj| {
try outlist.appendSlice(".{");
for (obj.polyline) |point| {
var pointf = try std.fmt.bufPrint(&outbuffer, ".{{ {}, {} }},", .{ @floatToInt(i32, obj.x + point.x), @floatToInt(i32, obj.y + point.y) });
var a1 = true;
var a2 = true;
for (obj.properties) |p| {
if (std.mem.eql(u8, p.name, "anchor1")) a1 = p.value.@"bool";
if (std.mem.eql(u8, p.name, "anchor2")) a2 = p.value.@"bool";
}
var of = try std.fmt.bufPrint(&outbuffer, ".a1 = {}, .a2 = {},", .{ a1, a2 });
try outlist.appendSlice(of);
for (obj.polyline) |point, i| {
var pointf = try std.fmt.bufPrint(&outbuffer, ".p{} = Vec2{{ {}, {} }},", .{ i + 1, @floatToInt(i32, obj.x + point.x), @floatToInt(i32, obj.y + point.y) });
try outlist.appendSlice(pointf);
}
try outlist.appendSlice("}, ");

View File

@ -132,8 +132,8 @@ export fn start() void {
}) catch showErr("Creating player");
for (assets.wire) |wire| {
const begin = Vec2f{ @intToFloat(f32, wire[0][0]), @intToFloat(f32, wire[0][1]) };
const end = Vec2f{ @intToFloat(f32, wire[1][0]), @intToFloat(f32, wire[1][1]) };
const begin = vec2tovec2f(wire.p1);
const end = vec2tovec2f(wire.p2);
const size = end - begin;
var nodes = std.BoundedArray(Pos, 10).init(0) catch showErr("Nodes");
@ -142,7 +142,7 @@ export fn start() void {
const pos = begin + @splat(2, @intToFloat(f32, i)) * size / @splat(2, @as(f32, 5));
nodes.append(Pos.init(pos)) catch showErr("Appending nodes");
}
const w = Wire{ .nodes = nodes, .anchored = .{ .stationary, null } };
const w = Wire{ .nodes = nodes, .anchored = .{ if (wire.a1) .stationary else null, if (wire.a2) .stationary else null } };
_ = world.create(.{
.wire = w,
}) catch showErr("Adding wire entity");