using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; namespace SemiColinGames { public sealed class ShmupWorld : IWorld { public class ShmupPlayer { public TextureRef Texture = Textures.Yellow2; // Center of player sprite. public Vector2 Position = new Vector2(48, 1080 / 8); public Vector2 HalfSize = new Vector2(16, 10); public float Speed = 150f; } public class Shot { static int color = 0; public TextureRef Texture; public Vector2 Position; public Vector2 HalfSize = new Vector2(11, 4); public Rectangle Bounds; public Vector2 Velocity; public Shot(Vector2 position, Vector2 velocity) { Texture = (color % 5) switch { 0 => Textures.Projectile1, 1 => Textures.Projectile2, 2 => Textures.Projectile3, 3 => Textures.Projectile4, _ => Textures.Projectile5 }; color++; 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 Rectangle Bounds; public readonly ShmupPlayer Player; public readonly ProfilingList Shots; public ShmupWorld() { Bounds = new Rectangle(0, 0, 1920 / 4, 1080 / 4); Player = new ShmupPlayer(); Shots = new ProfilingList(100, "shots"); } ~ShmupWorld() { Dispose(); } public void Dispose() { GC.SuppressFinalize(this); } public void Update(float modelTime, History input) { 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, 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, Bounds.Size.Y - Player.HalfSize.Y); foreach (Shot shot in Shots) { shot.Update(modelTime); } if (input[0].Attack && !input[1].Attack) { Vector2 shotOffset = new Vector2(12, 2); Vector2 shotPosition = Vector2.Add(Player.Position, shotOffset); Shots.Add(new Shot(shotPosition, new Vector2(300, 40))); Shots.Add(new Shot(shotPosition, new Vector2(300, 20))); Shots.Add(new Shot(shotPosition, new Vector2(300, 0))); Shots.Add(new Shot(shotPosition, new Vector2(300, -20))); Shots.Add(new Shot(shotPosition, new Vector2(300, -40))); new Shot(shotPosition, Vector2.Zero); } Shots.RemoveAll(shot => !Bounds.Intersects(shot.Bounds)); } } }