Parallax-scroll the background by Y.

This commit is contained in:
Colin McMillen 2020-03-25 11:04:49 -04:00
parent fccca0c676
commit 4a598fb377
2 changed files with 7 additions and 4 deletions

View File

@ -11,6 +11,8 @@ namespace SemiColinGames {
public int Height { get => bbox.Height; } public int Height { get => bbox.Height; }
public int Left { get => bbox.Left; } public int Left { get => bbox.Left; }
public int Top { get => bbox.Top; } public int Top { get => bbox.Top; }
public int Bottom { get => bbox.Bottom; }
public Point HalfSize { get => new Point(Width / 2, Height / 2); } public Point HalfSize { get => new Point(Width / 2, Height / 2); }
private readonly Random random = new Random(); private readonly Random random = new Random();

View File

@ -74,13 +74,14 @@ namespace SemiColinGames {
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null); spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height); Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
float xScale = 1f / 16; float bgScale = 1f / 16;
for (int i = 0; i < Textures.Backgrounds.Length; i++) { for (int i = 0; i < Textures.Backgrounds.Length; i++) {
int yOffset = Textures.Backgrounds[i].Get.Height - camera.Height - 24; float yDiff = (world.Height - camera.Bottom) * bgScale;
float yOffset = Textures.Backgrounds[i].Get.Height - camera.Height - yDiff;
Rectangle bgSource = new Rectangle( Rectangle bgSource = new Rectangle(
(int) (camera.Left * xScale), yOffset, camera.Width, camera.Height); (int) (camera.Left * bgScale), (int) yOffset, camera.Width, camera.Height);
spriteBatch.Draw(Textures.Backgrounds[i].Get, bgTarget, bgSource, Color.White); spriteBatch.Draw(Textures.Backgrounds[i].Get, bgTarget, bgSource, Color.White);
xScale *= 2; bgScale *= 2;
} }
spriteBatch.End(); spriteBatch.End();