示例#1
0
		public override void Use(Level level, ChunkLayer layer, int blockX, int blockY, out bool isConsumed)
		{
			base.Use(level, layer, blockX, blockY, out isConsumed);

			// TODO: I'm not really happy with the organization of this function.

			if ((layer == ChunkLayer.Background) && (level[layer, blockX, blockY] == 0))
			{
				// The background is the highest layer, and it's empty, so place the block there to fill the hole.
				level[layer, blockX, blockY] = BlockId;
				isConsumed = true;
			}
			else
			{
				if (layer.HasLayerAbove()) // you cannot place blocks above the ceiling
				{
					var useLayer = layer.GetLayerAbove();
					if (level[useLayer, blockX, blockY] == 0)
					{
						level[useLayer, blockX, blockY] = BlockId;
						isConsumed = true;
					}
				}
			}
		}
示例#2
0
        public int this[ChunkLayer layer, int x, int y]
        {
            get
            {
                if (MathHelper.IsInRange(x, 0, Width) && MathHelper.IsInRange(y, 0, Height))
                {
                    return(_blockIndex[(int)layer, y, x]);
                }
                else
                {
                    return(0);
                }
            }
            set
            {
                if (MathHelper.IsInRange(x, 0, Width) && MathHelper.IsInRange(y, 0, Height))
                {
                    _blockIndex[(int)layer, y, x] = value;

                    if (value == 0)
                    {
                        SetMetadata(layer, x, y, 0);
                    }
                }
            }
        }
示例#3
0
        public void SetMetadata(ChunkLayer layer, int x, int y, int metadata)
        {
            var chunk  = GetChunk(x, y);
            var coords = ToChunkCoordinates(x, y);

            chunk.SetMetadata(layer, coords.X, coords.Y, metadata);
        }
示例#4
0
        public int GetMetadata(ChunkLayer layer, int x, int y)
        {
            var chunk  = GetChunk(x, y);
            var coords = ToChunkCoordinates(x, y);

            return(chunk.GetMetadata(layer, coords.X, coords.Y));
        }
示例#5
0
        public void DrawBase(CanvasRenderingContext2D canvas,
                 Point position,
                 ChunkLayer layer,
                 bool xFlip,
                 bool yFlip)
        {
            var drawOrderIndex = 0;
            drawOrderIndex = xFlip ? (yFlip ? 0 : 1) : (yFlip ? 2 : 3);

            int tilePieceLength = 8;

            var ac = CanvasInformation.Create(tilePieceLength * 2, tilePieceLength * 2, false);
            var i = 0;

            var localPoint = new Point(0, 0);
            foreach (TileInfo tileItem in Tiles.Array())
            {
                var tile = tileItem.GetTile();
                if (tile.Truthy())
                {
                    if (tileItem.Priority == ((int)layer == 1))
                    {
                        var _xf = xFlip ^ tileItem.XFlip;
                        var _yf = yFlip ^ tileItem.YFlip;
                        var df = DrawInfo[DrawOrder[drawOrderIndex][i]];
                        localPoint.X = df[0] * tilePieceLength;
                        localPoint.Y = df[1] * tilePieceLength;
                        tile.DrawBase(ac.Context, localPoint, _xf, _yf, tileItem.Palette);
                    }
                }
                i++;
            }
            canvas.DrawImage(ac.Canvas, position.X, position.Y);
              
        }
		public void DrawVerticalLine(Chunk chunk, ChunkLayer layer, int top, int bottom, int x, int blockId)
		{
			for (var row = top; row <= bottom; row++)
			{
				chunk[layer, x, row] = blockId;
			}
		}
示例#7
0
		public int this[ChunkLayer layer, int x, int y]
		{
			get
			{
				if (MathHelper.IsInRange(x, 0, Width) && MathHelper.IsInRange(y, 0, Height))
				{
					return _blockIndex[(int)layer, y, x];
				}
				else
				{
					return 0;
				}
			}
			set
			{
				if (MathHelper.IsInRange(x, 0, Width) && MathHelper.IsInRange(y, 0, Height))
				{
					_blockIndex[(int)layer, y, x] = value;

					if (value == 0)
					{
						SetMetadata(layer, x, y, 0);
					}
				}
			}
		}
		public void DrawHorizontalLine(IChunkAccess chunk, ChunkLayer layer, int y, int left, int right, int blockId)
		{
			for (var column = left; column <= right; column++)
			{
				chunk[layer, column, y] = blockId;
			}
		}
    bool CheckIfSurface(Vector3Int voxelIndexInLayer, ChunkLayer <Voxel> voxelLayer)
    {
        if (IsLayerBorder(voxelLayer, voxelIndexInLayer) == false)
        {
            Voxel thisVoxel = voxelLayer.GetVoxel(voxelIndexInLayer);
            if (thisVoxel.IsAir)
            {
                return(false);
            }
            foreach (int x in sides)
            {
                foreach (int y in sides)
                {
                    foreach (int z in sides)
                    {
                        Vector3Int index = voxelIndexInLayer + new Vector3Int(x, y, z);

                        Voxel voxel = voxelLayer.GetVoxel(index);
                        if (voxel.IsAir)
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
        return(true); // assuming border voxels are a
    }
    public InstancedMeshInfo MeshChunkBlocks(Vector3Int chunkIndex, ChunkLayer <Block> chunkLayer)
    {
        InstancedMeshInfo instancedMeshInfo = new InstancedMeshInfo(modelTypeCount);
        Chunk <Block>     chunk             = chunkLayer.GetChunk(chunkIndex);
        Vector3           chunkStart        = chunkIndex.Multiply(chunkSettings.ChunkWorldLen);

        for (int x = 0; x < chunkSL; x++)
        {
            for (int y = 0; y < chunkSL; y++)
            {
                for (int z = 0; z < chunkSL; z++)
                {
                    Vector3Int indexInChunk = new Vector3Int(x, y, z);
                    Vector3    position     = new Vector3(x, y, z) * chunkSettings.VoxelSize + chunkStart;
                    Block      block        = chunk.GetVoxel(indexInChunk);

                    TechnicalBlock technicalBlock = block.technicalBlock;
                    if (technicalBlock != null)
                    {
                        instancedMeshInfo.AddModelInfos(technicalBlock.GetStaticMesh(), Matrix4x4.Translate(position) * technicalBlock.localTransform * Matrix4x4.Scale(Vector3.one * 0.5f));
                    }
                }
            }
        }
        return(instancedMeshInfo);
    }
示例#11
0
    public void SetBlock(int x, int y, int z, BlockData block)
    {
        int l, b;

        HeightToLayerAndBlock(y, out l, out b);

        if (m_layers.ContainsKey(l))
        {
            var layer = m_layers[l];

            layer.SetBlock(x, b, z, block);

            if (layer.IsEmpty())
            {
                m_layers.Remove(l);
            }
        }
        else
        {
            if (block == BlockData.GetDefault())
            {
                return;
            }
            var layer = new ChunkLayer();
            layer.SetBlock(x, b, z, block);
            m_layers.Add(l, layer);
        }
    }
    /*
     */
    public InstancedMeshInfo MeshChunkItems(Vector3Int chunkIndex, ChunkLayer <Block> chunkLayer)
    {
        InstancedMeshInfo instancedMeshInfo = new InstancedMeshInfo(modelTypeCount);
        Chunk <Block>     chunk             = chunkLayer.GetChunk(chunkIndex);
        Vector3           chunkStart        = chunkIndex.Multiply(chunkSettings.ChunkWorldLen);

        for (int x = 0; x < chunkSL; x++)
        {
            for (int y = 0; y < chunkSL; y++)
            {
                for (int z = 0; z < chunkSL; z++)
                {
                    Vector3Int     indexInChunk   = new Vector3Int(x, y, z);
                    Vector3        position       = new Vector3(x, y, z) * chunkSettings.VoxelSize + chunkStart;
                    Block          block          = chunk.GetVoxel(indexInChunk);
                    int            blockType      = block.blockType;
                    TechnicalBlock technicalBlock = block.technicalBlock;
                    if (technicalBlock != null)
                    {
                        TechnicalBlock.ModelInfo[] itemMeshes = technicalBlock.GetDynamicMesh();
                        for (int i = 0; i < itemMeshes.Length; i++)
                        {
                            if (itemMeshes[i].modelType != 0)
                            {
                                Matrix4x4 transform = itemMeshes[i].transform;
                                transform = Matrix4x4.Translate(position) * transform;
                                instancedMeshInfo.AddInstance(transform, itemMeshes[i].modelType);
                            }
                        }
                    }
                }
            }
        }
        return(instancedMeshInfo);
    }
示例#13
0
        public override void Use(Level level, ChunkLayer layer, int blockX, int blockY, out bool isConsumed)
        {
            base.Use(level, layer, blockX, blockY, out isConsumed);

            // TODO: I'm not really happy with the organization of this function.

            if ((layer == ChunkLayer.Background) && (level[layer, blockX, blockY] == 0))
            {
                // The background is the highest layer, and it's empty, so place the block there to fill the hole.
                level[layer, blockX, blockY] = BlockId;
                isConsumed = true;
            }
            else
            {
                if (layer.HasLayerAbove())                 // you cannot place blocks above the ceiling
                {
                    var useLayer = layer.GetLayerAbove();
                    if (level[useLayer, blockX, blockY] == 0)
                    {
                        level[useLayer, blockX, blockY] = BlockId;
                        isConsumed = true;
                    }
                }
            }
        }
示例#14
0
 public void DrawHorizontalLine(IChunkAccess chunk, ChunkLayer layer, int y, int left, int right, int blockId)
 {
     for (var column = left; column <= right; column++)
     {
         chunk[layer, column, y] = blockId;
     }
 }
示例#15
0
 public void DrawVerticalLine(Chunk chunk, ChunkLayer layer, int top, int bottom, int x, int blockId)
 {
     for (var row = top; row <= bottom; row++)
     {
         chunk[layer, x, row] = blockId;
     }
 }
示例#16
0
        public override void Render(ITessellator tessellator, IChunkAccess chunk, ChunkLayer layer, int x, int y)
        {
            base.Render(tessellator, chunk, layer, x, y);

            var color = tessellator.CurrentColor;

            tessellator.BindColor(OutlineColor);
            if (chunk[layer, x - 1, y] != chunk[layer, x, y])
            {
                _connectedWallTiles.Render(tessellator, _connectedWallTiles.GetTileIndexFromName(_westWall));
            }
            if (chunk[layer, x + 1, y] != chunk[layer, x, y])
            {
                _connectedWallTiles.Render(tessellator, _connectedWallTiles.GetTileIndexFromName(_eastWall));
            }
            if (chunk[layer, x, y - 1] != chunk[layer, x, y])
            {
                _connectedWallTiles.Render(tessellator, _connectedWallTiles.GetTileIndexFromName(_northWall));
            }
            if (chunk[layer, x, y + 1] != chunk[layer, x, y])
            {
                _connectedWallTiles.Render(tessellator, _connectedWallTiles.GetTileIndexFromName(_southWall));
            }
            tessellator.BindColor(color);
        }
    void BufferIntoDensityArray(Vector3Int chunkIndex, ChunkLayer <Voxel> chunkLayer)
    {
        Chunk <Voxel> chunk = chunkLayer.GetChunk(chunkIndex);
        Vector3Int    voxelZeroLayerIndex = chunkIndex.Multiply(chunkSettings.ChunkSL);
        Vector3Int    layerVoxelWorldSLs  = chunkLayer.ChunkLayerSLs.Multiply(chunkSettings.ChunkSL);

        for (int z = 0; z < chunkSettings.ChunkSL + 1; z++)
        {
            bool zOut = (z == chunkSettings.ChunkSL);
            for (int y = 0; y < chunkSettings.ChunkSL + 1; y++)
            {
                bool yOut = (y == chunkSettings.ChunkSL);
                for (int x = 0; x < chunkSettings.ChunkSL + 1; x++)
                {
                    bool       xOut    = (x == chunkSettings.ChunkSL);
                    Vector3Int index3D = new Vector3Int(x, y, z);
                    if (zOut || yOut || xOut)
                    {
                        Vector3Int indexInLayer = new Vector3Int(x, y, z) + voxelZeroLayerIndex;

                        // stay in range
                        if (indexInLayer.x < layerVoxelWorldSLs.x && indexInLayer.y < layerVoxelWorldSLs.y && indexInLayer.z < layerVoxelWorldSLs.z)
                        {
                            BufferSingleVoxel(chunkLayer, indexInLayer, index3D);
                        }
                    }
                    else
                    {
                        BufferSingleVoxel(chunk, index3D);
                    }
                }
            }
        }
    }
示例#18
0
        private List <List <Vector2I> > GetRegions(Chunk chunk, ChunkLayer layer, int tileId)
        {
            // This is the collection of regions we have found..
            var regions = new List <List <Vector2I> >();

            // Track whether a position has been checked.
            var mapFlags = new bool[chunk.Height, chunk.Width];

            for (var x = 0; x < chunk.Width; x++)
            {
                for (var y = 0; y < chunk.Height; y++)
                {
                    if (!mapFlags[y, x])
                    {
                        if (((layer == ChunkLayer.Floor) && (chunk[ChunkLayer.Floor, x, y] == tileId) && (chunk[ChunkLayer.Blocking, x, y] == 0)) ||
                            ((layer == ChunkLayer.Blocking) && (chunk[ChunkLayer.Floor, x, y] == tileId)))
                        {
                            var newRegion = GetRegionPoints(chunk, layer, x, y);
                            regions.Add(newRegion);

                            // Mark each position in the newly-found region as checked, so we don't try putting a second region here.
                            foreach (var point in newRegion)
                            {
                                mapFlags[point.Y, point.X] = true;
                            }
                        }
                    }
                }
            }

            return(regions);
        }
示例#19
0
    Vector3Int GetIndexInLayer(Vector3 position, ChunkLayer <T> layer)
    {
        //position += layer.VoxelSize * 0.5f * Vector3.one;
        Vector3    quotinent  = position / layer.VoxelSize;
        Vector3Int layerIndex = quotinent.ToVector3Int();

        return(layerIndex);
    }
示例#20
0
        public bool CanSeeSky(ChunkLayer layer, int blockX, int blockY)
        {
            var chunk  = GetChunk(blockX, blockY);
            var chunkX = (int)MathHelper.Modulo(blockX, CHUNK_WIDTH);
            var chunkY = (int)MathHelper.Modulo(blockY, CHUNK_HEIGHT);

            return(chunk.CanSeeSky(layer, chunkX, chunkY));
        }
示例#21
0
        public void CacheBase(ChunkLayer layer)
        {
            if (layer == ChunkLayer.Low ? (OnlyForeground()) : (OnlyBackground())) return;

            BaseCanvasCache[layer] = CanvasInformation.Create(TilePieceSideLength * TilePiecesSquareSize, TilePieceSideLength * TilePiecesSquareSize, false);

            drawTilePiecesBase(BaseCanvasCache[layer].Context, layer, TilePiecesSquareSize);
        }
示例#22
0
    void AllocateMainLayer()
    {
        Vector3Int chunkSLs = new Vector3Int(voxelSLs.x / chunkSettings.ChunkSL, voxelSLs.y / chunkSettings.ChunkSL, voxelSLs.z / chunkSettings.ChunkSL);

        mainLayer = new ChunkLayer <T>(chunkSLs, chunkSettings.VoxelSize * chunkSettings.ChunkSL, chunkSettings);

        //worldStart = -new Vector3(mainLevel.ChunkLayerSLs.x * 0.5f, mainLevel.ChunkLayerSLs.y * 0.5f, mainLevel.ChunkLayerSLs.z * 0.5f) * chunkSettings.ChunkWorldLen;
        worldStart = Vector3.zero;
    }
示例#23
0
		// TODO: Might need to pass in the owner entity, so that if a potion is used up, the player will get an empty bottle.
		public void Use(Level level, ChunkLayer layer, int blockX, int blockY)
		{
			var isConsumed = false;
			GetItem().Use(level, layer, blockX, blockY, out isConsumed);
			if (isConsumed)
			{
				StackSize--;
			}
		}
示例#24
0
        public virtual void Update(TimeSpan elapsed, Level level, ChunkLayer layer, int blockX, int blockY)
        {
            // TODO: Blocks need behavioral components.  Best if written in Lua.

            foreach (var behavior in _behaviors)
            {
                behavior.Update(elapsed, level, layer, blockX, blockY);
            }
        }
示例#25
0
		public void Fill(Chunk chunk, ChunkLayer layer, RectI bounds, int blockId)
		{
			for (var row = bounds.Top; row <= bounds.Bottom; row++)
			{
				for (var column = bounds.Left; column <= bounds.Right; column++)
				{
					chunk[layer, column, row] = blockId;
				}
			}
		}
示例#26
0
    GameObject RenderChunk2(Vector3Int index, ChunkLayer <Voxel> layer)
    {
        Mesh       chunkMesh = chunkMesherVoxel.MeshChunk(index, layer);
        GameObject chunkGO   = Instantiate(chunkPrefab);

        chunkGO.transform.localScale            *= world.chunkSettings.VoxelSize;
        chunkGO.GetComponent <MeshFilter>().mesh = chunkMesh;
        renderedChunks.Add(chunkGO);
        return(chunkGO);
    }
示例#27
0
        // TODO: Might need to pass in the owner entity, so that if a potion is used up, the player will get an empty bottle.
        public void Use(Level level, ChunkLayer layer, int blockX, int blockY)
        {
            var isConsumed = false;

            GetItem().Use(level, layer, blockX, blockY, out isConsumed);
            if (isConsumed)
            {
                StackSize--;
            }
        }
示例#28
0
 public void Fill(Chunk chunk, ChunkLayer layer, RectI bounds, int blockId)
 {
     for (var row = bounds.Top; row <= bounds.Bottom; row++)
     {
         for (var column = bounds.Left; column <= bounds.Right; column++)
         {
             chunk[layer, column, row] = blockId;
         }
     }
 }
示例#29
0
 public bool CanSeeSky(ChunkLayer layer, int blockX, int blockY)
 {
     if (!layer.HasLayerAbove())
     {
         return(true);
     }
     else
     {
         var layerAbove = layer.GetLayerAbove();
         return(CanSeeSky(layerAbove, blockX, blockY) && ((this[layerAbove, blockX, blockY] == NULL_BLOCK_ID) || !BlockRegistry.Instance.GetById(this[layerAbove, blockX, blockY]).IsOpaque));
     }
 }
示例#30
0
    void RenderChunk(Vector3Int index, ChunkLayer <Voxel> layer)
    {
        float chunkWorldLen = world.chunkSettings.ChunkWorldLen;
        // render 3 chunks

        Chunk <Voxel> chunk   = layer.GetChunk(index);
        GameObject    chunkGO = RenderChunk2(index, layer);

        Vector3 chunkCenterPos = (Vector3)index * chunkWorldLen;

        chunkGO.transform.position = chunkCenterPos;
    }
    // better not use this
    bool IsLayerBorder(ChunkLayer <Voxel> layer, Vector3Int voxelIndexInLayer)
    {
        Vector3Int layerVoxelSL = Utility.Multiply(layer.ChunkLayerSLs, chunkSettings.ChunkSL);

        if (voxelIndexInLayer.x == 0 || voxelIndexInLayer.y == 0 || voxelIndexInLayer.z == 0 || voxelIndexInLayer.x == layerVoxelSL.x - 1 || voxelIndexInLayer.y == layerVoxelSL.y - 1 || voxelIndexInLayer.z == layerVoxelSL.z - 1)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
示例#32
0
		// TODO: I can make farmland; now I need something to plant.

		/// <summary>
		/// You can only till dirt or grass.
		/// </summary>
		public override void Use(Level level, ChunkLayer layer, int blockX, int blockY, out bool isConsumed)
		{
			base.Use(level, layer, blockX, blockY, out isConsumed);

			layer = level.GetHighestVisibleLayer(blockX, blockY);
			var blockId = level[layer, blockX, blockY];
			if ((blockId == _dirtId) || (blockId == _grassId))
			{
				level[layer, blockX, blockY] = _tilledSoil;
			}

			// TODO: If durability <= 0, isConsumed = true.
			isConsumed = false;
		}
示例#33
0
 private void UpdateBlocks(TimeSpan elapsed, Level level, ChunkLayer layer)
 {
     for (var blockX = 0; blockX < Width; blockX++)
     {
         for (var blockY = 0; blockY < Height; blockY++)
         {
             var blockId = _blockIndex[(int)layer, blockY, blockX];
             if (blockId != NULL_BLOCK_ID)
             {
                 BlockRegistry.Instance.GetById(blockId).Update(elapsed, level, layer, blockX, blockY);
             }
         }
     }
 }
示例#34
0
		public override void Update(TimeSpan elapsed, Level level, ChunkLayer layer, int blockX, int blockY)
		{
			_totalElapsedTime += elapsed;
			if (_totalElapsedTime.TotalSeconds > 5)
			{
				_totalElapsedTime = TimeSpan.Zero;

				var oldMetadata = level.GetMetadata(layer, blockX, blockY);
				var newMetadata = oldMetadata + 1;
				if (newMetadata <= 7)
				{
					level.SetMetadata(layer, blockX, blockY, newMetadata);
				}
			}
		}
示例#35
0
        // TODO: I can make farmland; now I need something to plant.

        /// <summary>
        /// You can only till dirt or grass.
        /// </summary>
        public override void Use(Level level, ChunkLayer layer, int blockX, int blockY, out bool isConsumed)
        {
            base.Use(level, layer, blockX, blockY, out isConsumed);

            layer = level.GetHighestVisibleLayer(blockX, blockY);
            var blockId = level[layer, blockX, blockY];

            if ((blockId == _tilledSoil))
            {
                level[layer, blockX, blockY] = _wheatPlant;
                isConsumed = true;
            }

            // TODO: If durability <= 0, isConsumed = true.
        }
    void BufferSingleVoxel(ChunkLayer <Voxel> chunkLayer, Vector3Int indexInLayer, Vector3Int index3D)
    {
        int index = index3D.z * slSqr + index3D.y * sl + index3D.x;

        Voxel voxel = chunkLayer.GetVoxel(indexInLayer);

        try
        {
            renderBuffer[index] = ConvertToArrayDensity(voxel);
        }
        catch
        {
            Debug.Log("hey");
        }
    }
示例#37
0
        public override void Update(TimeSpan elapsed, Level level, ChunkLayer layer, int blockX, int blockY)
        {
            _totalElapsedTime += elapsed;
            if (_totalElapsedTime.TotalSeconds > 5)
            {
                _totalElapsedTime = TimeSpan.Zero;

                var oldMetadata = level.GetMetadata(layer, blockX, blockY);
                var newMetadata = oldMetadata + 1;
                if (newMetadata <= 7)
                {
                    level.SetMetadata(layer, blockX, blockY, newMetadata);
                }
            }
        }
示例#38
0
        public void InitCache()
        {
            BaseCanvasCache = new ChunkLayer<CanvasInformation>();
            PaletteAnimationCanvasesCache = new ChunkLayer<JsDictionary<int, PaletteAnimationCanvasFrames>>();
            TileAnimationCanvasesCache = new ChunkLayer<JsDictionary<int, TileAnimationCanvasFrames>>();


            TileAnimationCanvasesCache[ChunkLayer.Low] = new JsDictionary<int, TileAnimationCanvasFrames>();
            TileAnimationCanvasesCache[ChunkLayer.High] = new JsDictionary<int, TileAnimationCanvasFrames>();

            PaletteAnimationCanvasesCache[ChunkLayer.Low] = new JsDictionary<int, PaletteAnimationCanvasFrames>();
            PaletteAnimationCanvasesCache[ChunkLayer.High] = new JsDictionary<int, PaletteAnimationCanvasFrames>();
            CurrentTileAnimationFrameIndexCache = new List<int>();
            CurrentPaletteAnimationFrameIndexCache = new List<int>();
        }
示例#39
0
        public static ChunkLayer GetLayerAbove(this ChunkLayer @this)
        {
            switch (@this)
            {
            case ChunkLayer.Background:
                return(ChunkLayer.Floor);

            case ChunkLayer.Floor:
                return(ChunkLayer.Blocking);

            case ChunkLayer.Blocking:
                return(ChunkLayer.Ceiling);

            default:
                throw new InvalidOperationException("This isn't possible.");
            }
        }
示例#40
0
		// TODO: When farmland is broken, it should drop a dirt block.

		public override void Use(Level level, ChunkLayer layer, int blockX, int blockY, out bool isConsumed)
		{
			base.Use(level, layer, blockX, blockY, out isConsumed);

			var blockId = level[layer, blockX, blockY];
			if (blockId > 0)
			{
				level[layer, blockX, blockY] = 0;

				var blockEntity = new BlockEntity(blockId);
				blockEntity.MoveTo(level, new Vector2(blockX, blockY));
				level.AddEntity(blockEntity);
			}

			// TODO: If durability <= 0, isConsumed = true.
			isConsumed = false;
		}
    public void CalculateVoxel(Vector3Int voxelIndexInLayer, ChunkLayer <Voxel> voxelLayer)
    {
        Voxel voxel = voxelLayer.GetVoxel(voxelIndexInLayer);

        if (CheckIfSurface(voxelIndexInLayer, voxelLayer))
        {
            voxel.IsSurface = true;
            Debug.DrawRay(voxelIndexInLayer, Random.insideUnitSphere * 0.2f, Color.green, 1000);
            surfaceCounter++;
        }
        else
        {
            voxel.IsSurface = false;
            internalCounter++;
        }
        voxelLayer.SetVoxel(voxelIndexInLayer, voxel);
    }
    /*
     * public void SetElement(Vector3 position ,Quaternion rotation, Block element, TechnicalGoInfo technicalGoInfo)
     * {
     *  // Call previous technicalBlock's OnDestroy
     *  Block previousElement = GetElement(position);
     *  if (previousElement.technicalBlock != null) { previousElement.technicalBlock.OnDestroy(); }
     *
     *  // Create the new element
     *  InitializeGoConnection(position ,ref element, rotation, technicalGoInfo);
     *  world.SetVoxel(position, element);
     * }
     */
    public void SimulateWorld(float deltaTime)
    {
        ChunkLayer <Block> chunkLayer    = world.mainLayer;
        Vector3Int         chunkLayerSLs = chunkLayer.ChunkLayerSLs;

        for (int x = 0; x < chunkLayerSLs.x; x++)
        {
            for (int y = 0; y < chunkLayerSLs.y; y++)
            {
                for (int z = 0; z < chunkLayerSLs.z; z++)
                {
                    Vector3Int chunkIndex = new Vector3Int(x, y, z);
                    worldSimulator.SimulateChunk(chunkIndex, chunkLayer, deltaTime);
                }
            }
        }
    }
示例#43
0
		public int this[ChunkLayer layer, int blockX, int blockY]
		{
			get
			{
				var chunk = GetChunk(blockX, blockY);

				var chunkX = (int)MathHelper.Modulo(blockX, CHUNK_WIDTH);
				var chunkY = (int)MathHelper.Modulo(blockY, CHUNK_HEIGHT);
				return chunk[layer, chunkX, chunkY];
			}
			set
			{
				var chunk = GetChunk(blockX, blockY);

				var chunkX = (int)MathHelper.Modulo(blockX, CHUNK_WIDTH);
				var chunkY = (int)MathHelper.Modulo(blockY, CHUNK_HEIGHT);
				chunk[layer, chunkX, chunkY] = value;
			}
		}
示例#44
0
		public void Render(ITessellator tessellator, IChunkAccess chunk, ChunkLayer layer, int x, int y)
		{
			var metadata = chunk.GetMetadata(layer, x, y);
			if (_layers.ContainsKey(metadata))
			{
				_layers[metadata].Render(tessellator);
			}
			else
			{
				if (metadata > _maxKey)
				{
					_layers[_maxKey].Render(tessellator);
				}
				else
				{
					_layers[_minKey].Render(tessellator);
				}
			}
		}
示例#45
0
        public void CachePaletteAnimation(ChunkLayer layer)
        {
            var paletteAnimationCanvases = PaletteAnimationCanvasesCache[layer];
            foreach (var paletteAnimationIndex in GetAllPaletteAnimationIndexes())
            {
                var rect = getAnimationPaletteSurfaceInformation(paletteAnimationIndex, layer);

                if (rect == null)
                {
                    continue;
                }
                var paletteAnimationCanvasFrames = paletteAnimationCanvases[paletteAnimationIndex] = new PaletteAnimationCanvasFrames(paletteAnimationIndex);

                TilePaletteAnimation tilePaletteAnimation = SonicManager.Instance.TilePaletteAnimationManager.Animations[paletteAnimationIndex];

                paletteAnimationCanvasFrames.Position = new Point(rect.X * TilePiecesSquareSize, rect.Y * TilePiecesSquareSize);



                foreach (var currentFrame in tilePaletteAnimation.Frames)
                {
                    tilePaletteAnimation.CurrentFrame = currentFrame.FrameIndex;

                    var paletteAnimationCanvasFrame = paletteAnimationCanvasFrames.Frames[currentFrame.FrameIndex] = new PaletteAnimationCanvasFrame();
                    currentFrame.SetPalette();
                    var tilePaletteCanvas = CanvasInformation.Create(rect.Width * TilePiecesSquareSize, rect.Height * TilePiecesSquareSize, false);

                    paletteAnimationCanvasFrame.Canvas = tilePaletteCanvas;



                    paletteAnimationCanvasFrame.Canvas.Context.Save();
                    paletteAnimationCanvasFrame.Canvas.Context.Translate(-rect.X * TilePiecesSquareSize, -rect.Y * TilePiecesSquareSize);
                    drawTilePiecesAnimatedPalette(tilePaletteCanvas.Context, layer, TilePiecesSquareSize, paletteAnimationIndex);
                    paletteAnimationCanvasFrame.Canvas.Context.Restore();


                    currentFrame.ClearPalette();
                }
                tilePaletteAnimation.CurrentFrame = 0;
            }
        }
		public override void Render(ITessellator tessellator, IChunkAccess chunk, ChunkLayer layer, int x, int y)
		{
			base.Render(tessellator, chunk, layer, x, y);

			var color = tessellator.CurrentColor;
			tessellator.BindColor(OutlineColor);
			if (chunk[layer, x - 1, y] != chunk[layer, x, y])
			{
				_connectedWallTiles.Render(tessellator, _connectedWallTiles.GetTileIndexFromName(_westWall));
			}
			if (chunk[layer, x + 1, y] != chunk[layer, x, y])
			{
				_connectedWallTiles.Render(tessellator, _connectedWallTiles.GetTileIndexFromName(_eastWall));
			}
			if (chunk[layer, x, y - 1] != chunk[layer, x, y])
			{
				_connectedWallTiles.Render(tessellator, _connectedWallTiles.GetTileIndexFromName(_northWall));
			}
			if (chunk[layer, x, y + 1] != chunk[layer, x, y])
			{
				_connectedWallTiles.Render(tessellator, _connectedWallTiles.GetTileIndexFromName(_southWall));
			}
			tessellator.BindColor(color);
		}
示例#47
0
		private void UpdateBlocks(TimeSpan elapsed, Level level, ChunkLayer layer)
		{
			for (var blockX = 0; blockX < Width; blockX++)
			{
				for (var blockY = 0; blockY < Height; blockY++)
				{
					var blockId = _blockIndex[(int)layer, blockY, blockX];
					if (blockId != NULL_BLOCK_ID)
					{
						BlockRegistry.Instance.GetById(blockId).Update(elapsed, level, layer, blockX, blockY);
					}
				}
			}
		}
示例#48
0
		public void SetMetadata(ChunkLayer layer, int x, int y, int metadata)
		{
			_blockMetadata[(int)layer, x, y] = metadata;
		}
示例#49
0
		public int GetMetadata(ChunkLayer layer, int x, int y)
		{
			return _blockMetadata[(int)layer, x, y];
		}
示例#50
0
		public bool CanSeeSky(ChunkLayer layer, int blockX, int blockY)
		{
			if (!layer.HasLayerAbove())
			{
				return true;
			}
			else
			{
				var layerAbove = layer.GetLayerAbove();
				return CanSeeSky(layerAbove, blockX, blockY) && ((this[layerAbove, blockX, blockY] == NULL_BLOCK_ID) || !BlockRegistry.Instance.GetById(this[layerAbove, blockX, blockY]).IsOpaque);
			}
		}
示例#51
0
		public bool CanSeeSky(ChunkLayer layer, int blockX, int blockY)
		{
			var chunk = GetChunk(blockX, blockY);
			var chunkX = (int)MathHelper.Modulo(blockX, CHUNK_WIDTH);
			var chunkY = (int)MathHelper.Modulo(blockY, CHUNK_HEIGHT);
			return chunk.CanSeeSky(layer, chunkX, chunkY);
		}
示例#52
0
        private Rectangle getAnimationPaletteSurfaceInformation(int paletteAnimationIndex, ChunkLayer layer)
        {

            int lowestX = int.MaxValue;
            int highestX = int.MinValue;
            int lowestY = int.MaxValue;
            int highestY = int.MinValue;



            for (int pieceY = 0; pieceY < TilePieceSideLength; pieceY++)
            {
                for (int pieceX = 0; pieceX < TilePieceSideLength; pieceX++)
                {
                    var piece = TilePieces[pieceX][pieceY].GetTilePiece();
                    if (piece == null) continue;
                    if (layer == ChunkLayer.Low ? (piece.OnlyForeground()) : (piece.OnlyBackground())) continue;

                    if (piece.AnimatedPaletteIndexes.IndexOfFast(paletteAnimationIndex) == -1) continue;

                    if (pieceX < lowestX)
                        lowestX = pieceX;
                    if (pieceX > highestX)
                        highestX = pieceX;

                    if (pieceY < lowestY)
                        lowestY = pieceY;
                    if (pieceY > highestY)
                        highestY = pieceY;
                }
            }
            if (lowestX == int.MaxValue) return null;

            return new Rectangle(lowestX, lowestY, highestX - lowestX + 1, highestY - lowestY + 1);
        }
示例#53
0
		public virtual void Render(ITessellator tessellator, IChunkAccess chunk, ChunkLayer layer, int x, int y)
		{
			Render(tessellator);
		}
示例#54
0
		public void Fill(Chunk chunk, ChunkLayer layer, int blockId)
		{
			Fill(chunk, layer, new RectI(0, 0, chunk.Width, chunk.Height), blockId);
		}
示例#55
0
		public int GetMetadata(ChunkLayer layer, int x, int y)
		{
			var chunk = GetChunk(x, y);
			var coords = ToChunkCoordinates(x, y);
			return chunk.GetMetadata(layer, coords.X, coords.Y);
		}
示例#56
0
        public void DrawAnimatedPalette(CanvasRenderingContext2D canvas, Point position, ChunkLayer layer, bool xFlip, bool yFlip, int animatedPaletteIndex)
        {


            var animatedPaletteCacheIndex = getAnimatedPaletteCacheIndex(xFlip, yFlip, animatedPaletteIndex, SonicManager.Instance.TilePaletteAnimationManager.GetPaletteAnimation(animatedPaletteIndex).CurrentFrame);


            CanvasInformation animatedPaletteCache = animatedPaletteCaches[animatedPaletteCacheIndex];
            if (animatedPaletteCache == null)
            {

                var drawOrderIndex = 0;
                drawOrderIndex = xFlip ? (yFlip ? 0 : 1) : (yFlip ? 2 : 3);

                int tilePieceLength = 8;

                var ac = CanvasInformation.Create(tilePieceLength * 2, tilePieceLength * 2, false);
                var i = 0;

                var localPoint = new Point(0, 0);
                foreach (TileInfo tileItem in Tiles.Array())
                {
                    var tile = tileItem.GetTile();
                    if (tile.Truthy())
                    {
                        if (tileItem.Priority == ((int)layer == 1))
                        {
                            var _xf = xFlip ^ tileItem.XFlip;
                            var _yf = yFlip ^ tileItem.YFlip;
                            var df = DrawInfo[DrawOrder[drawOrderIndex][i]];
                            localPoint.X = df[0] * tilePieceLength;
                            localPoint.Y = df[1] * tilePieceLength;
                            tile.DrawAnimatedPalette(ac.Context, localPoint, _xf, _yf, tileItem.Palette, animatedPaletteIndex);
                        }
                    }
                    i++;
                }
                animatedPaletteCaches[animatedPaletteCacheIndex] = animatedPaletteCache=ac;
            }

            canvas.DrawImage(animatedPaletteCache.Canvas, position.X, position.Y);
        }
示例#57
0
		public virtual void Update(TimeSpan elapsed, Level level, ChunkLayer layer, int blockX, int blockY)
		{
		}
示例#58
0
		public void SetMetadata(ChunkLayer layer, int x, int y, int metadata)
		{
			var chunk = GetChunk(x, y);
			var coords = ToChunkCoordinates(x, y);
			chunk.SetMetadata(layer, coords.X, coords.Y, metadata);
		}
示例#59
0
		/// <summary>
		/// Use this item on the selected location.
		/// </summary>
		public virtual void Use(Level level, ChunkLayer layer, int blockX, int blockY, out bool isConsumed)
		{
			isConsumed = false;
		}
示例#60
0
        public void DrawAnimationDebug(CanvasRenderingContext2D canvas, Point position, ChunkLayer layer, TileChunkDebugDrawOptions debugDrawOptions )
        {
            if (debugDrawOptions == null) return;
            canvas.Save();
            canvas.FillStyle = "White";
            canvas.TextBaseline = TextBaseline.Top;
            {
                int yOffset = layer == ChunkLayer.Low ? 0 : 64;
                if (debugDrawOptions.ShowBaseData)
                {
                    canvas.FillText("Base", position.X + 0, position.Y + yOffset);
                }

                if (debugDrawOptions.ShowPaletteAnimationData)
                {
                    if (HasPixelAnimations())
                    {
                        var paletteAnimationCanvases = PaletteAnimationCanvasesCache[layer];
                        foreach (var paletteAnimationIndex in GetAllPaletteAnimationIndexes())
                        {
                            var paletteAnimationCanvasFrames = paletteAnimationCanvases[paletteAnimationIndex];
                            if (paletteAnimationCanvasFrames == null) continue;

                            var currentFrame = SonicManager.Instance.TilePaletteAnimationManager.GetCurrentFrame(paletteAnimationIndex);

                            canvas.FillText("Palette " + paletteAnimationIndex + "-" + currentFrame.FrameIndex, position.X + 25, position.Y + yOffset + (paletteAnimationIndex*13));
                        }
                    }
                }
                if (debugDrawOptions.ShowTileAnimationData)
                {
                    if (HasTileAnimations())
                    {
                        var tileAnimationCanvases = TileAnimationCanvasesCache[layer];
                        foreach (var tileAnimationIndex in GetAllTileAnimationIndexes())
                        {
                            var tileAnimationCanvasFrames = tileAnimationCanvases[tileAnimationIndex];
                            if (tileAnimationCanvasFrames == null) continue;

                            var currentFrame = SonicManager.Instance.TileAnimationManager.GetCurrentFrame(tileAnimationIndex);
                            canvas.FillText("Tile " + tileAnimationIndex + "-" + currentFrame.FrameIndex, position.X + 75, position.Y + yOffset + (tileAnimationIndex*13));
                        }
                    }
                }
            }
            if (debugDrawOptions.OutlineChunk)
            {
                canvas.StrokeStyle = "black";
                canvas.StrokeRect(position.X, position.Y, 128, 128);
            }

            if (debugDrawOptions.OutlineTiles)
            {
                canvas.StrokeStyle = "green";
                for (int x = 0; x < TileSideLength; x++)
                {
                    for (int y = 0; y < TileSideLength; y++)
                    {
                        canvas.StrokeRect(position.X + (x * TileSquareSize), position.Y + (y * TileSquareSize), TileSquareSize, TileSquareSize);
                    }
                }
            }
            if (debugDrawOptions.OutlineTilePieces)
            {
                canvas.StrokeStyle = "purple";
                for (int x = 0; x < TilePieceSideLength; x++)
                {
                    for (int y = 0; y < TilePieceSideLength; y++)
                    {
                        canvas.StrokeRect(position.X + (x * TilePiecesSquareSize), position.Y + (y * TilePiecesSquareSize), TilePiecesSquareSize, TilePiecesSquareSize);
                    }
                }
            }
            if (debugDrawOptions.OutlineTile != null)
            {
                /*
                                canvas.StrokeStyle = "yellow";
                                for (int x = 0; x < TileSideLength; x++)
                                {
                                    for (int y = 0; y < TileSideLength; y++)
                                    {
                                        var tilePieceInfo = this.GetTilePiece(x, y);
                                        if (tilePieceInfo == null) continue;
                                        var tilePiece = tilePieceInfo.GetTilePiece();
                                        if (tilePiece == null) continue;

                        

                                        if (tilePiece == debugDrawOptions.OutlineTilePiece)
                                        {
                                            canvas.StrokeRect(position.X + (x * TileSquareSize), position.Y + (y * TileSquareSize), TileSquareSize, TileSquareSize);
                                        }
                                    }
                                }
                */
            }
            
            if (debugDrawOptions.OutlineTilePiece != null)
            {
                canvas.StrokeStyle = "yellow";
                for (int x = 0; x < TilePieceSideLength; x++)
                {
                    for (int y = 0; y < TilePieceSideLength; y++)
                    {
                        var tilePieceInfo = this.GetTilePieceInfo(x, y,false);
                        if (tilePieceInfo == null) continue;
                        var tilePiece = tilePieceInfo.GetTilePiece();
                        if (tilePiece == null) continue;
                        if (tilePiece.Index == debugDrawOptions.OutlineTilePiece.Block)
                        {
                            canvas.StrokeRect(position.X + (x * TilePiecesSquareSize), position.Y + (y * TilePiecesSquareSize), TilePiecesSquareSize, TilePiecesSquareSize);
                        }
                    }
                }
            }
            canvas.Restore();
        }