Get saving working better

master
Louis Pearson 2022-01-31 00:31:02 -07:00
parent 6d83dd0535
commit b1b25753a3
7 changed files with 105 additions and 32 deletions

View File

@ -107,6 +107,12 @@
"x":16,
"y":0
}],
"properties":[
{
"name":"divisions",
"type":"int",
"value":5
}],
"rotation":0,
"type":"wire",
"visible":true,

View File

@ -148,6 +148,9 @@
<polyline points="32,-64 -2,-46"/>
</object>
<object id="5" type="wire" x="20" y="436">
<properties>
<property name="divisions" type="int" value="5"/>
</properties>
<polyline points="0,0 16,0"/>
</object>
<object id="6" type="door" x="84" y="468">

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,10 +1,10 @@
const std = @import("std");
const PropertyType = enum { @"bool" };
const PropertyType = enum { @"bool", @"int" };
const Property = struct {
name: []const u8 = &.{},
@"type": PropertyType = .@"bool",
value: union(PropertyType) { @"bool": bool },
value: union(PropertyType) { @"bool": bool, @"int": i64 },
};
const Point = struct {
@ -110,7 +110,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 AABB = struct {pos: Vec2, size: Vec2};\n");
try outlist.appendSlice("const Wire = struct { p1: Vec2, p2: Vec2, a1: bool, a2: bool };\n");
try outlist.appendSlice("const Wire = struct { p1: Vec2, p2: Vec2, a1: bool, a2: bool, divisions: u8 };\n");
var outbuffer: [16 * KB]u8 = undefined;
for (map.layers) |layer| {
@ -163,26 +163,42 @@ pub fn do() !void {
}
}
pub fn length(vec: std.meta.Vector(2, i64)) i64 {
var squared = vec * vec;
return @intCast(i64, std.math.sqrt(@intCast(u64, @reduce(.Add, squared))));
}
pub fn appendWires(outlist: *std.ArrayList(u8), wirelist: std.ArrayList(Object)) !void {
var outbuffer: [4 * KB]u8 = undefined;
var outcontent = try std.fmt.bufPrint(&outbuffer, "pub const wire: [{}]Wire = [_]Wire{{", .{wirelist.items.len});
try outlist.appendSlice(outcontent);
for (wirelist.items) |obj| {
try outlist.appendSlice(".{");
var a1 = true;
var a2 = true;
var p1: std.meta.Vector(2, i64) = .{ 0, 0 };
var p2: std.meta.Vector(2, i64) = .{ 0, 0 };
var divisions: ?i64 = null;
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";
if (std.mem.eql(u8, p.name, "divisions")) divisions = p.value.@"int";
}
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);
switch (i) {
0 => p1 = .{ @floatToInt(i64, obj.x + point.x), @floatToInt(i64, obj.y + point.y) },
1 => p2 = .{ @floatToInt(i64, obj.x + point.x), @floatToInt(i64, obj.y + point.y) },
else => return error.TooManyPoints,
}
try outlist.appendSlice("}, ");
}
divisions = divisions orelse std.math.max(2, @divTrunc(length(p2 - p1), 6));
var of = try std.fmt.bufPrint(
&outbuffer,
".{{ .a1 = {}, .a2 = {}, .divisions = {}, .p1 = Vec2{{ {}, {} }}, .p2 = Vec2{{ {}, {} }} }}, ",
.{ a1, a2, divisions, p1[0], p1[1], p2[0], p2[1] },
);
try outlist.appendSlice(of);
}
try outlist.appendSlice("};\n");
}

View File

@ -60,7 +60,7 @@ const Kinematic = struct {
}
};
const Wire = struct {
nodes: std.BoundedArray(Pos, 32),
nodes: std.BoundedArray(Pos, 32) = std.BoundedArray(Pos, 32).init(0),
enabled: bool = false,
pub fn begin(this: *@This()) *Pos {
@ -70,6 +70,16 @@ const Wire = struct {
pub fn end(this: *@This()) *Pos {
return &this.nodes.slice()[this.nodes.len - 1];
}
pub fn straighten(this: *@This()) void {
const b = this.begin().pos;
const e = this.end().pos;
const size = e - b;
for (this.nodes.slice()) |*node, i| {
if (i == 0 or i == this.nodes.len - 1) continue;
node.pos = b + @splat(2, @intToFloat(f32, i)) * size / @splat(2, @intToFloat(f32, this.nodes.len));
}
}
};
const Physics = struct { gravity: Vec2f, friction: Vec2f };
const Player = struct {
@ -234,21 +244,19 @@ export fn start() void {
};
for (assets.wire) |wire| {
const begin = vec2tovec2f(wire.p1);
const end = vec2tovec2f(wire.p2);
const size = end - begin;
var nodes = std.BoundedArray(Pos, 32).init(0) catch showErr("Nodes");
var w = wires.addOne() catch showErr("New wire");
const divisions = wire.divisions;
var i: usize = 0;
const divisions = @floatToInt(usize, util.lengthf(size) / 6);
while (i <= divisions) : (i += 1) {
const pos = begin + @splat(2, @intToFloat(f32, i)) * size / @splat(2, @intToFloat(f32, divisions));
nodes.append(Pos.init(pos)) catch showErr("Appending nodes");
w.nodes.append(Pos.init(Vec2f{ 0, 0 })) catch showErr("Appending nodes");
}
if (wire.a1) nodes.slice()[0].pinned = true;
if (wire.a2) nodes.slice()[nodes.len - 1].pinned = true;
const w = Wire{ .nodes = nodes };
wires.append(w) catch showErr("Appending wire");
w.begin().pos = util.vec2ToVec2f(wire.p1);
w.end().pos = util.vec2ToVec2f(wire.p2);
w.begin().pinned = wire.a1;
w.end().pinned = wire.a2;
w.straighten();
}
for (assets.sources) |source| {
@ -259,6 +267,7 @@ export fn start() void {
circuit.addDoor(door);
}
// _ = w4.diskw("", 0);
if (!load()) {
for (assets.coins) |coin| {
coins.append(.{
@ -480,21 +489,25 @@ fn load() bool {
var begin = wires.slice()[id].begin();
begin.* = pos;
begin.pinned = true;
wires.slice()[id].straighten();
},
.WireBeginLoose => {
var begin = wires.slice()[id].begin();
begin.* = pos;
begin.pinned = false;
wires.slice()[id].straighten();
},
.WireEndPinned => {
var end = wires.slice()[id].end();
end.* = pos;
end.pinned = true;
wires.slice()[id].straighten();
},
.WireEndLoose => {
var end = wires.slice()[id].end();
end.* = pos;
end.pinned = false;
wires.slice()[id].straighten();
},
}
}

View File

@ -45,7 +45,7 @@ pub fn vec2ToVec2f(vec2: Vec2) Vec2f {
}
pub fn vec2fToVec2(vec2f: Vec2f) Vec2 {
return Vec2{ @floatToInt(i32, vec2f[0]), @floatToInt(i32, vec2f[1]) };
return Vec2{ @floatToInt(i32, @floor(vec2f[0])), @floatToInt(i32, @floor(vec2f[1])) };
}
pub const AABB = struct {
@ -80,4 +80,3 @@ pub fn Queue(comptime T: type, len: usize) type {
}
};
}