//This method serializes a Game, then writes the information to a file public static void Save(Game game, string filename) { if (!File.Exists(filename)) { File.CreateText(filename).Close(); using (var sr = new StreamWriter(File.OpenWrite(filename))) { sr.Write($@" {{ ""version"":""{Version}"", ""username"":{game.Username}, ""ticks"":{game.Ticks}, ""humanplayer"":{game.Player.Serialize()}, ""stablegravitywells"":" + JsonUtils.GameObjectsToJsonList(game.StableWells) + $@", ""unstablegravitywells"":{JsonUtils.GameObjectsToJsonList(game.UnstableWells)}, ""orbs"":{JsonUtils.GameObjectsToJsonList(game.Orbs)} ""aiplayers"":{JsonUtils.ToJsonList(game.AIShips)} }}"); } } else { File.Delete(filename); Save(game, filename); } }
//This method serializes the instance and returns the string. public override string Serialize() { return($@"{{ ""xcoor"":{Xcoor}, ""ycoor"":{Ycoor}, ""points"":{ParentGame.Points}, ""orblist"":{JsonUtils.ToJsonList(Orbs)}, ""powerups"":{JsonUtils.ToJsonList( GamePowerup.CurrentPowerups)}, }}"); }
public void Test_ToJsonArray() { var orbs = new Game(false).Orbs; string st = JsonUtils.ToJsonList(orbs); var s = JsonUtils.GetObjectsInArray(st); Assert.AreEqual(s.Count, orbs.Count); for (int i = 0; i < orbs.Count; i++) { Orb other = GameObject.FromJsonFactory <Orb>(s[i]); Assert.AreEqual(other.Xcoor, orbs[i].Xcoor, 0.5); Assert.AreEqual((int)other.Ycoor, (int)orbs[i].Ycoor, 0.5); Assert.AreEqual(other.Color, orbs[i].Color); } }