[Client] Add player health to HUD
This commit is contained in:
parent
7fa1c9699a
commit
6942786b32
@ -66,6 +66,11 @@ namespace TrueCraft.Client
|
||||
{
|
||||
"footstep.glass",
|
||||
"default_glass_footstep.ogg"
|
||||
},
|
||||
new[]
|
||||
{
|
||||
"hurt",
|
||||
"default_hurt.wav"
|
||||
}
|
||||
};
|
||||
foreach (var pack in packs)
|
||||
|
@ -1 +1,2 @@
|
||||
The footstep and mining/placement sound effects were sourced from Minetest under CC-BY-SA: https://github.com/minetest/minetest_game
|
||||
The footstep and mining/placement sound effects were sourced from Minetest under CC-BY-SA: https://github.com/minetest/minetest_game
|
||||
default_hurt.wav is the same sound effect Minecraft uses, which is CC-BY-SA from http://freesound.org/people/thecheeseman/sounds/44429/
|
BIN
TrueCraft.Client/Content/Audio/default_hurt.wav
Normal file
BIN
TrueCraft.Client/Content/Audio/default_hurt.wav
Normal file
Binary file not shown.
@ -16,6 +16,7 @@ namespace TrueCraft.Client.Handlers
|
||||
client.RegisterPacketHandler(new ChatMessagePacket().ID, HandleChatMessage);
|
||||
client.RegisterPacketHandler(new SetPlayerPositionPacket().ID, HandlePositionAndLook);
|
||||
client.RegisterPacketHandler(new LoginResponsePacket().ID, HandleLoginResponse);
|
||||
client.RegisterPacketHandler(new UpdateHealthPacket().ID, HandleUpdateHealth);
|
||||
|
||||
client.RegisterPacketHandler(new ChunkPreamblePacket().ID, ChunkHandlers.HandleChunkPreamble);
|
||||
client.RegisterPacketHandler(new ChunkDataPacket().ID, ChunkHandlers.HandleChunkData);
|
||||
@ -58,5 +59,11 @@ namespace TrueCraft.Client.Handlers
|
||||
client.LoggedIn = true;
|
||||
// TODO: Pitch and yaw
|
||||
}
|
||||
|
||||
public static void HandleUpdateHealth(IPacket _packet, MultiplayerClient client)
|
||||
{
|
||||
var packet = (UpdateHealthPacket)_packet;
|
||||
client.Health = packet.Health;
|
||||
}
|
||||
}
|
||||
}
|
@ -43,6 +43,7 @@ namespace TrueCraft.Client.Modules
|
||||
|
||||
DrawHotbar(gameTime);
|
||||
DrawHotbarItemSprites(gameTime);
|
||||
DrawLife(gameTime);
|
||||
|
||||
SpriteBatch.End();
|
||||
|
||||
@ -68,6 +69,18 @@ namespace TrueCraft.Client.Modules
|
||||
private static readonly Rectangle HotbarSelectionRect =
|
||||
new Rectangle(0, 22, 24, 24);
|
||||
|
||||
private static readonly Rectangle FullHeartRect =
|
||||
new Rectangle(52, 0, 9, 9);
|
||||
|
||||
private static readonly Rectangle HalfHeartRect =
|
||||
new Rectangle(61, 0, 9, 9);
|
||||
|
||||
private static readonly Rectangle EmptyHeartRect =
|
||||
new Rectangle(16, 0, 9, 9);
|
||||
|
||||
private static readonly Rectangle EmptyHeartHighlightedRect =
|
||||
new Rectangle(25, 0, 9, 9);
|
||||
|
||||
/// <summary>
|
||||
/// Draws the inventory hotbar.
|
||||
/// </summary>
|
||||
@ -89,6 +102,29 @@ namespace TrueCraft.Client.Modules
|
||||
HotbarSelectionRect, Color.White, 0, Vector2.Zero, Game.ScaleFactor * 2, SpriteEffects.None, 1);
|
||||
}
|
||||
|
||||
private void DrawLife(GameTime gameTime)
|
||||
{
|
||||
int x = (int)(Game.GraphicsDevice.Viewport.Width / 2 - Scale(HotbarBackgroundRect.Width / 2));
|
||||
int y = (int)(Game.GraphicsDevice.Viewport.Height - Scale(HotbarBackgroundRect.Height + 5));
|
||||
y -= (int)(Scale(EmptyHeartRect.Height) * 1.25);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
SpriteBatch.Draw(Icons, new Vector2(x + i * Scale(EmptyHeartRect.Width), y), EmptyHeartRect, Color.White,
|
||||
0, Vector2.Zero, Game.ScaleFactor * 2, SpriteEffects.None, 1);
|
||||
if (Game.Client.Health >= i * 2)
|
||||
{
|
||||
SpriteBatch.Draw(Icons, new Vector2(x + i * Scale(FullHeartRect.Width), y), FullHeartRect, Color.White,
|
||||
0, Vector2.Zero, Game.ScaleFactor * 2, SpriteEffects.None, 1);
|
||||
}
|
||||
else if (Game.Client.Health >= i * 2 - 1)
|
||||
{
|
||||
SpriteBatch.Draw(Icons, new Vector2(x + i * Scale(HalfHeartRect.Width), y), HalfHeartRect, Color.White,
|
||||
0, Vector2.Zero, Game.ScaleFactor * 2, SpriteEffects.None, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawHotbarItemSprites(GameTime gameTime)
|
||||
{
|
||||
var scale = new Point((int)(16 * Game.ScaleFactor * 2));
|
||||
|
@ -39,6 +39,7 @@ namespace TrueCraft.Client
|
||||
public bool LoggedIn { get; internal set; }
|
||||
public int EntityID { get; internal set; }
|
||||
public InventoryWindow Inventory { get; set; }
|
||||
public int Health { get; set; }
|
||||
|
||||
public bool Connected
|
||||
{
|
||||
@ -88,6 +89,7 @@ namespace TrueCraft.Client
|
||||
SocketPool = new SocketAsyncEventArgsPool(100, 200, 65536);
|
||||
connected = 0;
|
||||
cancel = new CancellationTokenSource();
|
||||
Health = 15;
|
||||
}
|
||||
|
||||
public void RegisterPacketHandler(byte packetId, PacketHandler handler)
|
||||
|
@ -364,6 +364,9 @@
|
||||
<Content Include="Content\Audio\default_wood_footstep.2.ogg">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="Content\Audio\default_hurt.wav">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Modules\" />
|
||||
|
@ -41,7 +41,8 @@ namespace TrueCraft.Client
|
||||
public Coordinates3D TargetBlock { get; set; }
|
||||
public AudioManager Audio { get; set; }
|
||||
|
||||
private List<IGameplayModule> Modules { get; set; }
|
||||
private List<IGameplayModule> InputModules { get; set; }
|
||||
private List<IGameplayModule> GraphicalModules { get; set; }
|
||||
private SpriteBatch SpriteBatch { get; set; }
|
||||
private KeyboardHandler KeyboardComponent { get; set; }
|
||||
private MouseHandler MouseComponent { get; set; }
|
||||
@ -113,7 +114,8 @@ namespace TrueCraft.Client
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
Modules = new List<IGameplayModule>();
|
||||
InputModules = new List<IGameplayModule>();
|
||||
GraphicalModules = new List<IGameplayModule>();
|
||||
|
||||
base.Initialize(); // (calls LoadContent)
|
||||
|
||||
@ -123,13 +125,17 @@ namespace TrueCraft.Client
|
||||
ChunkModule = new ChunkModule(this);
|
||||
DebugInfoModule = new DebugInfoModule(this, Pixel);
|
||||
ChatModule = new ChatModule(this, Pixel);
|
||||
var hud = new HUDModule(this, Pixel);
|
||||
|
||||
Modules.Add(ChunkModule);
|
||||
Modules.Add(new HighlightModule(this));
|
||||
Modules.Add(ChatModule);
|
||||
Modules.Add(new PlayerControlModule(this));
|
||||
Modules.Add(new HUDModule(this, Pixel));
|
||||
Modules.Add(DebugInfoModule);
|
||||
GraphicalModules.Add(ChunkModule);
|
||||
GraphicalModules.Add(new HighlightModule(this));
|
||||
GraphicalModules.Add(hud);
|
||||
GraphicalModules.Add(ChatModule);
|
||||
GraphicalModules.Add(DebugInfoModule);
|
||||
|
||||
InputModules.Add(ChatModule);
|
||||
InputModules.Add(new HUDModule(this, Pixel));
|
||||
InputModules.Add(new PlayerControlModule(this));
|
||||
|
||||
Client.PropertyChanged += HandleClientPropertyChanged;
|
||||
Client.Connect(EndPoint);
|
||||
@ -207,7 +213,7 @@ namespace TrueCraft.Client
|
||||
|
||||
private void OnKeyboardKeyDown(object sender, KeyboardKeyEventArgs e)
|
||||
{
|
||||
foreach (var module in Modules)
|
||||
foreach (var module in InputModules)
|
||||
{
|
||||
var input = module as IInputModule;
|
||||
if (input != null)
|
||||
@ -220,7 +226,7 @@ namespace TrueCraft.Client
|
||||
|
||||
private void OnKeyboardKeyUp(object sender, KeyboardKeyEventArgs e)
|
||||
{
|
||||
foreach (var module in Modules)
|
||||
foreach (var module in InputModules)
|
||||
{
|
||||
var input = module as IInputModule;
|
||||
if (input != null)
|
||||
@ -233,7 +239,7 @@ namespace TrueCraft.Client
|
||||
|
||||
private void OnGamePadButtonUp(object sender, GamePadButtonEventArgs e)
|
||||
{
|
||||
foreach (var module in Modules)
|
||||
foreach (var module in InputModules)
|
||||
{
|
||||
var input = module as IInputModule;
|
||||
if (input != null)
|
||||
@ -246,7 +252,7 @@ namespace TrueCraft.Client
|
||||
|
||||
private void OnGamePadButtonDown(object sender, GamePadButtonEventArgs e)
|
||||
{
|
||||
foreach (var module in Modules)
|
||||
foreach (var module in InputModules)
|
||||
{
|
||||
var input = module as IInputModule;
|
||||
if (input != null)
|
||||
@ -259,7 +265,7 @@ namespace TrueCraft.Client
|
||||
|
||||
private void OnMouseComponentScroll(object sender, MouseScrollEventArgs e)
|
||||
{
|
||||
foreach (var module in Modules)
|
||||
foreach (var module in InputModules)
|
||||
{
|
||||
var input = module as IInputModule;
|
||||
if (input != null)
|
||||
@ -272,7 +278,7 @@ namespace TrueCraft.Client
|
||||
|
||||
private void OnMouseComponentButtonDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
foreach (var module in Modules)
|
||||
foreach (var module in InputModules)
|
||||
{
|
||||
var input = module as IInputModule;
|
||||
if (input != null)
|
||||
@ -285,7 +291,7 @@ namespace TrueCraft.Client
|
||||
|
||||
private void OnMouseComponentButtonUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
foreach (var module in Modules)
|
||||
foreach (var module in InputModules)
|
||||
{
|
||||
var input = module as IInputModule;
|
||||
if (input != null)
|
||||
@ -298,7 +304,7 @@ namespace TrueCraft.Client
|
||||
|
||||
private void OnMouseComponentMove(object sender, MouseMoveEventArgs e)
|
||||
{
|
||||
foreach (var module in Modules)
|
||||
foreach (var module in InputModules)
|
||||
{
|
||||
var input = module as IInputModule;
|
||||
if (input != null)
|
||||
@ -316,6 +322,7 @@ namespace TrueCraft.Client
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
||||
using (var stream = File.OpenWrite(path))
|
||||
new PngWriter().Write(RenderTarget, stream);
|
||||
ChatModule.AddMessage("Screenshot saved to " + Path.GetFileName(path));
|
||||
}
|
||||
|
||||
public void FlushMainThreadActions()
|
||||
@ -350,7 +357,9 @@ namespace TrueCraft.Client
|
||||
NextPhysicsUpdate = DateTime.UtcNow.AddMilliseconds(50);
|
||||
}
|
||||
|
||||
foreach (var module in Modules)
|
||||
foreach (var module in InputModules)
|
||||
module.Update(gameTime);
|
||||
foreach (var module in GraphicalModules)
|
||||
module.Update(gameTime);
|
||||
|
||||
UpdateCamera();
|
||||
@ -383,7 +392,7 @@ namespace TrueCraft.Client
|
||||
GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
|
||||
|
||||
Mesh.ResetStats();
|
||||
foreach (var module in Modules)
|
||||
foreach (var module in GraphicalModules)
|
||||
{
|
||||
var drawable = module as IGraphicalModule;
|
||||
if (drawable != null)
|
||||
|
Loading…
x
Reference in New Issue
Block a user