From db01dcea2868663084d27b60e95284b1ca9a6ebb Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Thu, 19 Nov 2020 17:55:27 -0500 Subject: [PATCH] generate Shots on user input & reap them once offscreen --- Shared/ShmupWorld.cs | 40 +++++++++++++++++++++++++--------------- Shared/SneakGame.cs | 2 +- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/Shared/ShmupWorld.cs b/Shared/ShmupWorld.cs index ab54732..ec6a2c4 100644 --- a/Shared/ShmupWorld.cs +++ b/Shared/ShmupWorld.cs @@ -16,26 +16,33 @@ namespace SemiColinGames { public class Shot { public TextureRef Texture = Textures.Projectile1; public Vector2 Position; - public Vector2 HalfSize = new Vector2(11, 8); + public Vector2 HalfSize = new Vector2(11, 4); + public Rectangle Bounds; public Vector2 Velocity; public Shot(Vector2 position, Vector2 velocity) { Position = position; Velocity = velocity; + Update(0); // set Bounds + } + + public void Update(float modelTime) { + Position = Vector2.Add(Position, Vector2.Multiply(Velocity, modelTime)); + Bounds = new Rectangle( + (int) (Position.X - HalfSize.X), + (int) (Position.Y - HalfSize.Y), + (int) HalfSize.X * 2, + (int) HalfSize.Y * 2); } } - public readonly Point Size; + public readonly Rectangle Bounds; public readonly ShmupPlayer Player; public readonly ProfilingList Shots; public ShmupWorld() { - Size = new Point(1920 / 4, 1080 / 4); + Bounds = new Rectangle(0, 0, 1920 / 4, 1080 / 4); Player = new ShmupPlayer(); Shots = new ProfilingList(100, "shots"); - Vector2 velocity = new Vector2(100, 0); - Shots.Add(new Shot(new Vector2(96, 1080 / 8), velocity)); - Shots.Add(new Shot(new Vector2(96 * 2, 1080 / 8), velocity)); - Shots.Add(new Shot(new Vector2(96 * 4, 1080 / 8), velocity)); } ~ShmupWorld() { @@ -50,19 +57,22 @@ namespace SemiColinGames { Vector2 motion = Vector2.Multiply(input[0].Motion, modelTime * Player.Speed); Player.Position = Vector2.Add(Player.Position, motion); Player.Position.X = Math.Max(Player.Position.X, Player.HalfSize.X); - Player.Position.X = Math.Min(Player.Position.X, Size.X - Player.HalfSize.X); + Player.Position.X = Math.Min(Player.Position.X, Bounds.Size.X - Player.HalfSize.X); Player.Position.Y = Math.Max(Player.Position.Y, Player.HalfSize.Y); - Player.Position.Y = Math.Min(Player.Position.Y, Size.Y - Player.HalfSize.Y); + Player.Position.Y = Math.Min(Player.Position.Y, Bounds.Size.Y - Player.HalfSize.Y); foreach (Shot shot in Shots) { - shot.Position = Vector2.Add(shot.Position, Vector2.Multiply(shot.Velocity, modelTime)); - shot.Position.X = Math.Max(shot.Position.X, shot.HalfSize.X); - shot.Position.X = Math.Min(shot.Position.X, Size.X - shot.HalfSize.X); - shot.Position.X = Math.Max(shot.Position.X, shot.HalfSize.X); - shot.Position.X = Math.Min(shot.Position.X, Size.X - shot.HalfSize.X); + shot.Update(modelTime); + } + + if (input[0].Attack && !input[1].Attack) { + Vector2 shotOffset = new Vector2(12, 2); + Vector2 shotPosition = Vector2.Add(Player.Position, shotOffset); + Shot shot = new Shot(shotPosition, new Vector2(300, 0)); + Shots.Add(shot); } - // Shots.RemoveAll(shot => shot.Position.X > Size.X); + Shots.RemoveAll(shot => !Bounds.Intersects(shot.Bounds)); } } } diff --git a/Shared/SneakGame.cs b/Shared/SneakGame.cs index b084c15..fe6edd0 100644 --- a/Shared/SneakGame.cs +++ b/Shared/SneakGame.cs @@ -70,7 +70,7 @@ namespace SemiColinGames { scene?.Dispose(); // scene = new SneakScene(GraphicsDevice, ((SneakWorld) world).Camera); // scene = new TreeScene(GraphicsDevice); - scene = new ShmupScene(GraphicsDevice, ((ShmupWorld) world).Size); + scene = new ShmupScene(GraphicsDevice, ((ShmupWorld) world).Bounds.Size); GC.Collect(); GC.WaitForPendingFinalizers();