Build a Space Shooter with MonoGame – 5

May 20, 2019 5:56 pm Published by Leave your thoughts

The last two classes we’ll add will be devoted to the scrolling background system.  Create a new class, and name it ScrollingBackground.cs.  This class does not need to inherit anything.  It will still need the two usual using statements.  We will want to add two fields containing background textures and layers:

private List<Texture2D> textures = new List<Texture2D>();
private List<ScrollingBackgroundLayer> layers = new List<ScrollingBackgroundLayer>();

The step is to add the constructor.  We will be creating a vertical “stack” of three backgrounds, but we’ll be creating two more layers of backgrounds on top.  This will allow us to have a buffer of backgrounds that will display as the layers before move off screen. This is how we can scroll the backgrounds.  Let’s add the constructor:

public ScrollingBackground(List<Texture2D> textures)
{
	this.textures = textures;

	for (int i = -1; i < 2; i++) // position indexes
	{
    	for (int j = 0; j < 3; j++) { // 3 layers
        	Texture2D texture = textures[Game1.randInt(0, textures.Count - 1)];
        	Vector2 position = new Vector2(0, texture.Height * i);
        	Vector2 velocity = new Vector2(0, (j + 1) * 0.2f);
        	ScrollingBackgroundLayer layer = new ScrollingBackgroundLayer(this, texture, j, i, position, velocity);
        	layers.Add(layer);
    	}
	}
}

It’s time to add the more complicated part.  Let’s first define our Update method:

public void Update(GameTime gameTime) {


}

In the Update method, the first thing we’ll want to do is sort each background by depth.  We will then put the layer in the list of it’s corresponding depth. Add the following to start our Update method:

List<ScrollingBackgroundLayer> layersDepth0 = new List<ScrollingBackgroundLayer>();
List<ScrollingBackgroundLayer> layersDepth1 = new List<ScrollingBackgroundLayer>();
List<ScrollingBackgroundLayer> layersDepth2 = new List<ScrollingBackgroundLayer>();
List<ScrollingBackgroundLayer> layersToReset = new List<ScrollingBackgroundLayer>();
       	 
for (int i = 0; i < layers.Count; i++)
{
	layers[i].Update(gameTime);

	switch (layers[i].depth)
	{
    	case 0:
        	{
            	layersDepth0.Add(layers[i]);
            	break;
        	}

    	case 1:
        	{
            	layersDepth1.Add(layers[i]);
            	break;
        	}

    	case 2:
        	{
            	layersDepth2.Add(layers[i]);
            	break;
        	}
	}
}

Next, we will have to iterate through each depth list and check if the first background is more than Y coordinate zero.  We don’t want to run out of backgrounds so we will reset the position of each layer in the corresponding depth. Let’s add some more code to the Update function:

bool resetLayersDepth0 = false;
bool resetLayersDepth1 = false;
bool resetLayersDepth2 = false;

// Loop through layers in depth 0
for (int i = 0; i < layersDepth0.Count; i++)
{
	if (layersDepth0[i].positionIndex == -1)
	{
    	if (layersDepth0[i].position.Y > 0)
    	{
        		resetLayersDepth0 = true;
    	}
	}
}

// Loop through layers in depth 1
for (int i = 0; i < layersDepth1.Count; i++)
{
	if (layersDepth1[i].positionIndex == -1)
	{
    	if (layersDepth1[i].position.Y > 0)
    	{
        	resetLayersDepth1 = true;
    	}
	}
}

// Loop through layers in depth 2
for (int i = 0; i < layersDepth2.Count; i++)
{
	if (layersDepth2[i].positionIndex == -1)
	{
    	if (layersDepth2[i].position.Y > 0)
    	{
        	resetLayersDepth2 = true;
    	}
	}
}

if (resetLayersDepth0)
{
	for (int i = 0; i < layersDepth0.Count; i++)
	{
    		layersDepth0[i].position = layersDepth0[i].initialPosition;
	}
}

if (resetLayersDepth1)
{
	for (int i = 0; i < layersDepth1.Count; i++)
	{
    		layersDepth1[i].position = layersDepth1[i].initialPosition;
	}
}

if (resetLayersDepth2)
{
	for (int i = 0; i < layersDepth2.Count; i++)
	{
    		layersDepth2[i].position = layersDepth2[i].initialPosition;
	}
}

Last, we will add the Draw function, which will call the Draw method of each layer:

public void Draw(SpriteBatch spriteBatch)
{
	for (int i = 0; i < layers.Count; i++)
	{
    		layers[i].Draw(spriteBatch);
	}
}

The last class we need to add will represent a scrolling background layer.  Create a new class and call it ScrollingBackgroundLayer.cs.  This class will need to extend Entity.  Make sure to add the two usual using statements. We will also have to add several fields:

private ScrollingBackground scrollingBackground;
private Texture2D texture;
public Texture2D getTexture() { return texture; }
public int depth = 0;
public int positionIndex = 0;
public Vector2 initialPosition;

Let’s add the following constructor:

public ScrollingBackgroundLayer(ScrollingBackground scrollingBackground, Texture2D texture, int depth, int positionIndex, Vector2 position, Vector2 velocity) : base()
{
	this.scrollingBackground = scrollingBackground;
	this.texture = texture;
	this.depth = depth;
	this.positionIndex = positionIndex;
	this.position = position;
	initialPosition = this.position;
	body.velocity = velocity;
}

Then we will need to add the usual Update method:

public new void Update(GameTime gameTime)
{
	base.Update(gameTime);
}

Finally, we’ll add the Draw method, which will draw the background layer when called:

public void Draw(SpriteBatch spriteBatch)
{
	spriteBatch.Draw(texture, position, Color.White);
}

Let’s move on to part six where we finish up our game.

Tags: , , , , , , ,

Categorised in: ,

This post was written by Jared York

Leave a Reply

Your email address will not be published. Required fields are marked *