private void RegisterItemsAndBlocks()
        {
            blockRegistry.Put(new BlockAir());
            blockRegistry.Put(new BlockStone());
            blockRegistry.Put(new BlockGrass());
            blockRegistry.Put(new BlockDirt());
            blockRegistry.Put(new BlockCobbleStone());
            blockRegistry.Put(new BlockPlanks());
            blockRegistry.Put(new BlockBedrock());
            blockRegistry.Put(new BlockLog());
            blockRegistry.Put(new BlockLeaves());
            blockRegistry.Put(new BlockGlass());
            blockRegistry.Put(new BlockCraftingTable());
            blockRegistry.Put(new BlockFurnace());
            blockRegistry.Put(new BlockSlab());
            blockRegistry.Put(new BlockRare());
            blockRegistry.Put(new BlockLadder());
            blockRegistry.Put(new BlockTallGrass());

            //POST - MOD Blocks and Items
            foreach (ModMain mod in installedMods)
            {
                mod.OnItemsAndBlocksRegistry(new RegistryEventArgs(blockRegistry, itemRegistry));
            }

            foreach (var block in BlockRegistry.AllBlocks())
            {
                itemRegistry.Put(new ItemBlock(block));
            }

            JsonModelLoader loader = new JsonModelLoader(Block.DefaultShader);

            blockRegistry.RegisterBlocksPost(loader);
            itemRegistry.RegisterItemsPost(loader);
        }
Exemple #2
0
        private void RendererView_OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            RendererView_OnUnloaded(sender, null);

            _model = (RendererViewModel)DataContext;
            if (_model == null)
            {
                return;
            }
            _taskName = _model.TaskName;

            _window = Window.GetWindow(this);

            _window.KeyDown   += HandleKeyPress;
            _window.MouseDown += WindowOnMouseDown;
            _window.MouseUp   += WindowOnMouseUp;
            _window.MouseMove += WindowOnMouseMove;

            CompositionTarget.Rendering += CompositionTargetOnRendering;
            if (_taskName == "Task6")
            {
                _meshes            = new[] { JsonModelLoader.LoadMesh("Mesh/cylinder.babylon") };
                _meshes[0].Texture = new Texture("Mesh/yoba.png");
            }
            else
            {
                _meshes             = new Mesh[] { new Plane(10, 10, 20, 20, 20, (x, y) => (float)(Math.Cos(x) * Math.Cos(y))) };
                _meshes[0].Texture  = new Texture("Mesh/wireframe.png");
                _meshes[0].Rotation = new Vector3((float)Math.PI / 2, 0, 0);
                _model.Wireframe    = true;
            }
        }
        public void BreakBlock()
        {
            MouseOverObject moo = SharpCraft.Instance.MouseOverObject;

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

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

            if (JsonModelLoader.GetModelForBlock(state.Block.UnlocalizedName) != null)
            {
                SharpCraft.Instance.ParticleRenderer.SpawnDestroyParticles(moo.blockPos, state);
            }

            World.SetBlockState(moo.blockPos, BlockRegistry.GetBlock <BlockAir>().GetState());

            Vector3 motion = new Vector3(MathUtil.NextFloat(-0.15f, 0.15f), 0.3f, MathUtil.NextFloat(-0.15f, 0.15f));

            EntityItem entityDrop = new EntityItem(World, moo.blockPos.ToVec() + Vector3.One * 0.5f, motion, new ItemStack(new ItemBlock(state.Block), 1, state.Block.GetMetaFromState(state)));

            World.AddEntity(entityDrop);

            SharpCraft.Instance.GetMouseOverObject();
        }
Exemple #4
0
        public void RegisterBlocksPost(JsonModelLoader loader)
        {
            Block.SetDefaultShader(new Shader <ModelBlock>("block"));

            foreach (var pair in _registry)
            {
                pair.Value.RegisterState(loader,
                                         new BlockState(pair.Value, JsonModelLoader.GetModelForBlock(pair.Key)));
            }
        }
Exemple #5
0
        public void RegisterBlocksPost(JsonModelLoader loader)
        {
            loader.LoadBlockModels();

            foreach (var block in Registry.Values)
            {
                var count = JsonModelLoader.GetModelCount(block);

                for (var index = 0; index < count; index++)
                {
                    var state = new BlockState(block, (short)index);
                    block.RegisterState(loader, state);
                }
            }
        }
        protected virtual void RenderBlock(Block block, float x, float y, float scale)
        {
            var model = JsonModelLoader.GetModelForBlock(block.UnlocalizedName);

            if (model == null)
            {
                return;
            }

            ModelBlockRaw mbr = (ModelBlockRaw)model.RawModel;
            List <float>  uvs = new List <float>(8);

            mbr.AppendUvsForSide(FaceSides.South, uvs);

            Vector2 UVmin = new Vector2(uvs[0], uvs[1]);
            Vector2 UVmax = new Vector2(uvs[4], uvs[5]);

            Vector2 unit = new Vector2(1f / SharpCraft.Instance.ClientSize.Width, 1f / SharpCraft.Instance.ClientSize.Height);

            float width  = 16;
            float height = 16;

            float scaledWidth  = 16 * scale;
            float scaledHeight = 16 * scale;

            float posX = x + scaledWidth / 2;
            float posY = -y - scaledHeight / 2;

            Vector2 pos = new Vector2(posX, posY).Ceiling() * unit;

            Matrix4 mat = MatrixHelper.CreateTransformationMatrix(pos * 2 - Vector2.UnitX + Vector2.UnitY, scale * new Vector2(width, height) * unit);

            _item.Bind();

            Shader.Bind();
            Shader.UpdateGlobalUniforms();
            Shader.UpdateModelUniforms();
            Shader.UpdateInstanceUniforms(mat, UVmin, UVmax);

            GL.BindTexture(TextureTarget.Texture2D, JsonModelLoader.TEXTURE_BLOCKS);
            _item.RawModel.Render(PrimitiveType.Quads);

            _item.Unbind();
        }
        public ParticleDigging(World world, Vector3 pos, Vector3 motion, float particleScale, BlockState state, FaceSides side) : base(world, pos, motion, particleScale, JsonModelLoader.TEXTURE_BLOCKS)
        {
            State = state;

            ModelBlock model = JsonModelLoader.GetModelForBlock(state.Block.UnlocalizedName);

            if (model.RawModel is ModelBlockRaw mbr)
            {
                Vector2 start;
                Vector2 end;

                if (state.Block.IsFullCube)
                {
                    List <float> uvs = new List <float>(8);
                    mbr.AppendUvsForSide(side, uvs);

                    start = new Vector2(uvs[0], uvs[1]);
                    end   = new Vector2(uvs[4], uvs[5]); //4,5 because that's the 3. vertex and the local UV there is 1,1
                }
                else
                {
                    var tex = model.GetParticleTexture();

                    start = tex.UVMin;
                    end   = tex.UVMax;
                }

                Vector2 size = end - start;

                Vector2 pixel = size / 16;

                UVmin = start + pixel * new Vector2(MathUtil.NextFloat(0, 12), MathUtil.NextFloat(0, 12));
                UVmax = UVmin + pixel * 4;
            }

            if (side == FaceSides.Up)
            {
                Motion.Xz = SharpCraft.Instance.Camera.GetLookVec().Xz * 0.15f;
            }

            Vector3 vec = new Vector3(MathUtil.NextFloat(-1), MathUtil.NextFloat(-1), MathUtil.NextFloat(-1));

            _rotStep = vec.Normalized() * MathUtil.NextFloat(40, 75);
        }
Exemple #8
0
        protected virtual void RenderItem(Item item, float x, float y, float scale)
        {
            var model = JsonModelLoader.GetModelForItem(item.UnlocalizedName);

            if (model?.SlotTexture == null)
            {
                return;
            }

            Vector2 unit = new Vector2(1f / SharpCraft.Instance.ClientSize.Width, 1f / SharpCraft.Instance.ClientSize.Height);

            float width  = 16;
            float height = 16;

            float scaledWidth  = 16 * scale;
            float scaledHeight = 16 * scale;

            float posX = x + scaledWidth / 2;
            float posY = -y - scaledHeight / 2;

            Vector2 pos = new Vector2(posX, posY).Ceiling() * unit;

            Matrix4 mat = MatrixHelper.CreateTransformationMatrix(pos * 2 - Vector2.UnitX + Vector2.UnitY, scale * new Vector2(width, height) * unit);

            _item.Bind();

            Shader.Bind();
            Shader.SetMatrix4("transformationMatrix", mat);
            Shader.SetVector2("UVmin", model.SlotTexture.UVMin);
            Shader.SetVector2("UVmax", model.SlotTexture.UVMax);

            GL.BindTexture(TextureTarget.Texture2D, JsonModelLoader.TextureItems);
            _item.RawModel.Render();

            _item.Unbind();
        }
Exemple #9
0
        protected override void OnKeyDown(KeyboardKeyEventArgs e)
        {
            base.OnKeyDown(e);

            switch (e.Key)
            {
            case Key.Escape:
                if (GuiScreen is GuiScreenMainMenu || KeyboardState.IsKeyDown(Key.Escape))
                {
                    return;
                }

                if (GuiScreen != null)
                {
                    CloseGuiScreen();
                }
                else
                {
                    OpenGuiScreen(new GuiScreenIngameMenu());
                }
                break;

            case Key.E:
                if (KeyboardState.IsKeyDown(Key.E))
                {
                    return;
                }

                if (GuiScreen is GuiScreenInventory)
                {
                    CloseGuiScreen();
                    return;
                }

                if (GuiScreen == null)
                {
                    OpenGuiScreen(new GuiScreenInventory());
                }
                break;

            case Key.Up:

                if (KeyboardState.IsKeyDown(Key.Up))
                {
                    return;
                }

                break;

            case Key.Down:

                if (KeyboardState.IsKeyDown(Key.Down))
                {
                    return;
                }

                break;

            case Key.Enter:
                if (KeyboardState.IsKeyDown(Key.Enter))
                {
                    return;
                }

                if (GuiScreen is GuiChat gc)
                {
                    gc.SendMessage();
                    Instance.CloseGuiScreen();
                }
                else
                {
                    OpenGuiScreen(new GuiChat());
                }

                break;

            case Key.F2:
                _takeScreenshot = true;
                break;

            case Key.F11:
                if (WindowState != WindowState.Fullscreen)
                {
                    _lastWindowState = WindowState;
                    WindowState      = WindowState.Fullscreen;
                }
                else
                {
                    WindowState = _lastWindowState;
                }

                break;
            }

            if (AllowIngameInput())
            {
                switch (e.Key)
                {
                case Key.Q:
                    if (KeyboardState.IsKeyDown(Key.Q))
                    {
                        return;
                    }

                    if (e.Control)
                    {
                        Player.DropHeldStack();
                    }
                    else
                    {
                        Player?.DropHeldItem();
                    }
                    break;

                case Key.R:
                    if (!e.Control || KeyboardState.IsKeyDown(Key.R))
                    {
                        return;
                    }

                    Shader.ReloadAll();
                    SettingsManager.Load();
                    TextureManager.Reload();
                    SoundEngine.Reload();
                    LangUtil.Reload();

                    Camera.SetTargetFov(SettingsManager.GetFloat("fov"));

                    WorldRenderer.RenderDistance = SettingsManager.GetInt("renderdistance");
                    _sensitivity = SettingsManager.GetFloat("sensitivity");

                    if (e.Shift)
                    {
                        JsonModelLoader.Reload();
                        //TODO - World?.DestroyChunkModels();
                    }

                    break;
                }

                if (GuiScreen == null)
                {
                    for (int i = 0; i < 9; i++)
                    {
                        if (e.Key == Key.Number1 + i)
                        {
                            Player?.SetSelectedSlot(i);

                            break;
                        }
                    }
                }
            }

            if (e.Alt && e.Key == Key.F4)
            {
                Exit();
            }

            KeyboardState = e.Keyboard;
        }
 public void RegisterItemsPost(JsonModelLoader loader)
 {
     //Item.DefaultShader = new Shader<ModelItem>("item"); TODO
 }
Exemple #11
0
        public override void Render(float partialTicks)
        {
            Vector3 partialPos  = LastPos + (Pos - LastPos) * partialTicks;
            float   partialTime = _ticksLast + (_ticks - _ticksLast) * partialTicks;

            if (_stack == null || _stack.IsEmpty)
            {
                return;
            }

            float   offY = (float)((Math.Sin(partialTime / 90f * MathHelper.TwoPi) + 1) / 16);
            Vector3 vec  = Vector3.One * 0.5f;
            Vector3 pos  = partialPos - (Vector3.UnitX * 0.125f + Vector3.UnitZ * 0.125f) + Vector3.UnitY * offY;
            Matrix4 rot  = Matrix4.CreateRotationY(partialTime / 90f * MathHelper.TwoPi);
            Matrix4 t2   = Matrix4.CreateTranslation(-vec);

            if (_stack.Item is ItemBlock itemBlock)
            {
                ModelBlock model = itemBlock.Block.GetState(_stack.Meta).Model;

                if (model?.RawModel == null)
                {
                    return;
                }

                Shader.Bind();
                Shader.SetFloat("fogDistance", SharpCraft.Instance.WorldRenderer.RenderDistance);

                GL.BindVertexArray(model.RawModel.VaoID);

                GL.EnableVertexAttribArray(0);
                GL.EnableVertexAttribArray(1);
                GL.EnableVertexAttribArray(2);

                GL.BindTexture(TextureTarget.Texture2D, JsonModelLoader.TextureBlocks);

                int itemsToRender = 1;

                if (_stack.Count > 1)
                {
                    itemsToRender = 2;
                }
                if (_stack.Count >= 32 * 4)
                {
                    itemsToRender = 3;
                }
                if (_stack.Count == 64 * 4)
                {
                    itemsToRender = 4;
                }

                for (int i = 0; i < itemsToRender; i++)
                {
                    Vector3 posO = Vector3.One * (i / 8f);

                    Matrix4 s = Matrix4.CreateScale(0.25f);
                    Matrix4 t = Matrix4.CreateTranslation(pos + vec * 0.25f);

                    Matrix4 t3 = Matrix4.CreateTranslation(posO);

                    Matrix4 mat = t3 * t2 * (rot * s) * t;

                    Shader.SetMatrix4("transformationMatrix", mat);

                    model.RawModel.Render();
                }

                GL.BindVertexArray(0);

                GL.DisableVertexAttribArray(0);
                GL.DisableVertexAttribArray(1);
                GL.DisableVertexAttribArray(2);

                Shader.Unbind();
            }
            else
            {
                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
                GL.Disable(EnableCap.CullFace);
                ModelItem model = JsonModelLoader.GetModelForItem(_stack.Item);

                if (model?.RawModel == null)
                {
                    return;
                }

                Shader.Bind();

                GL.BindVertexArray(model.RawModel.VaoID);

                GL.EnableVertexAttribArray(0);
                GL.EnableVertexAttribArray(1);
                GL.EnableVertexAttribArray(2);

                GL.BindTexture(TextureTarget.Texture2D, JsonModelLoader.TextureItems);

                int itemsToRender = 1;

                if (_stack.Count > 1)
                {
                    itemsToRender = 2;
                }
                if (_stack.Count >= 32 * 4)
                {
                    itemsToRender = 3;
                }
                if (_stack.Count == 64 * 4)
                {
                    itemsToRender = 4;
                }

                for (int i = 0; i < itemsToRender; i++)
                {
                    Vector3 posO = Vector3.One * (i / 8f);

                    Matrix4 s  = Matrix4.CreateScale(0.35f);
                    Matrix4 t  = Matrix4.CreateTranslation(pos + vec * 0.35f);
                    Matrix4 t3 = Matrix4.CreateTranslation(posO);

                    Matrix4 mat = t3 * t2 * (rot * s) * t;

                    Shader.SetMatrix4("transformationMatrix", mat);
                    model.RawModel.Render();
                }

                GL.BindVertexArray(0);

                GL.DisableVertexAttribArray(0);
                GL.DisableVertexAttribArray(1);
                GL.DisableVertexAttribArray(2);

                Shader.Unbind();
                GL.Enable(EnableCap.CullFace);
                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
            }
        }
        public void SpawnDiggingParticle(MouseOverObject moo)
        {
            if (moo.hit == HitType.Block)
            {
                var state = SharpCraft.Instance.World.GetBlockState(moo.blockPos);

                if (JsonModelLoader.GetModelForBlock(state.Block.UnlocalizedName) == 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 / (MathUtil.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));
            }
        }
Exemple #13
0
 public void RegisterState(JsonModelLoader loader, BlockState state)
 {
     _states.Add(state);
 }
Exemple #14
0
        public override void Render(float partialTicks)
        {
            Vector3 partialPos  = LastPos + (Pos - LastPos) * partialTicks;
            float   partialTime = tick + partialTicks;

            if (stack?.Item is ItemBlock itemBlock)
            {
                ModelBlock model = JsonModelLoader.GetModelForBlock(itemBlock.Block.UnlocalizedName);

                if (model == null || model.RawModel == null)
                {
                    return;
                }

                float time = onGround ? (float)((Math.Sin(partialTime / 8) + 1) / 16) : 0;

                _shader.Bind();

                GL.BindVertexArray(model.RawModel.VaoID);

                GL.EnableVertexAttribArray(0);
                GL.EnableVertexAttribArray(1);
                GL.EnableVertexAttribArray(2);

                GL.BindTexture(TextureTarget.Texture2D, JsonModelLoader.TEXTURE_BLOCKS);

                int itemsToRender = 1;

                if (stack.Count > 1)
                {
                    itemsToRender = 2;
                }
                if (stack.Count >= 32 * 4)
                {
                    itemsToRender = 3;
                }
                if (stack.Count == 64 * 4)
                {
                    itemsToRender = 4;
                }

                for (int i = 0; i < itemsToRender; i++)
                {
                    Vector3 rot   = Vector3.UnitY * partialTime * 3;
                    Vector3 pos   = partialPos - (Vector3.UnitX * 0.125f + Vector3.UnitZ * 0.125f) + Vector3.UnitY * time;
                    Vector3 pos_o = Vector3.One * (i / 8f);

                    Matrix4 x = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(rot.X));
                    Matrix4 y = Matrix4.CreateRotationY(MathHelper.DegreesToRadians(rot.Y));
                    Matrix4 z = Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(rot.Z));

                    Vector3 vec = Vector3.One * 0.5f;

                    Matrix4 s  = Matrix4.CreateScale(0.25f);
                    Matrix4 t  = Matrix4.CreateTranslation(pos + vec * 0.25f);
                    Matrix4 t2 = Matrix4.CreateTranslation(-vec);
                    Matrix4 t3 = Matrix4.CreateTranslation(pos_o);

                    Matrix4 mat = t3 * t2 * (z * y * x * s) * t;

                    _shader.UpdateGlobalUniforms();
                    _shader.UpdateModelUniforms(model.RawModel);
                    _shader.UpdateInstanceUniforms(mat, this);
                    model.RawModel.Render(PrimitiveType.Quads);
                }

                GL.BindVertexArray(0);

                GL.DisableVertexAttribArray(0);
                GL.DisableVertexAttribArray(1);
                GL.DisableVertexAttribArray(2);

                _shader.Unbind();
            }
        }
Exemple #15
0
 public void RegisterItemsPost(JsonModelLoader loader)
 {
     loader.LoadItemModels();
 }
Exemple #16
0
        private void GameRegistry()
        {
            for (int i = 1; i < 6; i++)
            {
                //SoundEngine.RegisterSound($"block/grass/walk{i}");
            }

            //register materails
            Material.RegisterMaterial(new Material("air", true));
            Material.RegisterMaterial(new Material("tallgrass", true));
            Material.RegisterMaterial(new Material("grass", false));
            Material.RegisterMaterial(new Material("dirt", false));
            Material.RegisterMaterial(new Material("stone", false));
            Material.RegisterMaterial(new Material("wood", false));

            _blockRegistry.Put(new BlockAir());
            _blockRegistry.Put(new BlockStone());
            _blockRegistry.Put(new BlockGrass());
            _blockRegistry.Put(new BlockDirt());
            _blockRegistry.Put(new BlockCobbleStone());
            _blockRegistry.Put(new BlockPlanks());
            _blockRegistry.Put(new BlockBedrock());
            _blockRegistry.Put(new BlockLog());
            _blockRegistry.Put(new BlockLeaves());
            _blockRegistry.Put(new BlockGlass());
            _blockRegistry.Put(new BlockCraftingTable());
            _blockRegistry.Put(new BlockFurnace());
            //_blockRegistry.Put(new BlockSlab());
            _blockRegistry.Put(new BlockRare());
            _blockRegistry.Put(new BlockLadder());
            _blockRegistry.Put(new BlockTallGrass());
            _blockRegistry.Put(new BlockTulipRed());
            _blockRegistry.Put(new BlockTulipOrange());
            _blockRegistry.Put(new BlockTNT());

            //POST - MOD Blocks and Items
            foreach (ModMain mod in _installedMods)
            {
                mod.OnItemsAndBlocksRegistry(new RegistryEventArgs(_blockRegistry, _itemRegistry, _recipeRegistry));
            }

            foreach (var block in BlockRegistry.AllBlocks())
            {
                _itemRegistry.Put(new ItemBlock(block));
            }

            Item stick = new ItemStick();

            _itemRegistry.Put(new ItemPickaxe("wood"));
            _itemRegistry.Put(new ItemPickaxe("stone"));
            _itemRegistry.Put(new ItemPickaxe("rare"));
            _itemRegistry.Put(stick);

            var log    = ItemRegistry.GetItem(BlockRegistry.GetBlock <BlockLog>());
            var wood   = ItemRegistry.GetItem(BlockRegistry.GetBlock <BlockPlanks>());
            var cobble = ItemRegistry.GetItem(BlockRegistry.GetBlock <BlockCobbleStone>());
            var rare   = ItemRegistry.GetItem(BlockRegistry.GetBlock <BlockRare>());

            Item[] recipe =
            {
                cobble, cobble, cobble,
                null,   stick,  null,
                null,   stick,  null
            };
            _recipeRegistry.RegisterRecipe(recipe, ItemRegistry.GetItem("sharpcraft", "pick_stone"));

            recipe = new[]
            {
                rare, rare, rare,
                null, stick, null,
                null, stick, null
            };
            _recipeRegistry.RegisterRecipe(recipe, ItemRegistry.GetItem("sharpcraft", "pick_rare"));

            recipe = new[]
            {
                wood, wood, wood,
                null, stick, null,
                null, stick, null
            };
            _recipeRegistry.RegisterRecipe(recipe, ItemRegistry.GetItem("sharpcraft", "pick_wood"));

            recipe = new[]
            {
                cobble, cobble, cobble,
                cobble, null, cobble,
                cobble, cobble, cobble
            };
            _recipeRegistry.RegisterRecipe(recipe, ItemRegistry.GetItem(BlockRegistry.GetBlock <BlockFurnace>()));

            recipe = new[]
            {
                wood, wood, null,
                wood, wood, null,
                null, null, null
            };
            _recipeRegistry.RegisterRecipe(recipe, ItemRegistry.GetItem(BlockRegistry.GetBlock <BlockCraftingTable>()));

            recipe = new[]
            {
                log, null, null,
                null, null, null,
                null, null, null
            };
            _recipeRegistry.RegisterRecipe(recipe, new ItemStack(wood, 4), true);

            recipe = new[]
            {
                wood, null, null,
                wood, null, null,
                null, null, null
            };
            _recipeRegistry.RegisterRecipe(recipe, new ItemStack(stick, 4));

            recipe = new[]
            {
                wood, wood, wood,
                null, wood, null,
                wood, wood, wood
            };
            _recipeRegistry.RegisterRecipe(recipe, ItemRegistry.GetItem("sharpcraft", "ladder"));

            foreach (ModMain mod in _installedMods)
            {
                mod.OnRecipeRegistry(new RecipeRegistryEventArgs(_recipeRegistry));
            }

            Block.SetDefaultShader(new Shader("block", "fogDistance"));

            JsonModelLoader loader = new JsonModelLoader(Block.DefaultShader, Block.DefaultShader); //TODO - the second argument is the shader for items, which in this case can be the same as the block shader

            _blockRegistry.RegisterBlocksPost(loader);
            _itemRegistry.RegisterItemsPost(loader);

            LangUtil.LoadLang(SettingsManager.GetString("lang"));//TODO - find a proper placement for this line
        }
        public override void Render(float partialTicks)
        {
            float partialTime = _ticksLast + (_ticks - _ticksLast) * partialTicks;

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

            for (int y = 0; y < _grid.GetLength(1); y++)
            {
                for (int x = 0; x < _grid.GetLength(0); x++)
                {
                    var stack = _grid[x, y];

                    var offX = grid * x;
                    var offZ = grid * y;

                    var offsetX = (x + 1) * gap;
                    var offsetY = (y + 1) * gap;

                    if (stack != null && !stack.IsEmpty)
                    {
                        if (stack.Item is ItemBlock ib)
                        {
                            var model = ib.Block.GetState(stack.Meta).Model;//JsonModelLoader.GetModelForBlock(ib.Block));

                            var mat   = Matrix4.CreateTranslation(_pos.X + offX + offsetX, _pos.Y + 1, _pos.Z + offZ + offsetY);
                            var scale = Matrix4.CreateScale(grid);

                            GL.BindTexture(TextureTarget.Texture2D, JsonModelLoader.TextureBlocks);

                            model.Bind();
                            model.Shader.SetMatrix4("transformationMatrix", scale * mat);
                            model.RawModel.Render();
                            model.Unbind();
                        }
                        else
                        {
                            var model = JsonModelLoader.GetModelForItem(stack.Item);

                            var rot   = Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(90));
                            var mat   = rot * Matrix4.CreateTranslation(_pos.X + offX + offsetX + grid, _pos.Y + 1, _pos.Z + offZ + offsetY);
                            var scale = Matrix4.CreateScale(grid);

                            GL.BindTexture(TextureTarget.Texture2D, JsonModelLoader.TextureItems);

                            model.Bind();
                            model.Shader.SetMatrix4("transformationMatrix", scale * mat);
                            model.RawModel.Render();
                            model.Unbind();
                        }
                    }
                }
            }

            if (_product != null && !_product.IsEmpty)
            {
                float offY = (float)((Math.Sin(partialTime / 90f * MathHelper.TwoPi) + 1) / 16);

                var rot = Matrix4.CreateRotationY(partialTime / 90f * MathHelper.TwoPi);

                if (_product.Item is ItemBlock ib)
                {
                    var mat   = Matrix4.CreateTranslation(_pos.X + 0.5f, _pos.Y + 1.5f + offY, _pos.Z + 0.5f);
                    var scale = Matrix4.CreateTranslation(Vector3.One * -0.5f) * Matrix4.CreateScale(0.35f);
                    var model = ib.Block.GetState(_product.Meta).Model;

                    GL.BindTexture(TextureTarget.Texture2D, JsonModelLoader.TextureBlocks);

                    model.Bind();
                    model.Shader.SetMatrix4("transformationMatrix", scale * rot * mat);
                    model.RawModel.Render();
                    model.Unbind();
                }
                else
                {
                    var mat   = Matrix4.CreateTranslation(_pos.X + 0.5f, _pos.Y + 1.55f + offY, _pos.Z + 0.5f);
                    var scale = Matrix4.CreateTranslation(Vector3.One * -0.5f) * Matrix4.CreateScale(0.475f);
                    var model = JsonModelLoader.GetModelForItem(_product.Item);

                    GL.BindTexture(TextureTarget.Texture2D, JsonModelLoader.TextureItems);

                    model.Bind();
                    model.Shader.SetMatrix4("transformationMatrix", scale * rot * mat);
                    model.RawModel.Render();
                    model.Unbind();
                }
            }
        }
Exemple #18
0
        public void BuildChunkModelNow()
        {
            if (ModelBuilding || !QueuedForModelBuild)
            {
                return;
            }

            ModelBuilding = true;

            //ConcurrentDictionary<Shader<ModelBlock>, List<RawQuad>> modelRaw = new ConcurrentDictionary<Shader<ModelBlock>, List<RawQuad>>();

            //List<RawQuad> quads;

            Stopwatch sw = Stopwatch.StartNew(); //this is just a debug thing....

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

            var vertexes = new List <float>();
            var normals  = new List <float>();
            var uvs      = new List <float>();

            object locker = new object();

            //generate the model - fill MODEL_RAW
            Enumerable.Range(0, ChunkHeight).AsParallel().ForAll(y =>
            {
                for (int x = 0; x < ChunkSize; x++)
                {
                    for (int z = 0; z < ChunkSize; z++)
                    {
                        BlockPos worldPos = new BlockPos(x + Pos.WorldSpaceX(), y, z + Pos.WorldSpaceZ());

                        BlockState state = World.GetBlockState(worldPos);
                        if (state.Block == air)
                        {
                            continue;
                        }

                        BlockPos localPos = new BlockPos(x, y, z);

                        ModelBlock model  = JsonModelLoader.GetModelForBlock(state.Block.UnlocalizedName);
                        ModelBlockRaw mbr = (ModelBlockRaw)model?.RawModel;

                        if (mbr == null)
                        {
                            continue;
                        }

                        if (!state.Block.IsFullCube)
                        {
                            lock (locker)
                                mbr.AppendAllVertexData(vertexes, normals, uvs, localPos);

                            continue;
                        }

                        for (var index = 0; index < FaceSides.AllSides.Count; index++)
                        {
                            FaceSides dir = FaceSides.AllSides[index];

                            BlockPos worldPosO = worldPos.Offset(dir);
                            BlockState stateO  = World.GetBlockState(worldPosO);

                            if (!(stateO.Block == air ||
                                  stateO.Block.HasTransparency && !state.Block.HasTransparency) &&
                                stateO.Block.IsFullCube)
                            {
                                continue;
                            }

                            lock (locker)
                            {
                                mbr.AppendVertexDataForSide(dir, vertexes, normals, uvs, localPos);
                                //mbr.AppendNormalsForSide(dir, normals);
                                //mbr.AppendUvsForSide(dir, uvs);
                            }
                        }
                    }
                }
            });

            sw.Stop();
            Console.WriteLine($"DEBUG: built chunk model [{sw.Elapsed.TotalMilliseconds:F}ms]");

            float[] vtx = vertexes.ToArray(); //this is here because this takes time and I don't want it to slow down the main thread by running it in GlContext
            float[] nrm = normals.ToArray();
            float[] uv  = uvs.ToArray();

            SharpCraft.Instance.RunGlContext(() =>
            {
                if (_model == null)
                {
                    _model = new ModelChunk(vtx, nrm, uv, Block.DefaultShader);
                }
                else
                {
                    _model.OverrideData(vtx, nrm, uv);
                }

                ModelBuilding = false;
            });

            QueuedForModelBuild = false;
        }
        protected override void OnKeyDown(KeyboardKeyEventArgs e)
        {
            base.OnKeyDown(e);

            // If chat add to text
            if (guiChat != null && guiChat.visible == true)
            {
                guiChat.InputText(e.Key);
            }

            switch (e.Key)
            {
            case Key.P:
                Player?.World?.AddWaypoint(new BlockPos(Player.Pos).Offset(FaceSides.Up),
                                           new OpenTK.Color(255, 0, 0, 127), "TEST");
                break;

            case Key.Escape:
                if (GuiScreen is GuiScreenMainMenu || KeyboardState.IsKeyDown(Key.Escape))
                {
                    return;
                }

                if (GuiScreen != null)
                {
                    CloseGuiScreen();
                }
                else
                {
                    OpenGuiScreen(new GuiScreenIngameMenu());
                }
                break;

            case Key.E:
                if (KeyboardState.IsKeyDown(Key.E))
                {
                    return;
                }

                if (guiChat != null && guiChat.visible)
                {
                    return;
                }

                if (GuiScreen is GuiScreenInventory)
                {
                    CloseGuiScreen();
                    return;
                }

                if (GuiScreen == null)
                {
                    OpenGuiScreen(new GuiScreenInventory());
                }
                break;

            case Key.Up:

                if (KeyboardState.IsKeyDown(Key.Up))
                {
                    return;
                }

                if (guiChat != null)
                {
                    if (guiChat.visible == true)
                    {
                        guiChat.ShowHistoryUP();
                    }
                }

                break;

            case Key.Down:

                if (KeyboardState.IsKeyDown(Key.Down))
                {
                    return;
                }

                if (guiChat != null)
                {
                    if (guiChat.visible == true)
                    {
                        guiChat.ShowHistoryDOWN();
                    }
                }

                break;

            case Key.Enter:
                if (KeyboardState.IsKeyDown(Key.Enter))
                {
                    return;
                }

                if (guiChat == null)
                {
                    guiChat = new GuiChat();
                    return;
                }
                else
                {
                    guiChat.Init();
                    guiChat.ToggleVisibillity();
                }

                break;

            case Key.F2:
                _takeScreenshot = true;
                break;

            case Key.F11:
                if (WindowState != WindowState.Fullscreen)
                {
                    _lastWindowState = WindowState;
                    WindowState      = WindowState.Fullscreen;
                }
                else
                {
                    WindowState = _lastWindowState;
                }

                break;
            }

            if (AllowInput())
            {
                switch (e.Key)
                {
                case Key.Q:
                    if (KeyboardState.IsKeyDown(Key.Q))
                    {
                        return;
                    }

                    if (e.Control)
                    {
                        Player.DropHeldStack();
                    }
                    else
                    {
                        Player?.DropHeldItem();
                    }
                    break;

                case Key.R:
                    if (!e.Control || KeyboardState.IsKeyDown(Key.R))
                    {
                        return;
                    }

                    Shader.ReloadAll();
                    SettingsManager.Load();
                    TextureManager.Reload();

                    WorldRenderer.RenderDistance = SettingsManager.GetInt("renderdistance");
                    _sensitivity = SettingsManager.GetFloat("sensitivity");

                    if (e.Shift)
                    {
                        JsonModelLoader.Reload();
                        World?.DestroyChunkModels();
                    }

                    break;
                }

                if (GuiScreen == null)
                {
                    for (int i = 0; i < 9; i++)
                    {
                        if (e.Key == Key.Number1 + i)
                        {
                            Player?.SetSelectedSlot(i);

                            break;
                        }
                    }
                }
            }

            if (e.Alt && e.Key == Key.F4)
            {
                Exit();
            }

            KeyboardState = e.Keyboard;
        }