Ejemplo n.º 1
0
    private void createMap()
    {
        map = new HexMap(UnityEngine.Random.Range(minSize, maxSize), UnityEngine.Random.Range(minSize, maxSize));
        for (int column = 0; column < HexMap.NumColumns; column++)
        {
            for (int row = 0; row < HexMap.NumRows; row++)
            {
                // Instantiate a Hex
                HexContinent h   = new HexContinent(column, row);
                Vector3      pos = h.hex.PositionFromCamera(
                    Camera.main.transform.position,
                    HexMap.NumRows,
                    HexMap.NumColumns
                    );


                GameObject hexGO = (GameObject)Instantiate(
                    HexPrefab,
                    pos,
                    Quaternion.identity,
                    mapHolder.transform
                    );
                HexComponent hexC = hexGO.GetComponent <HexComponent>();
                hexC.Hex = h;
                //hexC.Hex.hex.HexMap = map;

                map.AddHex(hexC.Hex);

                // MeshRenderer mr = hexGO.GetComponentInChildren<MeshRenderer>();
                // mr.material = HexMaterials[Random.Range(0, HexMaterials.Length)];
            }
        }
        JsonManager <HexMap> .saveJson(map);
    }
Ejemplo n.º 2
0
    private void loadMap()
    {
        foreach (HexContinent h in map.hexList)
        {
            //loop through the map and create a position for each tile
            Vector3 pos = h.hex.PositionFromCamera(
                Camera.main.transform.position,
                HexMap.NumRows,
                HexMap.NumColumns
                );

            //Instantiate a hex component object for the game world
            GameObject hexGO = (GameObject)Instantiate(
                HexPrefab,
                pos,
                Quaternion.identity,
                mapHolder.transform
                );
            HexComponent hexC = hexGO.GetComponent <HexComponent>();
            hexC.Hex = h;
            //hexC.Hex.hex.HexMap = map;

            //Add any building to the tile
            if (h.hex.hasBuilding)
            {
                Debug.Log("Loading - " + Application.dataPath + "/prefabs/" + h.hex.BuildingType + ".prefab");
                hexC.ForceBuild((GameObject)UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/prefabs/" + h.hex.BuildingType + ".prefab", typeof(GameObject)));
            }
        }
    }
Ejemplo n.º 3
0
    //private void unselectAll()
    //{
    //    foreach (HexComponent h in selectedHexList)
    //    {
    //        h.hexModel.material.color = Color.white;

    //    }
    //    selectedHexList = new List<HexComponent>();

    //}

    public void removeHexFromSelected(HexComponent h)
    {
        if (h = selectedHex)
        {
            h.Hex.hex.selected        = false;
            h.hexModel.material.color = h.Hex.hex.curColor;
            lastRemoved = h;
        }
    }
Ejemplo n.º 4
0
    internal void AddCell(GameObject tile, int x, int y)
    {
        var worldPos = CalculateWorldPosition(x, y);

        var gridComponent = new HexComponent {
            RenderObject = tile, WorldPosition = worldPos
        };

        grid[x, y] = gridComponent;
    }
Ejemplo n.º 5
0
        public Vector2 GetBottomRightCornerOfMapInPixelCoords()
        {
            HexComponent bottomRight = MapNode.GetChild <HexComponent>(MapNode.GetChildCount() - 1);

            Sprite sprite = bottomRight.GetNode <Sprite>(bottomRight.SpritePath);

            var size = sprite.Texture.GetSize(); //width and height in pixels

            return(new Vector2(bottomRight.Position.x + (size.x / 2f), bottomRight.Position.y + (size.y / 2f)));
        }
Ejemplo n.º 6
0
 // If we can't use dictionary we will have to use this
 public HexContinent getHex(HexComponent h)
 {
     foreach (HexContinent hex in hexList)
     {
         if (hex == h.Hex)
         {
             return(hex);
         }
     }
     return(null);
 }
Ejemplo n.º 7
0
    public void PanToHex(HexComponent hexComp)
    {
        // TODO: Move camera to hexS

        StartCoroutine(disableMouse());

        transform.position = Vector3.Lerp(transform.position, new Vector3(hexComp.transform.position.x, transform.position.y, hexComp.transform.position.z), lerpSpeed * Time.time);



        //transform.position = new Vector3(hexComp.transform.position.x, transform.position.y, hexComp.transform.position.z);
    }
Ejemplo n.º 8
0
 // [SerializeField]int s;
 public void Construct(int q, int r, HexComponent hexComponent)
 {
     Q         = (byte)q;
     R         = (byte)r;
     component = hexComponent;
     component.meshRenderer.material = HexMap.instance.emptyHexMat;
     transform.localScale            = HEX_SIZE_MULTIPLIER * Vector3.one;
     //s = S();
     //  SetHeight(height);
     // compressedProperties = (byte)(hasResources ? (compressedProperties | HAS_RESOURCES) : (compressedProperties & ~HAS_RESOURCES));
     // compressedProperties = (byte)(isWater ? (compressedProperties | IS_WATER) : (compressedProperties & ~IS_WATER));
     //S = -(q + r);
 }
Ejemplo n.º 9
0
    public void UpdateHexVisuals()
    {
        for (int column = 0; column < NumColumns; column++)
        {
            for (int row = 0; row < NumRows; row++)
            {
                Hex        h     = hexes[column, row];
                GameObject hexGO = hexToGameObjectMap[h];

                HexComponent hexComp = hexGO.GetComponentInChildren <HexComponent>();
                MeshRenderer mr      = hexGO.GetComponentInChildren <MeshRenderer>();
                MeshFilter   mf      = hexGO.GetComponentInChildren <MeshFilter>();


                h.TerrainType   = Hex.TERRAIN_TYPE.GRASSLANDS;
                h.ElevationType = Hex.ELEVATION_TYPE.FLAT;
                mf.mesh         = MeshFlat;

                if (h.Elevation >= HeightMountain)
                {
                    mr.material     = MatMountains;
                    mf.mesh         = MeshMountain;
                    h.ElevationType = Hex.ELEVATION_TYPE.MOUNTAIN;
                }
                else if (h.Elevation >= HeightHill)
                {
                    h.ElevationType        = Hex.ELEVATION_TYPE.HILL;
                    mf.mesh                = MeshHill;
                    hexComp.VerticalOffset = 0.25f;
                }
                else if (h.Elevation >= HeightFlat)
                {
                    h.ElevationType = Hex.ELEVATION_TYPE.FLAT;
                    mf.mesh         = MeshFlat;
                }
                else
                {
                    h.ElevationType = Hex.ELEVATION_TYPE.WATER;
                    mr.material     = MatOcean;
                    mf.mesh         = MeshWater;
                }

                hexGO.GetComponentInChildren <TextMesh>().text =
                    string.Format("{0},{1}", column, row);
            }
        }
    }
Ejemplo n.º 10
0
    public void UpdateHexVisuals()
    {
        for (int column = 0; column < numColumns; column++)
        {
            for (int row = 0; row < numRows; row++)
            {
                Hex          hex     = hexes[column, row];
                GameObject   hexGO   = hexToGameObjectMap[hex];
                HexComponent hexComp = hexGO.GetComponentInChildren <HexComponent>();
                MeshRenderer hexMR   = hexGO.GetComponentInChildren <MeshRenderer>();
                MeshFilter   hexMF   = hexGO.GetComponentInChildren <MeshFilter>();

                setHexTypeFromElevationAndMoisture(hex, hexMR, hexMF, hexGO, hexComp);
                hexGO.GetComponentInChildren <TextMesh>().text = string.Format("{0}, {1} \n{2}", column, row, hex.BaseMovementCost(false, false, false));
            }
        }
    }
Ejemplo n.º 11
0
    public void UpdateHexTerrains()
    {
        for (int column = 0; column < MapColumns; column++)
        {
            for (int row = 0; row < MapRows; row++)
            {
                Hex h = hexes[column, row];

                GameObject hexGO = _hexToGameObjectMap[h];

                HexComponent hexComponent = hexGO.GetComponentInChildren <HexComponent>();
                MeshRenderer meshRenderer = hexGO.GetComponentInChildren <MeshRenderer>();

                int biomeId = 0; // In the future biome id will be determinated procedurally

                if (h.Elevation >= HeightMountain)
                {
                    h.hexTerrain = getTypeRandomFromDatabaseBy(TerrainType.MOUNTAIN, biomeId);
                }
                else if (h.Elevation >= HeightHill)
                {
                    h.hexTerrain = getTypeRandomFromDatabaseBy(TerrainType.HILL, biomeId);
                }
                else if (h.Elevation >= HeightFlat)
                {
                    h.hexTerrain = getTypeRandomFromDatabaseBy(TerrainType.FLAT, biomeId);
                }
                else
                {
                    h.hexTerrain = getTypeRandomFromDatabaseBy(TerrainType.OCEAN, biomeId);
                }

                meshRenderer.material = h.hexTerrain.Material; // Update material;
            }
        }
    }
Ejemplo n.º 12
0
    public void UpdateHexVisuals()
    {
        for (int column = 0; column < NumColumns; column++)
        {
            for (int row = 0; row < NumRows; row++)
            {
                Hex        h     = hexes[column, row];
                GameObject hexGO = hexToGameObjectMap[h];

                HexComponent hexComp = hexGO.GetComponentInChildren <HexComponent>();
                MeshRenderer mr      = hexGO.GetComponentInChildren <MeshRenderer>();
                MeshFilter   mf      = hexGO.GetComponentInChildren <MeshFilter>();


                if (h.Elevation >= HeightFlat && h.Elevation < HeightMountain)
                {
                    if (h.Moisture >= MoistureJungle)
                    {
                        mr.material   = MatGrasslands;
                        h.TerrainType = Hex.TERRAIN_TYPE.GRASSLANDS;
                        h.FeatureType = Hex.FEATURE_TYPE.RAINFOREST;

                        // Spawn trees
                        Vector3 p = hexGO.transform.position;
                        if (h.Elevation >= HeightHill)
                        {
                            p.y += 0.25f;
                        }


                        GameObject.Instantiate(JunglePrefab, p, Quaternion.identity, hexGO.transform);
                    }
                    else if (h.Moisture >= MoistureForest)
                    {
                        mr.material   = MatGrasslands;
                        h.TerrainType = Hex.TERRAIN_TYPE.GRASSLANDS;
                        h.FeatureType = Hex.FEATURE_TYPE.FOREST;

                        // Spawn trees
                        Vector3 p = hexGO.transform.position;
                        if (h.Elevation >= HeightHill)
                        {
                            p.y += 0.25f;
                        }
                        GameObject.Instantiate(ForestPrefab, p, Quaternion.identity, hexGO.transform);
                    }
                    else if (h.Moisture >= MoistureGrasslands)
                    {
                        mr.material   = MatGrasslands;
                        h.TerrainType = Hex.TERRAIN_TYPE.GRASSLANDS;
                    }
                    else if (h.Moisture >= MoisturePlains)
                    {
                        mr.material   = MatPlains;
                        h.TerrainType = Hex.TERRAIN_TYPE.PLAINS;
                    }
                    else
                    {
                        mr.material   = MatDesert;
                        h.TerrainType = Hex.TERRAIN_TYPE.DESERT;
                    }
                }

                if (h.Elevation >= HeightMountain)
                {
                    mr.material     = MatMountains;
                    mf.mesh         = MeshMountain;
                    h.ElevationType = Hex.ELEVATION_TYPE.MOUNTAIN;
                }
                else if (h.Elevation >= HeightHill)
                {
                    h.ElevationType        = Hex.ELEVATION_TYPE.HILL;
                    mf.mesh                = MeshHill;
                    hexComp.VerticalOffset = 0.25f;
                }
                else if (h.Elevation >= HeightFlat)
                {
                    h.ElevationType = Hex.ELEVATION_TYPE.FLAT;
                    mf.mesh         = MeshFlat;
                }
                else
                {
                    h.ElevationType = Hex.ELEVATION_TYPE.WATER;
                    mr.material     = MatOcean;
                    mf.mesh         = MeshWater;
                }

                hexGO.GetComponentInChildren <TextMesh>().text =
                    string.Format("{0},{1}\n{2}", column, row, h.BaseMovementCost(false, false, false));
            }
        }
    }
Ejemplo n.º 13
0
    void setHexTypeFromElevationAndMoisture(Hex h, MeshRenderer mr, MeshFilter mf, GameObject hexGO, HexComponent hexComp)
    {
        if (h.Elevation >= HeightFlat && h.Elevation < HeightMountain)
        {
            if (h.Moisture >= MoistureJungle)
            {
                mr.material   = MatGrasslands;
                h.TerrainType = Hex.TERRAIN_TYPE.GRASSLANDS;
                h.FeatureType = Hex.FEATURE_TYPE.RAINFOREST;

                // Spawn trees
                Vector3 p = hexGO.transform.position;
                if (h.Elevation >= HeightHill)
                {
                    p.y += 0.25f;
                }


                GameObject.Instantiate(JunglePrefab, p, Quaternion.identity, hexGO.transform);
            }
            else if (h.Moisture >= MoistureForest)
            {
                mr.material   = MatGrasslands;
                h.TerrainType = Hex.TERRAIN_TYPE.GRASSLANDS;
                h.FeatureType = Hex.FEATURE_TYPE.FOREST;

                // Spawn trees
                Vector3 p = hexGO.transform.position;
                if (h.Elevation >= HeightHill)
                {
                    p.y += 0.25f;
                }
                GameObject.Instantiate(ForestPrefab, p, Quaternion.identity, hexGO.transform);
            }
            else if (h.Moisture >= MoistureGrasslands)
            {
                mr.material   = MatGrasslands;
                h.TerrainType = Hex.TERRAIN_TYPE.GRASSLANDS;
            }
            else if (h.Moisture >= MoisturePlains)
            {
                mr.material   = MatPlain;
                h.TerrainType = Hex.TERRAIN_TYPE.PLAINS;
            }
            else
            {
                mr.material   = MatDesert;
                h.TerrainType = Hex.TERRAIN_TYPE.DESERT;
            }
        }

        if (h.Elevation >= HeightMountain)
        {
            mr.material     = MatMountain;
            mf.mesh         = MeshMountain;
            h.ElevationType = Hex.ELEVATION_TYPE.MOUNTAIN;
        }
        else if (h.Elevation >= HeightHill)
        {
            h.ElevationType        = Hex.ELEVATION_TYPE.HILL;
            mf.mesh                = MeshHill;
            hexComp.VerticalOffset = 0.25f;
        }
        else if (h.Elevation >= HeightFlat)
        {
            h.ElevationType = Hex.ELEVATION_TYPE.FLAT;
            mf.mesh         = MeshFlat;
        }
        else
        {
            h.ElevationType = Hex.ELEVATION_TYPE.WATER;
            mr.material     = MatOcean;
            mf.mesh         = MeshWater;
        }
    }
 public void openUI(HexComponent h)
 {
     BuildUI.SetActive(true);
     hexC = h;
 }
Ejemplo n.º 15
0
    void Update()
    {
        RaycastHit hit;
        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, Mathf.Infinity, mouseControllerLayer) && !EventSystem.current.IsPointerOverGameObject())
        {
            //Debug.Log(hit.point);
            if (hit.transform.GetComponent <HexComponent>())
            {
                HexComponent hexC = hit.transform.GetComponent <HexComponent>();
                Hex          h    = hexC.Hex.hex;
                if (Input.GetMouseButton(0) && clickAvailable)
                {
                    clickAvailable = false;
                    if (h.selected)
                    {
                        h.selected = false;
                        hexC.hexModel.material.color = h.curColor;
                        lastRemoved = hexC;
                        buildMan.closeUI();
                        return;
                    }
                    if (selectedHex != null)
                    {
                        selectedHex.hexModel.material.color = h.curColor;
                        selectedHex.Hex.hex.selected        = false;
                        lastRemoved = selectedHex;
                    }
                    h.selected = true;
                    hexC.hexModel.material.color = Color.blue;
                    selectedHex = hexC;
                    //open build ui
                    buildMan.openUI(hexC);
                    Debug.DrawRay(Camera.main.transform.position, hexC.transform.position);
                }
                else if (Input.GetMouseButton(1))
                {
                    if (h.selected)
                    {
                        hexC.hexModel.material.color = Color.green;
                        Camera.main.GetComponent <CameraController>().PanToHex(hexC);
                        h.selected = false;
                        buildMan.closeUI();
                    }
                }
                else
                {
                    if (!h.selected)
                    {
                        if (curHover != null && !curHover.Hex.hex.selected)
                        {
                            curHover.hexModel.material.color = curHover.Hex.hex.curColor;
                        }
                        curHover = hexC;
                        curHover.hexModel.material.color = Color.red;
                    }
                }
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            clickAvailable = true;
        }
    }