Exemplo n.º 1
0
        /// <summary>
        /// We can't serialize dictionary so we transform to an array before saving. Don't use this in any other context
        /// </summary>
        SimpleBuildingInstance[] GetBuildingInstances()
        {
            SimpleBuildingInstance[] items = new SimpleBuildingInstance[this.buildings.Count];
            int counter = 0;

            foreach (var item in buildings)
            {
                item.Value.buildingInstanceData.key = item.Key;
                items[counter] = item.Value.buildingInstanceData;
                counter++;
            }

            return(items);
        }
Exemplo n.º 2
0
        public virtual void ConstructBuildingInTile(int x, int y, int buildingIndex, int direction)
        {
            SimpleBuildingInstance newBuilding = new SimpleBuildingInstance(direction, buildingIndex);

            if (IsBuildingPositionAllowed(x, y, newBuilding.direction, buildingTypes[newBuilding.buildingTypeIndex]))
            {
                int key = world.GetTileCoordKey(x, y);
                newBuilding.key = key;
                InstantiateBuilding(x, y, newBuilding);
            }
            else
            {
                Debug.Log("Building placement was rejected even though it had been approved in player controller. Maybe terrain was edited?");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Instantiates the building by actually creating the gameobject and assigning it to the world
        /// </summary>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        /// <param name="buildingInstance">Building instance.</param>
        protected void InstantiateBuilding(int x, int y, SimpleBuildingInstance buildingInstance)
        {
            SimpleBuildingType buildingType = buildingTypes[buildingInstance.buildingTypeIndex];

            int width  = buildingType.buildArea.width;
            int length = buildingType.buildArea.length;

            //If we turned the building -90 or 90 degrees then switch width and length
            if (buildingInstance.direction % 2 == 1)
            {
                width  = buildingType.buildArea.length;
                length = buildingType.buildArea.width;
            }


            int startX = x;
            int stopX  = x + width - 1;
            int startY = y;
            int stopY  = y + length - 1;

            buildingInstance.x = x;
            buildingInstance.y = y;

            //Calculate dead center of building area to place the actual game object
            Vector3 pos = (world.GetWorldPositionFromTile(startX, startY, true) + world.GetWorldPositionFromTile(stopX, stopY, true)) / 2;

            GameObject building = (GameObject)GameObject.Instantiate(buildingType.prefab, pos, Quaternion.identity);

            building.name = buildingType.name;

            Collider buildingCollider = building.GetComponent <Collider>();

            if (buildingCollider != null)
            {
                buildingCollider.enabled = true;
            }
            Vector3 scale = building.transform.localScale;

            scale = Vector3.Scale(buildingType.scale, scale);
            building.transform.localScale = scale;

            Vector3 rot = building.transform.eulerAngles;

            rot.y = 90 * buildingInstance.direction;
            building.transform.rotation = Quaternion.Euler(rot);

            if (buildingType.doLeanWithTerrain)
            {
                if (world.isTileLeaningDownX(x, y))
                {
                    building.transform.Rotate(45, 0, 0);
                }
                if (world.isTileLeaningUpX(x, y))
                {
                    building.transform.Rotate(-45, 0, 0);
                }
                if (world.isTileLeaningDownY(x, y))
                {
                    building.transform.Rotate(-45, 0, 0);
                }
                if (world.isTileLeaningUpY(x, y))
                {
                    building.transform.Rotate(45, 0, 0);
                }
            }

            SimpleBuildingBehaviour buildingBehaviour = building.GetComponent <SimpleBuildingBehaviour>();

            buildingBehaviour.positionKey          = world.GetTileCoordKey(x, y);
            buildingBehaviour.buildingType         = buildingType;
            buildingBehaviour.buildingInstanceData = buildingInstance;
            buildingBehaviour.width  = width;
            buildingBehaviour.length = length;
            buildingBehaviour.Setup();

            //Add construction utilities if applicable
            if (buildingBehaviour.underConstructionBuilding != null &&
                constructionUtilitiesPrefab != null)
            {
                GameObject constructionUtilities = Instantiate <GameObject>(constructionUtilitiesPrefab);
                constructionUtilities.transform.SetParent(buildingBehaviour.underConstructionBuilding.transform);
                constructionUtilities.transform.localPosition = Vector3.zero;
            }

            //We add a reference to this building in every tile it occupies
            for (int i = x; i <= stopX; i++)
            {
                for (int j = y; j <= stopY; j++)
                {
                    int key = world.GetTileCoordKey(i, j);
                    buildingTiles.Add(key, buildingBehaviour);
                }
            }

            if (OnBuildingsChanged != null)
            {
                OnBuildingsChanged();
            }
        }