Exemplo n.º 1
0
            /// <summary>
            /// Stores the unit state in an instance of <see cref="StUnit"/> for serialization.
            /// </summary>
            /// <param name="unit">The unit to save.</param>
            /// <returns>Stored state of the unit.</returns>
            public static StUnit Save(Unit unit)
            {
                var storedUnit = new StUnit
                {
                    Id         = unit.ID,
                    Position   = unit.Position.ToStVector3(),
                    Rotation   = unit.Node.Rotation.ToStQuaternion(),
                    PlayerID   = unit.Player.ID,
                    TypeID     = unit.UnitType.ID,
                    UserPlugin = new PluginData()
                };

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

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

                return(storedUnit);
            }
Exemplo n.º 2
0
            /// <summary>
            /// Loads unit component from <paramref name="storedUnit"/> and all other needed components
            /// </summary>
            /// <param name="level"></param>
            /// <param name="storedUnit">stored unit</param>
            public Loader(LevelManager level, StUnit storedUnit)
            {
                this.level               = level;
                this.storedUnit          = storedUnit;
                this.defComponentLoaders = new List <DefaultComponentLoader>();

                type = level.Package.GetUnitType(storedUnit.TypeID);
                if (type == null)
                {
                    throw new ArgumentException("Type of this unit was not loaded");
                }
            }