Ejemplo 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);
        }
Ejemplo n.º 2
0
        public EntityPlayerSp GetPlayer(World world)
        {
            EntityPlayerSp player = new EntityPlayerSp(world, new Vector3(x, y, z));

            SharpCraft.Instance.Camera.Pitch = pitch;
            SharpCraft.Instance.Camera.Yaw   = yaw;

            foreach (var pair in hotbar)
            {
                if (TryParseStack(pair.Value, out var stack))
                {
                    player.Hotbar[pair.Key] = stack;
                }
            }

            foreach (var pair in inventory)
            {
                if (TryParseStack(pair.Value, out var stack))
                {
                    player.Inventory[pair.Key] = stack;
                }
            }

            player.Health = health;

            return(player);
        }
Ejemplo n.º 3
0
        public WorldPlayerNode(EntityPlayerSp player)
        {
            pitch = SharpCraft.Instance.Camera.Pitch;
            yaw   = SharpCraft.Instance.Camera.Yaw;

            x = player.Pos.X;
            y = player.Pos.Y;
            z = player.Pos.Z;

            for (var i = 0; i < player.Hotbar.Length; i++)
            {
                ItemStack stack = player.Hotbar[i];

                if (TryParseStack(stack, out var node))
                {
                    hotbar.Add(i, node);
                }
            }
            for (var i = 0; i < player.Inventory.Length; i++)
            {
                ItemStack stack = player.Inventory[i];

                if (TryParseStack(stack, out var node))
                {
                    inventory.Add(i, node);
                }
            }

            health = player.Health;
        }
Ejemplo n.º 4
0
        public void OnRightClicked(World world, Vector3 hitVec, ItemStack with, EntityPlayerSp clicked)
        {
            if (clicked.IsSneaking && _product != null)
            {
                ClearGrid();
                Save();

                var pos = new Vector3(_pos.X + 0.5f, hitVec.Y, _pos.Z + 0.5f);

                world.AddEntity(new EntityItem(world, pos, Vector3.UnitY * 0.25f, _product));
                _product = null;

                return;
            }

            if (IsEmpty() && clicked.IsSneaking)
            {
                return;
            }

            var gap  = 1 / 16f;
            var slot = 1 / 4f;

            var localPos = new Vector2(hitVec.X, hitVec.Z) - new Vector2(_pos.X, _pos.Z);

            localPos.X %= 1.0f;
            localPos.Y %= 1.0f;

            var indexX = (int)(localPos.X / (slot + 4 * gap / 3));
            var indexY = (int)(localPos.Y / (slot + 4 * gap / 3));

            if (_placeDelay[indexX, indexY] > 0)
            {
                return;
            }

            _placeDelay[indexX, indexY] = 6;

            var item = _grid[indexX, indexY];

            var stackEmpty = with == null || with.IsEmpty;

            if (item != null)
            {
                SetGridElement(indexX, indexY, null);
                world?.AddEntity(new EntityItem(world, hitVec + Vector3.UnitY * 0.25f, Vector3.UnitY * 0.1f, item, true));

                //TODO - maybe make the items swap?
            }
            else
            {
                if (!stackEmpty)
                {
                    SetGridElement(indexX, indexY, with.Copy(1));
                    with.Count--;
                }
            }
        }
Ejemplo n.º 5
0
 private void ResetDestroyProgress(EntityPlayerSp player)
 {
     foreach (DestroyProgress progress in DestroyProgresses.Values)
     {
         if (progress.Player == player)
         {
             DestroyProgresses.TryRemove(progress.Pos, out DestroyProgress removed);
         }
     }
 }
Ejemplo n.º 6
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.º 7
0
        public static World LoadWorld(string saveName)
        {
            string dir = $"{SharpCraft.Instance.GameFolderDir}/saves/{saveName}";

            if (!Directory.Exists(dir))
            {
                return(null);
            }

            WorldDataNode   wdn = null;
            WorldPlayerNode wpn = null;

            try
            {
                string json = File.ReadAllText(dir + "/player.dat");
                wpn = JsonConvert.DeserializeObject <WorldPlayerNode>(json);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }

            try
            {
                string json = File.ReadAllText(dir + "/level.dat");
                wdn = JsonConvert.DeserializeObject <WorldDataNode>(json);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }

            var world = wdn?.GetWorld(saveName);

            EntityPlayerSp player = wpn?.GetPlayer(world);

            if (player != null)
            {
                world?.AddEntity(player);

                world?.LoadChunk(new BlockPos(player.Pos).ChunkPos());
                SharpCraft.Instance.Player = player;
            }

            return(world);
        }
Ejemplo n.º 8
0
        public void Disconnect()
        {
            ParticleRenderer = null;
            SkyboxRenderer   = null;

            if (World is WorldClient wc)
            {
                WorldLoader.SaveWorld(wc);
            }

            var wmp = (WorldClientServer)World;

            wmp?.DestroyChunkModels();

            Player = null;

            wmp?.ChunkData.Cleanup();
            wmp?.LoadManager.Cleanup();
            World = null;
        }
Ejemplo 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);
        }
Ejemplo n.º 10
0
 public virtual void OnPlaced(World world, BlockPos pos, EntityPlayerSp placer)
 {
 }
Ejemplo n.º 11
0
 public virtual bool CanBlockBePlacedAtSide(World world, BlockPos blockPos, FaceSides sideHit, EntityPlayerSp placer)
 {
     return(true);
 }
Ejemplo n.º 12
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.º 13
0
 public virtual void OnDestroyed(World world, BlockPos pos, BlockState oldState, EntityPlayerSp destroyer)
 {
     world.RemoveTileEntity(pos);
 }
Ejemplo n.º 14
0
        public override bool CanBlockBePlacedAtSide(World world, BlockPos blockPos, FaceSides sideHit, EntityPlayerSp placer)
        {
            if (sideHit != FaceSides.Up ||
                !(world.GetTileEntity(blockPos) is TileEntityCraftingGrid tecg))
            {
                return(true);
            }

            return(tecg.IsEmpty() && placer.IsSneaking);
        }