From fcb4f1cf05a8391f3f419f5e836d1dbc541db433 Mon Sep 17 00:00:00 2001 From: Brian Bicknell Date: Tue, 6 Jan 2026 22:22:03 -0500 Subject: [PATCH] Added basic turret logic. --- AlientAttack.MonoGame/Content/Content.mgcb | 12 +-- AlientAttack.MonoGame/GameLoops/GameLoop.cs | 25 +++++ .../Things/Enemies/Turrets/GreenTurret.cs | 6 ++ .../Things/Enemies/Turrets/OrangeTurret.cs | 6 ++ .../Things/Enemies/Turrets/RedTurret.cs | 6 ++ .../Things/Enemies/Turrets/TealTurret.cs | 6 ++ .../Things/Enemies/Turrets/Turret.cs | 98 +++++++++++++++++++ 7 files changed, 153 insertions(+), 6 deletions(-) create mode 100644 AlientAttack.MonoGame/Things/Enemies/Turrets/GreenTurret.cs create mode 100644 AlientAttack.MonoGame/Things/Enemies/Turrets/OrangeTurret.cs create mode 100644 AlientAttack.MonoGame/Things/Enemies/Turrets/RedTurret.cs create mode 100644 AlientAttack.MonoGame/Things/Enemies/Turrets/TealTurret.cs create mode 100644 AlientAttack.MonoGame/Things/Enemies/Turrets/Turret.cs diff --git a/AlientAttack.MonoGame/Content/Content.mgcb b/AlientAttack.MonoGame/Content/Content.mgcb index 5aea3ad..8bf4506 100644 --- a/AlientAttack.MonoGame/Content/Content.mgcb +++ b/AlientAttack.MonoGame/Content/Content.mgcb @@ -1997,7 +1997,7 @@ /processorParam:ResizeToPowerOfTwo=False /processorParam:MakeSquare=False /processorParam:TextureFormat=Color -/build:Sprites/GunTurret_ExampleGun_png_processed.png +/build:Sprites/GunTurret_ExampleGun_png_processed.png;Sprites/GunTurret_ExampleGun.png #begin Sprites/GunTurret_Green_png_processed.png /importer:TextureImporter @@ -2009,7 +2009,7 @@ /processorParam:ResizeToPowerOfTwo=False /processorParam:MakeSquare=False /processorParam:TextureFormat=Color -/build:Sprites/GunTurret_Green_png_processed.png +/build:Sprites/GunTurret_Green_png_processed.png;Sprites/GunTurret_Green.png #begin Sprites/GunTurret_Orange_png_processed.png /importer:TextureImporter @@ -2021,7 +2021,7 @@ /processorParam:ResizeToPowerOfTwo=False /processorParam:MakeSquare=False /processorParam:TextureFormat=Color -/build:Sprites/GunTurret_Orange_png_processed.png +/build:Sprites/GunTurret_Orange_png_processed.png;Sprites/GunTurret_Orange.png #begin Sprites/GunTurret_Red_png_processed.png /importer:TextureImporter @@ -2033,7 +2033,7 @@ /processorParam:ResizeToPowerOfTwo=False /processorParam:MakeSquare=False /processorParam:TextureFormat=Color -/build:Sprites/GunTurret_Red_png_processed.png +/build:Sprites/GunTurret_Red_png_processed.png;Sprites/GunTurret_Red.png #begin Sprites/GunTurret_Teal_png_processed.png /importer:TextureImporter @@ -2045,7 +2045,7 @@ /processorParam:ResizeToPowerOfTwo=False /processorParam:MakeSquare=False /processorParam:TextureFormat=Color -/build:Sprites/GunTurret_Teal_png_processed.png +/build:Sprites/GunTurret_Teal_png_processed.png;Sprites/GunTurret_Teal.png #begin Sprites/GunTurretMount_png_processed.png /importer:TextureImporter @@ -2057,7 +2057,7 @@ /processorParam:ResizeToPowerOfTwo=False /processorParam:MakeSquare=False /processorParam:TextureFormat=Color -/build:Sprites/GunTurretMount_png_processed.png +/build:Sprites/GunTurretMount_png_processed.png;Sprites/GunTurretMount.png #begin Sprites/Laser_Large_png_processed.png /importer:TextureImporter diff --git a/AlientAttack.MonoGame/GameLoops/GameLoop.cs b/AlientAttack.MonoGame/GameLoops/GameLoop.cs index ce7177d..0881525 100644 --- a/AlientAttack.MonoGame/GameLoops/GameLoop.cs +++ b/AlientAttack.MonoGame/GameLoops/GameLoop.cs @@ -1,6 +1,7 @@ using AlienAttack.MonoGame.Things; using AlienAttack.MonoGame.Things.Enemies; using AlienAttack.MonoGame.Things.Enemies.Mines; +using AlienAttack.MonoGame.Things.Enemies.Turrets; using AlienAttack.MonoGame.Things.Stars; using AlienAttack.MonoGame.View; using Microsoft.Xna.Framework; @@ -381,6 +382,30 @@ internal class GameLoop : GameLoopBase Sprites.Add(enemy); _spawnNewEnemyThreshold = 100 + _random.Next(0, 100); } + else if (randomNumber == 9) + { + GreenTurret enemy = new(_random.Next(0, ViewTransform.ScreenWidth - Turret.MountWidth), -Turret.MountHeight); + Sprites.Add(enemy); + _spawnNewEnemyThreshold = 100 + _random.Next(0, 100); + } + else if (randomNumber == 10) + { + RedTurret enemy = new(_random.Next(0, ViewTransform.ScreenWidth - Turret.MountWidth), -Turret.MountHeight); + Sprites.Add(enemy); + _spawnNewEnemyThreshold = 100 + _random.Next(0, 100); + } + else if (randomNumber == 11) + { + OrangeTurret enemy = new(_random.Next(0, ViewTransform.ScreenWidth - Turret.MountWidth), -Turret.MountHeight); + Sprites.Add(enemy); + _spawnNewEnemyThreshold = 100 + _random.Next(0, 100); + } + else if (randomNumber == 12) + { + TealTurret enemy = new(_random.Next(0, ViewTransform.ScreenWidth - Turret.MountWidth), -Turret.MountHeight); + Sprites.Add(enemy); + _spawnNewEnemyThreshold = 100 + _random.Next(0, 100); + } } } } diff --git a/AlientAttack.MonoGame/Things/Enemies/Turrets/GreenTurret.cs b/AlientAttack.MonoGame/Things/Enemies/Turrets/GreenTurret.cs new file mode 100644 index 0000000..e5ace1c --- /dev/null +++ b/AlientAttack.MonoGame/Things/Enemies/Turrets/GreenTurret.cs @@ -0,0 +1,6 @@ +namespace AlienAttack.MonoGame.Things.Enemies.Turrets; + +public class GreenTurret(int x, int y) : Turret(x, y) +{ + protected override string TurretColor => "Green"; +} \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Enemies/Turrets/OrangeTurret.cs b/AlientAttack.MonoGame/Things/Enemies/Turrets/OrangeTurret.cs new file mode 100644 index 0000000..7ed3dc6 --- /dev/null +++ b/AlientAttack.MonoGame/Things/Enemies/Turrets/OrangeTurret.cs @@ -0,0 +1,6 @@ +namespace AlienAttack.MonoGame.Things.Enemies.Turrets; + +public class OrangeTurret(int x, int y) : Turret(x, y) +{ + protected override string TurretColor => "Orange"; +} \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Enemies/Turrets/RedTurret.cs b/AlientAttack.MonoGame/Things/Enemies/Turrets/RedTurret.cs new file mode 100644 index 0000000..cd90d97 --- /dev/null +++ b/AlientAttack.MonoGame/Things/Enemies/Turrets/RedTurret.cs @@ -0,0 +1,6 @@ +namespace AlienAttack.MonoGame.Things.Enemies.Turrets; + +public class RedTurret(int x, int y) : Turret(x, y) +{ + protected override string TurretColor => "Red"; +} \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Enemies/Turrets/TealTurret.cs b/AlientAttack.MonoGame/Things/Enemies/Turrets/TealTurret.cs new file mode 100644 index 0000000..1ad3fa3 --- /dev/null +++ b/AlientAttack.MonoGame/Things/Enemies/Turrets/TealTurret.cs @@ -0,0 +1,6 @@ +namespace AlienAttack.MonoGame.Things.Enemies.Turrets; + +public class TealTurret(int x, int y) : Turret(x, y) +{ + protected override string TurretColor => "Teal"; +} \ No newline at end of file diff --git a/AlientAttack.MonoGame/Things/Enemies/Turrets/Turret.cs b/AlientAttack.MonoGame/Things/Enemies/Turrets/Turret.cs new file mode 100644 index 0000000..2ad7fb5 --- /dev/null +++ b/AlientAttack.MonoGame/Things/Enemies/Turrets/Turret.cs @@ -0,0 +1,98 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace AlienAttack.MonoGame.Things.Enemies.Turrets; + +public abstract class Turret : MoveableSprite +{ + public const int MountWidth = 63; + public const int MountHeight = 63; + + public const int TurretWidth = 38; + public const int TurretHeight = 33; + + public const int GunWidth = 8; + public const int GunHeight = 38; + + protected float Rotation = 0; + + protected Vector2 MountOrigin = new(MountWidth / 2, MountHeight /2); + protected Vector2 TurretOrigin = new(TurretWidth / 2, TurretHeight / 2); + protected Vector2 GunOrigin = new(GunWidth / 2, GunHeight / 2); + + protected abstract string TurretColor { get; } + + public Turret(int x, int y) : base(x, y) + { + BoundBox = new(10, 10, 44, 44); // TODO + YVelocity = 1; + XVelocity = 0; + } + + public override void Draw(SpriteDrawArgs args) + { + DrawMount(args); + DrawTurret(args); + DrawGun(args); + //DrawCollisionBox(args); + } + + private void DrawMount(SpriteDrawArgs args) + { + Texture2D texture = args.Content.Load("Sprites/GunTurretMount"); + args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, MountOrigin, 1f, SpriteEffects.None, 1); + } + + private void DrawTurret(SpriteDrawArgs args) + { + Texture2D texture = args.Content.Load($"Sprites/GunTurret_{TurretColor}"); + args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, TurretOrigin, 1f, SpriteEffects.None, 1); + } + + private void DrawGun(SpriteDrawArgs args) + { + Texture2D texture = args.Content.Load("Sprites/GunTurret_ExampleGun"); + args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, GunOrigin, 1f, SpriteEffects.None, 1); + } + + 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 override sealed void Update(SpriteUpdateContext context) + { + base.Update(context); + + CollisionBox = new Rectangle((int)XPosition + BoundBox.X - (int)MountOrigin.X, (int)YPosition + BoundBox.Y - (int)MountOrigin.Y, BoundBox.Width, BoundBox.Height); + + if (YPosition - MountOrigin.Y > context.ViewTransform.ScreenHeight) + { + IsDead = true; + return; + } + + Rotation += 0.01f; + + if (Rotation > 360f) + { + Rotation = 0; + } + } +} \ No newline at end of file