-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWireModWorld.cs
50 lines (44 loc) · 1.36 KB
/
WireModWorld.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Collections.Generic;
using System.Linq;
using Terraria.DataStructures;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using WireMod.Devices;
namespace WireMod
{
public class WireModWorld : ModWorld
{
public override TagCompound Save()
{
var wires = WireMod.Pins.Where(p => p.Type == "In" && p.IsConnected()).Select(p => new TagCompound
{
["src"] = p.Location,
["dest"] = ((PinIn)p).ConnectedPin.Location,
["points"] = p.GetWire(((PinIn)p).ConnectedPin).GetPoints(p, false),
}).ToList();
return new TagCompound
{
["devices"] = WireMod.Devices,
["wires"] = wires
};
}
public override void Load(TagCompound tag)
{
foreach (var device in tag.Get<List<Device>>("devices"))
{
if (WireMod.Debug) mod.Logger.Info($"Loading device \"{device.Name}\": X {device.LocationRect.X}, Y: {device.LocationRect.Y}");
WireMod.PlaceDevice(device, device.LocationRect.X, device.LocationRect.Y);
}
foreach (var conn in tag.GetList<TagCompound>("wires"))
{
var src = WireMod.GetDevicePin(conn.Get<Point16>("src"));
var dest = WireMod.GetDevicePin(conn.Get<Point16>("dest"));
if (src == null || dest == null) continue;
var points = conn.GetList<Point16>("points").ToList();
var wire = new Wire(src, dest, points);
src.Connect(dest, wire);
dest.Connect(src, wire);
}
}
}
}