Exemple #1
0
    private HexCellData CreateCell(int row, int col)
    {
        Vector3 position;

        if (row % 2 == 0)
        {
            position.x = HexConst.innerRadius + col * HexConst.innerRadius * 2f;
        }
        else
        {
            position.x = col * HexConst.innerRadius * 2f;
        }
        position.y = 0f;
        position.z = -row * HexConst.outerRadius * 1.5f;

        HexCell cell = Instantiate <HexCell>(cellPrefab);

        cell.transform.SetParent(transform, false);
        cell.transform.localPosition = position;
        HexCoordinate pos  = new HexCoordinate(row, col);
        HexCellData   data = new HexCellData();

        data.SetPos(pos);
        cell.SetData(data);
        Cells[row, col] = cell;
        return(data);
    }
    public TerrainData(HexGrid grid)
    {
        xChunks = grid.chunkCountX;
        zChunks = grid.chunkCountZ;

        int numCells = grid.cells.Length;

        hexCells = new HexCellData[numCells];

        for (int i = 0; i < numCells; i++)
        {
            HexCell     hexCell     = grid.cells[i];
            HexCellData hexCellData = new HexCellData();
            hexCellData.cellType = hexCell.CellType;

            if (hexCell.OccupyingObject)
            {
                hexCellData.occupier         = GetOccupierName(hexCell);
                hexCellData.occupierRotation = GetOccupierRotation(hexCell);
            }
            else
            {
                hexCellData.occupier         = "null";
                hexCellData.occupierRotation = 0f;
            }

            hexCells[i] = hexCellData;
        }
    }
Exemple #3
0
        private void adjustWaterArea(List <HexCell> area)
        {
            int     minElevation = int.MaxValue;
            HexCell neighbor;
            int     neighborElevation;

            foreach (HexCell cell in area)
            {
                int cellElevation = cell.Elevation;
                if (cellElevation < minElevation)
                {
                    minElevation = cellElevation;
                }

                for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
                {
                    neighbor = cell.GetNeighbor(d);
                    if (neighbor != null)
                    {
                        neighborElevation = neighbor.Elevation;
                        if (neighborElevation < minElevation)
                        {
                            minElevation = neighborElevation;
                        }
                    }
                }
            }
            foreach (HexCell cell in area)
            {
                HexCellData cellData = cell.Data;
                cell.Data = new HexCellData(minElevation, cell.Data.Biome, cell.Data.WaterDepth);
            }
        }
Exemple #4
0
        private void ApplyBiomes()
        {
            parsedBiomes = new Dictionary <HexCellBiome, int>();
            foreach (HexCellBiome biome in Enum.GetValues(typeof(HexCellBiome)))
            {
                parsedBiomes.Add(biome, 0);
            }

            for (int z = 0; z < CELL_COUNT_Z; z++)
            {
                for (int x = 0; x < CELL_COUNT_X; x++)
                {
                    HexCellBiome biome      = parseBiome(x, z);
                    int          height     = parseHeight(x, z);
                    byte         waterDepth = 0;
                    if (biome == HexCellBiome.WATER)
                    {
                        waterDepth = 1;
                    }
                    parsedBiomes[biome] += 1;
                    HexCellData cellData = new HexCellData(height, biome, waterDepth);
                    hexGrid.GetCell(x, z).Data = cellData;
                }
            }
        }
Exemple #5
0
 static void LoadHeightMap_override(ref float[] heightMap, XmlNode HeightMap, int width, int height)
 {
     foreach (XmlNode HexCellData in HeightMap.SelectNodes("Cell"))
     {
         int   Xindex = int.Parse(HexCellData.SelectSingleNode("Xindex").InnerText);
         float Y      = float.Parse(HexCellData.SelectSingleNode("Y").InnerText, CInfo);
         int   Zindex = int.Parse(HexCellData.SelectSingleNode("Zindex").InnerText);
         heightMap[Zindex * width + Xindex] = Y;
     }
 }
Exemple #6
0
 public void InitMapData(int row, int col, List <HexCellData> datas)
 {
     mapData = new HexCellData[row, col];
     for (int i = 0; i < datas.Count; i++)
     {
         HexCellData dt = datas[i];
         mapData[dt.Row, dt.Column] = dt;
     }
     RaiseEvent(MapEventType.MAP_INIT);
 }
Exemple #7
0
    public void Save(HexGridData hexGridData)
    {
        hexGridData.CellCountX = cellCountX;
        hexGridData.CellCountZ = cellCountZ;

        hexGridData.Cells = new List <HexCellData>();

        for (int i = 0; i < cells.Length; i++)
        {
            HexCellData hexCellData = new HexCellData();
            cells[i].Save(hexCellData);
            hexGridData.Cells.Add(hexCellData);
        }
    }
 public void Save(HexCellData hexCellData)
 {
     hexCellData.UrbanLevel       = this.UrbanLevel;
     hexCellData.Elevation        = this.Elevation;
     hexCellData.FarmLevel        = this.FarmLevel;
     hexCellData.HasIncomingRiver = this.HasIncomingRiver;
     hexCellData.HasOutgoingRiver = this.HasOutgoingRiver;
     hexCellData.IncomingRiver    = (int)this.IncomingRiver;
     hexCellData.OutgoingRiver    = (int)this.OutgoingRiver;
     hexCellData.PlantLevel       = (int)this.PlantLevel;
     hexCellData.RoadFlags        = this.RoadFlags;
     hexCellData.SpecialIndex     = this.SpecialIndex;
     hexCellData.TerrainTypeIndex = this.TerrainTypeIndex;
     hexCellData.Walled           = this.Walled;
     hexCellData.WaterLevel       = this.WaterLevel;
 }
Exemple #9
0
 private void UpdateRocks()
 {
     foreach (HexCell cell in hexGrid.cells)
     {
         if (cell.Data.Biome != HexCellBiome.WATER)
         {
             for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
             {
                 if (cell.GetElevationDifference(d) > 100)
                 {
                     HexCellData data = cell.Data;
                     cell.Data = new HexCellData(data.Elevation, HexCellBiome.ROCK, data.WaterDepth);
                 }
             }
         }
     }
 }
Exemple #10
0
    public void SaveMap(string path)
    {
        HexCellData[] cellsData = new HexCellData[cells.Length];

        for (int i = 0; i < cells.Length; i++)
        {
            HexCellData data = HexCell.TurnIntoCellData(cells[i]);
            cellsData[i] = data;
        }

        HexGridData mapData = new HexGridData(cellsData);

        BinaryFormatter bf   = new BinaryFormatter();
        FileStream      file = File.Open(path, FileMode.OpenOrCreate);

        bf.Serialize(file, mapData);
        file.Close();

        Debug.Log("Save ended, you can now change scenes");
    }
Exemple #11
0
    public void LoadTerrain()
    {
        TerrainData data = SaveSystem.LoadTerrain();

        if (data == null)
        {
            return;
        }

        chunkCountX = data.xChunks;
        chunkCountZ = data.zChunks;

        // Create a basic terrain
        CreateChunks();
        CreateCells();

        PlacePlayerBase();

        // Update all cells according to stored data
        for (int i = 0; i < cells.Length; i++)
        {
            HexCellData hexCellData = data.hexCells[i];
            cells[i].CellType = hexCellData.cellType;

            if (hexCellData.occupier == "null" || !nameToGameObject.ContainsKey(hexCellData.occupier))
            {
                continue;
            }

            GameObject sceneryObject = cells[i].InstantiatePrefabOnCell(nameToGameObject[hexCellData.occupier]);
            if (sceneryObject.GetComponent <RotatableTowerLogic>() is RotatableTowerLogic rotatableTowerLogic)
            {
                Quaternion rotation = rotatableTowerLogic.rotAxis.rotation;
                rotatableTowerLogic.rotAxis.rotation = rotation * Quaternion.Euler(0f, hexCellData.occupierRotation, 0f);
            }
            else
            {
                sceneryObject.transform.rotation = Quaternion.Euler(0f, hexCellData.occupierRotation, 0f);
            }
        }
    }
Exemple #12
0
        static void LoadColorMap_override(ref Color[] colorMap, XmlNode ColorMap, int width, int height, Color defaultColor)
        {
            for (int i = 0; i < colorMap.Length; i++)
            {
                colorMap[i] = defaultColor;
            }

            foreach (XmlNode HexCellData in ColorMap.SelectNodes("Cell"))
            {
                int Xindex = int.Parse(HexCellData.SelectSingleNode("Xindex").InnerText);
                int Zindex = int.Parse(HexCellData.SelectSingleNode("Zindex").InnerText);

                XmlNode colorNode = HexCellData.SelectSingleNode("Color");

                float r = float.Parse(colorNode.SelectSingleNode("Red").InnerText, CInfo);
                float g = float.Parse(colorNode.SelectSingleNode("Green").InnerText, CInfo);
                float b = float.Parse(colorNode.SelectSingleNode("Blue").InnerText, CInfo);

                colorMap[Zindex * width + Xindex] = new Color(r, g, b);
            }
        }
    /// <summary>
    /// AddCell is what the individual cell objects use to dynamically generate the level at runtime
    /// Cell objects detect their location in the world and pass that data to the LevelController via this method.
    /// This method generates corresponding entries in all relevant areas of the game state.
    /// </summary>
    /// <param name="q">column</param>
    /// <param name="r">row</param>
    /// <param name="h">height</param>
    /// <param name="cellObj">Hex cell object</param>
    public void AddCell(int q, int r, int h, GameObject cellObj)
    {
        //Create a hex data object to go into the level grid
        HexCellData newCell = new HexCellData(q, r, h, cellObj);

        levelGrid[q, r, h] = newCell;

        //Create an pathing hex object to go into the pathing grid
        PathCell pathCell = new PathCell(q, r, h);

        aiController[q, r, h] = pathCell;

        //Create a UI cell object to go into the UI grid
        UICell uiCell = new UICell(q, r, h);

        uiController.addCellToUIMap(uiCell);
        //Set the scale of the object to equal the world hex it represents
        uiController[q, r, h].setModelScale(cellObj.GetComponent <HexCellObj>().modelScale);

        cellsReady++;
    }
Exemple #14
0
 private void updateWaterDepth()
 {
     foreach (HexCell cell in hexGrid.cells)
     {
         if (cell.Data.Biome == HexCellBiome.WATER)
         {
             int     waterCount = 0;
             HexCell neighbor;
             for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
             {
                 neighbor = cell.GetNeighbor(d);
                 if (neighbor != null && neighbor.Data.Biome == HexCellBiome.WATER)
                 {
                     waterCount++;
                 }
             }
             HexCellData data = cell.Data;
             cell.Data = new HexCellData(data.Elevation, data.Biome, (byte)(1 + waterCount / 2));
         }
     }
 }
    //Kills monsters, sets outer tiles extra crispy, and ends explosion.
    IEnumerator FinishExplosion()
    {
        Debug.Log("begin explosion end");
        //now the monsters have caught up
        foreach (MonsterStats m in markedMonsters)
        {
            m.Health -= 50;
        }

        //now turn the remaining tiles in the AoE extra crispy
        PathCell[] surroundingCells = aiController.pathGrid.GetRadius(parentTile.q, parentTile.r, parentTile.h, 1, 5, true);
        foreach (PathCell cell in surroundingCells)
        {
            HexCellData cellData = levelController.levelGrid[cell.q, cell.r, cell.h];
            cellData.hexCellObject.gameObject.GetComponent <Renderer>().material = this.burntTileMaterial;
        }
        yield return(new WaitForSeconds(explosionFinishTime));

        explosion.SetActive(false);
        Debug.Log("end explosion end");
        completed = true;
    }
Exemple #16
0
    public void LoadMap(string path)
    {
        if (!File.Exists(path))
        {
            return;                    //Redundante pero por si acaso
        }
        BinaryFormatter bf      = new BinaryFormatter();
        FileStream      file    = File.Open(path, FileMode.Open);
        HexGridData     mapData = (HexGridData)bf.Deserialize(file);

        file.Close();

        RemoveMap();

        int cellAmount = mapData.cellData.Length;

        cells = new HexCell[cellAmount];
        if (createLabels)
        {
            labels = new Text[side * side];
        }

        for (int i = 0; i < cellAmount; i++)
        {
            HexCellData cellData = mapData.cellData[i];
            if (!cellData.valid)
            {
                continue;
            }
            int x = cellData.coordinates.X, z = cellData.coordinates.Z;
            CreateCell(x, z, i);
            cells[i].pfWeight = cellData.pfWeight;
            cells[i].faction  = cellData.faction;
            cells[i].Type     = cellData.type;
            //Debug.Log(cellData.type);
        }

        InitializeAllNeighbors();
    }
Exemple #17
0
    /// <summary>
    /// Fireball ability
    /// </summary>
    /// <returns>True once fireball has been cast</returns>
    public bool Fireball()
    {
        //If the player is standing on a hex (not falling, jumping)
        if (levelController[q, r, h] != null)
        {
            //Get the area around the current hex
            uiController.ShowValidTopDownRadius(q, r, h, 5, true);
        }

        UICellObj hitObj = null;

        //Non VR Movement
        if (!vrActive)
        {
            //Get the cell the player is looking at
            Vector3    lineOfSight = playerCamera.transform.forward * 1000;
            RaycastHit hit;
            if (Physics.Raycast(playerCamera.transform.position, lineOfSight, out hit))
            {
                //Get the UI hex cell the player is looking at
                hitObj = hit.transform.gameObject.GetComponent <UICellObj>() as UICellObj;
            }
            //VR Specific movement
        }
        else
        {
            hitObj = VRHitObj;
        }

        //if it isn't null
        if (hitObj != null)
        {
            //get the selected cell; if it's within 5 units of the starting cell show the ability AoE
            PathCell lookedCell = aiController[hitObj.q, hitObj.r, hitObj.h];
            PathCell startCell  = aiController[q, r, h];

            //False until proven true
            validVRHitObj = false;
            //if this is in the right range to cast this, it's a valid move.
            if (aiController.DistBetween(lookedCell, startCell) <= 5)
            {
                validVRHitObj = true;
                uiController.ShowValidTopDownRadius(hitObj.q, hitObj.r, hitObj.h, 1, true, TargetingMaterial.TARGETED_ZONE);

                //If the player presses the mouse button
                if (Input.GetMouseButtonUp(0) || vrPressUp)
                {
                    //PathCell targetedCell = aiController[hitObj.q, hitObj.r, hitObj.h];
                    HexCellData targetCell = levelController[hitObj.q, hitObj.r, hitObj.h];
                    ////kill any monsters on the cells you target
                    //foreach (Monster m in aiController.monsters) {
                    //    PathCell monsterLoc = aiController.pathGrid[m.CurrentCell[0], m.CurrentCell[1], m.CurrentCell[2]];
                    //    if (aiController.DistBetween(targetedCell, monsterLoc) <= 1) {
                    //        m.gameObject.GetComponent<MonsterStats>().Health -= 50;
                    //    }
                    //}

                    ////now turn the remaining tiles extra crispy
                    //PathCell[] surroundingCells = aiController.pathGrid.GetRadius(hitObj.q, hitObj.r, hitObj.h, 1, -1, true);
                    //foreach (PathCell cell in surroundingCells) {
                    //    HexCellData cellData = levelController.levelGrid[cell.q, cell.r, cell.h];
                    //    cellData.hexCellObject.gameObject.GetComponent<Renderer>().material = this.burntTileMaterial;
                    //}



                    actionPoints -= 1;
                    uiController.ClearCells();
                    uiController.setVisibility(false);
                    currentAction = AbilityEnum.NOT_USING_ABILITIES;

                    //Freeze the turn so that we don't continue past fireball if it's the last action on our turn.
                    levelController.turnFrozen = true;

                    //Make a new fireball
                    GameObject.Instantiate(fireballPrefab, targetCell.hexCellObject.transform.position, Quaternion.identity, targetCell.hexCellObject.transform);
                    return(true);
                }
            }
        }

        //Not finished casting
        return(false);
    }
Exemple #18
0
    /// <summary>
    /// Fireball ability
    /// </summary>
    /// <returns>True once fireball has been cast</returns>
    public bool Fireball()
    {
        //If the player is standing on a hex (not falling, jumping)
        if (levelController[q, r, h] != null)
        {
            //Get the area around the current hex
            uiController.ShowValidTopDownRadius(q, r, h, 5, true);
        }

        //Get the cell the player is looking at
        Vector3    lineOfSight = playerCamera.transform.forward * 1000;
        RaycastHit hit;
        UICellObj  hitObj = null;

        if (Physics.Raycast(playerCamera.transform.position, lineOfSight, out hit))
        {
            //Get the UI hex cell the player is looking at
            hitObj = hit.transform.gameObject.GetComponent <UICellObj>() as UICellObj;
        }
        //if it isn't null
        if (hitObj != null)
        {
            //get the selected cell; if it's within 5 units of the starting cell show the ability AoE
            PathCell lookedCell = aiController[hitObj.q, hitObj.r, hitObj.h];
            PathCell startCell  = aiController[q, r, h];

            //if this is in the right range to cast this, it's a valid move.
            if (aiController.DistBetween(lookedCell, startCell) <= 5)
            {
                uiController.ShowValidTopDownRadius(hitObj.q, hitObj.r, hitObj.h, 1, true, TargetingMaterial.TARGETED_ZONE);

                //If the player presses the mouse button
                if (Input.GetMouseButtonUp(0))
                {
                    PathCell targetedCell = aiController[hitObj.q, hitObj.r, hitObj.h];
                    //kill any monsters on the cells you target
                    foreach (Monster m in aiController.monsters)
                    {
                        PathCell monsterLoc = aiController.pathGrid[m.CurrentCell[0], m.CurrentCell[1], m.CurrentCell[2]];
                        if (aiController.DistBetween(targetedCell, monsterLoc) <= 1)
                        {
                            m.gameObject.GetComponent <MonsterStats>().Health -= 9001;
                        }
                    }

                    //now turn the remaining tiles extra crispy
                    PathCell[] surroundingCells = aiController.pathGrid.GetRadius(hitObj.q, hitObj.r, hitObj.h, 1, -1, true);
                    foreach (PathCell cell in surroundingCells)
                    {
                        HexCellData cellData = levelController.levelGrid[cell.q, cell.r, cell.h];
                        cellData.hexCellObject.gameObject.GetComponent <Renderer>().material = this.burntTileMaterial;
                    }

                    actionPoints -= 1;
                    uiController.ClearCells();
                    uiController.setVisibility(false);
                    return(true);
                }
            }
        }

        //Not finished casting
        return(false);
    }
Exemple #19
0
 public void SetData(HexCellData _data)
 {
     data     = _data;
     neighbor = new HexCell[6];
 }