Skip to content

Instantly share code, notes, and snippets.

@piedoom
Created October 30, 2016 06:50
Show Gist options
  • Select an option

  • Save piedoom/c3585b9f4e021466192a45e0ae9140b8 to your computer and use it in GitHub Desktop.

Select an option

Save piedoom/c3585b9f4e021466192a45e0ae9140b8 to your computer and use it in GitHub Desktop.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Nez;
using Nez.Sprites;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// Generic parallax starfield effect
/// </summary>
namespace ThreeThirty.Scenes.Effects
{
public class Starfield : Component, IUpdatable
{
TiledSprite background;
float scrollAmount;
public Starfield(float scroll)
{
scrollAmount = scroll;
}
public override void onAddedToEntity()
{
base.onAddedToEntity();
// load our sprite
background = new TiledSprite(Core.content.Load<Texture2D>("backgrounds/starfield"));
entity.addComponent(background);
entity.transform.localScale = new Vector2(scrollAmount, scrollAmount);
// tile and position it correctly
Core.emitter.addObserver(CoreEvents.GraphicsDeviceReset, onGraphicsDeviceReset);
onGraphicsDeviceReset();
}
/// <summary>
/// Reset dimensions when screen changes size
/// </summary>
private void onGraphicsDeviceReset()
{
background.width = Core.graphicsDevice.Viewport.Width;
background.height = Core.graphicsDevice.Viewport.Height;
background.origin = new Vector2(background.width / 2, background.height / 2);
}
/// <summary>
/// Parallax effect
/// </summary>
public void update()
{
background.scrollX = (int)(entity.transform.position.X * scrollAmount);
background.scrollY = (int)(entity.transform.position.Y * scrollAmount);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment