public void Recover(IPersistableReader rd) { Global.log.info($"{nameof(PlayPersistable)}::recover called"); // verify save file version var readVersion = rd.ReadInt(); if (version != readVersion) { Global.log.err($"save file version mismatch (got {readVersion}, expected {version})"); } setup.rehydrated = true; // indicate that this play context is rehydrated // load game time Time.TotalTime = rd.ReadFloat() + SAVE_TIME_ADVANCE; // load map seed setup.state.mapgenSeed = rd.ReadInt(); Global.log.trace($"loaded mapgen seed: {setup.state.mapgenSeed}"); // read player var playerWd = rd.readWing(); var playerBodyData = rd.readBody(); var player = setup.createPlayer(Vector2.Zero); player.uid = playerWd.uid; player.core.energy = playerWd.energy; player.mind.soul.ply = playerWd.ply; playerBodyData.copyTo(player.body); player.changeClass(playerWd.wingClass); if (playerWd.armed) { player.AddComponent <Shooter>(); } // load all wings var wingCount = rd.ReadInt(); for (var i = 0; i < wingCount; i++) { var wd = rd.readWing(); var wing = setup.createNpcWing(wd.name, Vector2.Zero, wd.ply); wing.uid = wd.uid; if (wd.armed) { wing.AddComponent <Shooter>(); } var bd = rd.readBody(); // rd.readWingMemory(wing.mind); bd.copyTo(wing.body); wing.changeClass(wd.wingClass); Global.log.trace( $"rehydrated wing {wing.name}, pos{wing.body.pos.RoundToPoint()}, ply{wing.mind.soul.ply}"); } // load world things var thingCount = rd.ReadInt(); var thingHelper = new ThingLoader(this); var loadedThings = new List <ThingLoader.LoadedThing>(); for (var i = 0; i < thingCount; i++) { // load and inflate thing var load = thingHelper.loadThing(rd); if (load != null) { loadedThings.Add(load); } else { Global.log.err("attempted to load thing, but it was NULL"); } } // now resolve all things var resolvedThings = thingHelper.resolveThings(loadedThings); foreach (var thing in resolvedThings) { // add to context setup.addThing(thing); } }
public LoadedThing loadThing(IPersistableReader rd) { var kind = (ThingKind)rd.ReadInt(); var res = default(Thing); var uid = rd.ReadLong(); var senderUid = default(long); var creatorUid = default(long); switch (kind) { case ThingKind.Capsule: { var nt = new Entity("cap"); var cap = nt.AddComponent(new Capsule()); cap.acquired = rd.ReadBool(); // read body var bodyData = rd.readBody(); bodyData.copyTo(cap.body); // read other capsule info cap.energy = rd.ReadFloat(); cap.firstAvailableAt = rd.ReadFloat(); cap.despawnAt = rd.ReadFloat(); senderUid = rd.ReadLong(); creatorUid = rd.ReadLong(); // if acquired then throw away if (cap.acquired) { cap = null; // ick } res = cap; break; } case ThingKind.Tree: { var nt = new Entity("tree"); var tree = nt.AddComponent(new Tree()); // load tree tree.Entity.Position = rd.ReadVec2(); tree.stage = rd.ReadInt(); tree.harvest = rd.ReadInt(); tree.uid = uid; tree.updateStage(); res = tree; break; } default: // unrecognized thing Global.log.err($"unrecognized thing kind: {kind}"); res = null; break; } if (res != null) { Global.log.trace($"rehydrated entity {res.GetType().Name}, pos{res.Entity.Position.RoundToPoint()}"); var loadedThing = new LoadedThing(res) { interactorUid = senderUid, creatorUid = creatorUid }; return(loadedThing); // yee } return(null); }