Survival: add basic structure for a healthbar

This commit is contained in:
UnknownShadow200 2016-10-20 16:52:37 +11:00
parent ae5b73993d
commit 71799ba86c
9 changed files with 74 additions and 15 deletions

View File

@ -32,9 +32,7 @@ namespace ClassicalSharp.Gui {
/// <summary> Causes the gui element to recreate all of its sub-elements and/or textures. </summary> /// <summary> Causes the gui element to recreate all of its sub-elements and/or textures. </summary>
/// <remarks> Typically used when bitmap font changes. </remarks> /// <remarks> Typically used when bitmap font changes. </remarks>
public virtual void Recreate() { public void Recreate() { Dispose(); Init(); }
Dispose(); Init();
}
/// <summary> Called when the game window is resized. </summary> /// <summary> Called when the game window is resized. </summary>
public abstract void OnResize( int width, int height ); public abstract void OnResize( int width, int height );

View File

@ -113,7 +113,8 @@ namespace ClassicalSharp.Gui.Screens {
int index = 0; int index = 0;
Texture tex = posAtlas.tex; Texture tex = posAtlas.tex;
tex.X1 = 2; tex.Width = (short)posAtlas.offset; tex.X1 = 2; tex.Width = (short)posAtlas.offset;
IGraphicsApi.Make2DQuad( ref tex, FastColour.White, game.ModelCache.vertices, ref index ); IGraphicsApi.Make2DQuad( ref tex, FastColour.WhitePacked,
game.ModelCache.vertices, ref index );
Vector3I pos = Vector3I.Floor( game.LocalPlayer.Position ); Vector3I pos = Vector3I.Floor( game.LocalPlayer.Position );
posAtlas.curX = posAtlas.offset + 2; posAtlas.curX = posAtlas.offset + 2;

View File

@ -42,7 +42,7 @@ namespace ClassicalSharp.Gui.Screens {
VertexP3fT2fC4b[] vertices = game.ModelCache.vertices; VertexP3fT2fC4b[] vertices = game.ModelCache.vertices;
int index = 0, atlasIndex = 0; int index = 0, atlasIndex = 0;
int drawnY = 0, height = game.Height; int drawnY = 0, height = game.Height;
FastColour col = new FastColour( 64, 64, 64 ); int col = new FastColour( 64, 64, 64 ).Pack();
int texLoc = game.BlockInfo.GetTextureLoc( Block.Dirt, Side.Top ); int texLoc = game.BlockInfo.GetTextureLoc( Block.Dirt, Side.Top );
TerrainAtlas1D atlas = game.TerrainAtlas1D; TerrainAtlas1D atlas = game.TerrainAtlas1D;

View File

@ -56,7 +56,8 @@ namespace ClassicalSharp {
part.U2 = part.U1 + width / (float)totalWidth; part.U2 = part.U1 + width / (float)totalWidth;
curX += width; curX += width;
IGraphicsApi.Make2DQuad( ref part, FastColour.White, vertices, ref index ); IGraphicsApi.Make2DQuad( ref part, FastColour.WhitePacked,
vertices, ref index );
} }
public void AddInt( int value, VertexP3fT2fC4b[] vertices, ref int index ) { public void AddInt( int value, VertexP3fT2fC4b[] vertices, ref int index ) {

View File

@ -70,8 +70,7 @@ namespace ClassicalSharp.Gui.Widgets {
public override void MoveTo( int newX, int newY ) { public override void MoveTo( int newX, int newY ) {
X = newX; Y = newY; X = newX; Y = newY;
Dispose(); Recreate();
Init();
} }
void MakeBackgroundTexture() { void MakeBackgroundTexture() {

View File

@ -0,0 +1,58 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using ClassicalSharp.GraphicsAPI;
namespace ClassicalSharp.Gui.Widgets {
public sealed class SurvivalHudWidget : Widget {
public SurvivalHudWidget( Game game ) : base( game ) {
HorizontalAnchor = Anchor.Centre;
VerticalAnchor = Anchor.BottomOrRight;
}
// TODO: scaling
public override void Init() {
//float scale = 2 * game.GuiHotbarScale;
Width = (int)(9 * 10);// * scale);
Height = (int)9;// * scale);
X = game.Width / 2 - Width / 2;
Y = game.Height - Height - 100;
}
public override void Render( double delta ) {
Model.ModelCache cache = game.ModelCache;
int index = 0, health = game.LocalPlayer.Health;
for( int heart = 0; heart < 10; heart++ ) {
Texture tex = new Texture( 0, X + 16 * heart, Y - 18, 18, 18, backRec );
IGraphicsApi.Make2DQuad( ref tex, FastColour.WhitePacked,
cache.vertices, ref index );
if( health >= 2 ) {
tex = new Texture( 0, X + 16 * heart + 2, Y - 18 + 2, 14, 14, fullRec );
} else if( health == 1 ) {
tex = new Texture( 0, X + 16 * heart + 2, Y - 18 + 2, 14, 14, halfRec );
} else {
continue;
}
IGraphicsApi.Make2DQuad( ref tex, FastColour.WhitePacked,
cache.vertices, ref index );
health -= 2;
}
game.Graphics.Texturing = true;
game.Graphics.BindTexture( game.Gui.IconsTex );
game.Graphics.UpdateDynamicIndexedVb( DrawMode.Triangles, cache.vb, cache.vertices, index );
game.Graphics.Texturing = false;
}
static TextureRec backRec = new TextureRec( 16 / 256f, 0 / 256f, 9 / 256f, 9 / 256f );
static TextureRec fullRec = new TextureRec( 53 / 256f, 1 / 256f, 7 / 256f, 7 / 256f );
static TextureRec halfRec = new TextureRec( 62 / 256f, 1 / 256f, 7 / 256f, 7 / 256f );
public override void Dispose() { }
public override void MoveTo( int newX, int newY ) { X = newX; Y = newY; }
}
}

View File

@ -137,6 +137,7 @@
<Compile Include="2D\Widgets\PlayerList\ExtPlayerListWidget.cs" /> <Compile Include="2D\Widgets\PlayerList\ExtPlayerListWidget.cs" />
<Compile Include="2D\Widgets\PlayerList\NormalPlayerListWidget.cs" /> <Compile Include="2D\Widgets\PlayerList\NormalPlayerListWidget.cs" />
<Compile Include="2D\Widgets\PlayerList\PlayerListWidget.cs" /> <Compile Include="2D\Widgets\PlayerList\PlayerListWidget.cs" />
<Compile Include="2D\Widgets\SurvivalHudWidget.cs" />
<Compile Include="2D\Widgets\TextWidget.cs" /> <Compile Include="2D\Widgets\TextWidget.cs" />
<Compile Include="2D\Widgets\Widget.cs" /> <Compile Include="2D\Widgets\Widget.cs" />
<Compile Include="Audio\AudioPlayer.cs" /> <Compile Include="Audio\AudioPlayer.cs" />

View File

@ -14,6 +14,8 @@ namespace ClassicalSharp.Entities {
public float SpawnYaw, SpawnPitch; public float SpawnYaw, SpawnPitch;
public short Health = 20;
/// <summary> The distance (in blocks) that players are allowed to /// <summary> The distance (in blocks) that players are allowed to
/// reach to and interact/modify blocks in. </summary> /// reach to and interact/modify blocks in. </summary>
public float ReachDistance = 5f; public float ReachDistance = 5f;

View File

@ -61,12 +61,12 @@ namespace ClassicalSharp.GraphicsAPI {
internal int texVb; internal int texVb;
public virtual void Draw2DTexture( ref Texture tex, FastColour col ) { public virtual void Draw2DTexture( ref Texture tex, FastColour col ) {
int index = 0; int index = 0;
Make2DQuad( ref tex, col, texVerts, ref index ); Make2DQuad( ref tex, col.Pack(), texVerts, ref index );
SetBatchFormat( VertexFormat.P3fT2fC4b ); SetBatchFormat( VertexFormat.P3fT2fC4b );
UpdateDynamicIndexedVb( DrawMode.Triangles, texVb, texVerts, 4 ); UpdateDynamicIndexedVb( DrawMode.Triangles, texVb, texVerts, 4 );
} }
public static void Make2DQuad( ref Texture tex, FastColour col, public static void Make2DQuad( ref Texture tex, int col,
VertexP3fT2fC4b[] vertices, ref int index ) { VertexP3fT2fC4b[] vertices, ref int index ) {
float x1 = tex.X, y1 = tex.Y, x2 = tex.X + tex.Width, y2 = tex.Y + tex.Height; float x1 = tex.X, y1 = tex.Y, x2 = tex.X + tex.Width, y2 = tex.Y + tex.Height;
#if USE_DX #if USE_DX
@ -75,11 +75,10 @@ namespace ClassicalSharp.GraphicsAPI {
x1 -= 0.5f; x2 -= 0.5f; x1 -= 0.5f; x2 -= 0.5f;
y1 -= 0.5f; y2 -= 0.5f; y1 -= 0.5f; y2 -= 0.5f;
#endif #endif
int c = col.Pack(); vertices[index++] = new VertexP3fT2fC4b( x1, y1, 0, tex.U1, tex.V1, col );
vertices[index++] = new VertexP3fT2fC4b( x1, y1, 0, tex.U1, tex.V1, c ); vertices[index++] = new VertexP3fT2fC4b( x2, y1, 0, tex.U2, tex.V1, col );
vertices[index++] = new VertexP3fT2fC4b( x2, y1, 0, tex.U2, tex.V1, c ); vertices[index++] = new VertexP3fT2fC4b( x2, y2, 0, tex.U2, tex.V2, col );
vertices[index++] = new VertexP3fT2fC4b( x2, y2, 0, tex.U2, tex.V2, c ); vertices[index++] = new VertexP3fT2fC4b( x1, y2, 0, tex.U1, tex.V2, col );
vertices[index++] = new VertexP3fT2fC4b( x1, y2, 0, tex.U1, tex.V2, c );
} }
/// <summary> Updates the various matrix stacks and properties so that the graphics API state /// <summary> Updates the various matrix stacks and properties so that the graphics API state