private void UpdateLightList()
    {
        foreach (var item in litTiles)
        {
            SelectionStatusHandler vH = item.Key.GetComponent <SelectionStatusHandler>();
            if (item.Value > 0)
            {
                item.Key.IsDark = false;
                vH.darkness.SetActive(false);
                vH.ChangeSelectionStatus(Tile.SelectionStatus.Default);
                item.Key.IsDiscoveredFog = false;
            }
            if (item.Value < 1)
            {
                item.Key.IsDiscoveredFog = true;
                //item.Key.IsDark = true;
                //vH.darkness.SetActive(true);
                vH.ChangeSelectionStatus(Tile.SelectionStatus.Fog);
            }
        }
        var toRemove = litTiles.Where(x => x.Value == 0).Select(x => x.Key).ToList();

        foreach (var item in toRemove)
        {
            litTiles.Remove(item);
        }
        Debug.Log("Updated light sources");
    }
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        Tile tile = target as Tile;

        if (GUILayout.Button("Update all Tiles"))
        {
            if (map)
            {
                foreach (Transform child in tiles)
                {
                    Map.ClearTile(child, false);
                    Tile t = child.GetComponent <Tile>();
                    //VisualHandler vH = child.GetComponent<VisualHandler>();
                    //vH.ChangeVisibility(t.visibility);
                    map.SetTilePropTo(child, t.property);
                    itemManager.SpawnItemAtTile(t, t.item);
                }
                foreach (Transform child in tiles)
                {
                    Tile t = child.GetComponent <Tile>();
                    if (t.property == Tile.Property.Bonfire)
                    {
                        map.SetBonfire(child);
                    }
                }
                UnityEngine.Debug.Log("Updated all Tiles");
            }
        }
        if (GUILayout.Button("Clear all Tiles"))
        {
            if (map)
            {
                foreach (Transform child in tiles)
                {
                    Map.ClearTile(child, false);
                }
                UnityEngine.Debug.Log("Cleared all Tiles");
            }
        }


        if (GUILayout.Button("Update Tile"))
        {
            if (map)
            {
                Map.ClearTile(tile.transform, false);
                Tile t = tile.GetComponent <Tile>();
                SelectionStatusHandler vH = tile.GetComponent <SelectionStatusHandler>();
                vH.ChangeSelectionStatus(t.selectionStatus);
                map.SetTilePropTo(tile.transform, tile.property);
                UnityEngine.Debug.Log("Updated Tile: " + tile.gameObject.name);
            }
        }

        serializedObject.ApplyModifiedProperties();
    }
    private void UpdateHoveredTile(Tile tile)
    {
        if (hoveredTile)
        {
            SelectionStatusHandler vH = hoveredTile.GetComponent <SelectionStatusHandler>();
            vH.ChangeSelectionStatus(Tile.SelectionStatus.Default);
        }
        hoveredTile = tile;
        // This event gets picked up by the visual handlers of hovered tile and
        // sets its tile visibility


        // Update Highlight Area
        if (highlightArea.Count > 0)
        {
            foreach (var item in highlightArea)
            {
                SelectionStatusHandler vH = item.GetComponent <SelectionStatusHandler>();
                vH.ChangeSelectionStatus(Tile.SelectionStatus.Highlighted);
            }
            foreach (var item in neighbours)
            {
                SelectionStatusHandler vH = item.GetComponent <SelectionStatusHandler>();
                vH.ChangeSelectionStatus(Tile.SelectionStatus.Hovered);
            }
            if (highlightArea.Contains(hoveredTile))
            {
                //_aStar.
                var search = new AStarSearch(_grid, currentSelected[0].GetComponent <Unit>().currTile.index, hoveredTile.index);
                movePath = search.FindPath();
                // Then Update Path
                if (movePath.Count > 0)
                {
                    foreach (var item in movePath)
                    {
                        Tile _t = _grid.TileAt(item);
                        SelectionStatusHandler vH = _t.GetComponent <SelectionStatusHandler>();
                        vH.ChangeSelectionStatus(Tile.SelectionStatus.Path);
                    }
                }
            }
        }
        SelectionStatusHandler vHnew = hoveredTile.GetComponent <SelectionStatusHandler>();

        vHnew.ChangeSelectionStatus(Tile.SelectionStatus.Hovered);
    }
Exemple #4
0
    public void SetBonfire(Transform tileTransform)
    {
        Tile       tile    = tileTransform.GetComponent <Tile>();
        GameObject bonfire = Instantiate(Resources.Load("Buildings/" + bonfirePrefab.name), tileTransform.position, Quaternion.identity, tileTransform) as GameObject;

        bonfire.name = bonfirePrefab.name;
        Bonfire fire = bonfire.GetComponent <Bonfire>();

        fire.currTile = tile;
        tile.Passable = false;
        tile.tag      = TagHandler.buildingBonfireString;
        MeshRenderer t_meshRen = tile.GetComponent <MeshRenderer>();

        t_meshRen.material = (dirtMaterial) ? dirtMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
        SelectionStatusHandler vH = tile.GetComponent <SelectionStatusHandler>();

        vH.originalMat = t_meshRen.material;
    }
 private void ResetHighLightArea()
 {
     if (highlightArea.Count > 0)
     {
         foreach (var item in highlightArea)
         {
             SelectionStatusHandler vH = item.GetComponent <SelectionStatusHandler>();
             vH.ChangeSelectionStatus(Tile.SelectionStatus.Default);
         }
         foreach (var item in neighbours)
         {
             SelectionStatusHandler vH = item.GetComponent <SelectionStatusHandler>();
             vH.ChangeSelectionStatus(Tile.SelectionStatus.Default);
         }
     }
     neighbours.Clear();
     highlightArea.Clear();
 }
 private void ResetSelection()
 {
     if (currentSelected.Count > 0)
     {
         foreach (GameObject obj in currentSelected)
         {
             _selectable            = obj.GetComponent <Selectable>();
             _selectable.IsSelected = false;
             if (obj.layer == 8)
             {
                 SelectionStatusHandler vH = obj.GetComponent <SelectionStatusHandler>();
                 vH.ChangeSelectionStatus(Tile.SelectionStatus.Default);
             }
         }
         Debug.Log("Deselected all current selected objects");
     }
     currentSelected.Clear();
 }
Exemple #7
0
 public static void SetBuildingSize(List <Tile> buildingSizeTiles, Tile.Property buildingProperty)
 {// For cleanup of tiles within a buildings tiles (spanning over several tiles)
     foreach (var _t in buildingSizeTiles)
     {
         ClearTile(_t.transform, false);
         MeshRenderer t_meshRen    = _t.GetComponent <MeshRenderer>();
         var          dirtMaterial = (Material)Resources.Load("Materials/DirtMat");
         t_meshRen.material = (dirtMaterial) ? dirtMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
         SelectionStatusHandler vH = _t.GetComponent <SelectionStatusHandler>();
         vH.originalMat = t_meshRen.material;
         _t.property    = buildingProperty;
         _t.Passable    = false;
         _t.tag         = TagHandler.buildingBonfireString;
         if (_t.GetComponent <Resource>())
         {
             Destroy(_t.GetComponent <Resource>());
         }
     }
 }
    private void Select(GameObject obj)
    {
        // Select unit if clicked tile has unit on it
        if (obj.GetComponent <Tile>() && obj.GetComponent <Tile>().unit != null)
        {
            obj = obj.GetComponent <Tile>().unit.gameObject;
        }

        _selectable            = obj.GetComponent <Selectable>();
        _selectable.IsSelected = true;

        // Add object to selection array if not selected yet
        if (!currentSelected.Contains(obj))
        {
            currentSelected.Add(obj);
        }

        if (obj.layer == 8) // Tile Layer
        {
            Tile tile = obj.GetComponent <Tile>();
            EventHandler.current.TileSelected(tile);

            SelectionStatusHandler vH = obj.GetComponent <SelectionStatusHandler>();
            vH.ChangeSelectionStatus(Tile.SelectionStatus.Selected);
            //if (highlightArea.Contains(hoveredTile))
            //{
            //    currentSelected[0].GetComponent<Unit>().destTilePos = hoveredTile;
            //}
        }
        if (obj.layer == 9) // PlayerUnit Layer
        {
            Unit unit = obj.GetComponent <Unit>();
            EventHandler.current.SelectUnit(unit);

            // If the unit can gather resources add the neighbour tiles
            // to the neighbour list
            UpdateNeighboursAndHighlightArea();
        }
        Debug.Log("Selected " + obj.name);
    }
    public void UpdateNeighboursAndHighlightArea()
    {
        if (currentSelected.Count > 0)
        {
            Unit unit = null;
            if (currentSelected[0].GetComponent <Unit>())
            {
                unit = currentSelected[0].GetComponent <Unit>();
            }

            GetNeighbours(unit);

            foreach (var item in highlightArea)
            {
                SelectionStatusHandler vH = item.GetComponent <SelectionStatusHandler>();
                if (item.IsDiscoveredFog)
                {
                    vH.ChangeSelectionStatus(Tile.SelectionStatus.Fog);
                }
                else
                {
                    vH.ChangeSelectionStatus(Tile.SelectionStatus.Default);
                }
            }
            highlightArea = unit.GetMoveAreaTilesByIndices();
            foreach (var item in highlightArea)
            {
                SelectionStatusHandler vH = item.GetComponent <SelectionStatusHandler>();
                vH.ChangeSelectionStatus(Tile.SelectionStatus.Highlighted);
            }
            foreach (var item in neighbours)
            {
                SelectionStatusHandler vH = item.GetComponent <SelectionStatusHandler>();
                vH.ChangeSelectionStatus(Tile.SelectionStatus.Hovered);
            }
            Debug.Log("Updated Neighbours and Highlight Area");
        }
    }
Exemple #10
0
    public void SetTilePropTo(Transform tileTransform, Tile.Property newProperty)
    {
        Tile         tile    = tileTransform.GetComponent <Tile>();
        MeshRenderer meshRen = tileTransform.GetComponent <MeshRenderer>();

        tile.property = newProperty;

        if (newProperty == Tile.Property.Tree)
        {
            meshRen.material = (grassMaterial) ? grassMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            SelectionStatusHandler tileMat = tileTransform.GetComponent <SelectionStatusHandler>();
            tileMat.originalMat = meshRen.material;
            tile.tag            = TagHandler.resourceWoodString;
            var randomOffset = Random.Range(0.05f, 0.15f);
            var randomDir    = Random.insideUnitCircle.normalized;
            for (int i = 0; i < 2; i++)
            {
                GameObject tree = Instantiate(Resources.Load("Nature/" + treePrefab.name), tileTransform.position, Quaternion.Euler(new Vector3(Random.Range(-7f, 7f), Random.Range(0, 360), Random.Range(-7f, 7f))), tileTransform) as GameObject;
                tree.name = treePrefab.name;
                var randomScale = Random.Range(0.75f, 1.3f);
                if (i == 0)
                {
                    tree.transform.position += Vector3.forward * randomOffset + new Vector3(randomDir.x, 0, randomDir.y);
                }
                else
                {
                    tree.transform.position -= Vector3.forward * randomOffset + new Vector3(randomDir.x, 0, randomDir.y);
                }
                tree.transform.localScale += new Vector3(randomScale, randomScale, randomScale);
            }
            tile.Passable = false;
        }
        if (newProperty == Tile.Property.Grass)
        {
            meshRen.material = (grassMaterial) ? grassMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            SelectionStatusHandler tileMat = tileTransform.GetComponent <SelectionStatusHandler>();
            tileMat.originalMat = meshRen.material;
            tile.Passable       = true;
            tile.tag            = TagHandler.walkGroundString;
        }
        if (newProperty == Tile.Property.Water)
        {
            meshRen.material = (waterMaterial) ? waterMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            SelectionStatusHandler tileMat = tileTransform.GetComponent <SelectionStatusHandler>();
            tileMat.originalMat = meshRen.material;
            tile.Passable       = false;
        }
        if (newProperty == Tile.Property.Stone)
        {
            meshRen.material = (rockMaterial) ? rockMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            SelectionStatusHandler tileMat = tileTransform.GetComponent <SelectionStatusHandler>();
            tileMat.originalMat = meshRen.material;
            tile.tag            = TagHandler.resourceStoneString;
            GameObject rock = Instantiate(Resources.Load("Nature/" + rockPrefab.name), tileTransform.position, Quaternion.identity, tileTransform) as GameObject;
            rock.name = rockPrefab.name;
            rock.transform.localScale += new Vector3(1f, 1f, 1f);
            tile.Passable              = false;
        }
        if (newProperty == Tile.Property.OldTree)
        {
            meshRen.material = (grassMaterial) ? grassMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            SelectionStatusHandler tileMat = tileTransform.GetComponent <SelectionStatusHandler>();
            tileMat.originalMat = meshRen.material;
            tile.tag            = TagHandler.magicTreeString;
            GameObject oldTree = Instantiate(Resources.Load("Nature/" + oldTreePrefab.name), tileTransform.position, Quaternion.identity, tileTransform) as GameObject;
            oldTree.name  = oldTreePrefab.name;
            tile.Passable = false;
        }
        if (newProperty == Tile.Property.Bagpack)
        {
            meshRen.material = (grassMaterial) ? grassMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            SelectionStatusHandler tileMat = tileTransform.GetComponent <SelectionStatusHandler>();
            tileMat.originalMat = meshRen.material;
            tile.tag            = TagHandler.buildingBagpackString;
            GameObject bagPack = Instantiate(Resources.Load("Buildings/" + bagpackPrefab.name), tileTransform.position, Quaternion.identity, tileTransform) as GameObject;
            bagPack.name  = bagpackPrefab.name;
            tile.Passable = false;
        }

        if (newProperty == Tile.Property.WoodStorage)
        {
            meshRen.material = (dirtMaterial) ? dirtMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            SelectionStatusHandler tileMat = tileTransform.GetComponent <SelectionStatusHandler>();
            tileMat.originalMat = meshRen.material;
            tile.tag            = TagHandler.buildingWoodStorageString;
            GameObject woodStorage = Instantiate(Resources.Load("Buildings/" + woodStoragePrefab.name), tileTransform.position, Quaternion.Euler(0, 120, 0), tileTransform) as GameObject;
            woodStorage.transform.localScale = new Vector3(0.4f, 0.4f, 0.4f);
            woodStorage.name = woodStoragePrefab.name;
            tile.Passable    = false;
        }
        if (newProperty == Tile.Property.StoneStorage)
        {
            meshRen.material = (rockMaterial) ? rockMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            SelectionStatusHandler tileMat = tileTransform.GetComponent <SelectionStatusHandler>();
            tileMat.originalMat = meshRen.material;
            tile.tag            = TagHandler.buildingStoneStorageString;
            GameObject woodStorage = Instantiate(Resources.Load("Buildings/" + stoneStoragePrefab.name), tileTransform.position, Quaternion.Euler(0, 120, 0), tileTransform) as GameObject;
            woodStorage.name = stoneStoragePrefab.name;
            tile.Passable    = false;
        }
        if (newProperty == Tile.Property.Cauldron)
        {
            meshRen.material = (dirtMaterial) ? dirtMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            SelectionStatusHandler tileMat = tileTransform.GetComponent <SelectionStatusHandler>();
            tileMat.originalMat = meshRen.material;
            tile.tag            = TagHandler.buildingCauldronString;
            GameObject cauldron = Instantiate(Resources.Load("Buildings/" + cauldronPrefab.name), tileTransform.position, Quaternion.identity, tileTransform) as GameObject;
            tile.Passable = false;
            cauldron.name = woodStoragePrefab.name;
        }
        if (newProperty == Tile.Property.House)
        {
            meshRen.material = (dirtMaterial) ? dirtMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            SelectionStatusHandler tileMat = tileTransform.GetComponent <SelectionStatusHandler>();
            tileMat.originalMat = meshRen.material;
            tile.tag            = TagHandler.buildingHouseString;
            GameObject house = Instantiate(Resources.Load("Buildings/" + housePrefab.name), tileTransform.position, Quaternion.identity, tileTransform) as GameObject;
            house.name    = housePrefab.name;
            tile.Passable = false;
        }
        if (newProperty == Tile.Property.Construction)
        {
            meshRen.material = (dirtMaterial) ? dirtMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            SelectionStatusHandler tileMat = tileTransform.GetComponent <SelectionStatusHandler>();
            tileMat.originalMat = meshRen.material;
            tile.Passable       = false;
            tile.tag            = TagHandler.buildingConstructionString;
        }

        if (newProperty == Tile.Property.Default)
        {
            ClearTile(tileTransform, true);
            tile.tag = null;
        }
        Debug.Log("Set " + tile.gameObject.name + " to " + tile.property);
    }