From 123ea724d00a30a7b860d278b0c228e57ef8b6e3 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Mon, 8 Feb 2021 16:08:01 -0500 Subject: [PATCH] enemy now moves --- Shared/ShmupWorld.cs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Shared/ShmupWorld.cs b/Shared/ShmupWorld.cs index eba64f2..5997bca 100644 --- a/Shared/ShmupWorld.cs +++ b/Shared/ShmupWorld.cs @@ -82,14 +82,34 @@ namespace SemiColinGames { } } + public interface IMoveBehavior { + public Vector2 Velocity(float modelTime); + } + + public class MoveLeft : IMoveBehavior { + public Vector2 Velocity(float modelTime) { + return new Vector2(-100, 0); + } + } + public class Enemy { public TextureRef Texture = Textures.Blue1; // Center of sprite. public Vector2 Position = new Vector2(1920 / 4 - 48, 1080 / 8); // TODO: use a bounds rect instead of HalfSize. public Vector2 HalfSize = new Vector2(16, 10); + public Rectangle Bounds; + private IMoveBehavior moveBehavior = new MoveLeft(); public void Update(float modelTime) { + Vector2 velocity = moveBehavior.Velocity(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 void Draw(SpriteBatch spriteBatch) { @@ -142,8 +162,9 @@ namespace SemiColinGames { Rectangle paddedBounds = Bounds; paddedBounds.Inflate(16, 16); Shots.RemoveAll(shot => !paddedBounds.Intersects(shot.Bounds)); + Enemies.RemoveAll(enemy => !paddedBounds.Intersects(enemy.Bounds)); - Debug.AddToast("shots: " + Shots.Count); + Debug.AddToast("shots: " + Shots.Count + " enemies: " + Enemies.Count); } } }