예제 #1
0
파일: Building.cs 프로젝트: MK4H/MHUrho
            /// <summary>
            /// Builds the building at <paramref name="topLeftCorner"/> if it is possible.
            /// </summary>
            /// <param name="id">The identifier of the new building.</param>
            /// <param name="topLeftCorner">Position of the top left corner of the building in the game world.</param>
            /// <param name="rotation">Initial rotation of the building after it is built.</param>
            /// <param name="type">Type of the building.</param>
            /// <param name="player">Owner of the building.</param>
            /// <param name="level">The level the building is being built in.</param>
            /// <returns>Null if it is not possible to build the building there, new Building if it is possible</returns>
            /// <exception cref="CreationException">Thrown when there was an exception during building creation, like missing assets or error in the plugin.</exception>
            public static Building CreateNew(int id,
                                             IntVector2 topLeftCorner,
                                             Quaternion rotation,
                                             BuildingType type,
                                             IPlayer player,
                                             ILevelManager level)
            {
                if (!type.CanBuild(topLeftCorner, player, level))
                {
                    return(null);
                }

                var rect = new IntRect(topLeftCorner.X,
                                       topLeftCorner.Y,
                                       topLeftCorner.X + type.Size.X,
                                       topLeftCorner.Y + type.Size.Y);
                Vector2 center   = rect.Center();
                Vector3 position = new Vector3(center.X,
                                               level.Map.GetTerrainHeightAt(center),
                                               center.Y);


                Node buildingNode;

                try {
                    buildingNode = type.Assets.Instantiate(level, position, rotation);
                }
                catch (Exception e)
                {
                    string message = $"There was an Exception while creating a new building: {e.Message}";
                    Urho.IO.Log.Write(LogLevel.Error, message);
                    throw new CreationException(message, e);
                }

                Building newBuilding = null;

                try {
                    new BuildingComponentSetup().SetupComponentsOnNode(buildingNode, level);

                    newBuilding = new Building(id, level, rect, type, player);
                    buildingNode.AddComponent(newBuilding);

                    newBuilding.BuildingPlugin = newBuilding.BuildingType.GetNewInstancePlugin(newBuilding, level);
                    return(newBuilding);
                }
                catch (Exception e) {
                    if (newBuilding != null)
                    {
                        newBuilding.RemoveFromLevel();
                    }
                    else
                    {
                        buildingNode.Remove();
                    }
                    string message = $"There was an Exception while creating a new building: {e.Message}";
                    Urho.IO.Log.Write(LogLevel.Error, message);
                    throw new CreationException(message, e);
                }
            }
예제 #2
0
파일: Building.cs 프로젝트: MK4H/MHUrho
            /// <summary>
            /// Executes the first step of loading process.
            /// </summary>
            public void StartLoading()
            {
                if (type.ID != storedBuilding.TypeID)
                {
                    throw new ArgumentException("Provided type is not the type of the stored building", nameof(type));
                }

                IntVector2 topLeftCorner = storedBuilding.Location.ToIntVector2();
                var        rect          = new IntRect(topLeftCorner.X,
                                                       topLeftCorner.Y,
                                                       topLeftCorner.X + type.Size.X,
                                                       topLeftCorner.Y + type.Size.Y);
                Vector2 center   = rect.Center();
                Vector3 position = new Vector3(center.X,
                                               level.Map.GetTerrainHeightAt(center),
                                               center.Y);
                Quaternion rotation = storedBuilding.Rotation.ToQuaternion();


                Node buildingNode = type.Assets.Instantiate(level, position, rotation);

                new BuildingComponentSetup().SetupComponentsOnNode(buildingNode, level);

                loadingBuilding = new Building(storedBuilding.Id, level, rect, type);
                buildingNode.AddComponent(loadingBuilding);


                loadingBuilding.BuildingPlugin = type.GetInstancePluginForLoading(loadingBuilding, level);

                foreach (var defaultComponent in storedBuilding.DefaultComponents)
                {
                    var componentLoader =
                        level.DefaultComponentFactory
                        .StartLoadingComponent(defaultComponent,
                                               level,
                                               loadingBuilding.BuildingPlugin);

                    componentLoaders.Add(componentLoader);
                    loadingBuilding.AddComponent(componentLoader.Component);
                }
            }