Added texture cache and sprite origins.

This commit is contained in:
2026-01-14 23:09:13 -05:00
parent fcb4f1cf05
commit 61d51f5188
36 changed files with 319 additions and 83 deletions

View File

@@ -1,5 +1,6 @@
using AlienAttack.MonoGame.Audio;
using AlienAttack.MonoGame.GameLoops;
using AlienAttack.MonoGame.Textures;
using AlienAttack.MonoGame.View;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
@@ -22,6 +23,7 @@ public class AlienAttackGame : Game
public SpriteBatch SpriteBatch => _spriteBatch;
public ViewTransform ViewTransform => _viewTransform;
public AudioManager Audio { get; private set; } = new();
public TextureCache Textures { get; } = new();
private const string SDL = "SDL2.dll";
@@ -76,6 +78,7 @@ public class AlienAttackGame : Game
// TODO: use this.Content to load your game content here
Audio.LoadContent(Content);
Textures.Initialize(Content);
}
protected override void Update(GameTime gameTime)

View File

@@ -1337,7 +1337,7 @@
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Sprites/Asteroid 01_png_processed.png
/build:Sprites/Asteroid 01_png_processed.png;Sprites/Asteroid 01.png
#begin Sprites/Asteroid 02_png_processed.png
/importer:TextureImporter
@@ -1349,7 +1349,7 @@
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Sprites/Asteroid 02_png_processed.png
/build:Sprites/Asteroid 02_png_processed.png;Sprites/Asteroid 02.png
#begin Sprites/Asteroid 03_png_processed.png
/importer:TextureImporter
@@ -1361,7 +1361,7 @@
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Sprites/Asteroid 03_png_processed.png
/build:Sprites/Asteroid 03_png_processed.png;Sprites/Asteroid 03.png
#begin Sprites/Asteroid 04_png_processed.png
/importer:TextureImporter
@@ -1373,7 +1373,7 @@
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Sprites/Asteroid 04_png_processed.png
/build:Sprites/Asteroid 04_png_processed.png;Sprites/Asteroid 04.png
#begin Sprites/Cover_Blue_png_processed.png
/importer:TextureImporter

View File

@@ -1,4 +1,5 @@
using AlienAttack.MonoGame.Things;
using AlienAttack.MonoGame.Things.Asteroids;
using AlienAttack.MonoGame.Things.Enemies;
using AlienAttack.MonoGame.Things.Enemies.Mines;
using AlienAttack.MonoGame.Things.Enemies.Turrets;
@@ -263,7 +264,8 @@ internal class GameLoop : GameLoopBase
Random = _random,
GameTime = gameTime,
Content = Content,
SpawnSprite = _spritesToAdd.Add
SpawnSprite = _spritesToAdd.Add,
Player = _player
};
foreach (Sprite sprite in Sprites)
@@ -406,6 +408,12 @@ internal class GameLoop : GameLoopBase
Sprites.Add(enemy);
_spawnNewEnemyThreshold = 100 + _random.Next(0, 100);
}
else if (randomNumber == 13)
{
Asteroid enemy = new(_random.Next(0, ViewTransform.ScreenWidth - Asteroid.Width), -Asteroid.Height);
Sprites.Add(enemy);
_spawnNewEnemyThreshold = 100 + _random.Next(0, 100);
}
}
}
}

View File

@@ -1,4 +1,5 @@
using AlienAttack.MonoGame.Audio;
using AlienAttack.MonoGame.Textures;
using AlienAttack.MonoGame.View;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
@@ -13,6 +14,7 @@ internal abstract class GameLoopBase(AlienAttackGame game) : IGameLoop
protected readonly SpriteBatch SpriteBatch = game.SpriteBatch;
protected readonly ViewTransform ViewTransform = game.ViewTransform;
protected readonly AudioManager Audio = game.Audio;
protected readonly TextureCache Textures = game.Textures;
public void Draw(GameTime gameTime)
{

View File

@@ -0,0 +1,26 @@
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
namespace AlienAttack.MonoGame.Textures;
public sealed class TextureCache
{
private readonly Dictionary<string, Texture2D> _cache = [];
private ContentManager _content = default!;
public void Initialize(ContentManager content) => _content = content;
public Texture2D Get(string assetName)
{
if (_cache.TryGetValue(assetName, out var texture))
return texture;
texture = _content.Load<Texture2D>(assetName);
_cache[assetName] = texture;
return texture;
}
public void Clear() => _cache.Clear();
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AlienAttack.MonoGame.Textures;
//public enum TextureId
//{
// LaserBulletSmall,
// LaserBulletMedium,
// LaserBulletLarge,
// MinigunBulletSmall,
// LaserBulletMedium,
// LaserBulletLarge,
// LaserBulletSmall,
// LaserBulletMedium,
// LaserBulletLarge,
// LaserBulletSmall,
// LaserBulletMedium,
// LaserBulletLarge,
//}
public static class TextureNames
{
public static class Bullets
{
public const string LaserSmall = "Sprites/Laser_Small";
public const string LaserMedium = "Sprites/Laser_Medium";
public const string LaserLarge = "Sprites/Laser_Large";
public const string MinigunSmall = "Sprites/Minigun_Small";
public const string MinigunMedium = "Sprites/Minigun_Medium";
public const string MinigunLarge = "Sprites/Minigun_Large";
public const string PlasmaSmall = "Sprites/Plasma_Small";
public const string PlasmaMedium = "Sprites/Plasma_Medium";
public const string PlasmaLarge = "Sprites/Plasma_Large";
public const string ProtonSmall = "Sprites/Proton_Small";
public const string ProtonMedium = "Sprites/Proton_Medium";
public const string ProtonLarge = "Sprites/Proton_Large";
}
}

View File

@@ -0,0 +1,48 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace AlienAttack.MonoGame.Things.Asteroids;
public class Asteroid : MoveableSprite
{
public const int Width = 39;
public const int Height = 37;
protected float Rotation = 0;
public Asteroid(int x, int y) : base(x, y)
{
Origin = new(Width / 2, Height / 2);
BoundBox = new(0, 0, Width, Height);
YVelocity = 1;
XVelocity = 0;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>("Sprites/Asteroid 01");
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, Origin, 1f, SpriteEffects.None, 1);
base.Draw(args);
}
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 (YPosition - Origin.Y > context.ViewTransform.ScreenHeight)
{
IsDead = true;
return;
}
Rotation += 0.01f;
if (Rotation > 360f)
{
Rotation = 0;
}
}
}

View File

@@ -1,7 +1,4 @@
using AlienAttack.MonoGame.Things.Items;
using AlienAttack.MonoGame.Things.Muzzles;
using Microsoft.Xna.Framework;
using System;
namespace AlienAttack.MonoGame.Things.Bullets;

View File

@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using AlienAttack.MonoGame.Textures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -11,17 +12,17 @@ public class LaserBulletLarge : Bullet
public LaserBulletLarge(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
Origin = new(Width/2, Height/2);
BoundBox = new(0, 0, Width, Height);
Damage = 3;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Laser_Large");
Texture2D texture = args.Textures.Get(TextureNames.Bullets.LaserLarge);
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);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, Origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using AlienAttack.MonoGame.Textures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -11,17 +12,17 @@ public class LaserBulletMedium : Bullet
public LaserBulletMedium(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
Origin = new(Width / 2, Height / 2);
BoundBox = new(0, 0, Width, Height);
Damage = 2;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Laser_Medium");
Texture2D texture = args.Textures.Get(TextureNames.Bullets.LaserMedium);
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);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, Origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using AlienAttack.MonoGame.Textures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -11,17 +12,17 @@ public class LaserBulletSmall : Bullet
public LaserBulletSmall(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
Origin = new(Width / 2, Height / 2);
BoundBox = new(0, 0, Width, Height);
Damage = 1;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Laser_Small");
Texture2D texture = args.Textures.Get(TextureNames.Bullets.LaserSmall);
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);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, Origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using AlienAttack.MonoGame.Textures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -11,17 +12,17 @@ public class MinigunBulletLarge : Bullet
public MinigunBulletLarge(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
Origin = new(Width / 2, Height / 2);
BoundBox = new(0, 0, Width, Height);
Damage = 3;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Minigun_Large");
Texture2D texture = args.Textures.Get(TextureNames.Bullets.MinigunLarge);
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);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, Origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using AlienAttack.MonoGame.Textures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -11,17 +12,17 @@ public class MinigunBulletMedium : Bullet
public MinigunBulletMedium(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
Origin = new(Width / 2, Height / 2);
BoundBox = new(0, 0, Width, Height);
Damage = 2;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Minigun_Medium");
Texture2D texture = args.Textures.Get(TextureNames.Bullets.MinigunMedium);
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);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, Origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using AlienAttack.MonoGame.Textures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -11,17 +12,17 @@ public class MinigunBulletSmall : Bullet
public MinigunBulletSmall(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
Origin = new(Width / 2, Height / 2);
BoundBox = new(0, 0, Width, Height);
Damage = 1;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Minigun_Small");
Texture2D texture = args.Textures.Get(TextureNames.Bullets.MinigunSmall);
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);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, Origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using AlienAttack.MonoGame.Textures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -11,17 +12,17 @@ public class PlasmaBulletLarge : Bullet
public PlasmaBulletLarge(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
Origin = new(Width / 2, Height / 2);
BoundBox = new(0, 0, Width, Height);
Damage = 3;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Plasma_Large");
Texture2D texture = args.Textures.Get(TextureNames.Bullets.PlasmaLarge);
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);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, Origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using AlienAttack.MonoGame.Textures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -11,17 +12,17 @@ public class PlasmaBulletMedium : Bullet
public PlasmaBulletMedium(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
Origin = new(Width / 2, Height / 2);
BoundBox = new(0, 0, Width, Height);
Damage = 2;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Plasma_Medium");
Texture2D texture = args.Textures.Get(TextureNames.Bullets.PlasmaMedium);
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);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, Origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using AlienAttack.MonoGame.Textures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -11,17 +12,17 @@ public class PlasmaBulletSmall : Bullet
public PlasmaBulletSmall(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
Origin = new(Width / 2, Height / 2);
BoundBox = new(0, 0, Width, Height);
Damage = 1;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Plasma_Small");
Texture2D texture = args.Textures.Get(TextureNames.Bullets.PlasmaSmall);
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);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, Origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using AlienAttack.MonoGame.Textures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -11,17 +12,17 @@ public class ProtonBulletLarge : Bullet
public ProtonBulletLarge(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
Origin = new(Width / 2, Height / 2);
BoundBox = new(0, 0, Width, Height);
Damage = 3;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Proton_Large");
Texture2D texture = args.Textures.Get(TextureNames.Bullets.ProtonLarge);
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);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, Origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using AlienAttack.MonoGame.Textures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -11,17 +12,17 @@ public class ProtonBulletMedium : Bullet
public ProtonBulletMedium(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
Origin = new(Width / 2, Height / 2);
BoundBox = new(0, 0, Width, Height);
Damage = 2;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Proton_Medium");
Texture2D texture = args.Textures.Get(TextureNames.Bullets.ProtonMedium);
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);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, Origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using AlienAttack.MonoGame.Textures;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -11,17 +12,17 @@ public class ProtonBulletSmall : Bullet
public ProtonBulletSmall(float x, float y, float xVel, float yVel, Sprite owner) : base(x, y, xVel, yVel, owner)
{
Origin = new(Width / 2, Height / 2);
BoundBox = new(0, 0, Width, Height);
Damage = 1;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Proton_Small");
Texture2D texture = args.Textures.Get(TextureNames.Bullets.ProtonSmall);
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);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, rotation, Origin, 1f, SpriteEffects.None, 1);
}
}

View File

@@ -27,7 +27,8 @@ public class Enemy02Green : EnemyShip
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Enemy02Green_Frame_{frame}_png_processed");
//SpriteEffects spriteEffects = SpriteEffects.None;
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);
//args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, Origin, 1, spriteEffects, 1);
base.Draw(args);
}

View File

@@ -27,7 +27,8 @@ public class Enemy02Red : EnemyShip
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Enemy02Red_Frame_{frame}_png_processed");
//SpriteEffects spriteEffects = SpriteEffects.None;
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);
//args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, Origin, 1, spriteEffects, 1);
base.Draw(args);
}

View File

@@ -27,7 +27,8 @@ public class Enemy02Teal : EnemyShip
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Enemy02_Teal_Frame_{frame}_png_processed");
//SpriteEffects spriteEffects = SpriteEffects.None;
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);
//args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, Origin, 1, spriteEffects, 1);
base.Draw(args);
}

View File

@@ -1,13 +1,19 @@
using AlienAttack.MonoGame.Things.Bullets;
using Microsoft.Xna.Framework;
namespace AlienAttack.MonoGame.Things.Enemies;
public abstract class EnemyShip(int x, int y) : MoveableSprite(x, y)
public abstract class EnemyShip : MoveableSprite
{
protected abstract int Health { get; set; }
public virtual int CrashDamage => 10;
public EnemyShip(int x, int y) : base(x, y)
{
Origin = new(32, 32);
}
public override sealed void Update(SpriteUpdateContext context)
{
if (Health <= 0)
@@ -22,7 +28,7 @@ public abstract class EnemyShip(int x, int y) : MoveableSprite(x, y)
TryMove(context);
if (YPosition > context.ViewTransform.ScreenHeight)
if (YPosition - Origin.Y > context.ViewTransform.ScreenHeight)
{
IsDead = true;
return;

View File

@@ -29,7 +29,8 @@ public class GreenEnemy : EnemyShip
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Enemy01_Green_Frame_1_png_processed");
SpriteEffects spriteEffects = SpriteEffects.None;
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);
//args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, Origin, 1, spriteEffects, 1);
base.Draw(args);
}
@@ -56,8 +57,11 @@ public class GreenEnemy : EnemyShip
protected override void OnKilled(SpriteUpdateContext context)
{
int itemXPosition = (int)XPosition + 32 - Item.Width / 2;
int itemYPosition = (int)YPosition + 32 - Item.Height / 2;
//int itemXPosition = (int)XPosition + 32 - Item.Width / 2;
//int itemYPosition = (int)YPosition + 32 - Item.Height / 2;
int itemXPosition = (int)XPosition;
int itemYPosition = (int)YPosition;
switch (context.Random.Next(0, 5))
{

View File

@@ -1,5 +1,6 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace AlienAttack.MonoGame.Things.Enemies.Mines;
@@ -9,12 +10,12 @@ public abstract class Mine : MoveableSprite
public const int Height = 64;
protected float Rotation = 0;
protected Vector2 Origin = new(30, 33);
protected abstract string CoverColor { get; }
public Mine(int x, int y) : base(x, y)
{
Origin = new(30, 33);
BoundBox = new(10, 14, 40, 38);
YVelocity = 1;
XVelocity = 0;
@@ -74,6 +75,18 @@ public abstract class Mine : MoveableSprite
CollisionBox = new Rectangle((int)XPosition + BoundBox.X - (int)Origin.X, (int)YPosition + BoundBox.Y - (int)Origin.Y, BoundBox.Width, BoundBox.Height);
float xDiff = (XPosition + Origin.X) - (context.Player.XPosition + context.Player.Origin.X);
float yDiff = (YPosition + Origin.Y) - (context.Player.YPosition + context.Player.Origin.Y);
double distance = Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
if (distance < 100)
{
IsDead = true;
SpawnExplosion(context);
return;
}
if (YPosition - Origin.Y > context.ViewTransform.ScreenHeight)
{
IsDead = true;
@@ -87,4 +100,10 @@ public abstract class Mine : MoveableSprite
Rotation = 0;
}
}
private void SpawnExplosion(SpriteUpdateContext context)
{
context.SpawnSprite(new Explosion((int)XPosition, (int)YPosition, XVelocity, YVelocity));
context.AudioManager.PlayExplosion();
}
}

View File

@@ -23,7 +23,8 @@ public class RedEnemy : EnemyShip
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Enemy01_Red_Frame_{frame}_png_processed");
//SpriteEffects spriteEffects = SpriteEffects.None;
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);
//args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, Origin, 1, spriteEffects, 1);
base.Draw(args);
}

View File

@@ -24,7 +24,8 @@ public class TealEnemy : EnemyShip
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Enemy01_Teal_Frame_{frame}_png_processed");
//SpriteEffects spriteEffects = SpriteEffects.None;
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);
//args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, Origin, 1, spriteEffects, 1);
base.Draw(args);
}

View File

@@ -3,17 +3,25 @@ using Microsoft.Xna.Framework.Graphics;
namespace AlienAttack.MonoGame.Things;
public class Explosion(int x, int y, float xVel, float yVel) : Sprite(x, y)
public class Explosion : MoveableSprite
{
protected int CurrentFrame { get; private set; } = 1;
protected int MaxFrames => 9;
protected int AnimationThreshold => 3; //5;
protected int CurrentThreshold { get; private set; } = 5;
public Explosion(int x, int y, float xVel, float yVel) : base(x, y)
{
Origin = new(32.5f, 32);
XVelocity = xVel;
YVelocity = yVel;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Explosion01_Frame_0{CurrentFrame}_png_processed");
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 1);
//args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, Origin, 1f, SpriteEffects.None, 1);
base.Draw(args);
}
@@ -22,8 +30,8 @@ public class Explosion(int x, int y, float xVel, float yVel) : Sprite(x, y)
{
base.Update(context);
XPosition += xVel;
YPosition += yVel;
//XPosition += XVelocity;
//YPosition += YVelocity;
if (CurrentThreshold > 0)
{

View File

@@ -22,6 +22,8 @@ public abstract class Item : MoveableSprite
public Item(int x, int y) : base(x, y)
{
YVelocity = .5f;
Origin = new(Width / 2, Height / 2);
BoundBox = new Rectangle(0, 0, Width, Height);
_anchor = new Vector2(x, y);
@@ -34,7 +36,7 @@ public abstract class Item : MoveableSprite
Texture2D texture = args.Content.Load<Texture2D>(TextureName);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), _scale, SpriteEffects.None, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, Origin, _scale, SpriteEffects.None, 1);
}
public override void Update(SpriteUpdateContext context)

View File

@@ -3,17 +3,24 @@ using Microsoft.Xna.Framework.Graphics;
namespace AlienAttack.MonoGame.Things;
public class MiniExplosion(int x, int y, float xVel, float yVel) : Sprite(x, y)
public class MiniExplosion : MoveableSprite
{
protected int CurrentFrame { get; private set; } = 1;
protected int MaxFrames => 9;
protected int AnimationThreshold => 3; //5;
protected int CurrentThreshold { get; private set; } = 5;
public MiniExplosion(int x, int y, float xVel, float yVel) : base(x, y)
{
Origin = new(32.5f, 32);
XVelocity = xVel;
YVelocity = yVel;
}
public override void Draw(SpriteDrawArgs args)
{
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Explosion01_Frame_0{CurrentFrame}_png_processed");
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), .25f, SpriteEffects.None, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, Origin, .25f, SpriteEffects.None, 1);
base.Draw(args);
}
@@ -22,8 +29,8 @@ public class MiniExplosion(int x, int y, float xVel, float yVel) : Sprite(x, y)
{
base.Update(context);
XPosition += xVel;
YPosition += yVel;
//XPosition += xVel;
//YPosition += yVel;
if (CurrentThreshold > 0)
{

View File

@@ -7,7 +7,7 @@ public static class MuzzleMath
public static Vector2 GetMuzzleWorld(Sprite owner, Vector2 muzzleLocalPx, float ownerScale = 1f)
{
// Assumes owner.Position is the top-left of the sprite
return owner.Position + muzzleLocalPx * ownerScale;
return owner.Position - owner.Origin + muzzleLocalPx * ownerScale;
}
public static Vector2 CenterBulletOn(Vector2 muzzleWorld, int bulletW, int bulletH)

View File

@@ -30,9 +30,10 @@ public enum MoveFlag
Boost = 16
}
public class DrwaState
public record DrawState
{
public int Frame { get; init; }
public SpriteEffects SpriteEffects { get; init; }
}
public class Player : MoveableSprite
@@ -42,6 +43,8 @@ public class Player : MoveableSprite
protected ulong MoveThreshold = 0;
protected ICollection<IWeapon> ActiveWeapons = [];
protected DrawState DrawState;
protected int CurrentExhaustFrame = 1;
protected int MaxExhaustFrames = 6;
protected int ExhaustAnimationThreshold = 30;
@@ -51,7 +54,7 @@ public class Player : MoveableSprite
public int Health { get; protected set; }
public int Shield { get; protected set; }
public IReadOnlyList<Muzzle> Muzzles { get; } =
public IReadOnlyList<Muzzle> Muzzles { get; protected set; } =
[
new Muzzle(MuzzleId.Center, new Vector2(31.5f, 1f)),
new Muzzle(MuzzleId.Left, new Vector2(15f, 27f)),
@@ -60,6 +63,15 @@ public class Player : MoveableSprite
public Player(int x, int y) : base(x, y)
{
Origin = new(32, 32);
Muzzles =
[
new Muzzle(MuzzleId.Center, new Vector2(31.5f, 1f)),
new Muzzle(MuzzleId.Left, new Vector2(15f, 27f)),
new Muzzle(MuzzleId.Right, new Vector2(47f, 27f)),
];
BoundBox = new Rectangle(0, 0, 64, 64);
//ActiveWeapons.Add(new Minigun());
//ActiveWeapons.Add(new MinigunTripleSpreadFast());
@@ -67,6 +79,12 @@ public class Player : MoveableSprite
Health = 100;
Shield = 100;
DrawState = new()
{
Frame = 1,
SpriteEffects = SpriteEffects.None
};
// Weapon 1
WeaponDef weaponDef = new()
{
@@ -133,7 +151,9 @@ public class Player : MoveableSprite
Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\PlayerRed_Frame_{frameNumber}");
//args.SpriteBatch.Draw(texture, Position, DrawColor);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);
args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, Origin, 1, spriteEffects, 1);
base.Draw(args);
}
private void DrawExhaust(SpriteDrawArgs args)

View File

@@ -8,6 +8,7 @@ public class Sprite(float x, float y)
public float XPosition { get; protected set; } = x;
public float YPosition { get; protected set; } = y;
public Vector2 Position => new(XPosition, YPosition);
public Vector2 Origin { get; protected set; }
public Rectangle BoundBox { get; protected set; }
protected Rectangle CollisionBox;
@@ -18,7 +19,8 @@ public class Sprite(float x, float y)
public virtual void Update(SpriteUpdateContext context)
{
CollisionBox = new Rectangle((int)XPosition + BoundBox.X, (int)YPosition + BoundBox.Y, BoundBox.Width, BoundBox.Height);
//CollisionBox = new Rectangle((int)XPosition + BoundBox.X, (int)YPosition + BoundBox.Y, BoundBox.Width, BoundBox.Height);
CollisionBox = new Rectangle((int)(Position.X - Origin.X) + BoundBox.X, (int)(Position.Y - Origin.Y) + BoundBox.Y, BoundBox.Width, BoundBox.Height);
}
public bool Intersects(Sprite sprite)
@@ -29,6 +31,28 @@ public class Sprite(float x, float y)
public virtual void Draw(SpriteDrawArgs args)
{
//spriteBatch.Draw(Texture, Position, DrawColor);
DrawCollisionBox(args);
}
private void DrawCollisionBox(SpriteDrawArgs args)
{
//var pixel = DebugPixel; // static cached
Texture2D pixel = new Texture2D(args.SpriteBatch.GraphicsDevice, 1, 1);
pixel.SetData(new[] { Color.White });
//Rectangle r = GetWorldCollisionBox();
Color c = Color.LimeGreen; // debug color
// Top
args.SpriteBatch.Draw(pixel, new Rectangle(CollisionBox.X, CollisionBox.Y, CollisionBox.Width, 1), c);
// Bottom
args.SpriteBatch.Draw(pixel, new Rectangle(CollisionBox.X, CollisionBox.Bottom - 1, CollisionBox.Width, 1), c);
// Left
args.SpriteBatch.Draw(pixel, new Rectangle(CollisionBox.X, CollisionBox.Y, 1, CollisionBox.Height), c);
// Right
args.SpriteBatch.Draw(pixel, new Rectangle(CollisionBox.Right - 1, CollisionBox.Y, 1, CollisionBox.Height), c);
}
public virtual void OnCollision(SpriteCollisionContext context)

View File

@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework.Content;
using AlienAttack.MonoGame.Textures;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
@@ -8,4 +9,5 @@ public class SpriteDrawArgs(AlienAttackGame game)
{
public SpriteBatch SpriteBatch => game.SpriteBatch;
public ContentManager Content => game.Content;
public TextureCache Textures => game.Textures;
}

View File

@@ -14,6 +14,7 @@ public class SpriteUpdateContext(AlienAttackGame game)
public required GameTime GameTime { get; init; }
public required ContentManager Content { get; init; }
public AudioManager AudioManager => game.Audio;
public required Player Player { get; init; }
}
public class SpriteCollisionContext(AlienAttackGame game)