Exemple #1
0
    /// <summary>
    /// Load grid data and restore their contents.
    /// </summary>
    public void LoadDataFromSave()
    {
        if (gridInfo.Count == 0)
        {
            return;
        }

        List <BuildingMap> buildingMap = BuildingLibrary.GetBuildingsForCategory(BuildingCategory.All);

        //Loop and restore each grid entity for each grid.
        for (int i = 0; i < gridInfo.Count; i++)
        {
            Game.CurrentSession.City.CreateGrid(gridInfo[i].width, gridInfo[i].height, gridInfo[i].worldPos);
            GridSystem grid = Game.CurrentSession.City.GetGrid(i);

            for (int j = 0; j < gridInfo[i].gridEntities.Count; j++)
            {
                int x     = gridInfo[i].gridEntities[j].x;
                int y     = gridInfo[i].gridEntities[j].y;
                int index = gridInfo[i].gridEntities[j].z;

                //Manually handle the road typing.
                Type entityType;
                if (index == -1)
                {
                    entityType = typeof(Road);
                }
                else
                {
                    entityType = buildingMap[index].BuildingType;
                }

                TileEntity tileEnt = Activator.CreateInstance(entityType) as TileEntity;

                grid.AddTileEntityToTile(x, y, tileEnt);
            }

            //Restore residents AFTER grid entities have been restored.
            foreach (ResidentData resInfo in gridInfo[i].residentData)
            {
                Resident newResident = null; //Silly vs
                try
                {
                    newResident = new Resident(resInfo);
                    Game.CurrentSession.City.Residents.Add(newResident);
                    Residential res = (Residential)Game.CurrentSession.City.GetGrid(resInfo.HomeGridID).GetTile(resInfo.HomePosition)?.Entity;
                    if (res != null)
                    {
                        res.AddResident(newResident);
                    }
                    else
                    {
                        Game.CurrentSession.City.ProcessHomelessResident(newResident);
                    }

                    foreach (Job j in ((Commercial)Game.CurrentSession.City.GetGrid(resInfo.JobGridID).GetTile(resInfo.JobPosition).Entity).Jobs)
                    {
                        if (!j.Taken)
                        {
                            newResident.SetJob(j);
                            break;
                        }
                    }
                    if (newResident.Job == null)
                    {
                        //Coult not give this resident a job? leave it unemployed.
                        Game.CurrentSession.City.UnemployedResidents.Add(newResident);
                    }
                }
                catch (Exception)
                {
                    //Abandon loading this resident.
                    //Try to remove it if we added it earlier.
                    if (newResident != null)
                    {
                        if (Game.CurrentSession.City.Residents.Contains(newResident))
                        {
                            Game.CurrentSession.City.Residents.Remove(newResident);
                        }
                    }
                    //Increase demand to offset the lost resident.
                    Game.CurrentSession.Settings.ResidentialDemand++;
                }
            }
        }
    }