Add item rendering for inventory

This commit is contained in:
Drew DeVault 2015-10-08 18:32:34 -04:00
parent 90f39b5696
commit 3550bcb1a1
6 changed files with 145 additions and 36 deletions

View File

@ -162,8 +162,7 @@ namespace TrueCraft.Client.Modules
continue;
var position = origin + new Point((int)Scale(i * 20), 0);
var rect = new Rectangle(position, scale);
IconRenderer.RenderBlockIcon(Game, Items, provider,
(byte)item.Metadata, rect, Color.White); // TODO: Fuck, metadata was supposed to be a short
IconRenderer.RenderBlockIcon(Game, provider, (byte)item.Metadata, rect);
}
}
@ -178,7 +177,7 @@ namespace TrueCraft.Client.Modules
var item = Game.Client.Inventory.Hotbar[i];
if (item.Empty || item.Count == 1)
continue;
int offset = 12;
int offset = 10;
if (item.Count >= 10)
offset -= 6;
var position = origin + new Point((int)Scale(i * 20 + offset), (int)Scale(5));

View File

@ -6,6 +6,10 @@ using TrueCraft.API.Logic;
using TrueCraft.Client.Input;
using Microsoft.Xna.Framework.Input;
using TrueCraft.Core.Networking.Packets;
using TrueCraft.API.Windows;
using System;
using TrueCraft.API;
using TrueCraft.Core.Windows;
namespace TrueCraft.Client.Modules
{
@ -16,7 +20,14 @@ namespace TrueCraft.Client.Modules
private Texture2D Inventory { get; set; }
private Texture2D Items { get; set; }
private FontRenderer Font { get; set; }
private Texture2D Background { get; set; }
private int SelectedSlot { get; set; }
private enum RenderStage
{
Sprites,
Models,
Text
}
public WindowModule(TrueCraftGame game, FontRenderer font)
{
@ -25,8 +36,7 @@ namespace TrueCraft.Client.Modules
SpriteBatch = new SpriteBatch(game.GraphicsDevice);
Inventory = game.TextureMapper.GetTexture("gui/inventory.png");
Items = game.TextureMapper.GetTexture("gui/items.png");
Background = new Texture2D(game.GraphicsDevice, 1, 1);
Background.SetData<Color>(new[] { new Color(Color.Black, 180) });
SelectedSlot = -1;
}
private static readonly Rectangle InventoryWindowRect = new Rectangle(0, 0, 176, 166);
@ -36,12 +46,30 @@ namespace TrueCraft.Client.Modules
if (Game.Client.CurrentWindow != null)
{
SpriteBatch.Begin(samplerState: SamplerState.PointClamp, blendState: BlendState.NonPremultiplied);
SpriteBatch.Draw(Background, new Rectangle(0, 0,
Game.GraphicsDevice.Viewport.Width, Game.GraphicsDevice.Viewport.Height), Color.White);
SpriteBatch.Draw(Game.White1x1, new Rectangle(0, 0,
Game.GraphicsDevice.Viewport.Width, Game.GraphicsDevice.Viewport.Height), new Color(Color.Black, 180));
switch (Game.Client.CurrentWindow.Type)
{
case -1:
DrawInventoryWindow();
SpriteBatch.Draw(Inventory, new Vector2(
Game.GraphicsDevice.Viewport.Width / 2 - Scale(InventoryWindowRect.Width / 2),
Game.GraphicsDevice.Viewport.Height / 2 - Scale(InventoryWindowRect.Height / 2)),
InventoryWindowRect, Color.White, 0, Vector2.Zero, Game.ScaleFactor * 2, SpriteEffects.None, 1);
DrawInventoryWindow(RenderStage.Sprites);
break;
}
SpriteBatch.End();
switch (Game.Client.CurrentWindow.Type)
{
case -1:
DrawInventoryWindow(RenderStage.Models);
break;
}
SpriteBatch.Begin(samplerState: SamplerState.PointClamp, blendState: BlendState.NonPremultiplied);
switch (Game.Client.CurrentWindow.Type)
{
case -1:
DrawInventoryWindow(RenderStage.Text);
break;
}
SpriteBatch.End();
@ -71,12 +99,75 @@ namespace TrueCraft.Client.Modules
return base.KeyDown(gameTime, e);
}
private void DrawInventoryWindow()
private void DrawInventoryWindow(RenderStage stage)
{
SpriteBatch.Draw(Inventory, new Vector2(
Game.GraphicsDevice.Viewport.Width / 2 - Scale(InventoryWindowRect.Width / 2),
Game.GraphicsDevice.Viewport.Height / 2 - Scale(InventoryWindowRect.Height / 2)),
InventoryWindowRect, Color.White, 0, Vector2.Zero, Game.ScaleFactor * 2, SpriteEffects.None, 1);
DrawWindowArea(Game.Client.Inventory.MainInventory, 8, 84, InventoryWindowRect, stage);
DrawWindowArea(Game.Client.Inventory.Hotbar, 8, 142, InventoryWindowRect, stage);
DrawWindowArea(Game.Client.Inventory.CraftingGrid, 88, 26, InventoryWindowRect, stage);
DrawWindowArea(Game.Client.Inventory.Armor, 8, 8, InventoryWindowRect, stage);
}
private void DrawWindowArea(IWindowArea area, int _x, int _y, Rectangle frame, RenderStage stage)
{
var mouse = Mouse.GetState().Position.ToVector2();
var scale = new Point((int)(16 * Game.ScaleFactor * 2));
var origin = new Point((int)(
Game.GraphicsDevice.Viewport.Width / 2 - Scale(frame.Width / 2) + Scale(_x)),
(int)(Game.GraphicsDevice.Viewport.Height / 2 - Scale(frame.Height / 2) + Scale(_y)));
for (int i = 0; i < area.Length; i++)
{
var item = area[i];
int x = (int)((i % area.Width) * Scale(18));
int y = (int)((i / area.Width) * Scale(18));
if (area is CraftingWindowArea)
{
// yes I know this is a crappy hack, bite me
if (i == 0)
{
if (area.Width == 2)
{
x = (int)Scale(144 - _x);
y = (int)Scale(36 - _y);
}
else
{
x = (int)Scale(119);
y = (int)Scale(30);
}
}
else
{
i--;
x = (int)((i % area.Width) * Scale(18));
y = (int)((i / area.Width) * Scale(18));
i++;
}
}
var position = origin + new Point(x, y);
var rect = new Rectangle(position, scale);
if (stage == RenderStage.Sprites && rect.Contains(mouse))
{
SelectedSlot = area.StartIndex + i;
SpriteBatch.Draw(Game.White1x1, rect, Color.LightGray);
}
if (item.Empty)
continue;
var provider = Game.ItemRepository.GetItemProvider(item.ID);
var texture = provider.GetIconTexture((byte)item.Metadata);
if (texture != null && stage == RenderStage.Sprites)
IconRenderer.RenderItemIcon(SpriteBatch, Items, provider,
(byte)item.Metadata, rect, Color.White);
if (texture == null && stage == RenderStage.Models && provider is IBlockProvider)
IconRenderer.RenderBlockIcon(Game, provider as IBlockProvider, (byte)item.Metadata, rect);
if (stage == RenderStage.Text && item.Count > 1)
{
int offset = 10;
if (item.Count >= 10)
offset -= 6;
position += new Point((int)Scale(offset), (int)Scale(5));
Font.DrawText(SpriteBatch, position.X, position.Y, item.Count.ToString(), Game.ScaleFactor);
}
}
}
public override void Update(GameTime gameTime)
@ -85,11 +176,6 @@ namespace TrueCraft.Client.Modules
base.Update(gameTime);
}
/// <summary>
/// Scales a float depending on the game's scale factor.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private float Scale(float value)
{
return value * Game.ScaleFactor * 2;

View File

@ -53,14 +53,34 @@ namespace TrueCraft.Client.Rendering
};
}
/// <summary>
///
/// </summary>
/// <param name="spriteBatch"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="text"></param>
/// <param name="scale"></param>
public Point MeasureText(string text, float scale = 1.0f)
{
var dx = 0;
var height = 0;
var font = Fonts[0];
for (int i = 0; i < text.Length; i++)
{
if (text[i] == '§')
{
i++;
var code = string.Format("§{0}", text[i]);
if (ChatFormat.IsValid(code))
font = GetFont(code);
}
else
{
var glyph = font.GetGlyph(text[i]);
if (glyph != null)
{
dx += (int)(glyph.XAdvance * scale);
if (glyph.Height > height)
height = glyph.Height;
}
}
}
return new Point(dx, height);
}
public void DrawText(SpriteBatch spriteBatch, int x, int y, string text, float scale = 1.0f, byte alpha = 255)
{
var dx = x;
@ -91,12 +111,12 @@ namespace TrueCraft.Client.Rendering
(int)(glyph.Width * scale),
(int)(glyph.Height * scale));
var shadowRectangle = new Rectangle(
dx + (int)(glyph.XOffset * scale) + 2,
dy + (int)(glyph.YOffset * scale) + 2,
dx + (int)(glyph.XOffset * scale) + 4,
dy + (int)(glyph.YOffset * scale) + 4,
(int)(glyph.Width * scale),
(int)(glyph.Height * scale));
spriteBatch.Draw(font.GetTexture(glyph.Page), shadowRectangle, sourceRectangle, new Color(63, 63, 21, alpha));
spriteBatch.Draw(font.GetTexture(glyph.Page), shadowRectangle, sourceRectangle, new Color(21, 21, 21, alpha));
spriteBatch.Draw(font.GetTexture(glyph.Page), destRectangle, sourceRectangle, new Color(color, alpha));
dx += (int)(glyph.XAdvance * scale);
}

View File

@ -34,9 +34,10 @@ namespace TrueCraft.Client.Rendering
RenderEffect.Texture = game.TextureMapper.GetTexture("terrain.png");
RenderEffect.TextureEnabled = true;
RenderEffect.VertexColorEnabled = true;
RenderEffect.EnableDefaultLighting();
// TODO: Figure out how to make the lighting give the cubes some sense of depth
RenderEffect.DirectionalLight0.Direction = new Vector3(-0.75f, -0.75f, -0.75f);
RenderEffect.LightingEnabled = true;
RenderEffect.DirectionalLight0.Direction = new Vector3(10, -10, -0.8f);
RenderEffect.DirectionalLight0.DiffuseColor = Color.White.ToVector3();
RenderEffect.DirectionalLight0.Enabled = true;
RenderEffect.Projection = Matrix.CreateOrthographicOffCenter(
0, game.GraphicsDevice.Viewport.Width,
0, game.GraphicsDevice.Viewport.Height,
@ -53,8 +54,7 @@ namespace TrueCraft.Client.Rendering
spriteBatch.Draw(texture, destination, source, color);
}
public static void RenderBlockIcon(TrueCraftGame game, Texture2D texture, IBlockProvider provider,
byte metadata, Rectangle destination, Color color)
public static void RenderBlockIcon(TrueCraftGame game, IBlockProvider provider, byte metadata, Rectangle destination)
{
var mesh = BlockMeshes[provider.ID];
if (mesh != null)

View File

@ -44,7 +44,7 @@
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<Commandlineparameters>localhost TestUser</Commandlineparameters>
<Commandlineparameters>localhost SirCmpwn</Commandlineparameters>
<DebugType>
</DebugType>
<DebugSymbols>true</DebugSymbols>

View File

@ -40,6 +40,7 @@ namespace TrueCraft.Client
public DateTime EndDigging { get; set; }
public Coordinates3D TargetBlock { get; set; }
public AudioManager Audio { get; set; }
public Texture2D White1x1 { get; set; }
private List<IGameplayModule> InputModules { get; set; }
private List<IGameplayModule> GraphicalModules { get; set; }
@ -119,6 +120,9 @@ namespace TrueCraft.Client
base.Initialize(); // (calls LoadContent)
White1x1 = new Texture2D(GraphicsDevice, 1, 1);
White1x1.SetData<Color>(new[] { Color.White });
Audio = new AudioManager();
Audio.LoadDefaultPacks(Content);