Example #1
0
    private void CreateBlocks(ILevelConfiguration config)
    {
        if (_blocks != null)
        {
            foreach (var block in _blocks)
            {
                block.Release();
            }
            _blocks.Clear();
        }
        else
        {
            _blocks = new List <IBlock>();
        }

        _currentBlocksCount = 0;

        foreach (var blockPlaceholder in config.BlocksPlaceholders)
        {
            var block = BlockFactory.Create(blockPlaceholder);
            _blocks.Add(block);
            block.Destroyed += OnBlockDestroy;
        }

        CurrentBlocksCount = _blocks.Count;
    }
Example #2
0
    /// <summary>
    /// Places block of given type at _pos if possible, if successful rebuilds mesh, if at chunk border rebuilds neighbour mesh aswell
    /// </summary>
    /// <param name="type">what block to place</param>
    /// <param name="_pos">where to place block (world pos)</param>
    /// <returns>returns placed block, returns null if spot is blocked by player / occupied by solid block</returns>
    public Block PlaceBlock(BlockType type, Vector3 _pos, bool ignoreEntities = false)
    {
        var idx      = GetBlockIdx(_pos);
        var blockPos = Vector3Int.FloorToInt(_pos);
        var block    = Blocks[idx.x, idx.y, idx.z];

        if (block != null)
        {
            if (block is Fluid)
            {
                block.OnDestroyed();
            }
            else
            {
                return(null);
            }
        }
        var newBlock = BlockFactory.Create(type, blockPos);

        if (!ignoreEntities && !newBlock.CanPlaceInEntity && Physics.CheckBox(blockPos + Vector3.one * 0.5f, Vector3.one * 0.45f))
        {
            return(null);
        }
        Blocks[idx.x, idx.y, idx.z] = newBlock;
        newBlock.OnPlaced();
        BuildMesh();
        UpdateAdjacentChunks(idx);
        return(newBlock);
    }
Example #3
0
    public void TwoArguments()
    {
        var n1 = A.Dummy <INode>();
        var n2 = A.Dummy <INode>();

        var sut    = new BlockFactory();
        var result = sut.Create(n1, n2);

        result.Nodes.Should().BeEquivalentTo(new [] { n1, n2 });
    }
Example #4
0
    public void While()
    {
        var nodes  = new List <INode>();
        var @while = A.Dummy <ICondition>();

        var sut    = new BlockFactory();
        var result = sut.Create(nodes, @while);

        result.Nodes.Should().BeSameAs(nodes);
        result.While.Should().BeSameAs(@while);
    }
Example #5
0
        public void CheckIfBlockFactoryCreatesBlocksWithCorrectWeight()
        {
            var blockFactory = new BlockFactory(5.2);
            var block        = blockFactory.Create();

            Assert.AreEqual(block.Weight, 5.2);

            blockFactory = new BlockFactory(5);
            block        = blockFactory.Create();
            Assert.AreEqual(block.Weight, 5);
        }
Example #6
0
    //TODO make Tree blueprint a SO ref in the biome
    private void AddTree(int x, int y, int z)
    {
        var height = rng.Next(4, 8);

        for (int y1 = 0; y1 < height; y1++)
        {
            Blocks[x, y + y1, z] = BlockFactory.Create(TerrainGenerator.Instance.CurrentBiome.log, new Vector3Int(x + Pos.x * SIZE, y + y1, z + Pos.y * SIZE));
        }
        for (int x1 = x - 2; x1 < x + 2; x1++)
        {
            for (int y1 = y + height - 2; y1 < y + height + 2; y1++)
            {
                for (int z1 = z - 2; z1 < z + 2; z1++)
                {
                    if (Blocks[x1, y1, z1] == null)
                    {
                        Blocks[x1, y1, z1] = BlockFactory.Create(TerrainGenerator.Instance.CurrentBiome.leaves, new Vector3Int(x1 + Pos.x * SIZE, y1, z1 + Pos.y * SIZE));
                    }
                }
            }
        }
    }
Example #7
0
    /// <summary>
    /// Places block of given type at _pos if possible, does not rebuild meshes but stores affected chunks, useful for placing multiple blocks at once
    /// </summary>
    /// <param name="type">what block to place</param>
    /// <param name="_pos">where to place block (world pos)</param>
    /// <param name="affectedChunks">hashset to store affected chunks in</param>
    /// <returns>returns false if spot is blocked by player / occupied by solid block</returns>
    public Block PlaceBlockSilent(BlockType type, Vector3 _pos, HashSet <Chunk> affectedChunks)
    {
        var idx      = GetBlockIdx(_pos);
        var blockPos = Vector3Int.FloorToInt(_pos);
        var block    = Blocks[idx.x, idx.y, idx.z];

        if (!(block is Fluid))
        {
            if (block != null)
            {
                return(null);
            }
        }
        Blocks[idx.x, idx.y, idx.z] = BlockFactory.Create(type, blockPos);
        Blocks[idx.x, idx.y, idx.z].OnPlaced();
        affectedChunks.Add(this);
        var tg = TerrainGenerator.Instance;

        if (idx.x == 0)
        {
            affectedChunks.Add(tg.GetChunkByIdx(Pos + Vector2Int.left));
        }
        else if (idx.x == SIZE - 1)
        {
            affectedChunks.Add(tg.GetChunkByIdx(Pos + Vector2Int.right));
        }
        if (idx.z == 0)
        {
            affectedChunks.Add(tg.GetChunkByIdx(Pos + Vector2Int.down));
        }
        else if (idx.z == SIZE - 1)
        {
            affectedChunks.Add(tg.GetChunkByIdx(Pos + Vector2Int.up));
        }
        return(Blocks[idx.x, idx.y, idx.z]);
    }
Example #8
0
 /// <summary>
 /// Создать платформу 3х3 из тайлов
 /// </summary>
 /// <returns>Блок</returns>
 private Block CreateSpawnPlatform()
 {
     return(_blockFactory.Create(3, 3, new Vector3()));
 }
Example #9
0
 /// <summary>
 /// Generates blocks / water using TerrainGenerator settings / noise
 /// </summary>
 public virtual Task Generate()
 {
     return(Task.Run(() =>
     {
         Blocks = new Block[SIZE, HEIGHT, SIZE];
         var tg = TerrainGenerator.Instance;
         var biome = tg.CurrentBiome;
         rng = new System.Random(Pos.GetHashCode() + biome.surfaceNoise.seed);
         for (int x = 0; x < SIZE; x++)
         {
             for (int z = 0; z < SIZE; z++)
             {
                 var localSurfaceLevel = tg.SurfaceNoise(x + Pos.x * SIZE, z + Pos.y * SIZE);
                 var highestBlock = localSurfaceLevel;
                 if (biome.surfaceFluidLevel > highestBlock)
                 {
                     highestBlock = biome.surfaceFluidLevel;
                 }
                 for (int y = highestBlock; y >= 0; y--)
                 {
                     Block block = null;
                     var blockPos = new Vector3Int(x + Pos.x * SIZE, y, z + Pos.y * SIZE);
                     if (y == 0)
                     {
                         block = BlockFactory.Create(BlockType.BottomStone, blockPos);
                     }
                     else if (y > localSurfaceLevel)
                     {
                         block = BlockFactory.Create(biome.surfaceFluid, blockPos);
                     }
                     else if (y <= Mathf.Max(biome.surfaceFluidLevel, localSurfaceLevel) - biome.minCaveSurfaceDistance &&
                              tg.CaveNoise(x + Pos.x * SIZE, y, z + Pos.y * SIZE) > biome.caveNoiseThreshold)
                     {
                         if (y < biome.caveFluidLevel)
                         {
                             block = BlockFactory.Create(biome.caveFluid, blockPos);
                         }
                         else
                         {
                             var above = Blocks[x, y + 1, z];
                             if (above is Fluid)
                             {
                                 block = BlockFactory.Create(above.Type, blockPos);
                             }
                             else
                             {
                                 continue;
                             }
                         }
                     }
                     else if (y == localSurfaceLevel)
                     {
                         if (y >= biome.surfaceFluidLevel)
                         {
                             block = BlockFactory.Create(biome.surface, blockPos);
                         }
                         else
                         {
                             block = BlockFactory.Create(biome.floodedSurface, blockPos);
                         }
                     }
                     else if (y > localSurfaceLevel - biome.surfaceLayerSize)
                     {
                         block = BlockFactory.Create(biome.surfaceLayer, blockPos);
                     }
                     else
                     {
                         foreach (var layer in biome.customLayers)
                         {
                             if (y >= layer.minHeight && y < layer.minHeight + layer.size)
                             {
                                 block = BlockFactory.Create(layer.block, blockPos);
                                 break;
                             }
                         }
                         if (block == null)
                         {
                             block = BlockFactory.Create(biome.defaultBlock, blockPos);
                         }
                     }
                     Blocks[x, y, z] = block;
                 }
             }
         }
     }));
 }