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.Core.Networking.Packets;
using TrueCraft.API.Windows; using TrueCraft.API.Windows;
using TrueCraft.API; using TrueCraft.API;
using TrueCraft.Core.Windows;
using TrueCraft.Core.Logic;
using TrueCraft.API.Logic;
namespace TrueCraft.Client.Handlers namespace TrueCraft.Client.Handlers
{ {
@ -14,9 +17,7 @@ namespace TrueCraft.Client.Handlers
if (packet.WindowID == 0) if (packet.WindowID == 0)
client.Inventory.SetSlots(packet.Items); client.Inventory.SetSlots(packet.Items);
else else
{ client.CurrentWindow.SetSlots(packet.Items);
// TODO
}
} }
public static void HandleSetSlot(IPacket _packet, MultiplayerClient client) public static void HandleSetSlot(IPacket _packet, MultiplayerClient client)
@ -26,9 +27,7 @@ namespace TrueCraft.Client.Handlers
if (packet.WindowID == 0) if (packet.WindowID == 0)
window = client.Inventory; window = client.Inventory;
else else
{ window = client.CurrentWindow;
// TODO
}
if (window != null) if (window != null)
{ {
if (packet.SlotIndex >= 0 && packet.SlotIndex < window.Length) 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 WindowItemsPacket().ID, InventoryHandlers.HandleWindowItems);
client.RegisterPacketHandler(new SetSlotPacket().ID, InventoryHandlers.HandleSetSlot); 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) public static void HandleChatMessage(IPacket _packet, MultiplayerClient client)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -106,7 +106,10 @@ namespace TrueCraft.Core.Windows
public virtual void SetSlots(ItemStack[] slots) public virtual void SetSlots(ItemStack[] slots)
{ {
foreach (var windowArea in WindowAreas) 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] public virtual ItemStack this[int index]

View File

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