Exemple #1
0
    private bool isConnectable(TileConnector tc1, TileConnector tc2)
    {
        if (tc1.connected || tc2.connected)
        {
            return(false);
        }
        bool blnOppositeSides = false;
        bool blnSameTypes     = tc1.type == tc2.type;

        if (tc1.side == "l" && tc2.side == "r")
        {
            blnOppositeSides = true;
        }
        else if (tc1.side == "r" && tc2.side == "l")
        {
            blnOppositeSides = true;
        }
        else if (tc1.side == "t" && tc2.side == "b")
        {
            blnOppositeSides = true;
        }
        else if (tc1.side == "b" && tc2.side == "t")
        {
            blnOppositeSides = true;
        }

        return(blnOppositeSides && blnSameTypes);
    }
    int DistanceFromStartTile(TileConnector t1, TileConnector t2)
    {
        float distT1 = ZUtils.GetDistanceBetweenVector(t1.transform.position, _startTile.transform.position, false, true, false);
        float distT2 = ZUtils.GetDistanceBetweenVector(t1.transform.position, _startTile.transform.position, false, true, false);

        return(distT2.CompareTo(distT1));
    }
Exemple #3
0
    public TileConnector GetOppositeConnector(TileConnector.DIRECTION dirn)
    {
        for (int i = 0; i < connections.Length; ++i)
        {
            if (connections[i].direction == TileConnector.GetOppositeDirection(dirn))
            {
                return(connections[i]);
            }
        }

        return(null);
    }
Exemple #4
0
    //Calculate the rotation and translation required to match both tile connectors and apply to the latter tile.
    private void MatchConnections(TileConnector oldCon, TileConnector newCon)
    {
        var newTile = newCon.transform.parent;
        var forwardVectorToMatch = -oldCon.transform.forward;
        var correctiveRot        = Azimuth(forwardVectorToMatch) - Azimuth(newCon.transform.forward);

        newTile.RotateAround(newCon.transform.position, Vector3.up, correctiveRot);
        var correctiveTranslation = oldCon.transform.position - newCon.transform.position;

        newTile.transform.position += correctiveTranslation;
        newCon.isMatched            = true;
    }
    public void InitializeLevel()
    {
        IsPaused       = true;
        player         = GameObject.Find("PlayerTest");
        tileManager    = GameObject.Find("TileManager").GetComponent <TileManager>();
        _tileConnector = GameObject.Find("TileConnector").GetComponent <TileConnector>();
        //Initialize the level data, serialized level data and unique objects list to prevent nullreferences.
        levelData      = new LevelData();
        serializedData = new SerializedLevelData();
        _uniqueObjects = new UniqueObjects();

        //ghostObjects = new List<GameObject>();
    }
    bool CloseFreeConnection()
    {
        if (_listOfFreeConnections.Count > 0)
        {
            // Start with first free connection
            TileConnector newConnector = _listOfFreeConnections[0];
            _listOfFreeConnections.RemoveAt(0);
            // Find a tile for this connection
            CreateTile(GetNextCell(newConnector), TILE_TYPE.CONNECTION_FILLER, newConnector);
            return(true);
        }

        RefreshFreeConnections();
        return(false);
    }
    bool GenerateNextTile()
    {
        if (_numTilesCreated < numTiles && _listOfFreeConnections.Count > 0)
        {
            // Start with first free connection
            TileConnector newConnector = _listOfFreeConnections[0];
            _listOfFreeConnections.RemoveAt(0);
            // Find a tile for this connection
            CreateTile(GetNextCell(newConnector), TILE_TYPE.CONNECTION, newConnector);
            return(true);
        }

        RefreshFreeConnections();
        return(false);
    }
Exemple #8
0
 void ConnectIfPossible(CellGrid.Cell nextCell)
 {
     if (nextCell.IsOccupied())
     {
         TileConnector nextCellConnector = nextCell.tile.GetOppositeConnector(direction);
         if (nextCellConnector != null)
         {
             Connect(this, nextCellConnector);
         }
         else
         {
             _isInvalid = true;
         }
     }
 }
    void GenerateEndTile()
    {
        if (_listOfFreeConnections.Count > 0)
        {
            // Start with first free connection
            TileConnector newConnector = _listOfFreeConnections[0];
            _listOfFreeConnections.RemoveAt(0);
            // Find a tile for this connection
            CreateTile(GetNextCell(newConnector), TILE_TYPE.END, newConnector);

            RefreshFreeConnections();
        }
        else
        {
            Debug.Log("Error: No free connections for end tile");
        }
    }
    public void HandleCustomProperties(UnityEngine.GameObject gameObject,
                                       IDictionary <string, string> props)
    {
        // Simply add a component to our GameObject
        // BETTER STYLE
        if (props.ContainsKey("side"))
        {
            gameObject.name = "TileConnector";
            TileConnector tileConnector = gameObject.AddComponent <TileConnector>();
            tileConnector.side      = props["side"];
            tileConnector.connected = false;

            if (props.ContainsKey("type"))
            {
                tileConnector.type = Convert.ToInt32(props["type"]);
            }
        }


        // OLD STYLE
//		if (props.ContainsKey("side"))
//		{
//			string prefabPath = "Assets/Prefabs/TileConnector.prefab";
//			UnityEngine.Object connector = UnityEditor.AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));
//			if (connector != null)
//			{
//				GameObject connectorInstance = (GameObject)GameObject.Instantiate(connector);
//				connectorInstance.name = connector.name;
//				TileConnector tileConnector = connectorInstance.GetComponent("TileConnector") as TileConnector;
//				tileConnector.side = props["side"];
//
//				if(props.ContainsKey("type")) {
//					tileConnector.type = Convert.ToInt32(props["type"]);
//				}
//
//				// Use the position of the game object we're attached to
//				connectorInstance.transform.parent = gameObject.transform;
//				connectorInstance.transform.localPosition = Vector3.zero;
//			}
//		}
    }
    bool CloseDeadEnds()
    {
        if (_listOfFreeConnections.Count > 0)
        {
            // Start with first free connection
            TileConnector newConnector = _listOfFreeConnections[0];
            _listOfFreeConnections.RemoveAt(0);
            // Find a tile for this connection
            CreateTile(GetNextCell(newConnector), TILE_TYPE.DEAD_END, newConnector);
            return(true);
        }

        RefreshFreeConnections();

        if (_listOfFreeConnections.Count > 0)
        {
            Debug.Log("Error: Unable to fill " + _listOfFreeConnections.Count.ToString() + " connections");
        }

        return(false);
    }
    CellGrid.Cell GetNextCell(TileConnector tileConnector)
    {
        Tile ownerTile = tileConnector.ownerTile;

        if (tileConnector.direction == TileConnector.DIRECTION.TOP)
        {
            return(_cellGrid.GetCell(ownerTile.Cell.row + 1, ownerTile.Cell.col));
        }
        else if (tileConnector.direction == TileConnector.DIRECTION.BOT)
        {
            return(_cellGrid.GetCell(ownerTile.Cell.row - 1, ownerTile.Cell.col));
        }
        else if (tileConnector.direction == TileConnector.DIRECTION.LEFT)
        {
            return(_cellGrid.GetCell(ownerTile.Cell.row, ownerTile.Cell.col - 1));
        }
        else if (tileConnector.direction == TileConnector.DIRECTION.RIGHT)
        {
            return(_cellGrid.GetCell(ownerTile.Cell.row, ownerTile.Cell.col + 1));
        }

        return(null);
    }
    Tile FindEndTile(TileConnector parentConnector)
    {
        for (int i = 0; i < _totalTilesInBaseTileSet; ++i)
        {
            Tile baseTile       = _baseTileSet[i];
            int  numConnections = baseTile.connections.Length;

            if (numConnections > 1)
            {
                continue;
            }

            for (int j = 0; j < numConnections; ++j)
            {
                if (baseTile.connections[j].direction == TileConnector.GetOppositeDirection(parentConnector.direction))
                {
                    return(baseTile);
                }
            }
        }

        Debug.Log("Error: Unable to find a end tile to fill");
        return(null);
    }
    void CreateTile(CellGrid.Cell cell, TILE_TYPE tileType, TileConnector parentConnector = null)
    {
        ZUtils.RandomizeList <Tile>(ref _baseTileSet);

        Tile newTile = null;

        if (tileType == TILE_TYPE.START)
        {
            newTile = FindStartTile();
        }
        else if (tileType == TILE_TYPE.CONNECTION)
        {
            newTile = FindConnectionTile(cell);
        }
        else if (tileType == TILE_TYPE.CONNECTION_FILLER)
        {
            newTile = FindConnectionTileToFill(cell);
        }
        else if (tileType == TILE_TYPE.END)
        {
            newTile  = FindEndTile(parentConnector);
            _endTile = _endTile;
        }
        else if (tileType == TILE_TYPE.DEAD_END)
        {
            newTile = FindDeadEndTile(parentConnector);
        }

        if (newTile == null)
        {
            // No valid option right now, move to next step
            return;
        }

        GameObject newTileGO = Instantiate(newTile.gameObject);
        Transform  newTileT  = newTileGO.transform;

        newTileT.SetParent(_dungeonT);
        newTileT.position = cell.Position;

        newTile = newTileGO.GetComponent <Tile>();

        if (parentConnector != null)
        {
            TileConnector childConnector = newTile.GetOppositeConnector(parentConnector.direction);
            if (childConnector != null)
            {
                TileConnector.Connect(childConnector, parentConnector);
            }
            else
            {
                Debug.Log("Error: Unable to find a connector so skipping");
                return;
            }
        }

        newTile.Init(cell, _cellGrid);

        if (AllowNewConnections(tileType))
        {
            // Add free connections
            List <TileConnector> freeConnectionList = newTile.GetFreeConnections();
            for (int i = 0; i < freeConnectionList.Count; ++i)
            {
                _listOfFreeConnections.Add(freeConnectionList[i]);
            }
        }

        // Reiterate free connection list for new blockers
        for (int i = 0; i < _listOfFreeConnections.Count; ++i)
        {
            _listOfFreeConnections[i].UpdateBasedOnGrd(_cellGrid);
            if (!_listOfFreeConnections[i].IsFree())
            {
                _listOfFreeConnections[i].SetToInvalid();
                _listOfFreeConnections.RemoveAt(i);
                --i;
            }
        }

        _dungeonTiles.Add(newTile);
        _cellObjectList.Add(newTileGO);
        _numTilesCreated++;

        if (tileType == TILE_TYPE.START)
        {
            _startTile = newTile;
        }
        else if (tileType == TILE_TYPE.END)
        {
            _endTile = newTile;
        }

        //Debug.Log("GenNextTile: "  + _numCellsCreated.ToString() + " " + _listOfFreeConnections.Count.ToString());
    }
    private bool isConnectable(TileConnector tc1, TileConnector tc2)
    {
        if (tc1.connected || tc2.connected)
            return false;
        bool blnOppositeSides = false;
        bool blnSameTypes = tc1.type == tc2.type;
        if (tc1.side == "l" && tc2.side == "r")
            blnOppositeSides = true;
        else if (tc1.side == "r" && tc2.side == "l")
            blnOppositeSides = true;
        else if (tc1.side == "t" && tc2.side == "b")
            blnOppositeSides = true;
        else if (tc1.side == "b" && tc2.side == "t")
            blnOppositeSides = true;

        return blnOppositeSides && blnSameTypes;
    }
Exemple #16
0
    private UnityEngine.GameObject checkThenAddRoom(GameObject newRoom, GameObject oldRoom = null, float defaultXpos = 0.0f, float defaultYpos = 0.0f)
    {
        GameObject addedRoom = null;

        if (oldRoom == null)
        {
            addedRoom = Instantiate(newRoom, new Vector3(defaultXpos, defaultYpos, 0), Quaternion.identity) as GameObject;
        }
        else
        {
            //Connector layers
            Transform nConnLayer = getConnectorLayer(newRoom);
            Transform oConnLayer = getConnectorLayer(oldRoom);
            //New Connector
            Transform     nConnObj = null;
            TileConnector nTC      = null;
            //Current Old Connector
            Transform     oConnObj = null;
            TileConnector oTC      = null;

            IList <KeyValuePair <Transform, Transform> >         validConnnObjs = new List <KeyValuePair <Transform, Transform> >();
            IList <KeyValuePair <TileConnector, TileConnector> > validTCs       = new List <KeyValuePair <TileConnector, TileConnector> >();
            IList <KeyValuePair <int, int> > validTCIdxs = new List <KeyValuePair <int, int> >();

            //TEMP
            IList <TileConnector> nTCs = new List <TileConnector>();

            //Move all old connectors into a list to be iterated through later.
            IList <Transform>     oConnObjs = new List <Transform>();
            IList <TileConnector> oTCs      = new List <TileConnector>();
            for (int i = 0; i < oConnLayer.childCount; i++)
            {
                //Get current old connector
                oConnObj = getConnectorObject(oConnLayer, i);
                oTC      = getTileConnector(oConnObj);
                oConnObjs.Add(oConnObj);
                oTCs.Add(oTC);
            }

            //Find all valid connections for old room to new room
            for (int i = 0; i < nConnLayer.childCount; i++)
            {
                //Get current new connector
                nConnObj = getConnectorObject(nConnLayer, i);
                nTC      = getTileConnector(nConnObj);
                nTCs.Add(nTC);
                for (int j = 0; j < oConnObjs.Count; j++)
                {
                    //Get current old connector
                    oConnObj = oConnObjs[j];
                    oTC      = oTCs[j];
                    if (nTC.side != "r" && isConnectable(nTC, oTC))
                    {
                        validConnnObjs.Add(new KeyValuePair <Transform, Transform>(nConnObj, oConnObj));
                        validTCs.Add(new KeyValuePair <TileConnector, TileConnector>(nTC, oTC));
                        validTCIdxs.Add(new KeyValuePair <int, int>(i, j));
                    }
                }
            }
            //if a valid connection has been found
            if (validConnnObjs.Count > 0)
            {
                //Get a random valid connection
                int randIdx = Random.Range(0, validConnnObjs.Count);
                nConnObj = validConnnObjs[randIdx].Key;
                oConnObj = validConnnObjs[randIdx].Value;
                nTC      = validTCs[randIdx].Key;
                oTC      = validTCs[randIdx].Value;
                int nTCIdx = validTCIdxs[randIdx].Key;

                float xPos = oldRoom.transform.position.x;
                float yPos = oldRoom.transform.position.y;
                if (oTC.side == "t")
                {
                    yPos += newRoom.GetComponent <TiledMap>().MapHeightInPixels *MAP_SCALE;
                }
                else if (oTC.side == "r")
                {
                    xPos += oldRoom.GetComponent <TiledMap>().MapWidthInPixels *MAP_SCALE;
                }
                else if (oTC.side == "b")
                {
                    yPos -= oldRoom.GetComponent <TiledMap>().MapHeightInPixels *MAP_SCALE;
                }
                else if (oTC.side == "l")
                {
                    xPos -= newRoom.GetComponent <TiledMap>().MapWidthInPixels *MAP_SCALE;
                }
                //Render room
                addedRoom = Instantiate(newRoom, new Vector3(xPos, yPos, 0), Quaternion.identity) as GameObject;
                //Set clone's connector as connected.
                getTileConnector(getConnectorLayer(addedRoom), nTCIdx).connected = true;
                oTC.connected = true;
            }
        }
        return(addedRoom);
    }
Exemple #17
0
 public static void Connect(TileConnector tc1, TileConnector tc2)
 {
     tc1.Connect(tc2);
     tc2.Connect(tc1);
 }
Exemple #18
0
 public void Connect(TileConnector tc)
 {
     _connectedTile = tc;
 }