Ejemplo n.º 1
0
        public override bool OnActivated(MouseOverObject moo, EntityPlayerSp clicked)
        {
            if (moo.sideHit != FaceSides.Up ||
                !(clicked.World.GetTileEntity(moo.blockPos) is TileEntityCraftingGrid tecg))
            {
                return(false);
            }

            //var wasEmpty = tecg.IsEmpty();

            tecg.OnRightClicked(clicked.World, moo.hitVec, clicked.GetEquippedItemStack(), clicked);

            //if (wasEmpty)
            //return true;

            return(false);
        }
Ejemplo n.º 2
0
        public void SpawnDiggingParticle(MouseOverObject moo)
        {
            if (moo.hit == HitType.Block)
            {
                var state = SharpCraft.Instance.World.GetBlockState(moo.blockPos);

                if (state.Model == null)
                {
                    return;
                }

                float f0 = moo.hitVec.X
                           + MathUtil.NextFloat(-0.21f, 0.21f) * Math.Abs(moo.boundingBox.Max.X - moo.boundingBox.Min.X);
                float f1 = moo.hitVec.Y
                           + MathUtil.NextFloat(0, 0.1f) * Math.Abs(moo.boundingBox.Max.Y - moo.boundingBox.Min.Y);
                float f2 = moo.hitVec.Z
                           + MathUtil.NextFloat(-0.21f, 0.21f) * Math.Abs(moo.boundingBox.Max.Z - moo.boundingBox.Min.Z);

                if (moo.sideHit == FaceSides.Down)
                {
                    f1 = moo.boundingBox.Min.Y - 0.05f;
                }
                else if (moo.sideHit == FaceSides.East)
                {
                    f0 = moo.boundingBox.Max.X + 0.05f;
                }
                else if (moo.sideHit == FaceSides.North)
                {
                    f2 = moo.boundingBox.Min.Z - 0.05f;
                }
                else if (moo.sideHit == FaceSides.South)
                {
                    f2 = moo.boundingBox.Max.Z + 0.05f;
                }
                else if (moo.sideHit == FaceSides.Up)
                {
                    f1 = moo.boundingBox.Max.Y + 0.1f;
                }
                else if (moo.sideHit == FaceSides.West)
                {
                    f0 = moo.boundingBox.Min.X - 0.05f;
                }

                Vector3 pos = new Vector3(f0, f1, f2) + moo.normal * 0.1f;

                Vector3 motion = moo.normal * MathUtil.NextFloat(0.0075f, 0.03f);
                float   mult   = 0.75f / (Vector3.Distance(pos, moo.hitVec) + 0.01f);
                motion.Xz *= mult;

                motion.Y += 0.02f;

                bool ok = SharpCraft.Instance.DestroyProgresses.TryGetValue(moo.blockPos, out DestroyProgress progress);

                AddParticle(new ParticleDigging(
                                SharpCraft.Instance.World,
                                pos,
                                motion,
                                0.35f + (ok ? progress.PartialProgress * 0.5f : 0),
                                state,
                                moo.sideHit));
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// If the returned value is true, the held block is not going to be placed.
 /// </summary>
 /// <param name="moo"></param>
 /// <param name="clicked"></param>
 /// <returns></returns>
 public virtual bool OnActivated(MouseOverObject moo, EntityPlayerSp clicked)
 {
     return(false);
 }
Ejemplo n.º 4
0
        public void GetMouseOverObject()
        {
            if (World == null)
            {
                return;
            }

            float radius = 5.5f;

            MouseOverObject final = new MouseOverObject();

            float dist = float.MaxValue;

            Vector3 camPos = Vector3.One * 0.5f + Camera.Pos;

            var air = BlockRegistry.GetBlock <BlockAir>();

            for (float z = -radius; z <= radius; z++)
            {
                for (float y = -radius; y <= radius; y++)
                {
                    for (float x = -radius; x <= radius; x++)
                    {
                        Vector3 vec = camPos;
                        vec.X += x;
                        vec.Y += y;
                        vec.Z += z;

                        float f = (vec - Camera.Pos).LengthFast;

                        if (f <= radius + 0.5f)
                        {
                            BlockPos   pos   = new BlockPos(vec);
                            BlockState state = World.GetBlockState(pos);

                            if (state.Block != air)
                            {
                                AxisAlignedBb bb = state.Block.BoundingBox.Offset(pos.ToVec());

                                bool hitSomething = RayHelper.RayIntersectsBB(Camera.Pos,
                                                                              Camera.GetLookVec(), bb, out Vector3 hitPos, out Vector3 normal);

                                if (hitSomething)
                                {
                                    FaceSides sideHit = FaceSides.Null;

                                    if (normal.X < 0)
                                    {
                                        sideHit = FaceSides.West;
                                    }
                                    else if (normal.X > 0)
                                    {
                                        sideHit = FaceSides.East;
                                    }
                                    if (normal.Y < 0)
                                    {
                                        sideHit = FaceSides.Down;
                                    }
                                    else if (normal.Y > 0)
                                    {
                                        sideHit = FaceSides.Up;
                                    }
                                    if (normal.Z < 0)
                                    {
                                        sideHit = FaceSides.North;
                                    }
                                    else if (normal.Z > 0)
                                    {
                                        sideHit = FaceSides.South;
                                    }

                                    BlockPos p = new BlockPos(hitPos - normal * bb.Size / 2);

                                    if (sideHit == FaceSides.Null)
                                    {
                                        continue;
                                    }

                                    float l = Math.Abs((Camera.Pos - (p.ToVec() + bb.Size / 2)).Length);

                                    if (l < dist)
                                    {
                                        dist = l;

                                        final.hit      = HitType.Block;
                                        final.hitVec   = hitPos;
                                        final.blockPos = p;
                                        final.normal   = normal;
                                        final.sideHit  = sideHit;

                                        final.boundingBox = bb;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            MouseOverObject = final;
        }
Ejemplo n.º 5
0
        private void GameLoop()
        {
            if (GuiScreen == null && !Focused)
            {
                OpenGuiScreen(new GuiScreenIngameMenu());
            }

            if (Player != null)
            {
                if (GameTicks % 5 == 0)
                {
                    SoundEngine.PlaySound($"block/grass/walk{MathUtil.NextInt(1, 6)}");
                }

                if (AllowIngameInput())
                {
                    if (World?.GetChunk(new BlockPos(Player.Pos).ChunkPos()) == null)
                    {
                        Player.Motion = Vector3.Zero;
                    }

                    bool lmb = _mouseButtonsDown.Contains(MouseButton.Left);
                    bool rmb = _mouseButtonsDown.Contains(MouseButton.Right);

                    if (lmb || rmb)
                    {
                        _interactionTickCounter++;

                        BlockPos lastPos = _lastMouseOverObject.blockPos;

                        if (lmb && _lastMouseOverObject.hit == HitType.Block)
                        {
                            if (Player.GameMode == GameMode.Creative)
                            {
                                Player.BreakBlock();
                            }
                            else
                            {
                                ParticleRenderer.SpawnDiggingParticle(_lastMouseOverObject);

                                if (_lastMouseOverObject.hit == MouseOverObject.hit &&
                                    lastPos == MouseOverObject.blockPos)
                                {
                                    if (!DestroyProgresses.TryGetValue(lastPos, out DestroyProgress progress))
                                    {
                                        if (World?.GetBlockState(lastPos).Block.Hardness != -1)
                                        {
                                            DestroyProgresses.TryAdd(lastPos,
                                                                     progress = new DestroyProgress(lastPos, Player));
                                            progress.Progress = 0;
                                        }
                                    }
                                    else
                                    {
                                        progress.Progress +=
                                            Player.GetEquippedItemStack() is ItemStack st && !st.IsEmpty
                                                ? st.Item.GetMiningSpeed(World?.GetBlockState(lastPos).Block.Material)
                                                : 1;
                                    }

                                    if (progress.Destroyed)
                                    {
                                        DestroyProgresses.TryRemove(progress.Pos, out DestroyProgress removed);
                                    }
                                }
                                else
                                {
                                    ResetDestroyProgress(Player);
                                }
                            }
                        }

                        _lastMouseOverObject = MouseOverObject;

                        if (_interactionTickCounter % 4 == 0)
                        {
                            GetMouseOverObject();

                            if (rmb)
                            {
                                Player.OnClick(MouseButton.Right);
                            }
                        }
                    }
                    else
                    {
                        _interactionTickCounter = 0;
                        ResetDestroyProgress(Player);
                    }
                }
                else
                {
                    ResetDestroyProgress(Player);
                }

                World?.Update(Player.Pos, WorldRenderer.RenderDistance);
            }

            WorldRenderer?.Update();
            SkyboxRenderer?.Update();
            ParticleRenderer?.TickParticles();

            GameTicks++;
        }
Ejemplo n.º 6
0
        private void GameLoop()
        {
            TargetRenderFrequency = Focused ? 0 : 30;

            if (GuiScreen == null && !Focused)
            {
                OpenGuiScreen(new GuiScreenIngameMenu());
            }

            float wheelValue = Mouse.WheelPrecise;

            if (Player != null) // && GuiScreen == null)
            {
                if (AllowInput())
                {
                    if (wheelValue < _mouseWheelLast)
                    {
                        Player.SelectNextItem();
                    }
                    else if (wheelValue > _mouseWheelLast)
                    {
                        Player.SelectPreviousItem();
                    }

                    if (World?.GetChunk(new BlockPos(Player.Pos).ChunkPos()) == null)
                    {
                        Player.Motion = Vector3.Zero;
                    }

                    bool lmb = _mouseButtonsDown.Contains(MouseButton.Left);
                    bool rmb = _mouseButtonsDown.Contains(MouseButton.Right);

                    if (lmb || rmb)
                    {
                        _interactionTickCounter++;

                        BlockPos lastPos = _lastMouseOverObject.blockPos;

                        if (lmb && _lastMouseOverObject.hit == HitType.Block)
                        {
                            ParticleRenderer.SpawnDiggingParticle(_lastMouseOverObject);

                            if (_lastMouseOverObject.hit == MouseOverObject.hit && lastPos == MouseOverObject.blockPos)
                            {
                                if (!DestroyProgresses.TryGetValue(lastPos, out DestroyProgress progress))
                                {
                                    if (World?.GetBlockState(lastPos).Block.Hardness != -1)
                                    {
                                        DestroyProgresses.TryAdd(lastPos,
                                                                 progress = new DestroyProgress(lastPos, Player));
                                        progress.Progress = 0;
                                    }
                                }
                                else
                                {
                                    progress.Progress++;
                                }

                                if (progress.Destroyed)
                                {
                                    DestroyProgresses.TryRemove(progress.Pos, out DestroyProgress removed);
                                }
                            }
                            else
                            {
                                ResetDestroyProgress(Player);
                            }
                        }

                        _lastMouseOverObject = MouseOverObject;

                        if (_interactionTickCounter % 4 == 0)
                        {
                            GetMouseOverObject();

                            if (rmb)
                            {
                                Player.PlaceBlock();
                            }
                        }
                    }
                    else
                    {
                        _interactionTickCounter = 0;
                        ResetDestroyProgress(Player);
                    }
                }
                else
                {
                    ResetDestroyProgress(Player);
                }
            }

            _mouseWheelLast = wheelValue;

            World?.Update(Player, WorldRenderer.RenderDistance);

            WorldRenderer?.Update();
            SkyboxRenderer?.Update();
            ParticleRenderer?.TickParticles();
        }