Add crafting bench support to client

This commit is contained in:
Drew DeVault 2015-10-09 08:33:04 -04:00
parent 26e8e9108b
commit 95f49dd277
10 changed files with 92 additions and 32 deletions

View File

@ -3,6 +3,9 @@ using TrueCraft.API.Networking;
using TrueCraft.Core.Networking.Packets;
using TrueCraft.API.Windows;
using TrueCraft.API;
using TrueCraft.Core.Windows;
using TrueCraft.Core.Logic;
using TrueCraft.API.Logic;
namespace TrueCraft.Client.Handlers
{
@ -14,9 +17,7 @@ namespace TrueCraft.Client.Handlers
if (packet.WindowID == 0)
client.Inventory.SetSlots(packet.Items);
else
{
// TODO
}
client.CurrentWindow.SetSlots(packet.Items);
}
public static void HandleSetSlot(IPacket _packet, MultiplayerClient client)
@ -26,9 +27,7 @@ namespace TrueCraft.Client.Handlers
if (packet.WindowID == 0)
window = client.Inventory;
else
{
// TODO
}
window = client.CurrentWindow;
if (window != null)
{
if (packet.SlotIndex >= 0 && packet.SlotIndex < window.Length)
@ -37,5 +36,24 @@ namespace TrueCraft.Client.Handlers
}
}
}
public static void HandleOpenWindowPacket(IPacket _packet, MultiplayerClient client)
{
var packet = (OpenWindowPacket)_packet;
IWindow window = null;
switch (packet.Type)
{
case 1: // Crafting bench window
window = new CraftingBenchWindow(client.CraftingRepository, client.Inventory);
break;
}
window.ID = packet.WindowID;
client.CurrentWindow = window;
}
public static void HandleCloseWindowPacket(IPacket _packet, MultiplayerClient client)
{
client.CurrentWindow = null;
}
}
}

View File

@ -24,6 +24,8 @@ namespace TrueCraft.Client.Handlers
client.RegisterPacketHandler(new WindowItemsPacket().ID, InventoryHandlers.HandleWindowItems);
client.RegisterPacketHandler(new SetSlotPacket().ID, InventoryHandlers.HandleSetSlot);
client.RegisterPacketHandler(new CloseWindowPacket().ID, InventoryHandlers.HandleCloseWindowPacket);
client.RegisterPacketHandler(new OpenWindowPacket().ID, InventoryHandlers.HandleOpenWindowPacket);
}
public static void HandleChatMessage(IPacket _packet, MultiplayerClient client)

View File

@ -21,6 +21,7 @@ namespace TrueCraft.Client.Modules
private TrueCraftGame Game { get; set; }
private SpriteBatch SpriteBatch { get; set; }
private Texture2D Inventory { get; set; }
private Texture2D Crafting { get; set; }
private Texture2D Items { get; set; }
private FontRenderer Font { get; set; }
private short SelectedSlot { get; set; }
@ -39,12 +40,14 @@ namespace TrueCraft.Client.Modules
Font = font;
SpriteBatch = new SpriteBatch(game.GraphicsDevice);
Inventory = game.TextureMapper.GetTexture("gui/inventory.png");
Crafting = game.TextureMapper.GetTexture("gui/crafting.png");
Items = game.TextureMapper.GetTexture("gui/items.png");
SelectedSlot = -1;
HeldItem = ItemStack.EmptyStack;
}
private static readonly Rectangle InventoryWindowRect = new Rectangle(0, 0, 176, 166);
private static readonly Rectangle CraftingWindowRect = new Rectangle(0, 0, 176, 166);
public void Draw(GameTime gameTime)
{
@ -73,6 +76,13 @@ namespace TrueCraft.Client.Modules
InventoryWindowRect, Color.White, 0, Vector2.Zero, Game.ScaleFactor * 2, SpriteEffects.None, 1);
DrawInventoryWindow(RenderStage.Sprites);
break;
case 1: // Crafting bench
SpriteBatch.Draw(Crafting, new Vector2(
Game.GraphicsDevice.Viewport.Width / 2 - Scale(CraftingWindowRect.Width / 2),
Game.GraphicsDevice.Viewport.Height / 2 - Scale(CraftingWindowRect.Height / 2)),
CraftingWindowRect, Color.White, 0, Vector2.Zero, Game.ScaleFactor * 2, SpriteEffects.None, 1);
DrawCraftingWindow(RenderStage.Sprites);
break;
}
if (provider != null)
{
@ -88,6 +98,9 @@ namespace TrueCraft.Client.Modules
case -1:
DrawInventoryWindow(RenderStage.Models);
break;
case 1: // Crafting bench
DrawCraftingWindow(RenderStage.Models);
break;
}
if (provider != null)
{
@ -102,6 +115,9 @@ namespace TrueCraft.Client.Modules
case -1:
DrawInventoryWindow(RenderStage.Text);
break;
case 1: // Crafting bench
DrawCraftingWindow(RenderStage.Text);
break;
}
if (provider != null)
{
@ -151,8 +167,8 @@ namespace TrueCraft.Client.Modules
if (SelectedSlot > -1)
item = Game.Client.CurrentWindow[SelectedSlot];
var packet = new ClickWindowPacket(id, SelectedSlot, e.Button == MouseButton.Right,
0, Keyboard.GetState().IsKeyDown(Keys.LeftShift) || Keyboard.GetState().IsKeyDown(Keys.RightShift),
item.ID, item.Count, item.Metadata);
0, Keyboard.GetState().IsKeyDown(Keys.LeftShift) || Keyboard.GetState().IsKeyDown(Keys.RightShift),
item.ID, item.Count, item.Metadata);
if (packet.SlotIndex == -999)
{
// Special case (throwing item) TODO
@ -161,7 +177,7 @@ namespace TrueCraft.Client.Modules
{
var backup = Game.Client.CurrentWindow.GetSlots();
var staging = (ItemStack)HeldItem.Clone();
Window.HandleClickPacket(packet, Game.Client.CurrentWindow, ref staging); // just for updating staging
Window.HandleClickPacket(packet, Game.Client.CurrentWindow, ref staging);
HeldItem = staging;
Game.Client.CurrentWindow.SetSlots(backup);
}
@ -198,6 +214,14 @@ namespace TrueCraft.Client.Modules
DrawWindowArea(Game.Client.Inventory.Armor, 8, 8, InventoryWindowRect, stage);
}
private void DrawCraftingWindow(RenderStage stage)
{
var window = (CraftingBenchWindow)Game.Client.CurrentWindow;
DrawWindowArea(window.CraftingGrid, 29, 16, CraftingWindowRect, stage);
DrawWindowArea(window.MainInventory, 8, 84, CraftingWindowRect, stage);
DrawWindowArea(window.Hotbar, 8, 142, CraftingWindowRect, stage);
}
private void DrawWindowArea(IWindowArea area, int _x, int _y, Rectangle frame, RenderStage stage)
{
var mouse = Mouse.GetState().Position.ToVector2();
@ -222,8 +246,8 @@ namespace TrueCraft.Client.Modules
}
else
{
x = (int)Scale(119);
y = (int)Scale(30);
x = (int)Scale(124 - _x);
y = (int)Scale(35 - _y);
}
}
else
@ -239,7 +263,7 @@ namespace TrueCraft.Client.Modules
if (stage == RenderStage.Sprites && rect.Contains(mouse))
{
SelectedSlot = (short)(area.StartIndex + i);
SpriteBatch.Draw(Game.White1x1, rect, Color.LightGray);
SpriteBatch.Draw(Game.White1x1, rect, new Color(Color.White, 150));
}
if (item.Empty)
continue;

View File

@ -18,6 +18,7 @@ using TrueCraft.API.Physics;
using TrueCraft.Core.Physics;
using TrueCraft.Core.Windows;
using TrueCraft.API.Windows;
using TrueCraft.API.Logic;
namespace TrueCraft.Client
{
@ -42,6 +43,7 @@ namespace TrueCraft.Client
public InventoryWindow Inventory { get; set; }
public int Health { get; set; }
public IWindow CurrentWindow { get; set; }
public ICraftingRepository CraftingRepository { get; set; }
public bool Connected
{
@ -92,6 +94,9 @@ namespace TrueCraft.Client
connected = 0;
cancel = new CancellationTokenSource();
Health = 20;
var crafting = new CraftingRepository();
CraftingRepository = crafting;
crafting.DiscoverRecipes();
}
public void RegisterPacketHandler(byte packetId, PacketHandler handler)

View File

@ -5,7 +5,7 @@ using TrueCraft.API;
using TrueCraft.API.Windows;
using TrueCraft.API.Logic;
namespace TrueCraft
namespace TrueCraft.Core.Logic
{
public class CraftingRepository : ICraftingRepository
{
@ -16,7 +16,7 @@ namespace TrueCraft
Recipes.Add(recipe);
}
internal void DiscoverRecipes()
public void DiscoverRecipes()
{
var recipeTypes = new List<Type>();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())

View File

@ -354,6 +354,7 @@
<Compile Include="Entities\WolfEntity.cs" />
<Compile Include="Windows\FurnaceWindow.cs" />
<Compile Include="Paths.cs" />
<Compile Include="Logic\CraftingRepository.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

View File

@ -13,27 +13,34 @@ namespace TrueCraft.Core.Windows
public CraftingBenchWindow(ICraftingRepository craftingRepository, InventoryWindow inventory)
{
WindowAreas = new[]
{
new CraftingWindowArea(craftingRepository, CraftingOutputIndex, 3, 3),
new WindowArea(MainIndex, 27, 9, 3), // Main inventory
new WindowArea(HotbarIndex, 9, 9, 1) // Hotbar
};
inventory.MainInventory.CopyTo(MainInventory);
inventory.Hotbar.CopyTo(Hotbar);
{
new CraftingWindowArea(craftingRepository, CraftingOutputIndex, 3, 3),
new WindowArea(MainIndex, 27, 9, 3), // Main inventory
new WindowArea(HotbarIndex, 9, 9, 1) // Hotbar
};
if (inventory != null)
{
inventory.MainInventory.CopyTo(MainInventory);
inventory.Hotbar.CopyTo(Hotbar);
}
foreach (var area in WindowAreas)
area.WindowChange += (s, e) => OnWindowChange(new WindowChangeEventArgs(
(s as WindowArea).StartIndex + e.SlotIndex, e.Value));
(s as WindowArea).StartIndex + e.SlotIndex, e.Value));
Copying = false;
inventory.WindowChange += (sender, e) =>
if (inventory != null)
{
if (Copying) return;
if ((e.SlotIndex >= InventoryWindow.MainIndex && e.SlotIndex < InventoryWindow.MainIndex + inventory.MainInventory.Length)
|| (e.SlotIndex >= InventoryWindow.HotbarIndex && e.SlotIndex < InventoryWindow.HotbarIndex + inventory.Hotbar.Length))
inventory.WindowChange += (sender, e) =>
{
inventory.MainInventory.CopyTo(MainInventory);
inventory.Hotbar.CopyTo(Hotbar);
}
};
if (Copying)
return;
if ((e.SlotIndex >= InventoryWindow.MainIndex && e.SlotIndex < InventoryWindow.MainIndex + inventory.MainInventory.Length)
|| (e.SlotIndex >= InventoryWindow.HotbarIndex && e.SlotIndex < InventoryWindow.HotbarIndex + inventory.Hotbar.Length))
{
inventory.MainInventory.CopyTo(MainInventory);
inventory.Hotbar.CopyTo(Hotbar);
}
};
}
}
#region Variables

View File

@ -8,6 +8,7 @@ namespace TrueCraft.Core.Windows
{
public static readonly int CraftingOutput = 0;
public ICraftingRepository Repository { get; set; }
public bool Process { get; set; }
public CraftingWindowArea(ICraftingRepository repository, int startIndex, int width = 2, int height = 2)
: base(startIndex, width * height + 1, width, height)

View File

@ -106,7 +106,10 @@ namespace TrueCraft.Core.Windows
public virtual void SetSlots(ItemStack[] slots)
{
foreach (var windowArea in WindowAreas)
Array.Copy(slots, windowArea.StartIndex, windowArea.Items, 0, windowArea.Length);
{
if (windowArea.StartIndex < slots.Length && windowArea.StartIndex + windowArea.Length <= slots.Length)
Array.Copy(slots, windowArea.StartIndex, windowArea.Items, 0, windowArea.Length);
}
}
public virtual ItemStack this[int index]

View File

@ -64,7 +64,6 @@
<Compile Include="Handlers\InteractionHandlers.cs" />
<Compile Include="Commands\DebugCommands.cs" />
<Compile Include="Exceptions\PlayerDisconnectException.cs" />
<Compile Include="CraftingRepository.cs" />
<Compile Include="ServerConfiguration.cs" />
<Compile Include="MobManager.cs" />
<Compile Include="Rules\OverworldSpawnRules.cs" />