From 61d51f5188dfcd7068910edc65eb468bbdcd9fad Mon Sep 17 00:00:00 2001 From: Brian Bicknell Date: Wed, 14 Jan 2026 23:09:13 -0500 Subject: [PATCH] Added texture cache and sprite origins. --- AlientAttack.MonoGame/AlienAttackGame.cs | 3 ++ AlientAttack.MonoGame/Content/Content.mgcb | 8 ++-- AlientAttack.MonoGame/GameLoops/GameLoop.cs | 10 +++- .../GameLoops/GameLoopBase.cs | 2 + .../Textures/TextureCache.cs | 26 ++++++++++ AlientAttack.MonoGame/Textures/TextureId.cs | 42 ++++++++++++++++ .../Things/Asteroids/Asteroid.cs | 48 +++++++++++++++++++ .../Things/Bullets/Bullet.cs | 3 -- .../Things/Bullets/LaserBulletLarge.cs | 9 ++-- .../Things/Bullets/LaserBulletMedium.cs | 9 ++-- .../Things/Bullets/LaserBulletSmall.cs | 9 ++-- .../Things/Bullets/MinigunBulletLarge.cs | 9 ++-- .../Things/Bullets/MinigunBulletMedium.cs | 9 ++-- .../Things/Bullets/MinigunBulletSmall.cs | 9 ++-- .../Things/Bullets/PlasmaBulletLarge.cs | 9 ++-- .../Things/Bullets/PlasmaBulletMedium.cs | 9 ++-- .../Things/Bullets/PlasmaBulletSmall.cs | 9 ++-- .../Things/Bullets/ProtonBulletLarge.cs | 9 ++-- .../Things/Bullets/ProtonBulletMedium.cs | 9 ++-- .../Things/Bullets/ProtonBulletSmall.cs | 9 ++-- .../Things/Enemies/Enemy02Green.cs | 3 +- .../Things/Enemies/Enemy02Red.cs | 3 +- .../Things/Enemies/Enemy02Teal.cs | 3 +- .../Things/Enemies/EnemyShip.cs | 10 +++- .../Things/Enemies/GreenEnemy.cs | 10 ++-- .../Things/Enemies/Mines/Mine.cs | 21 +++++++- .../Things/Enemies/RedEnemy.cs | 3 +- .../Things/Enemies/TealEnemy.cs | 3 +- AlientAttack.MonoGame/Things/Explosion.cs | 16 +++++-- AlientAttack.MonoGame/Things/Items/Item.cs | 4 +- AlientAttack.MonoGame/Things/MiniExplosion.cs | 15 ++++-- .../Things/Muzzles/MuzzleMath.cs | 2 +- AlientAttack.MonoGame/Things/Player.cs | 28 +++++++++-- AlientAttack.MonoGame/Things/Sprite.cs | 26 +++++++++- .../Things/SpriteDrawArgs.cs | 4 +- .../Things/SpriteUpdateContext.cs | 1 + 36 files changed, 319 insertions(+), 83 deletions(-) create mode 100644 AlientAttack.MonoGame/Textures/TextureCache.cs create mode 100644 AlientAttack.MonoGame/Textures/TextureId.cs create mode 100644 AlientAttack.MonoGame/Things/Asteroids/Asteroid.cs diff --git a/AlientAttack.MonoGame/AlienAttackGame.cs b/AlientAttack.MonoGame/AlienAttackGame.cs index d41ed45..e3bb891 100644 --- a/AlientAttack.MonoGame/AlienAttackGame.cs +++ b/AlientAttack.MonoGame/AlienAttackGame.cs @@ -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) diff --git a/AlientAttack.MonoGame/Content/Content.mgcb b/AlientAttack.MonoGame/Content/Content.mgcb index 8bf4506..54c0766 100644 --- a/AlientAttack.MonoGame/Content/Content.mgcb +++ b/AlientAttack.MonoGame/Content/Content.mgcb @@ -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 diff --git a/AlientAttack.MonoGame/GameLoops/GameLoop.cs b/AlientAttack.MonoGame/GameLoops/GameLoop.cs index 0881525..6c50c93 100644 --- a/AlientAttack.MonoGame/GameLoops/GameLoop.cs +++ b/AlientAttack.MonoGame/GameLoops/GameLoop.cs @@ -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); + } } } } diff --git a/AlientAttack.MonoGame/GameLoops/GameLoopBase.cs b/AlientAttack.MonoGame/GameLoops/GameLoopBase.cs index f7d12a9..ca62afd 100644 --- a/AlientAttack.MonoGame/GameLoops/GameLoopBase.cs +++ b/AlientAttack.MonoGame/GameLoops/GameLoopBase.cs @@ -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) { diff --git a/AlientAttack.MonoGame/Textures/TextureCache.cs b/AlientAttack.MonoGame/Textures/TextureCache.cs new file mode 100644 index 0000000..e5ee86e --- /dev/null +++ b/AlientAttack.MonoGame/Textures/TextureCache.cs @@ -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 _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(assetName); + _cache[assetName] = texture; + + return texture; + } + + public void Clear() => _cache.Clear(); +} \ No newline at end of file diff --git a/AlientAttack.MonoGame/Textures/TextureId.cs b/AlientAttack.MonoGame/Textures/TextureId.cs new file mode 100644 index 0000000..630fa4b --- /dev/null +++ b/AlientAttack.MonoGame/Textures/TextureId.cs @@ -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"; + } +} \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Asteroids/Asteroid.cs b/AlientAttack.MonoGame/Things/Asteroids/Asteroid.cs new file mode 100644 index 0000000..0115e1b --- /dev/null +++ b/AlientAttack.MonoGame/Things/Asteroids/Asteroid.cs @@ -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("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; + } + } +} \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Bullets/Bullet.cs b/AlientAttack.MonoGame/Things/Bullets/Bullet.cs index 6707eec..5ec8746 100644 --- a/AlientAttack.MonoGame/Things/Bullets/Bullet.cs +++ b/AlientAttack.MonoGame/Things/Bullets/Bullet.cs @@ -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; diff --git a/AlientAttack.MonoGame/Things/Bullets/LaserBulletLarge.cs b/AlientAttack.MonoGame/Things/Bullets/LaserBulletLarge.cs index babc976..865f86e 100644 --- a/AlientAttack.MonoGame/Things/Bullets/LaserBulletLarge.cs +++ b/AlientAttack.MonoGame/Things/Bullets/LaserBulletLarge.cs @@ -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(@$"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); } } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Bullets/LaserBulletMedium.cs b/AlientAttack.MonoGame/Things/Bullets/LaserBulletMedium.cs index 4e85fdc..eb554d0 100644 --- a/AlientAttack.MonoGame/Things/Bullets/LaserBulletMedium.cs +++ b/AlientAttack.MonoGame/Things/Bullets/LaserBulletMedium.cs @@ -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(@$"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); } } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Bullets/LaserBulletSmall.cs b/AlientAttack.MonoGame/Things/Bullets/LaserBulletSmall.cs index d947575..07e019f 100644 --- a/AlientAttack.MonoGame/Things/Bullets/LaserBulletSmall.cs +++ b/AlientAttack.MonoGame/Things/Bullets/LaserBulletSmall.cs @@ -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(@$"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); } } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Bullets/MinigunBulletLarge.cs b/AlientAttack.MonoGame/Things/Bullets/MinigunBulletLarge.cs index 5df7885..0cb6631 100644 --- a/AlientAttack.MonoGame/Things/Bullets/MinigunBulletLarge.cs +++ b/AlientAttack.MonoGame/Things/Bullets/MinigunBulletLarge.cs @@ -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(@$"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); } } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Bullets/MinigunBulletMedium.cs b/AlientAttack.MonoGame/Things/Bullets/MinigunBulletMedium.cs index 125f33f..fa36ee5 100644 --- a/AlientAttack.MonoGame/Things/Bullets/MinigunBulletMedium.cs +++ b/AlientAttack.MonoGame/Things/Bullets/MinigunBulletMedium.cs @@ -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(@$"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); } } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Bullets/MinigunBulletSmall.cs b/AlientAttack.MonoGame/Things/Bullets/MinigunBulletSmall.cs index 764c62a..2bf9b82 100644 --- a/AlientAttack.MonoGame/Things/Bullets/MinigunBulletSmall.cs +++ b/AlientAttack.MonoGame/Things/Bullets/MinigunBulletSmall.cs @@ -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(@$"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); } } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Bullets/PlasmaBulletLarge.cs b/AlientAttack.MonoGame/Things/Bullets/PlasmaBulletLarge.cs index 533350f..21ac5ab 100644 --- a/AlientAttack.MonoGame/Things/Bullets/PlasmaBulletLarge.cs +++ b/AlientAttack.MonoGame/Things/Bullets/PlasmaBulletLarge.cs @@ -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(@$"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); } } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Bullets/PlasmaBulletMedium.cs b/AlientAttack.MonoGame/Things/Bullets/PlasmaBulletMedium.cs index 4235804..b317abf 100644 --- a/AlientAttack.MonoGame/Things/Bullets/PlasmaBulletMedium.cs +++ b/AlientAttack.MonoGame/Things/Bullets/PlasmaBulletMedium.cs @@ -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(@$"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); } } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Bullets/PlasmaBulletSmall.cs b/AlientAttack.MonoGame/Things/Bullets/PlasmaBulletSmall.cs index 36981e9..4237559 100644 --- a/AlientAttack.MonoGame/Things/Bullets/PlasmaBulletSmall.cs +++ b/AlientAttack.MonoGame/Things/Bullets/PlasmaBulletSmall.cs @@ -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(@$"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); } } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Bullets/ProtonBulletLarge.cs b/AlientAttack.MonoGame/Things/Bullets/ProtonBulletLarge.cs index 609dfc8..d07d76c 100644 --- a/AlientAttack.MonoGame/Things/Bullets/ProtonBulletLarge.cs +++ b/AlientAttack.MonoGame/Things/Bullets/ProtonBulletLarge.cs @@ -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(@$"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); } } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Bullets/ProtonBulletMedium.cs b/AlientAttack.MonoGame/Things/Bullets/ProtonBulletMedium.cs index 5033130..42cf3f9 100644 --- a/AlientAttack.MonoGame/Things/Bullets/ProtonBulletMedium.cs +++ b/AlientAttack.MonoGame/Things/Bullets/ProtonBulletMedium.cs @@ -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(@$"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); } } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Bullets/ProtonBulletSmall.cs b/AlientAttack.MonoGame/Things/Bullets/ProtonBulletSmall.cs index e5b718a..b4273da 100644 --- a/AlientAttack.MonoGame/Things/Bullets/ProtonBulletSmall.cs +++ b/AlientAttack.MonoGame/Things/Bullets/ProtonBulletSmall.cs @@ -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(@$"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); } } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Enemies/Enemy02Green.cs b/AlientAttack.MonoGame/Things/Enemies/Enemy02Green.cs index 55e9d01..ec54566 100644 --- a/AlientAttack.MonoGame/Things/Enemies/Enemy02Green.cs +++ b/AlientAttack.MonoGame/Things/Enemies/Enemy02Green.cs @@ -27,7 +27,8 @@ public class Enemy02Green : EnemyShip Texture2D texture = args.Content.Load(@$"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); } diff --git a/AlientAttack.MonoGame/Things/Enemies/Enemy02Red.cs b/AlientAttack.MonoGame/Things/Enemies/Enemy02Red.cs index a5346d1..b116670 100644 --- a/AlientAttack.MonoGame/Things/Enemies/Enemy02Red.cs +++ b/AlientAttack.MonoGame/Things/Enemies/Enemy02Red.cs @@ -27,7 +27,8 @@ public class Enemy02Red : EnemyShip Texture2D texture = args.Content.Load(@$"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); } diff --git a/AlientAttack.MonoGame/Things/Enemies/Enemy02Teal.cs b/AlientAttack.MonoGame/Things/Enemies/Enemy02Teal.cs index 590a859..17e187f 100644 --- a/AlientAttack.MonoGame/Things/Enemies/Enemy02Teal.cs +++ b/AlientAttack.MonoGame/Things/Enemies/Enemy02Teal.cs @@ -27,7 +27,8 @@ public class Enemy02Teal : EnemyShip Texture2D texture = args.Content.Load(@$"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); } diff --git a/AlientAttack.MonoGame/Things/Enemies/EnemyShip.cs b/AlientAttack.MonoGame/Things/Enemies/EnemyShip.cs index 809fce2..120ecc6 100644 --- a/AlientAttack.MonoGame/Things/Enemies/EnemyShip.cs +++ b/AlientAttack.MonoGame/Things/Enemies/EnemyShip.cs @@ -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; diff --git a/AlientAttack.MonoGame/Things/Enemies/GreenEnemy.cs b/AlientAttack.MonoGame/Things/Enemies/GreenEnemy.cs index 264d47d..d3d1142 100644 --- a/AlientAttack.MonoGame/Things/Enemies/GreenEnemy.cs +++ b/AlientAttack.MonoGame/Things/Enemies/GreenEnemy.cs @@ -29,7 +29,8 @@ public class GreenEnemy : EnemyShip Texture2D texture = args.Content.Load(@$"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)) { diff --git a/AlientAttack.MonoGame/Things/Enemies/Mines/Mine.cs b/AlientAttack.MonoGame/Things/Enemies/Mines/Mine.cs index 0825fed..db67a2d 100644 --- a/AlientAttack.MonoGame/Things/Enemies/Mines/Mine.cs +++ b/AlientAttack.MonoGame/Things/Enemies/Mines/Mine.cs @@ -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(); + } } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Enemies/RedEnemy.cs b/AlientAttack.MonoGame/Things/Enemies/RedEnemy.cs index 62c614d..5d4465d 100644 --- a/AlientAttack.MonoGame/Things/Enemies/RedEnemy.cs +++ b/AlientAttack.MonoGame/Things/Enemies/RedEnemy.cs @@ -23,7 +23,8 @@ public class RedEnemy : EnemyShip Texture2D texture = args.Content.Load(@$"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); } diff --git a/AlientAttack.MonoGame/Things/Enemies/TealEnemy.cs b/AlientAttack.MonoGame/Things/Enemies/TealEnemy.cs index b8bf8cc..cc65e8a 100644 --- a/AlientAttack.MonoGame/Things/Enemies/TealEnemy.cs +++ b/AlientAttack.MonoGame/Things/Enemies/TealEnemy.cs @@ -24,7 +24,8 @@ public class TealEnemy : EnemyShip Texture2D texture = args.Content.Load(@$"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); } diff --git a/AlientAttack.MonoGame/Things/Explosion.cs b/AlientAttack.MonoGame/Things/Explosion.cs index 3d0be80..6467733 100644 --- a/AlientAttack.MonoGame/Things/Explosion.cs +++ b/AlientAttack.MonoGame/Things/Explosion.cs @@ -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(@$"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) { diff --git a/AlientAttack.MonoGame/Things/Items/Item.cs b/AlientAttack.MonoGame/Things/Items/Item.cs index 2f6d00b..211cf26 100644 --- a/AlientAttack.MonoGame/Things/Items/Item.cs +++ b/AlientAttack.MonoGame/Things/Items/Item.cs @@ -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(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) diff --git a/AlientAttack.MonoGame/Things/MiniExplosion.cs b/AlientAttack.MonoGame/Things/MiniExplosion.cs index e187fa5..d47d787 100644 --- a/AlientAttack.MonoGame/Things/MiniExplosion.cs +++ b/AlientAttack.MonoGame/Things/MiniExplosion.cs @@ -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(@$"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) { diff --git a/AlientAttack.MonoGame/Things/Muzzles/MuzzleMath.cs b/AlientAttack.MonoGame/Things/Muzzles/MuzzleMath.cs index 488d47c..b970f77 100644 --- a/AlientAttack.MonoGame/Things/Muzzles/MuzzleMath.cs +++ b/AlientAttack.MonoGame/Things/Muzzles/MuzzleMath.cs @@ -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) diff --git a/AlientAttack.MonoGame/Things/Player.cs b/AlientAttack.MonoGame/Things/Player.cs index 44f3476..b9bb04a 100644 --- a/AlientAttack.MonoGame/Things/Player.cs +++ b/AlientAttack.MonoGame/Things/Player.cs @@ -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 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 Muzzles { get; } = + public IReadOnlyList 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(@$"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) diff --git a/AlientAttack.MonoGame/Things/Sprite.cs b/AlientAttack.MonoGame/Things/Sprite.cs index 2acc9fd..ac5812f 100644 --- a/AlientAttack.MonoGame/Things/Sprite.cs +++ b/AlientAttack.MonoGame/Things/Sprite.cs @@ -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) diff --git a/AlientAttack.MonoGame/Things/SpriteDrawArgs.cs b/AlientAttack.MonoGame/Things/SpriteDrawArgs.cs index 4afb710..29b8e81 100644 --- a/AlientAttack.MonoGame/Things/SpriteDrawArgs.cs +++ b/AlientAttack.MonoGame/Things/SpriteDrawArgs.cs @@ -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; } \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/SpriteUpdateContext.cs b/AlientAttack.MonoGame/Things/SpriteUpdateContext.cs index ef97140..c39490e 100644 --- a/AlientAttack.MonoGame/Things/SpriteUpdateContext.cs +++ b/AlientAttack.MonoGame/Things/SpriteUpdateContext.cs @@ -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)