示例#1
0
    // add a tile to a position in this chunk
    public bool addTile(Vector3Int worldPos, ItemGroup itemGroup)
    {
        // get the locaion of this tile but to the local chunk Coord
        Vector3Int localPos = ChunkHelpers.convertWorldCoordToLocalCoord(worldPos, chunkX);

        // if the tile is outside the chunk then do not add it, or it is air
        if (!isThisTileInThisChunk(localPos) || itemGroup.id == 0)
        {
            return(false);
        }

        // if there is no tile in this area then add the new one else return false
        if (tilemap.GetTile(worldPos) == null)
        {
            // set the correct value in the tile id array
            tileIDs[localPos.x, localPos.y] = itemGroup.id;

            // change the tile map to fit that change
            tilemap.SetTile(worldPos, itemGroup.tile);

            // check if this tile needs a respective tile entity
            GameObject blockEntity = itemGroup.blockEntity;

            if (blockEntity != null)
            {
                // create a copy of the block entity needed and add that to the
                // block entity list
                GameObject createdBlockEntity = Instantiate(blockEntity, new Vector3(worldPos.x + 0.5f, worldPos.y + 0.5f, worldPos.z), new Quaternion(), transform);
                blockEntities.Add(createdBlockEntity);
            }

            return(true);
        }
        else
        {
            return(false);
        }
    }
示例#2
0
    // removes a tile from the location asked and returns the
    // id of that tile
    public int removeTile(Vector3Int worldPos)
    {
        Vector3Int localPos = ChunkHelpers.convertWorldCoordToLocalCoord(worldPos, chunkX);

        // if this tile is in the chunk bounds then return the tileID
        // at that location, else return the flag - 1
        if (isThisTileInThisChunk(localPos) && tilemap.GetTile(worldPos) != null)
        {
            // get the id of the tile that is there
            int idOfThatTile = tileIDs[localPos.x, localPos.y];

            // if the tile is indestructable then return minus and do not remove the tile
            if (refrenceManager.itemGroups[idOfThatTile].indestructable)
            {
                return(-1);
            }

            // remove that tile from the til map
            tilemap.SetTile(worldPos, null);

            // set the id of the tile in out tileID array to 0
            tileIDs[localPos.x, localPos.y] = 0;

            // if the item has an associated blovk entity
            if (refrenceManager.itemGroups[idOfThatTile].blockEntity != null)
            {
                // go through each block entity and see if is in the same position as the tile we are removing
                foreach (GameObject blockEntity in blockEntities)
                {
                    if (Mathf.FloorToInt(blockEntity.transform.position.x) == worldPos.x && Mathf.FloorToInt(blockEntity.transform.position.y) == worldPos.y)
                    {
                        GameObject.Destroy(blockEntity);
                    }
                }
            }

            int itemDropId = refrenceManager.itemGroups[idOfThatTile].dropItemId;

            // if the tile can drop an item
            if (itemDropId != 0)
            {
                // create an ItemEntity
                GameObject itemObject = Instantiate(itemEntityPrefab, new Vector3(worldPos.x + 0.5f, worldPos.y + 0.5f, worldPos.z), new Quaternion(0f, 0f, 0f, 0f), transform);
                ItemEntity itemEntity = itemObject.GetComponent <ItemEntity>();

                itemEntity.itemGroup = refrenceManager.itemGroups[itemDropId];

                // initzialize the entity
                itemEntity.init();

                // add the item object to the chunk entities
                itemEntities.Add(itemObject);
            }

            // return the id of the tile we broke
            return(idOfThatTile);
        }
        else
        {
            return(-1);
        }
    }