Exemple #1
0
        public override void OnPlace(Block oldBlock, bool triggerWorldUpdates, bool addToRender)
        {
            var render = _chunk.Region.Render;

            for (var i = 0; i < Faces; i++)
            {
                var block = GetNeighbour((BlockFace)i);
                var face  = (BlockFace)i;

                var visibleBySlab = face == BlockFace.Up && !_upside || face == BlockFace.Down && _upside;

                SetFaceVisible(i, visibleBySlab || block == null ||
                               !block.IsFaceOpaque(BlockFaceMethods.GetOpposite((BlockFace)i)));
                if (!addToRender || !IsFaceVisible(i))
                {
                    continue;
                }
                if (visibleBySlab)
                {
                    render.AddData(i, this, _blockLight.Light, _blockLight.Sunlight);
                }
                else
                {
                    render.AddData(i, this, block?.BlockLight.Light ?? 0,
                                   block?.BlockLight.Sunlight ?? 0);
                }
            }
        }
Exemple #2
0
        public override void HandleMousePush(MouseButtonEventArgs args)
        {
            //This method is provisional! It will be reformed when inventories are added.
            if (args.Button == MouseButton.Right)
            {
                var matInstance = _player.Inventory.Hotbar.SelectedBlock;
                if (_player.BlockRayTracer.Result == null)
                {
                    return;
                }
                var result   = _player.BlockRayTracer.Result;
                var position = result.Position + BlockFaceMethods.GetRelative(_player.BlockRayTracer.Face);
                Console.WriteLine(_player.BlockRayTracer.Face);
                if (!matInstance.CanBePlaced(position, _player.World))
                {
                    return;
                }
                if (!matInstance.Passable && _player.CollisionBox.Collides(matInstance.BlockModel.BlockCollision,
                                                                           _player.Position,
                                                                           position.ToFloat(), null, out var data) && data.Distance > 0.01f)
                {
                    return;
                }
                _player.World.SetBlock(matInstance, position);
            }
            else if (args.Button == MouseButton.Left)
            {
                if (_player.BlockRayTracer.Result == null)
                {
                    return;
                }
                _player.World.SetBlock(BlockSnapshotAir.Instance, _player.BlockRayTracer.Result.Position);
            }
            else if (args.Button == MouseButton.Middle)
            {
                if (_player.BlockRayTracer.Result == null)
                {
                    return;
                }
                var result   = _player.BlockRayTracer.Result;
                var position = result.Position;

                var clickedBlock = _player.World.GetBlock(position);
                position = result.Position + BlockFaceMethods.GetRelative(_player.BlockRayTracer.Face);
                var relativeBlock = _player.World.GetBlock(position);

                Console.WriteLine(clickedBlock?.BlockLight?.Light);
                Console.WriteLine(relativeBlock?.BlockLight?.Light);

                if (_player.BlockRayTracer.Result == null)
                {
                    return;
                }
                if (!BlockManager.TryGet(result.Id, out var snapshot))
                {
                    return;
                }
                _player.Inventory.Hotbar.SelectedBlock = snapshot;
            }
        }
Exemple #3
0
    private void MoveEnemy(Vector3 enemyToPlayer)
    {
        _state = EnemyState.Moving;
        Vector3 dir = Mathf.Abs(enemyToPlayer.x) > Mathf.Abs(enemyToPlayer.z) ?
                      Vector3.right * Mathf.Sign(enemyToPlayer.x) :
                      Vector3.forward * Mathf.Sign(enemyToPlayer.z);
        BlockFace face = BlockFaceMethods.BlockFaceFromNormal(dir.normalized);

        _enemyController.LookAtFaceDir(face);
        _face = face;
        _animator.SetTrigger("Bounce");
    }
Exemple #4
0
    void SetNewFace()
    {
        Vector3    direction       = SnapToCardinal(transform.forward);
        GameObject blockGameObject = GetGameObjInDir(direction);
        BlockFace  blockFaceOfNeighbouringBlock = BlockFaceMethods.BlockFaceFromNormal(-direction);

        if (blockGameObject == null)
        {
            return;
        }

        BlockBehaviour blockBehaviour = blockGameObject.GetComponent <BlockBehaviour>();

        blockBehaviour.OnFaceSelect(blockFaceOfNeighbouringBlock);
        _highlighted = blockBehaviour;
    }
Exemple #5
0
    private void MoveEnemy(Vector3 enemyToPlayer)
    {
        _state = EnemyState.Moving;
        Vector3 xDir = Vector3.right * Mathf.Sign(enemyToPlayer.x);
        Vector3 zDir = Vector3.forward * Mathf.Sign(enemyToPlayer.z);

        bool canMoveInX = Physics.Raycast(transform.position + xDir, Vector3.down, 1f, CollidableLayers);
        bool canMoveInZ = Physics.Raycast(transform.position + zDir, Vector3.down, 1f, CollidableLayers);

        if (!canMoveInX && !canMoveInZ)
        {
            _state = EnemyState.Idle;
            return;
        }
        Vector3 dir = Vector3.zero;

        if (Mathf.Abs(enemyToPlayer.x) > Mathf.Abs(enemyToPlayer.z))
        {
            if (canMoveInX)
            {
                dir = xDir;
            }
            else
            {
                dir = zDir;
            }
        }
        else
        {
            if (canMoveInZ)
            {
                dir = zDir;
            }
            else
            {
                dir = xDir;
            }
        }


        BlockFace face = BlockFaceMethods.BlockFaceFromNormal(dir.normalized);

        _enemyController.LookAtFaceDir(face);
        _face = face;
        _animator.SetTrigger("Bounce");
        _renderer.material.color = Color.red + Color.white;
    }
Exemple #6
0
        public override void OnPlace(Block oldBlock, bool triggerWorldUpdates, bool addToRender)
        {
            var render = _chunk.Region.Render;

            for (var i = 0; i < Faces; i++)
            {
                var block        = GetNeighbour((BlockFace)i);
                var oppositeFace = BlockFaceMethods.GetOpposite((BlockFace)i);
                var vis          = SetFaceVisible(i, block == null || !block.IsFaceOpaque(oppositeFace));
                if (!vis || !addToRender)
                {
                    continue;
                }
                var light = block?.BlockLight;
                render.AddData(i, this, light?.Light ?? 0, light?.Sunlight ?? 0);
            }
        }
Exemple #7
0
        public static void ExpandFrom(Block from, BlockLightSource source, sbyte light)
        {
            var queue = new Queue <Block>();

            for (var i = 0; i < Faces; i++)
            {
                var face = (BlockFace)i;
                if (!from.CanLightPassThrough(face))
                {
                    continue;
                }
                var opposite  = BlockFaceMethods.GetOpposite(face);
                var neighbour = from.GetNeighbour((BlockFace)i);
                Expand(queue, neighbour, source, light, from, opposite);
            }

            UpdateRender(queue);
        }
Exemple #8
0
    void SetNewFaces()
    {
        foreach (Vector3 direction in _directions)
        {
            GameObject blockGameObject = GetGameObjInDir(direction);
            BlockFace  blockFaceOfNeighbouringBlock = BlockFaceMethods.BlockFaceFromNormal(-direction);

            if (blockGameObject == null)
            {
                continue;
            }

            BlockBehaviour blockBehaviour = blockGameObject.GetComponent <BlockBehaviour>();

            blockBehaviour.OnFaceSelect(blockFaceOfNeighbouringBlock);
            _faceMap.Add(blockBehaviour);
        }
    }
Exemple #9
0
        private static void Expand(Queue <Block> updatedBlocks, Block to, BlockLightSource source,
                                   sbyte light, Block from, BlockFace fromFace)
        {
            if (to == null || !to.CanLightBePassedFrom(fromFace, from))
            {
                return;
            }
            var blockLight = to.BlockLight;

            if (blockLight.Light >= light)
            {
                return;
            }

            blockLight.Light  = light;
            blockLight.Source = source;
            updatedBlocks?.Enqueue(to);

            var toLight = (sbyte)(light - to.StaticData.BlockLightPassReduction);

            if (toLight < 1)
            {
                return;
            }

            for (var i = 0; i < Faces; i++)
            {
                var face = (BlockFace)i;

                if (!to.CanLightPassThrough(face))
                {
                    continue;
                }
                var opposite  = BlockFaceMethods.GetOpposite(face);
                var neighbour = to.GetNeighbour((BlockFace)i);
                if (neighbour == null)
                {
                    continue;
                }


                Expand(updatedBlocks, neighbour, source, toLight, to, opposite);
            }
        }
Exemple #10
0
    void SetNewFaces()
    {
        foreach (Vector3 direction in _directions)
        {
            GameObject blockGameObject = GetGameObjInDir(direction);
            BlockFace  blockFaceOfNeighbouringBlock = BlockFaceMethods.BlockFaceFromNormal(-direction);

            if (blockGameObject == null)
            {
                continue;
            }

            BlockFaceBehaviour blockFaceBehaviour = blockGameObject.GetComponent <BlockFaceBehaviour>();
            BlockBehaviour     blockBehaviour     = blockGameObject.GetComponent <BlockBehaviour>();

            if (blockBehaviour.IsTranslating())
            {
                continue;
            }

            foreach (BlockPlugin plugin in blockBehaviour.GetPlugins())
            {
                MoveablePlugin moveable = plugin as MoveablePlugin;
                if (moveable == null)
                {
                    continue;
                }

                if (!moveable.IsDisplaced() ||
                    blockFaceOfNeighbouringBlock == moveable.GetDisplacedFace())
                {
                    BlockFace moveableFace = moveable.IsDisplaced()
                        ? moveable.GetDisplacedFace()
                        : blockFaceOfNeighbouringBlock;
                    blockFaceBehaviour.HighlightFace(blockFaceOfNeighbouringBlock);
                    _faceMap.Add(blockFaceBehaviour, moveableFace);;
                }

                break;
            }
        }
    }
Exemple #11
0
        public override void OnNeighbourBlockChange(Block from, Block to, BlockFace relative)
        {
            var faceInt    = (int)relative;
            var oldVisible = IsFaceVisible(faceInt);
            var newVisible = !to.IsFaceOpaque(BlockFaceMethods.GetOpposite(relative));

            if (oldVisible == newVisible)
            {
                return;
            }
            SetFaceVisible(faceInt, newVisible);
            if (newVisible)
            {
                _chunk.Region.Render.AddData(faceInt, this, to.BlockLight.Light, to.BlockLight.Sunlight);
            }
            else
            {
                _chunk.Region.Render.RemoveData(faceInt, this);
            }
        }
Exemple #12
0
        public override void OnNeighbourBlockChange(Block from, Block to, BlockFace relative)
        {
            var slabFace = relative == BlockFace.Up && !_upside || relative == BlockFace.Down && _upside;

            if (slabFace)
            {
                return;
            }
            var faceInt    = (int)relative;
            var newVisible = !to.IsFaceOpaque(BlockFaceMethods.GetOpposite(relative));

            SetFaceVisible(faceInt, newVisible);
            if (newVisible)
            {
                _chunk.Region.Render.AddData(faceInt, this, to.BlockLight.Light, to.BlockLight.Sunlight);
            }
            else
            {
                _chunk.Region.Render.RemoveData(faceInt, this);
            }
        }
Exemple #13
0
    private void HandleMouseClick()
    {
        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        bool       didRayCastHit = Physics.Raycast(ray, out hit);

        // https://answers.unity.com/questions/50279/check-if-layer-is-in-layermask.html
        // looking for block collisions only
        bool isHitTargetInCollidableLayer =
            didRayCastHit ? CollidableLayer == (CollidableLayer | (1 << hit.transform.gameObject.layer)) : false;

        if (didRayCastHit && isHitTargetInCollidableLayer)
        {
            BlockFaceBehaviour blockFace = hit.transform.gameObject.GetComponent <BlockFaceBehaviour>();
            BlockFace          face      = BlockFaceMethods.BlockFaceFromNormal(hit.normal);
            if (_faceMap.ContainsKey(blockFace) && face == _faceMap[blockFace])
            {
                _animator.MoveBlock(blockFace, face);
            }
        }
    }
Exemple #14
0
    private void HandleInteractForward()
    {
        Vector3 GridSpaceCoordinate = new Vector3(
            Mathf.Round(transform.position.x),
            Mathf.Round(transform.position.y),
            Mathf.Round(transform.position.z));
        Ray        ray = new Ray(GridSpaceCoordinate, transform.forward);
        RaycastHit hit;
        bool       didRayCastHit = Physics.Raycast(ray, out hit, 0.6f, CollidableLayer);

        if (didRayCastHit)
        {
            BlockBehaviour block = hit.transform.gameObject.GetComponent <BlockBehaviour>();
            if (block == null)
            {
                return;
            }
            BlockFace face = BlockFaceMethods.BlockFaceFromNormal(hit.normal);

            _animator.AttemptToInteractWith(block, face);
        }
    }
Exemple #15
0
        private static void RemoveLight(ELinkedList <Block> removedBlocksList, Block block, Vector3i source)
        {
            var light = block.BlockLight;

            if (!source.Equals(light.SunlightSource))
            {
                return;
            }

            var oldLight = Equals(block.Position, source) ? Block.MaxBlockLight : light.Sunlight;

            light.Sunlight       = light.LinearSunlight;
            light.SunlightSource = block.Position;
            removedBlocksList.Add(block);

            for (var i = 0; i < Faces; i++)
            {
                var face      = (BlockFace)i;
                var opposite  = BlockFaceMethods.GetOpposite(face);
                var neighbour = block.GetNeighbour((BlockFace)i);
                if (neighbour == null)
                {
                    continue;
                }

                if (!block.CanLightPassThrough(face) || !neighbour.CanLightBePassedFrom(opposite, block))
                {
                    continue;
                }
                if (oldLight <= neighbour.BlockLight.Sunlight)
                {
                    continue;
                }
                RemoveLight(removedBlocksList, neighbour, source);
            }
        }
Exemple #16
0
        public override void Run()
        {
            var block = _world.GetBlock(_position);

            if (!(block is BlockWater water))
            {
                return;
            }
            if (water.Removing)
            {
                return;
            }
            var neighbours = block.Chunk.GetNeighbourBlocks(new Block[6], _position,
                                                            _position - (block.Chunk.Position << 4));

            var neighbourDown  = neighbours[(int)BlockFace.Down];
            var cantBeExpanded = (neighbourDown is BlockWater || neighbourDown is BlockTallGrass ||
                                  neighbourDown is BlockAir) && water.Parent != water.Position;

            foreach (var blockFace in BlockFaceMethods.All)
            {
                if (blockFace == BlockFace.Up)
                {
                    continue;
                }
                if ((water.WaterLevel == 0 || cantBeExpanded) && blockFace != BlockFace.Down)
                {
                    continue;
                }
                var target = neighbours[(int)blockFace];
                //Must delete this line later on.
                if (target == null)
                {
                    return;
                }
                if (target is BlockWater waterTarget)
                {
                    if ((waterTarget.WaterLevel >= water.WaterLevel ||
                         waterTarget.WaterLevel > water.WaterLevel && waterTarget.WaterLevel == 0) &&
                        (blockFace != BlockFace.Down || waterTarget.Parent == waterTarget.Position))
                    {
                        continue;
                    }

                    //Set parent
                    var parent = _world.GetBlock(waterTarget.Parent);
                    if (parent != null && parent is BlockWater waterParent)
                    {
                        waterParent.Children.Remove(waterTarget.Position);
                    }
                }

                if (!(target is BlockAir) && !(target is BlockTallGrass) && !(target is BlockWater))
                {
                    continue;
                }
                var pos = block.Position + BlockFaceMethods.GetRelative(blockFace);
                var set = _world.SetBlock(new BlockSnapshotWater(blockFace == BlockFace.Down
                    ? BlockWater.MaxWaterLevel
                    : water.WaterLevel - 1), pos);
                ((BlockWater)set).Parent = block.Position;
                water.Children.Add(set.Position);
            }
        }