Added HUD drawing logic.
This commit is contained in:
@@ -19,13 +19,17 @@ internal class GameLoop : GameLoopBase
|
||||
|
||||
private Starfield _starfield;
|
||||
private Texture2D _pixel;
|
||||
private Player _player;
|
||||
|
||||
protected readonly List<Sprite> Sprites = [];
|
||||
|
||||
public GameLoop(AlienAttackGame game) : base(game)
|
||||
{
|
||||
InitializeStarField(game);
|
||||
Sprites.Add(new Player(game.ViewTransform.ScreenWidth / 2 - 32, game.ViewTransform.ScreenHeight - 64));
|
||||
|
||||
_player = new Player(game.ViewTransform.ScreenWidth / 2 - 32, game.ViewTransform.ScreenHeight - 64);
|
||||
|
||||
Sprites.Add(_player);
|
||||
}
|
||||
|
||||
private void InitializeStarField(AlienAttackGame game)
|
||||
@@ -56,6 +60,7 @@ internal class GameLoop : GameLoopBase
|
||||
{
|
||||
_starfield.Draw(SpriteBatch, _pixel);
|
||||
//DrawBackground();
|
||||
DrawHud();
|
||||
DrawSprites();
|
||||
}
|
||||
|
||||
@@ -90,6 +95,84 @@ internal class GameLoop : GameLoopBase
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawHud()
|
||||
{
|
||||
DrawSideBars(SpriteBatch, ViewTransform.ScreenWidth, ViewTransform.ScreenHeight, _player.Health, _player.Shield);
|
||||
}
|
||||
|
||||
private void DrawSideBars(SpriteBatch sb, int screenW, int screenH, int health, int shield)
|
||||
{
|
||||
health = Math.Clamp(health, 0, 100);
|
||||
shield = Math.Clamp(shield, 0, 100);
|
||||
|
||||
// Tweak these to taste to match “Raptor-ish” proportions
|
||||
int marginTop = 8;
|
||||
int marginBottom = 8;
|
||||
int barWidth = 10; // thin like the screenshot
|
||||
int gap = 4; // small gap between segments
|
||||
int segments = 100;
|
||||
|
||||
int barHeight = screenH - marginTop - marginBottom;
|
||||
|
||||
// Compute segment height so 100 segments + gaps fit in the available height
|
||||
// If your resolution is small, this could become 0; clamp to 1.
|
||||
int segHeight = Math.Max(1, (barHeight - gap * (segments - 1)) / segments);
|
||||
|
||||
// Recompute barHeight actually used by segments (so we can bottom-align perfectly)
|
||||
int usedHeight = segments * segHeight + (segments - 1) * gap;
|
||||
|
||||
// Position bars along left/right edges
|
||||
int leftX = 6; // a few pixels from edge
|
||||
int rightX = screenW - 6 - barWidth;
|
||||
|
||||
int bottomY = marginTop + usedHeight; // bottom of the bar area
|
||||
|
||||
// Colors
|
||||
Color healthOn = new Color(255, 140, 0); // orange-ish
|
||||
Color shieldOn = new Color(60, 140, 255); // blue-ish
|
||||
Color off = new Color(30, 30, 30); // dark "empty" segment
|
||||
|
||||
DrawSegmentedBar(sb, leftX, bottomY, barWidth, segHeight, gap, health, healthOn, off, segments);
|
||||
DrawSegmentedBar(sb, rightX, bottomY, barWidth, segHeight, gap, shield, shieldOn, off, segments);
|
||||
}
|
||||
|
||||
private void DrawSegmentedBar(
|
||||
SpriteBatch sb,
|
||||
int x,
|
||||
int bottomY,
|
||||
int barWidth,
|
||||
int segHeight,
|
||||
int gap,
|
||||
int value0To100,
|
||||
Color onColor,
|
||||
Color offColor,
|
||||
int segments = 100)
|
||||
{
|
||||
float minBrightness = 0.55f;
|
||||
float maxBrightness = 1.00f;
|
||||
|
||||
for (int i = 0; i < segments; i++)
|
||||
{
|
||||
int y = bottomY - (i + 1) * segHeight - i * gap;
|
||||
|
||||
// i = 0 bottom, i = segments-1 top
|
||||
float t = i / (float)(segments - 1);
|
||||
float brightness = MathHelper.Lerp(minBrightness, maxBrightness, t);
|
||||
|
||||
Color baseColor = (i < value0To100) ? onColor : offColor;
|
||||
|
||||
// Apply brightness scaling
|
||||
Color finalColor = new Color(
|
||||
(byte)(baseColor.R * brightness),
|
||||
(byte)(baseColor.G * brightness),
|
||||
(byte)(baseColor.B * brightness),
|
||||
baseColor.A
|
||||
);
|
||||
|
||||
sb.Draw(_pixel, new Rectangle(x, y, barWidth, segHeight), finalColor);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawSprites()
|
||||
{
|
||||
SpriteDrawArgs args = new(Game);
|
||||
|
||||
Reference in New Issue
Block a user