pass in a newShots list instead of returning one

This commit is contained in:
Colin McMillen 2020-11-30 17:20:14 -05:00
parent 9c289bd132
commit 95d24a63ec

View File

@ -14,10 +14,9 @@ namespace SemiColinGames {
private float speed = 150f;
private float shotCooldown = 0f;
private ProfilingList<Shot> shots = new ProfilingList<Shot>(10, "playerShots");
public ProfilingList<Shot> Update(
float modelTime, History<Input> input, Rectangle worldBounds) {
public void Update(float modelTime, History<Input> input, Rectangle worldBounds,
ProfilingList<Shot> newShots) {
// Movement update.
Vector2 motion = Vector2.Multiply(input[0].Motion, modelTime * speed);
Position = Vector2.Add(Position, motion);
@ -28,14 +27,12 @@ namespace SemiColinGames {
// Check whether we need to add new shots.
shotCooldown -= modelTime;
shots.Clear();
if (input[0].Attack && shotCooldown <= 0) {
shotCooldown = 0.2f;
Vector2 shotOffset = new Vector2(12, 2);
Vector2 shotPosition = Vector2.Add(Position, shotOffset);
shots.Add(new Shot(shotPosition, new Vector2(300, 0)));
newShots.Add(new Shot(shotPosition, new Vector2(300, 0)));
}
return shots;
}
public void Draw(SpriteBatch spriteBatch) {
@ -108,6 +105,7 @@ namespace SemiColinGames {
public readonly ShmupPlayer Player;
public readonly ProfilingList<Enemy> Enemies;
public readonly ProfilingList<Shot> Shots;
private ProfilingList<Shot> newShots;
public ShmupWorld() {
Bounds = new Rectangle(0, 0, 1920 / 4, 1080 / 4);
@ -115,6 +113,7 @@ namespace SemiColinGames {
Enemies = new ProfilingList<Enemy>(100, "enemies");
Enemies.Add(new Enemy());
Shots = new ProfilingList<Shot>(100, "shots");
newShots = new ProfilingList<Shot>(100, "newShots");
}
~ShmupWorld() {
@ -127,7 +126,8 @@ namespace SemiColinGames {
public void Update(float modelTime, History<Input> input) {
// Update player, enemies, & shots.
ProfilingList<Shot> newPlayerShots = Player.Update(modelTime, input, Bounds);
newShots.Clear();
Player.Update(modelTime, input, Bounds, newShots);
foreach (Enemy enemy in Enemies) {
enemy.Update(modelTime);
}
@ -136,7 +136,7 @@ namespace SemiColinGames {
}
// Add new shots.
Shots.AddRange(newPlayerShots);
Shots.AddRange(newShots);
// Reap off-screen objects.
Rectangle paddedBounds = Bounds;