Exemplo n.º 1
0
        public bool ConnectToServer(string ip, int port)
        {
            var b = ServerHandler.Connect(ip, port);

            if (b)
            {
                ParticleRenderer = new ParticleRenderer();
                SkyboxRenderer   = new SkyboxRenderer();

                BlockPos playerPos = new BlockPos(0, 100, 0);//MathUtil.NextFloat(-100, 100));

                World = new WorldClientServer();

                Player = new EntityPlayerSp(World, playerPos.ToVec());

                World.AddEntity(Player);

                Player.SetItemStackInInventory(0, new ItemStack(ItemRegistry.GetItem(BlockRegistry.GetBlock <BlockCraftingTable>())));
                ResetMouse();

                MouseState state = Mouse.GetState();
                _mouseLast = new Point(state.X, state.Y);
            }

            return(b);
        }
Exemplo n.º 2
0
        public void SpawnDestroyParticles(BlockPos pos, BlockState state)
        {
            Vector3 posVec = pos.ToVec();

            int   perAxis = 4;
            float step    = 1f / perAxis;

            Vector3 halfVec = Vector3.One * 0.5f;

            for (float x = 0f; x < perAxis; x++)
            {
                for (float y = 0f; y < perAxis; y++)
                {
                    for (float z = 0f; z < perAxis; z++)
                    {
                        Vector3 localPos = new Vector3(x, y, z) * step;
                        Vector3 worldPos = localPos + halfVec * step;

                        Vector3 vec    = localPos - halfVec;
                        Vector3 motion = vec.Normalized() * 0.2f;

                        motion.X += MathUtil.NextFloat(-0.05f, 0.05f);
                        motion.Y += MathUtil.NextFloat(-0.05f, 0.05f);
                        motion.Z += MathUtil.NextFloat(-0.05f, 0.05f);

                        ParticleDigging particle = new ParticleDigging(SharpCraft.Instance.World, posVec + worldPos, motion, MathUtil.NextFloat(1, 1.5f), state);

                        AddParticle(particle);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public List <AxisAlignedBb> GetBlockCollisionBoxes(AxisAlignedBb box)
        {
            List <AxisAlignedBb> blocks = new List <AxisAlignedBb>();

            AxisAlignedBb bb = box.Union(box);

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

            for (int x = (int)bb.Min.X, maxX = (int)bb.Max.X; x < maxX; x++)
            {
                for (int y = (int)bb.Min.Y, maxY = (int)bb.Max.Y; y < maxY; y++)
                {
                    for (int z = (int)bb.Min.Z, maxZ = (int)bb.Max.Z; z < maxZ; z++)
                    {
                        BlockPos   pos   = new BlockPos(x, y, z);
                        BlockState state = SharpCraft.Instance.World.GetBlockState(pos);
                        if (state.Block == air || state.Block.Material.CanWalkThrough)
                        {
                            continue;
                        }

                        blocks.Add(state.Block.BoundingBox.Offset(pos.ToVec()));
                    }
                }
            }

            return(blocks);
        }
        public void PlaceBlock()
        {
            MouseOverObject moo = SharpCraft.Instance.MouseOverObject;

            if (moo.hit != HitType.Block)
            {
                return;
            }

            ItemStack stack = GetEquippedItemStack();

            if (!(stack?.Item is ItemBlock itemBlock))
            {
                return;
            }

            Block air   = BlockRegistry.GetBlock <BlockAir>();
            Block glass = BlockRegistry.GetBlock <BlockGlass>();
            Block grass = BlockRegistry.GetBlock <BlockGrass>();
            Block dirt  = BlockRegistry.GetBlock <BlockDirt>();


            BlockState clickedState = World.GetBlockState(moo.blockPos);

            bool replacing;

            BlockPos pos = (replacing = clickedState.Block.IsReplaceable && itemBlock.Block != clickedState.Block) ? moo.blockPos : moo.blockPos.Offset(moo.sideHit);

            Block         heldBlock = itemBlock.Block;
            AxisAlignedBB blockBb   = heldBlock.BoundingBox.offset(pos.ToVec());

            if (!replacing && World.GetBlockState(pos).Block != air || World.GetIntersectingEntitiesBBs(blockBb).Count > 0 && heldBlock.IsSolid)
            {
                return;
            }

            BlockPos posUnder = pos.Offset(FaceSides.Down);

            BlockState stateUnder = World.GetBlockState(posUnder);
            BlockState stateAbove = World.GetBlockState(pos.Offset(FaceSides.Up));

            if (stateUnder.Block == grass && heldBlock != glass && heldBlock.IsSolid)
            {
                World.SetBlockState(posUnder, dirt.GetState());
            }
            if (stateAbove.Block != air && stateAbove.Block != glass && heldBlock == grass && stateAbove.Block.IsSolid)
            {
                World.SetBlockState(pos, dirt.GetState());
            }
            else
            {
                World.SetBlockState(pos, heldBlock.GetState(stack.Meta));
            }

            stack.Count--;

            SharpCraft.Instance.GetMouseOverObject();
        }
Exemplo n.º 5
0
        public virtual bool LoadChunk(ChunkPos chunkPos)
        {
            byte[] data = ChunkData.GetChunkData(chunkPos);
            if (data == null)
            {
                return(false);
            }

            short[,,] blockData = new short[Chunk.ChunkSize, Chunk.ChunkHeight, Chunk.ChunkSize];
            Buffer.BlockCopy(data, 0, blockData, 0, data.Length);

            Chunk chunk = null;

            lock (Chunks)
            {
                chunk = PutChunk(chunkPos, blockData);
            }

            if (chunk != null)
            {
                var air = BlockRegistry.GetBlock <BlockAir>();

                Enumerable.Range(0, Chunk.ChunkHeight).AsParallel().ForAll(y =>
                {
                    for (int x = 0; x < Chunk.ChunkSize; x++)
                    {
                        for (int z = 0; z < Chunk.ChunkSize; z++)
                        {
                            var localPos = new BlockPos(x, y, z);

                            var state = chunk.GetBlockState(localPos);

                            if (state.Block == air)
                            {
                                continue;
                            }

                            var worldPos = new BlockPos(chunkPos.ToVec() + localPos.ToVec());

                            if (state.Block.CreateTileEntity(this, worldPos) is TileEntity te)
                            {
                                chunk.AddTileEntity(localPos, te);
                            }
                        }
                    }
                });
            }

            return(true);
        }
Exemplo n.º 6
0
        public void LoadWorld(string saveName)
        {
            if (World != null)
            {
                return;
            }

            ParticleRenderer = new ParticleRenderer();
            SkyboxRenderer   = new SkyboxRenderer();

            World loadedWorld = WorldLoader.LoadWorld(saveName);

            if (loadedWorld == null)
            {
                Console.WriteLine("DEBUG: generating World");

                BlockPos playerPos = new BlockPos(MathUtil.NextFloat(-100, 100), 10, MathUtil.NextFloat(-100, 100));

                World = new World("MyWorld", "Tomlow's Fuckaround",
                                  SettingsManager.GetValue("worldseed").GetHashCode());

                Player = new EntityPlayerSP(World, playerPos.ToVec());

                World.AddEntity(Player);

                Player.SetItemStackInInventory(0, new ItemStack(new ItemBlock(BlockRegistry.GetBlock <BlockCraftingTable>())));
                Player.SetItemStackInInventory(1, new ItemStack(new ItemBlock(BlockRegistry.GetBlock <BlockFurnace>())));
                Player.SetItemStackInInventory(2, new ItemStack(new ItemBlock(BlockRegistry.GetBlock <BlockCobbleStone>())));
                Player.SetItemStackInInventory(3, new ItemStack(new ItemBlock(BlockRegistry.GetBlock <BlockPlanks>())));
                Player.SetItemStackInInventory(4, new ItemStack(new ItemBlock(BlockRegistry.GetBlock <BlockGlass>())));
                Player.SetItemStackInInventory(5, new ItemStack(new ItemBlock(BlockRegistry.GetBlock <BlockCraftingTable>())));
                Player.SetItemStackInInventory(6, new ItemStack(new ItemBlock(BlockRegistry.GetBlock <BlockSlab>())));
                Player.SetItemStackInInventory(7, new ItemStack(new ItemBlock(BlockRegistry.GetBlock <BlockLadder>())));
                Player.SetItemStackInInventory(8, new ItemStack(new ItemBlock(BlockRegistry.GetBlock <BlockTallGrass>())));
            }
            else
            {
                World = loadedWorld;
            }

            ResetMouse();

            MouseState state = OpenTK.Input.Mouse.GetState();

            _mouseLast = new Point(state.X, state.Y);
        }
Exemplo n.º 7
0
        public virtual void Update()
        {
            LastPos = Pos;

            Motion.Y -= 0.04f * gravity;

            Vector3 motion = Motion;

            motion.Y = 0;

            if (onGround && Motion.Xz.Length > 0.0001f)
            {
                AxisAlignedBB bbO = boundingBox.Union(boundingBox.offset(motion));

                var list = SharpCraft.Instance.World.GetBlockCollisionBoxes(bbO).OrderBy(box => (box.min - new BlockPos(box.min).ToVec() + box.size).Y);

                foreach (var bb in list)
                {
                    var blockPos = new BlockPos(bb.min);
                    var bbTop    = bb.min + bb.size;
                    var b        = bbTop - blockPos.ToVec();

                    var step = bbTop.Y - Pos.Y;

                    if (step <= StepHeight && step > 0)
                    {
                        Motion.Y = 0;
                        Pos.Y    = blockPos.Y + b.Y;

                        TeleportTo(Pos);
                    }
                }
            }

            Move();

            Motion.Xz *= 0.8664021f;

            if (onGround)
            {
                Motion.Xz *= 0.6676801f;
            }
        }
Exemplo n.º 8
0
        public void RemoveTileEntity(BlockPos localPos)
        {
            if (localPos.Y < 0 || localPos.Y >= ChunkHeight)
            {
                return;
            }

            if (!_tileEntities.TryRemove(localPos, out var te))
            {
                return;
            }

            var worldPos = new BlockPos(Pos.ToVec() + localPos.ToVec());

            var file = $"{World.SaveRoot}/{World.Dimension}/te/te_{Pos.WorldSpaceX() + localPos.X}.{localPos.Y}.{Pos.WorldSpaceZ() + localPos.Z}.te";

            if (File.Exists(file))
            {
                File.Delete(file);
            }

            te.OnDestroyed(World, worldPos);
        }
Exemplo n.º 9
0
        public void LoadWorld(string saveName)
        {
            if (World != null)
            {
                return;
            }

            ParticleRenderer = new ParticleRenderer();
            SkyboxRenderer   = new SkyboxRenderer();

            World loadedWorld = WorldLoader.LoadWorld(saveName);

            if (loadedWorld == null)
            {
                Console.WriteLine("DEBUG: generating World");

                BlockPos playerPos = new BlockPos(0, 10, 0);//MathUtil.NextFloat(-100, 100));

                World = new WorldClient("MyWorld", "Tomlow's Fuckaround", SettingsManager.GetString("worldseed"));

                Player = new EntityPlayerSp(World, playerPos.ToVec());

                World.AddEntity(Player);

                Player.SetItemStackInInventory(0, new ItemStack(ItemRegistry.GetItem(BlockRegistry.GetBlock <BlockCraftingTable>())));
            }
            else
            {
                World = loadedWorld;
            }

            ResetMouse();

            MouseState state = Mouse.GetState();

            _mouseLast = new Point(state.X, state.Y);
        }
Exemplo n.º 10
0
        public void AddTileEntity(BlockPos localPos, TileEntity te)
        {
            if (localPos.Y < 0 || localPos.Y >= ChunkHeight)
            {
                return;
            }

            if (_tileEntities.TryAdd(localPos, te))
            {
                var worldPos = new BlockPos(localPos.ToVec() + Pos.ToVec());

                var file = $"{World.SaveRoot}/{World.Dimension}/te/te_{worldPos.X}.{worldPos.Y}.{worldPos.Z}.te";

                if (!File.Exists(file))
                {
                    return;
                }

                using (ByteBufferReader bbr = new ByteBufferReader(File.ReadAllBytes(file)))
                {
                    te.ReadData(bbr);
                }
            }
        }
Exemplo n.º 11
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;
        }