Example #1
0
    void PropagateObjectNodeRootConnection(NodeBase nodeA, Vector2Int _pos)
    {
        for (int i = 0; i < 4; i++)
        {
            ConnectorPorts flag = (ConnectorPorts)(1 << i);

            if (nodeA.ConnectorPorts.HasFlag(flag))
            {
                Vector2Int     pos       = _pos + portDirections[i];
                var            otherNode = MapGenerator.Instance.Grid.GetTile(pos.x, pos.y);
                ConnectorPorts otherFlag = (ConnectorPorts)(1 << i + portFlagLookup[i]);

                if (otherNode.NodeBase != null &&
                    otherNode.NodeBase.PortsOpen &&
                    !otherNode.NodeBase.ConnectedToRoot &&
                    otherNode.NodeBase.ConnectorPorts.HasFlag(otherFlag))
                {
                    nodeA.ConnectedNodes[i] = otherNode.NodeBase;
                    otherNode.NodeBase.ConnectedNodes[i + portFlagLookup[i]] = nodeA;

                    otherNode.NodeBase.ConnectedToRoot = true;
                    placedTiles.Add(otherNode);

                    PropagateObjectNodeRootConnection(otherNode.NodeBase, otherNode.GridPos);
                }
            }
        }
    }
Example #2
0
    List <GridTile> FindOpenConnections(GridTile current, GridTile next, Vector2Int _pos)
    {
        List <GridTile> nextTiles = new List <GridTile>();

        for (int i = 0; i < 4; i++)
        {
            ConnectorPorts flag = (ConnectorPorts)(1 << i);

            if (next.NodeBase.ConnectorPorts.HasFlag(flag))
            {
                Vector2Int pos       = _pos + GameManager.Instance.portDirections[i];
                var        otherNode = MapGenerator.Instance.Grid.GetTile(pos.x, pos.y);
                // ConnectorPorts otherFlag = (ConnectorPorts)(1 << i + GameManager.Instance.portFlagLookup[i]);

                if (otherNode != current && otherNode != next)
                {
                    if (otherNode.NodeBase != null && otherNode.NodeBase.PortsOpenNormalizedTime > 0.9f)
                    {
                        continue;
                    }

                    nextTiles.Add(otherNode);
                }
            }
        }
        return(nextTiles);
    }
Example #3
0
    bool CheckForRootNodeConnections(NodeBase nodeA, Vector2Int _pos)
    {
        bool connected = false;

        for (int i = 0; i < 4; i++)
        {
            ConnectorPorts flag = (ConnectorPorts)(1 << i);

            if (nodeA.ConnectorPorts.HasFlag(flag))
            {
                Vector2Int     pos       = _pos + portDirections[i];
                var            otherNode = MapGenerator.Instance.Grid.GetTile(pos.x, pos.y);
                ConnectorPorts otherFlag = (ConnectorPorts)(1 << i + portFlagLookup[i]);

                if (otherNode.NodeBase != null &&
                    otherNode.NodeBase.PortsOpen &&
                    otherNode.NodeBase.ConnectedToRoot &&
                    otherNode.NodeBase.ConnectorPorts.HasFlag(otherFlag))
                {
                    nodeA.ConnectedNodes[i] = otherNode.NodeBase;
                    otherNode.NodeBase.ConnectedNodes[i + portFlagLookup[i]] = nodeA;
                    connected = true;
                }
            }
        }

        nodeA.ConnectedToRoot = connected;

        return(connected);
    }
Example #4
0
    private void Awake()
    {
        spriteRenderer = GetComponent <SpriteRenderer>();

        openConnectorPorts = activeConnectorPorts;

        NoneSprite       = noneSprite;
        ConnectorSprites = connectorSprites;
        ObjectSprites    = objectSprites;
    }
Example #5
0
    int CountPorts(ConnectorPorts connectorPorts)
    {
        int count = 0;

        for (int i = 0; i < 4; i++)
        {
            ConnectorPorts flag = (ConnectorPorts)(1 << i);

            if (connectorPorts.HasFlag(flag))
            {
                count++;
            }
        }
        return(count);
    }
Example #6
0
    bool NextTileHasMatchingConnections(GridTile current, GridTile next)
    {
        if (next.NodeBase == null || !next.NodeBase.PortsOpen)
        {
            return(false);
        }

        Vector2Int nextTileDir = next.GridPos - current.GridPos;

        if (nextTileDir.x > 0)
        {
            ConnectorPorts nextFlag = ConnectorPorts.Left;

            if (next.NodeBase.ConnectorPorts.HasFlag(nextFlag))
            {
                return(true);
            }
        }
        else if (nextTileDir.x < 0)
        {
            ConnectorPorts nextFlag = ConnectorPorts.Right;

            if (next.NodeBase.ConnectorPorts.HasFlag(nextFlag))
            {
                return(true);
            }
        }
        else if (nextTileDir.y > 0)
        {
            ConnectorPorts nextFlag = ConnectorPorts.Down;

            if (next.NodeBase.ConnectorPorts.HasFlag(nextFlag))
            {
                return(true);
            }
        }
        else if (nextTileDir.y < 0)
        {
            ConnectorPorts nextFlag = ConnectorPorts.Up;

            if (next.NodeBase.ConnectorPorts.HasFlag(nextFlag))
            {
                return(true);
            }
        }

        return(false);
    }
Example #7
0
    public static (Sprite, int) GetSprite(ConnectorType connectorType, ConnectorPorts connectorPorts)
    {
        int rotation = -1;

        switch ((int)connectorPorts)
        {
        case (1 << 2):
        case (1 << 1) | (1 << 2):
        case (1 << 0) | (1 << 1) | (1 << 2):
            rotation = 180;
            break;

        case (1 << 1):
        case (1 << 1) | (1 << 3):
        case (1 << 1) | (1 << 2) | (1 << 3):
            rotation = -90;
            break;

        case (1 << 0):
        case (1 << 0) | (1 << 2):
        case (1 << 0) | (1 << 1):
        case (1 << 0) | (1 << 2) | (1 << 3):
            rotation = 90;
            break;

        case (1 << 3):
        case (1 << 2) | (1 << 3):
        case (1 << 0) | (1 << 3):
        case (1 << 0) | (1 << 1) | (1 << 3):
        case (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3):
        case ~0:
            rotation = 0;
            break;
        }

        int index = -1;

        switch ((int)connectorPorts)
        {
        case (1 << 0):
        case (1 << 1):
        case (1 << 2):
        case (1 << 3):
        case (1 << 0) | (1 << 1):
        case (1 << 2) | (1 << 3):
            index = 0;
            break;

        case (1 << 1) | (1 << 2):
        case (1 << 1) | (1 << 3):
        case (1 << 0) | (1 << 2):
        case (1 << 0) | (1 << 3):
            index = 1;
            break;

        case (1 << 0) | (1 << 1) | (1 << 2):
        case (1 << 0) | (1 << 2) | (1 << 3):
        case (1 << 1) | (1 << 2) | (1 << 3):
        case (1 << 0) | (1 << 1) | (1 << 3):
            index = 2;
            break;

        case ~0:
            index = 3;
            break;
        }

        Sprite sprite = NoneSprite;

        switch (connectorType)
        {
        case ConnectorType.None:
            sprite = NoneSprite;
            break;

        case ConnectorType.Fuse:
            sprite = ConnectorSprites[index];
            break;

        case ConnectorType.Object:
            sprite = ObjectSprites[index];
            break;
        }

        return(sprite, rotation);
    }
Example #8
0
    public void Generate()
    {
        var sw = new System.Diagnostics.Stopwatch();

        sw.Restart();

        int        halfGridSize = grid.GridSize / 2;
        Vector2Int gridCenter   = new Vector2Int(halfGridSize, halfGridSize);

        List <GridTile> spawnedTiles = new List <GridTile>();

        for (int i = 0; i < objectsToSpawn; i++)
        {
            Vector2Int pos = new Vector2Int(Random.Range(0, grid.GridSize), Random.Range(0, grid.GridSize));

            while (grid.GetTile(pos.x, pos.y).NodeBase != null ||
                   Vector2Int.Distance(pos, gridCenter) < centerObjectSpawnSafezone ||
                   ObjectInDistance(pos, distanceBetweenObjects))
            {
                pos = new Vector2Int(Random.Range(0, grid.GridSize), Random.Range(0, grid.GridSize));
            }

            var tile = grid.GetTile(pos.x, pos.y);

            ObjectNodeType objectNodeType = ObjectNode.GetRandomObjectNodeType();
            GameObject     nodePrefab     = null;

            switch (objectNodeType)
            {
            case ObjectNodeType.Burner:
                nodePrefab = GameObject.Instantiate(burnerObjectNodePrefab);
                break;

            case ObjectNodeType.Connector:
                nodePrefab = GameObject.Instantiate(connectorObjectNodePrefab);
                break;

            case ObjectNodeType.ScoreMultiplier:
                nodePrefab = GameObject.Instantiate(scoreMultiplierObjectNodePrefab);
                break;
            }

            nodePrefab.GetComponent <NodeBase>().Randomize();

            tile.SetNodePrefab(nodePrefab);

            spawnedTiles.Add(tile);
        }

        spawnedTiles.ForEach(
            e => {
            for (int i = 0; i < 4; i++)
            {
                ConnectorPorts flag = (ConnectorPorts)(1 << i);

                if (e.NodeBase.ConnectorPorts.HasFlag(flag))
                {
                    Vector2Int pos = e.GridPos + GameManager.Instance.portDirections[i];
                    var otherNode  = MapGenerator.Instance.Grid.GetTile(pos.x, pos.y);
                    if (otherNode == null)
                    {
                        continue;
                    }

                    ConnectorPorts otherFlag = (ConnectorPorts)(1 << i + GameManager.Instance.portFlagLookup[i]);

                    if (otherNode.NodeBase != null &&
                        otherNode.NodeBase.PortsOpen &&
                        otherNode.NodeBase.ConnectedToRoot &&
                        otherNode.NodeBase.ConnectorPorts.HasFlag(otherFlag))
                    {
                        e.NodeBase.ConnectedNodes[i] = otherNode.NodeBase;
                        otherNode.NodeBase.ConnectedNodes[i + GameManager.Instance.portFlagLookup[i]] = e.NodeBase;
                    }
                }
            }
        }
            );

        sw.Stop();
        UnityEngine.Debug.Log($"Map Gen. Time: {sw.ElapsedMilliseconds} ms");
    }