Browse Source

Parallax-scroll the background by Y.

master
Colin McMillen 4 years ago
parent
commit
4a598fb377
  1. 2
      Shared/Camera.cs
  2. 9
      Shared/Scene.cs

2
Shared/Camera.cs

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

9
Shared/Scene.cs

@ -74,13 +74,14 @@ namespace SemiColinGames {
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
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++) {
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(
(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);
xScale *= 2;
bgScale *= 2;
}
spriteBatch.End();

Loading…
Cancel
Save