Ejemplo n.º 1
0
    public void AddTile()
    {
        _emptySpaces.Clear();
        for (int i = 0; i < Items.Length; i++)
        {
            if (Items[i] != 0)
            {
                continue;
            }
            _emptySpaces.Add(i);
        }

        if (_emptySpaces.Count == 0)
        {
            OnNoSpace?.Invoke();
            return;
        }

        var random = _random.Next(0, _emptySpaces.Count);
        var index  = _emptySpaces[random];

        Items[index] = 1;

        OnTileAdded?.Invoke(index);
    }
        /// <summary>
        /// Adds the.
        /// </summary>
        /// <param name="tile">The tile.</param>
        public void Add(TileMask tile)
        {
            if (tile == null)
            {
                return;
            }
            if (Count > 0)
            {
                tile.Z = tiles.Last().Z + 1;
            }
            else
            {
                tile.Z = 0;
            }

            tiles.Add(tile);

            if (tile.X < Left || Left < 0)
            {
                Left = tile.X;
            }
            if (tile.X + tile.Width > Right)
            {
                Right = tile.X + tile.Width;
            }
            if (tile.Y < Top || Top < 0)
            {
                Top = tile.Y;
            }
            if (tile.Y + tile.Height > Bottom)
            {
                Bottom = tile.Y + tile.Height;
            }

            RequireRefresh = true;
            OnTileAdded?.Invoke(this, tile);
        }
Ejemplo n.º 3
0
    // Create a grid object and add it to the tileset
    public bool CreateGridObject(string objectName, Vector3Int tileLocation, EDirection direction, out GameObject placedObject)
    {
        // Get object data
        KeyValuePair <Transform, GridObjectData> data = GridObjectTypes[objectName];
        Transform      prefab     = data.Key;
        GridObjectData objectData = data.Value;

        // Check if the object fits within the groud boundries
        if (IsObjectValid(objectData, tileLocation, direction))
        {
            // Get the tile dimensions of the object
            Vector3Int dimensions = objectData.GetDimensions(direction);
            int        width      = dimensions.x;
            int        depth      = dimensions.y;
            int        length     = dimensions.z;

            // The offset to the origin of the object in tiles space
            Vector3Int globalOffset = tileLocation + objectData.GetTilesOffset(direction);

            // Calculate the world location of the new grid object
            Vector3 worldLocation = new Vector3();
            TileToWorld(tileLocation, out worldLocation);


            // Create that object and add it to the scene
            Transform sceneObject = GameObject.Instantiate(prefab);
            sceneObject.name     = objectName + ": " + tileLocation.ToString();
            sceneObject.position = worldLocation;
            sceneObject.parent   = transform;
            sceneObject.rotation = Quaternion.AngleAxis((int)direction * 90, Vector3.up);

            // Initialise the grid object variable to be placed into the grid slots
            GridObject obj = new GridObject(sceneObject, objectData, tileLocation, direction);

            for (int y = 0; y < depth; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    for (int z = 0; z < length; ++z)
                    {
                        // Only overwrite if the object tile is solid
                        if (objectData.GetIsSolid(x, y, z, direction))
                        {
                            // Get the location of the tile on the grid space
                            Vector3Int globalTileLoc = new Vector3Int(x, y, z) + globalOffset;
                            Tiles[globalTileLoc.x, globalTileLoc.y, globalTileLoc.z] = obj;

                            // Get the location of that tile
                            Vector3 worldLoc = new Vector3();
                            TileToWorld(globalTileLoc, out worldLoc);

                            // Generate a collider at that location
                            BoxCollider collider = ColliderObject.AddComponent <BoxCollider>();
                            collider.center = worldLoc;
                            collider.size   = Vector3.one * TileSize;

                            // Add to the collider objects
                            Colliders[globalTileLoc.x, globalTileLoc.y, globalTileLoc.z] = collider;
                        }
                    }
                }
            }

            // Invoke function
            OnTileAdded?.Invoke(this, new BlockArgs(objectData));

            // Finished placing object
            placedObject = sceneObject.gameObject;
            return(true);
        }
        else
        {
            // Object was not in bounds of the tile grid
            placedObject = null;
            return(false);
        }
    }