Example #1
0
        /// <summary>
        /// Load the campus game data.
        /// </summary>
        /// <param name="gameData">Campus game data.</param>
        protected override void LoadData(CampusData gameData)
        {
            var gridMeshArgs = new GridMeshArgs()
            {
                GridSquareSize = Constant.GridSize,
                GridStepSize   = Constant.GridStepSize,
                CountX         = gameData.Terrain.GridCountX,
                CountZ         = gameData.Terrain.GridCountZ,
                CountY         = gameData.Terrain.GridCountY,
                StartingHeight = gameData.Terrain.StartingHeight,
                GridMaterial   = gameData.Terrain.TerrainMaterial,
                SkirtPrefab    = gameData.Terrain.TerrainSkirt,
            };

            GridMesh terrain;

            CampusFactory.GenerateTerrain(transform, gridMeshArgs, out terrain);

            Terrain   = terrain;
            Buildings = new CampusBuildings(terrain);
            Paths     = new CampusPaths(terrain);

            Game.State.RegisterController(GameState.SelectingTerrain, new SelectingTerrainController(terrain));
            Game.State.RegisterController(GameState.EditingTerrain, new EditingTerrainController(terrain));
            Game.State.RegisterController(GameState.PlacingConstruction, new PlacingConstructionController(terrain));
            Game.State.RegisterController(GameState.SelectingPath, new SelectingPathController(terrain));
            Game.State.RegisterController(GameState.PlacingPath, new PlacingPathController(terrain));

            var footprintCreatorObject = new GameObject("FootprintCreator");

            using (var footprintCreator = footprintCreatorObject.AddComponent <FootprintCreator>())
            {
                // Load the buildings
                foreach (var buildingData in gameData.Buildings)
                {
                    buildingData.Footprint = footprintCreator.CalculateFootprint(buildingData.Mesh, Constant.GridSize);
                    _buildingRegistry[buildingData.Name] = buildingData;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Build a building at the location.
        /// Registers the taken grid location with the Safe Terrain Editor.
        /// </summary>
        /// <param name="building">The building to construct.</param>
        /// <param name="location">The location of the building.</param>
        public void Build(BuildingData building, Point3 location)
        {
            if (Application.isEditor)
            {
                var footprint    = building.Footprint;
                var validTerrain = _terrain.Editor.CheckFlatAndFree(location.x, location.z, footprint.GetLength(0), footprint.GetLength(1));
                for (int x = 0; x < footprint.GetLength(0); ++x)
                {
                    for (int z = 0; z < footprint.GetLength(1); ++z)
                    {
                        Assert.IsFalse(footprint[x, z] && !validTerrain[x, z], string.Format("Placing building {0} at an invalid location!!!", building.Name));
                    }
                }
            }

            _buildings.Add(building);
            CampusFactory.GenerateBuilding(
                building,
                Game.Campus.transform,
                _terrain.Convert.GridToWorld(location) + new Vector3(0f, 0.01f, 0f) /* Place just above the grass*/,
                Quaternion.identity);

            _terrain.Editor.SetAnchoredGrid(location.x, location.z, building.Footprint);
        }