Example #1
0
    private void DetectClick()
    {
        // Get mouse position
        Vector3 clickPosition;

        clickPosition = cam.ScreenToWorldPoint(Input.mousePosition) + Vector3.forward * 10.0f;
        if (Input.GetMouseButton(0))
        {
            // Get tile map index of mouse position
            Vector3Int mapIndex   = new Vector3Int((int)clickPosition.x, (int)clickPosition.y, 0);
            float      dstToClick = (clickPosition - transform.position).magnitude;
            // Check if mouse position is in range of player
            if (dstToClick <= clickReach && StaticMaps.worldMap.GetTile(mapIndex) != TileBook.GetTileByName("Space"))
            {
                breakTimer += Time.deltaTime;
                // Once break timer has exceeded value remove tile
                if (breakTimer >= 1.0f)
                {
                    if (StaticMaps.objectMap.GetTile(mapIndex) != TileBook.GetTileByName("NullObject") &&
                        StaticMaps.objectMap.GetTile(mapIndex) != null)
                    {
                        StaticMaps.SetTile(StaticMaps.MapType.Object, mapIndex, TileBook.GetTileByName("NullObject"));
                    }
                    else
                    {
                        StaticMaps.SetTile(StaticMaps.MapType.World, mapIndex, TileBook.GetTileByName("Space"));
                    }
                    breakTimer = 0.0f;
                    // Check for change in air seal
                    DetectSeal.CheckSeal();
                }
            }
        }
        else if (Input.GetMouseButtonDown(1))
        {
            // If click detected check if within place range
            float dstToClick = (clickPosition - transform.position).magnitude;
            if (dstToClick <= clickReach && canPlace && Toolbar.GetItemByIndex(Toolbar.currentIndex).itemTile != null)
            {
                // Set tile at index to current tile
                if (TileBook.GetTileDataByName(Toolbar.GetItemByIndex(Toolbar.currentIndex).itemName).GetTileType() == TileData.TileType.WorldTile)
                {
                    Vector3Int mapIndex = new Vector3Int((int)clickPosition.x, (int)clickPosition.y, 0);
                    StaticMaps.SetTile(StaticMaps.MapType.World, mapIndex, Toolbar.GetItemByIndex(Toolbar.currentIndex).itemTile);
                }
                else if (TileBook.GetTileDataByName(Toolbar.GetItemByIndex(Toolbar.currentIndex).itemName).GetTileType() == TileData.TileType.ObjectTile)
                {
                    Vector3Int mapIndex = new Vector3Int((int)clickPosition.x, (int)clickPosition.y, 0);
                    StaticMaps.SetTile(StaticMaps.MapType.Object, mapIndex, Toolbar.GetItemByIndex(Toolbar.currentIndex).itemTile);
                }
                DetectSeal.CheckSeal();
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            breakTimer = 0.0f;
        }

        StaticMaps.breakTimer = breakTimer;
    }
    private void Start()
    {
        // Get world grid object and get stored size
        grid = GameObject.FindObjectOfType <WorldGrid>();
        Vector3Int mapSize = new Vector3Int(grid.width, grid.height, 1);

        // Set tile map sizes to match world grid
        worldMap.size  = mapSize;
        objectMap.size = mapSize;
        // Placement map will display a single tile preview therefore is of size 1
        placementMap.size = new Vector3Int(1, 1, 1);

        // Initialise static tile maps
        StaticMaps.worldMap     = worldMap;
        StaticMaps.objectMap    = objectMap;
        StaticMaps.placementMap = placementMap;

        // Detect and initialise static tile map renderers
        StaticMaps.DetectMapRenderers();

        // Set placement map transform for moving tile placement preview
        StaticMaps.placementTransform = placementMap.gameObject.transform;

        StaticMaps.worldTileData = new TileData[grid.width, grid.height];
        for (int y = 0; y < grid.height; y++)
        {
            for (int x = 0; x < grid.width; x++)
            {
                StaticMaps.worldTileData[x, y] = new TileData();
            }
        }

        StaticMaps.objectTileData = new TileData[grid.width, grid.height];
        for (int y = 0; y < grid.height; y++)
        {
            for (int x = 0; x < grid.width; x++)
            {
                StaticMaps.objectTileData[x, y] = new TileData();
            }
        }

        Debug.Log("Tile Count: " + TileBook.GetTileCount());
        SetGridToSpace();
        CreateStartRoom();
        DetectSeal.CheckSeal();
    }