private HashSet <Sprite> DeserializeInteriorObjects(List <InteriorObjectSerialized> interiorObjsSaved, Interior interior) { HashSet <Sprite> ret = new HashSet <Sprite>(); foreach (InteriorObjectSerialized objSave in interiorObjsSaved) { Sprite sp = null; if (objSave.groundObj) { OnGroundState ogs = (OnGroundState)objSave.saveState; sp = DeserializeModel(ogs.objKey, objSave.saveState); sp.location = ogs.location; sp.regionKey = ogs.region; } else if (objSave.npcObj) { NpcState npcs = (NpcState)objSave.saveState; Npc npc = (Npc)DeserializeModel(npcs.objKey, npcs); npc.location = npcs.location; npc.inventory = DeserializeInventory(npcs.inventory); npc.regionKey = npcs.region; npc.onShip = npcs.onShip; npc.health = npcs.health; npc.npcInInterior = interior; sp = npc; } ret.Add(sp); } return(ret); }
private List <InteriorObjectSerialized> SerializeInteriorObjects(List <Sprite> interiorObjs, Guid interiorId) { List <InteriorObjectSerialized> ret = new List <InteriorObjectSerialized>(); foreach (Sprite obj in interiorObjs) { InteriorObjectSerialized ios = new InteriorObjectSerialized(); if (obj is IPlayer) { continue; // don't save player as part of interior state, player is done seperatly } if (obj is INPC) { NpcState state = new NpcState(); Npc npc = (Npc)obj; state.team = npc.teamType; state.location = npc.location; state.objKey = npc.bbKey; state.region = npc.regionKey; state.inventory = SerializeInventory(npc.inventory); state.onShip = npc.onShip; state.health = npc.health; state.npcInInteriorId = interiorId; ios.npcObj = true; ios.saveState = state; ret.Add(ios); } else { OnGroundState ogs = new OnGroundState(); ogs.objKey = obj.bbKey; ogs.region = obj.regionKey; ogs.team = TeamType.Gusto; ogs.location = obj.location; if (obj is IStorage) { Storage storage = (Storage)obj; ogs.inventory = SerializeInventory(storage.inventory); } else if (obj is IContainer) { Container cont = (Container)obj; ogs.inventory = SerializeInventory(cont.drops); } ios.groundObj = true; ios.saveState = ogs; ret.Add(ios); } } return(ret); }
private void InitializeLoadState(List <ISaveState> LoadFromState) { Dictionary <Guid, Interior> interiorForObjMap = new Dictionary <Guid, Interior>(); // key is the interiorObjFor and val is the interior that belongs to this obj foreach (ISaveState objState in LoadFromState) { // Interiors are deserialized first since they were serialized first if (objState.GetType() == typeof(InteriorState)) { InteriorState its = (InteriorState)objState; Interior i = DeserializeInterior(its); interiorForObjMap.Add(its.interiorForId, i); BoundingBoxLocations.interiorMap.Add(i.interiorId, i); } if (objState.GetType() == typeof(PlayerState)) { PlayerState ps = (PlayerState)objState; player.location = ps.location; player.inventory = DeserializeInventory(ps.inventory); player.inHand = (HandHeld)DeserializeInventoryItem(ps.inHandItemKey); if (ps.playerInInteriorId == Guid.Empty) { player.playerOnShip = null; player.playerInInterior = null; } else { player.entranceLoc = ps.interiorEntranceLocation; // check if the interior already exists foreach (KeyValuePair <Guid, Interior> interior in BoundingBoxLocations.interiorMap) { if (ps.playerInInteriorId == interior.Key) { player.playerInInterior = interior.Value; break; } } } player.onShip = ps.onShip; player.regionKey = ps.region; player.health = ps.health; UpdateOrder.Add(player); } else if (objState.GetType() == typeof(NpcState)) { NpcState npcs = (NpcState)objState; Npc npc = (Npc)DeserializeModel(npcs.objKey, npcs); npc.location = npcs.location; npc.inventory = DeserializeInventory(npcs.inventory); if (npcs.npcInInteriorId == Guid.Empty) { npc.npcInInterior = null; } else { // THIS SHOULD NEVER RUN BECAUSE NPCS THAT ARE IN INTERIORS ARE DESERIALIZED ELSEWHERE // check if the interior already exists foreach (KeyValuePair <Guid, Interior> interior in BoundingBoxLocations.interiorMap) { if (npcs.npcInInteriorId == interior.Key) { npc.npcInInterior = interior.Value; break; } } } npc.onShip = npcs.onShip; npc.regionKey = npcs.region; npc.health = npcs.health; UpdateOrder.Add(npc); } else if (objState.GetType() == typeof(ShipState)) { ShipState ss = (ShipState)objState; Ship s = (Ship)DeserializeModel(ss.objKey, ss); UpdateOrder.Add(s); } else if (objState.GetType() == typeof(StructureState)) { StructureState ss = (StructureState)objState; Structure s = (Structure)DeserializeModel(ss.objKey, ss); UpdateOrder.Add(s); } else if (objState.GetType() == typeof(WeatherSaveState)) { WeatherSaveState wss = (WeatherSaveState)objState; WeatherState.currentLightIntensity = wss.currentLightIntensity; WeatherState.currentMsOfDay = wss.currentMsOfDay; WeatherState.sunAngleX = wss.sunAngleX; WeatherState.shadowTransparency = wss.shadowTransparency; WeatherState.totalDays = wss.nDays; WeatherState.weatherDuration = wss.weatherDuration; WeatherState.msThroughWeather = wss.msThroughWeather; WeatherState.rainState = wss.rainState; WeatherState.rainIntensity = wss.rainIntensity; WeatherState.lightning = wss.lightning; // set the rain for (int i = 0; i < WeatherState.rainIntensity; i++) { WeatherState.rain.Add(new RainDrop(_content, _graphics)); } } else if (objState.GetType() == typeof(OnGroundState)) { OnGroundState ogs = (OnGroundState)objState; Sprite sp = null; if (ogs.inventoryItem) { InventoryItem ii = DeserializeInventoryItem(ogs.objKey); ii.amountStacked = ogs.amountStacked; ii.onGround = true; sp = ii; } else { sp = DeserializeModel(ogs.objKey, objState); } sp.location = ogs.location; sp.regionKey = ogs.region; ItemUtility.ItemsToUpdate.Add(sp); } } // go through newly created world state and set any interiors for the object that owns that interior foreach (Sprite sp in UpdateOrder) { if (sp is IHasInterior) { IHasInterior hasInterior = (IHasInterior)sp; Guid interiorForId = hasInterior.GetInteriorForId(); if (sp is IShip) { Ship sh = (Ship)sp; sh.shipInterior = interiorForObjMap[interiorForId]; } else if (sp is IStructure) { Structure st = (Structure)sp; st.structureInterior = interiorForObjMap[interiorForId]; } interiorForObjMap[interiorForId].interiorForObj = sp; } } // set player's current ship, the interior will be the ships interior if (player.onShip) { player.playerOnShip = (Ship)BoundingBoxLocations.interiorMap[player.playerInInterior.interiorId].interiorForObj; } }
public void SaveGameState() { // Create the save state List <ISaveState> SaveState = new List <ISaveState>(); Dictionary <Interior, Guid> interiorMap = new Dictionary <Interior, Guid>(); // save all interior states foreach (var interior in BoundingBoxLocations.interiorMap) { InteriorState its = SerializeInterior(interior.Value); interiorMap.Add(interior.Value, interior.Key); SaveState.Add(its); } // save the world state foreach (Sprite sp in UpdateOrder) { // PLAYER if (sp.GetType().BaseType == typeof(Gusto.Models.Animated.PlayerPirate)) { PlayerState state = new PlayerState(); state.team = player.teamType; state.location = player.location; state.interiorEntranceLocation = player.entranceLoc; state.region = player.regionKey; state.inventory = SerializeInventory(player.inventory); state.onShip = player.onShip; // set the player in their interior if they save while on ship if (player.onShip) { player.playerInInterior = player.playerOnShip.shipInterior; } if (player.playerInInterior != null) { state.playerInInteriorId = interiorMap[player.playerInInterior]; // interiorMap should be full since we ran interiors before this } else { state.playerInInteriorId = Guid.Empty; } state.inHandItemKey = player.inHand.itemKey; state.health = player.health; SaveState.Add(state); } else if (sp.GetType().BaseType == typeof(Gusto.Models.Animated.Npc)) { Npc npc = (Npc)sp; NpcState state = new NpcState(); state.team = npc.teamType; state.location = npc.location; state.objKey = npc.bbKey; state.region = npc.regionKey; state.inventory = SerializeInventory(npc.inventory); state.onShip = npc.onShip; state.npcInInteriorId = Guid.Empty; state.health = npc.health; SaveState.Add(state); } else if (sp.GetType().BaseType == typeof(Gusto.Models.Animated.Ship)) { Ship sh = (Ship)sp; ShipState state = new ShipState(); state.team = sh.teamType; state.location = sh.location; state.region = sh.regionKey; state.objKey = sh.bbKey; state.actionInventory = SerializeInventory(sh.actionInventory); state.playerAboard = sh.playerAboard; state.anchored = sh.anchored; state.health = sh.health; state.shipId = sh.GetInteriorForId(); SaveState.Add(state); } else if (sp.GetType().BaseType == typeof(Gusto.Models.Animated.Structure)) { Structure st = (Structure)sp; StructureState state = new StructureState(); state.team = st.teamType; state.location = st.location; state.region = st.regionKey; state.objKey = st.bbKey; state.structureId = st.GetInteriorForId(); SaveState.Add(state); } } // All objs on ground foreach (var item in ItemUtility.ItemsToUpdate) { OnGroundState ogs = new OnGroundState(); ogs.objKey = item.bbKey; ogs.region = item.regionKey; ogs.team = TeamType.Gusto; ogs.location = item.location; if (item is IInventoryItem) { ogs.inventoryItem = true; InventoryItem ii = (InventoryItem)item; ogs.amountStacked = ii.amountStacked; } else { ogs.inventoryItem = false; } if (item is IStorage) { Storage storage = (Storage)item; ogs.inventory = SerializeInventory(storage.inventory); } else if (item is IContainer) { Container cont = (Container)item; ogs.inventory = SerializeInventory(cont.drops); } SaveState.Add(ogs); } // Can do the weather state separately becasue it is not in the updateOrder WeatherSaveState wss = new WeatherSaveState(); wss.currentMsOfDay = WeatherState.currentMsOfDay; wss.currentLightIntensity = WeatherState.currentLightIntensity; wss.sunAngleX = WeatherState.sunAngleX; wss.shadowTransparency = WeatherState.shadowTransparency; wss.nDays = WeatherState.totalDays; wss.weatherDuration = WeatherState.weatherDuration; wss.msThroughWeather = WeatherState.msThroughWeather; wss.rainState = WeatherState.rainState; wss.rainIntensity = WeatherState.rainIntensity; wss.lightning = WeatherState.lightning; SaveState.Add(wss); // serialize save to file system DataContractSerializer s = new DataContractSerializer(typeof(List <ISaveState>)); using (FileStream fs = new FileStream(savePath + "GustoGame_" + gameName, FileMode.Create)) { s.WriteObject(fs, SaveState); } }