Added HUD drawing logic.

This commit is contained in:
2025-12-21 23:22:53 -05:00
parent ecdc501752
commit 42608a16e3
11 changed files with 247 additions and 39 deletions

View File

@@ -0,0 +1,24 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace AlienAttack.MonoGame.Things.Bullets;
internal class MinigunBulletLarge : Bullet
{
public MinigunBulletLarge(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
BoundBox = new(0, 0, 9, 27);
Damage = 3;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Minigun_Large");
float rotation = MathF.Atan2(YVelocity, XVelocity) + MathF.PI / 2f;
Vector2 origin = new(texture.Width / 2f, texture.Height / 2f);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -0,0 +1,24 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace AlienAttack.MonoGame.Things.Bullets;
internal class MinigunBulletMedium : Bullet
{
public MinigunBulletMedium(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
BoundBox = new(0, 0, 11, 21);
Damage = 2;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Minigun_Medium");
float rotation = MathF.Atan2(YVelocity, XVelocity) + MathF.PI / 2f;
Vector2 origin = new(texture.Width / 2f, texture.Height / 2f);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -8,9 +8,7 @@ internal class MinigunBulletSmall : Bullet
{
public MinigunBulletSmall(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
XVelocity = xVel;
YVelocity = yVel;
Owner = owner;
BoundBox = new(0, 0, 7, 17);
Damage = 1;
}

View File

@@ -0,0 +1,6 @@
namespace AlienAttack.MonoGame.Things.Enemies;
public abstract class EnemyShip(int x, int y) : MoveableSprite(x, y)
{
public virtual int CrashDamage => 10;
}

View File

@@ -9,7 +9,7 @@ using System.Collections.Generic;
namespace AlienAttack.MonoGame.Things.Enemies;
internal class GreenEnemy : MoveableSprite
internal class GreenEnemy : EnemyShip
{
//Enemy01_Green_Frame_1_png_processed
@@ -113,5 +113,10 @@ internal class GreenEnemy : MoveableSprite
{
Health -= bullet.Damage;
}
if (context.Sprite is Player)
{
Health = 0;
}
}
}

View File

@@ -4,7 +4,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace AlienAttack.MonoGame.Things.Enemies;
internal class RedEnemy : MoveableSprite
internal class RedEnemy : EnemyShip
{
protected int FireThreshold => 20;
protected int CurrentFireThreshold { get; set; } = 20;
@@ -92,5 +92,10 @@ internal class RedEnemy : MoveableSprite
{
Health -= bullet.Damage;
}
if (context.Sprite is Player)
{
Health = 0;
}
}
}

View File

@@ -4,7 +4,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace AlienAttack.MonoGame.Things.Enemies;
internal class TealEnemy : MoveableSprite
internal class TealEnemy : EnemyShip
{
protected int FireThreshold => 20;
protected int CurrentFireThreshold { get; set; } = 20;
@@ -85,5 +85,10 @@ internal class TealEnemy : MoveableSprite
{
Health -= bullet.Damage;
}
if (context.Sprite is Player)
{
Health = 0;
}
}
}

View File

@@ -1,9 +1,14 @@
using AlienAttack.MonoGame.Things.Weapons;
using AlienAttack.MonoGame.Things.Bullets;
using AlienAttack.MonoGame.Things.Enemies;
using AlienAttack.MonoGame.Things.Items;
using AlienAttack.MonoGame.Things.Weapons;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.IO;
using static System.Net.Mime.MediaTypeNames;
namespace AlienAttack.MonoGame.Things;
@@ -35,7 +40,6 @@ internal class Player : MoveableSprite
protected PlayerHorizontalMoveState MoveState = PlayerHorizontalMoveState.None;
protected MoveFlag MoveFlags;
protected ulong MoveThreshold = 0;
//protected ulong FireThreshold = 0;
protected ICollection<IWeapon> ActiveWeapons = [];
protected int CurrentExhaustFrame = 1;
@@ -44,11 +48,16 @@ internal class Player : MoveableSprite
protected int CurrentExhaustAnimationThreshold = 10;
protected int CurrentExhaustDirection = 1;
public int Health { get; protected set; }
public int Shield { get; protected set; }
public Player(int x, int y) : base(x, y)
{
BoundBox = new Rectangle(0, 0, 64, 64);
ActiveWeapons.Add(new Minigun());
ActiveWeapons.Add(new FastMinigun());
//ActiveWeapons.Add(new FastMinigun());
Health = 100;
Shield = 100;
}
//Texture2D texture = Game
@@ -244,12 +253,6 @@ internal class Player : MoveableSprite
private void CheckFire(SpriteUpdateContext context)
{
//if (FireThreshold > 0)
//{
// FireThreshold--;
// return;
//}
foreach (IWeapon weapon in ActiveWeapons)
{
weapon.UpdateFireThreshold();
@@ -262,14 +265,6 @@ internal class Player : MoveableSprite
{
weapon.TryFire(this, context);
}
//MinigunBulletSmall bullet1 = new((int)Position.X + 12, (int)Position.Y + 6, 0, -4, this);
//MinigunBulletSmall bullet2 = new((int)Position.X + BoundBox.Width - 20, (int)Position.Y + 6, 0, -4, this);
//context.SpawnSprite(bullet1);
//context.SpawnSprite(bullet2);
//FireThreshold = 15;
}
private static bool IsFireButtonPressed()
@@ -280,4 +275,68 @@ internal class Player : MoveableSprite
return keyState.IsKeyDown(Keys.Space) || mouseState.LeftButton == ButtonState.Pressed || gamePadState.Buttons.A == ButtonState.Pressed;
}
private void TakeDamage(int amount)
{
if (Shield >= amount)
{
TakeShieldDamage(amount);
return;
}
if (Shield > 0)
{
int remainder = amount - Shield;
TakeShieldDamage(Shield);
TakeHealthDamage(remainder);
return;
}
TakeHealthDamage(amount);
}
private void GiveShield(int amount)
{
Shield = Math.Clamp(Shield + amount, 0, 100);
}
private void TakeShieldDamage(int amount)
{
Shield = Math.Clamp(Shield - amount, 0, 100);
}
private void GiveHealth(int amount)
{
Health = Math.Clamp(Health + amount, 0, 100);
}
private void TakeHealthDamage(int amount)
{
Health = Math.Clamp(Health - amount, 0, 100);
}
public override void OnCollision(SpriteCollisionContext context)
{
if (context.Sprite is Bullet bullet && bullet.Owner is not Player)
{
TakeDamage(bullet.Damage);
}
if (context.Sprite is EnemyShip enemyShip)
{
TakeDamage(enemyShip.CrashDamage);
}
if (context.Sprite is Shields)
{
GiveShield(20);
}
if (context.Sprite is Health)
{
GiveHealth(10);
}
}
}

View File

@@ -1,5 +1,4 @@
using AlienAttack.MonoGame.Things.Bullets;
using Microsoft.Xna.Framework;
namespace AlienAttack.MonoGame.Things.Weapons;
@@ -10,13 +9,13 @@ public class Minigun : Weapon
public override void Fire(Sprite owner, SpriteUpdateContext context)
{
// Calculate bullet spawn positions relative to the player's bounding box
int x1 = (int)owner.XPosition + 12;
int x2 = (int)owner.XPosition + owner.BoundBox.Width - 20;
int y = (int)owner.YPosition + 6;
int x1 = (int)owner.XPosition + 14;
int x2 = (int)owner.XPosition + owner.BoundBox.Width - 16;
int y = (int)owner.YPosition + 10;
// Create bullets with velocity (0, -4)
MinigunBulletSmall bullet1 = new(x1, y, 0, -6, owner);
MinigunBulletSmall bullet2 = new(x2, y, 0, -6, owner);
MinigunBulletLarge bullet1 = new(x1, y, 0, -6, owner);
MinigunBulletLarge bullet2 = new(x2, y, 0, -6, owner);
// Queue the bullets for spawning
context.SpawnSprite(bullet1);