Example #1
0
        /// <summary>
        /// Creates a building in the given position.
        /// </summary>
        /// <returns>The building, if the position is available for
        /// construction, or <code>null</code>.</returns>
        ///
        /// <param name="position">The position of the building.</param>
        /// <param name="rotation">The rotation of the building.</param>
        /// <param name="type">The type of building.</param>
        /// <param name="race">The race this building belongs to.</param>
        public GameObject createBuilding(Vector3 position, Quaternion rotation,
                                         Storage.BuildingTypes type, Storage.Races race, bool checkFow, float yOffset = 0)
        {
            GameObject obj = null;

            position.y += yOffset;
            position    = grid.discretizeMapCoords(position);
            if (grid.isNewPositionAbleForConstrucction(position, checkFow))
            {
                position.y -= yOffset - 0.1f;
                obj         = Storage.Info.get.createBuilding(race, type, position, rotation);
                if (type == Storage.BuildingTypes.STRONGHOLD)
                {
                    grid.reservePositionForStronghold(position);
                }
                grid.reservePosition(position);
            }
            return(obj);
        }
Example #2
0
        /// <summary>
        /// Reads a map and syncronizes it with the construction grid
        /// </summary>
        /// <param name="mapData"></param>
        public void parseMapData(Texture2D mapData)
        {
            Color[] pixels = mapData.GetPixels();


            Vector2 center = ai.race == Races.ELVES ? new Vector2(mapData.width / 2 + 0.5f, mapData.height / 2 - 0.5f) : new Vector2(mapData.width / 2 - 0.5f, mapData.height / 2 - 1f);

            // Math Facts:
            // The equation to find te position of something is
            // Offset = (i , j) - Center
            // centerPos + GridSize * Offset

            Vector3 processingPos    = Vector3.zero;
            Vector2 processingOffset = Vector2.zero;

            processingPos.y = ai.rootBasePosition.y;

            for (int i = 0; i < mapData.height; i++)
            {
                for (int j = 0; j < mapData.width; j++)
                {
                    Color pixel = pixels[j + i * mapData.width];
                    processingOffset = new Vector2(i, j) - center;
                    processingPos.x  = ai.rootBasePosition.x + constructionGrid.getDimensions().x *processingOffset.x;
                    processingPos.z  = ai.rootBasePosition.z + constructionGrid.getDimensions().y *processingOffset.y;
                    processingPos    = constructionGrid.discretizeMapCoords(processingPos);

                    //In order to be more flexible we need to check if we can construct
                    if (!constructionGrid.isNewPositionAbleForConstrucction(processingPos, false))
                    {
                        continue;
                    }

                    if (CompareColors(pixel, stronghold))
                    {
                        Debug.Log("Stronghold");
                    }
                    else if (CompareColors(pixel, militaryBuilding))
                    {
                        avaliablePositions[StructureType.MILITARY_BUILDING].Add(processingPos);
                    }
                    else if (CompareColors(pixel, resourcesBuilding))
                    {
                        avaliablePositions[StructureType.RESOURCE_BUILDING].Add(processingPos);
                    }
                    else if (CompareColors(pixel, tower))
                    {
                        avaliablePositions[StructureType.TOWER].Add(processingPos);
                    }
                    else if (CompareColors(pixel, horizontallWall))
                    {
                        avaliablePositions[StructureType.HORIZONTALL_WALL].Add(processingPos);
                    }
                    else if (CompareColors(pixel, verticallWall))
                    {
                        avaliablePositions[StructureType.VERTICALL_WALL].Add(processingPos);
                    }
                    else if (CompareColors(pixel, cornerWall))
                    {
                        avaliablePositions[StructureType.CORNER_WALL].Add(processingPos);
                    }
                    else if (CompareColors(pixel, defenceZone))
                    {
                        baseCriticPoints.Add(processingPos);
                        Debug.Log(baseCriticPoints.Count);
                    }
                    else if (CompareColors(pixel, emptySpace))
                    {
                        continue;
                    }
                    else if (CompareColors(pixel, ignorePixel))
                    {
                        continue;
                    }
                    else
                    {
                        Debug.Log("Unknown Structure" + pixel.ToString());
                    }
                }
            }
            Debug.Log("Reading Complete");
        }