Updated asteriods, mines, and explosions.

This commit is contained in:
2026-01-18 22:11:20 -05:00
parent 8e0a68efdf
commit e402c498d6
21 changed files with 338 additions and 102 deletions

View File

@@ -1,4 +1,5 @@
using AlienAttack.MonoGame.Things.Bullets;
using AlienAttack.MonoGame.Things.Explosions;
using AlienAttack.MonoGame.Things.Items;
namespace AlienAttack.MonoGame.Things.Enemies;

View File

@@ -1,4 +1,5 @@
using AlienAttack.MonoGame.Things.Bullets;
using AlienAttack.MonoGame.Things.Explosions;
using Microsoft.Xna.Framework;
namespace AlienAttack.MonoGame.Things.Enemies;

View File

@@ -3,4 +3,6 @@
public class BlueMine(int x, int y) : Mine(x, y)
{
protected override string CoverColor => "Blue";
protected override float ExplodeRadius => 100f;
protected override int MaxHealth => 10;
}

View File

@@ -3,4 +3,6 @@
public class GreenMine(int x, int y) : Mine(x, y)
{
protected override string CoverColor => "Green";
protected override float ExplodeRadius => 100f;
protected override int MaxHealth => 10;
}

View File

@@ -1,4 +1,6 @@
using Microsoft.Xna.Framework;
using AlienAttack.MonoGame.Things.Bullets;
using AlienAttack.MonoGame.Things.Explosions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -9,7 +11,13 @@ public abstract class Mine : MoveableSprite
public const int Width = 65;
public const int Height = 64;
protected abstract int MaxHealth { get; }
protected abstract string CoverColor { get; }
protected abstract float ExplodeRadius { get; }
protected int Health { get; set; }
protected float RotationSpeed { get; set; } = 1.5f;
protected double DistanceToPlayer { get; set; } = double.MaxValue;
public Mine(int x, int y) : base(x, y)
{
@@ -17,38 +25,45 @@ public abstract class Mine : MoveableSprite
BoundBox = new(10, 14, 40, 38);
YVelocity = 1;
XVelocity = 0;
Health = MaxHealth;
}
public override void Draw(SpriteDrawArgs args)
{
DrawRotor(args);
DrawCover(args);
base.Draw(args);
}
private void DrawRotor(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>("Sprites/Rotor");
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, Origin, 1f, SpriteEffects.None, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, Origin, Scale, SpriteEffects.None, 1);
}
private void DrawCover(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>($"Sprites/Cover_{CoverColor}");
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, Origin, 1f, SpriteEffects.None, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, Origin, Scale, SpriteEffects.None, 1);
}
public override sealed void Update(SpriteUpdateContext context)
{
base.Update(context);
CollisionBox = new Rectangle((int)XPosition + BoundBox.X - (int)Origin.X, (int)YPosition + BoundBox.Y - (int)Origin.Y, BoundBox.Width, BoundBox.Height);
if (Health <= 0)
{
IsDead = true;
SpawnExplosion(context);
//OnKilled(context);
return;
}
float xDiff = (XPosition + Origin.X) - (context.Player.XPosition + context.Player.Origin.X);
float yDiff = (YPosition + Origin.Y) - (context.Player.YPosition + context.Player.Origin.Y);
UpdateDistanceToPlayer(context);
OnUpdate(context);
double distance = Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
if (distance < 100)
if (DistanceToPlayer <= ExplodeRadius)
{
IsDead = true;
SpawnExplosion(context);
@@ -61,17 +76,38 @@ public abstract class Mine : MoveableSprite
return;
}
Rotation += 0.01f;
Rotation = MathHelper.WrapAngle(Rotation + RotationSpeed * context.DeltaTime);
}
if (Rotation > 360f)
private void UpdateDistanceToPlayer(SpriteUpdateContext context)
{
float xDiff = (XPosition + Origin.X) - (context.Player.XPosition + context.Player.Origin.X);
float yDiff = (YPosition + Origin.Y) - (context.Player.YPosition + context.Player.Origin.Y);
DistanceToPlayer = Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
}
protected virtual void OnUpdate(SpriteUpdateContext context)
{
}
public override void OnCollision(SpriteCollisionContext context)
{
if (context.Sprite is Bullet bullet && bullet.Owner is Player)
{
Rotation = 0;
Health -= bullet.Damage;
}
if (context.Sprite is Player)
{
Health = 0;
}
}
private void SpawnExplosion(SpriteUpdateContext context)
protected void SpawnExplosion(SpriteUpdateContext context)
{
context.SpawnSprite(new Explosion((int)XPosition, (int)YPosition, XVelocity, YVelocity));
context.SpawnSprite(new MineExplosion((int)XPosition, (int)YPosition, XVelocity, YVelocity));
context.AudioManager.PlayExplosion();
}
}

View File

@@ -1,6 +1,78 @@
namespace AlienAttack.MonoGame.Things.Enemies.Mines;
using AlienAttack.MonoGame.Things.Bullets;
using Microsoft.Xna.Framework;
namespace AlienAttack.MonoGame.Things.Enemies.Mines;
public class RedMine(int x, int y) : Mine(x, y)
{
protected bool IsCharging = false;
protected Vector2 TargetCenter;
protected override string CoverColor => "Red";
protected override float ExplodeRadius => 18f;
protected override int MaxHealth => 10;
// Tunables
private const float DetectRange = 200f;
private const float ChargeSpeed = 5f; // try 3.56.0
protected override void OnUpdate(SpriteUpdateContext context)
{
// If not charging yet, see if we should lock on
if (!IsCharging)
{
if (DistanceToPlayer > DetectRange)
return;
Charge(context.Player);
}
// We are charging: steer directly toward the locked target point
Vector2 toTarget = TargetCenter - new Vector2(XPosition + Origin.X, YPosition + Origin.Y);
float dist = toTarget.Length();
if (dist <= ExplodeRadius)
{
IsDead = true;
// Optionally: explode at target (top-left adjustment)
XPosition = TargetCenter.X - Origin.X;
YPosition = TargetCenter.Y - Origin.Y;
SpawnExplosion(context); // uses current XPosition/YPosition
return;
}
// Normalize direction (avoid NaN if dist == 0)
Vector2 dir = toTarget / dist;
// Set velocities for base.Update to apply next frame
XVelocity = dir.X * ChargeSpeed;
YVelocity = dir.Y * ChargeSpeed;
// Optional: if you want it to stop “floating down” once charging
// (it will anyway, because we overwrite YVelocity)
}
public override void OnCollision(SpriteCollisionContext context)
{
base.OnCollision(context);
if (context.Sprite is Bullet bullet && bullet.Owner is Player player && IsCharging == false)
{
Charge(player);
}
}
private void Charge(Sprite target)
{
IsCharging = true;
float x = target.XPosition + target.Origin.X;
float y = target.YPosition + target.Origin.Y;
TargetCenter = new Vector2(x, y);
RotationSpeed *= ChargeSpeed;
}
}

View File

@@ -37,19 +37,19 @@ public abstract class Turret : MoveableSprite
private void DrawMount(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>("Sprites/GunTurretMount");
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, MountOrigin, 1f, SpriteEffects.None, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, MountOrigin, Scale, SpriteEffects.None, 1);
}
private void DrawTurret(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>($"Sprites/GunTurret_{TurretColor}");
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, TurretOrigin, 1f, SpriteEffects.None, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, TurretOrigin, Scale, SpriteEffects.None, 1);
}
private void DrawGun(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>("Sprites/GunTurret_ExampleGun");
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, GunOrigin, 1f, SpriteEffects.None, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, GunOrigin, Scale, SpriteEffects.None, 1);
}
public override sealed void Update(SpriteUpdateContext context)