Пример #1
0
            /// <summary>
            /// Stores the <paramref name="building"/> in a <see cref="StBuilding"/> instance for serialization.
            /// </summary>
            /// <param name="building">The building to store.</param>
            /// <returns>Building stored in <see cref="StBuilding"/> ready for serialization.</returns>
            public static StBuilding Save(Building building)
            {
                var stBuilding = new StBuilding {
                    Id         = building.ID,
                    TypeID     = building.BuildingType.ID,
                    PlayerID   = building.Player.ID,
                    Location   = building.TopLeft.ToStIntVector2(),
                    Rotation   = building.Node.Rotation.ToStQuaternion(),
                    UserPlugin = new PluginData()
                };

                try {
                    building.BuildingPlugin.SaveState(new PluginDataWrapper(stBuilding.UserPlugin, building.Level));
                }
                catch (Exception e)
                {
                    string message = $"Saving building plugin failed with Exception: {e.Message}";
                    Urho.IO.Log.Write(LogLevel.Error, message);
                    throw new SavingException(message, e);
                }

                foreach (var component in building.Node.Components)
                {
                    var defaultComponent = component as DefaultComponent;
                    if (defaultComponent != null)
                    {
                        stBuilding.DefaultComponents.Add(defaultComponent.SaveState());
                    }
                }

                return(stBuilding);
            }
Пример #2
0
            /// <summary>
            /// Creates a loader for the <paramref name="storedBuilding"/> that loads it into
            /// the <paramref name="level"/>.
            /// </summary>
            /// <param name="level">The level the building is being loaded to.</param>
            /// <param name="storedBuilding">The saved building to load.</param>
            public Loader(LevelManager level,
                          StBuilding storedBuilding)
            {
                this.level          = level;
                this.storedBuilding = storedBuilding;
                componentLoaders    = new List <DefaultComponentLoader>();

                type = level.Package.GetBuildingType(storedBuilding.TypeID);
                if (type == null)
                {
                    throw new ArgumentException("Type of this building was not loaded");
                }
            }