Skip to content

Instantly share code, notes, and snippets.

@edbartley
Created December 3, 2012 21:42
Show Gist options
  • Save edbartley/4198359 to your computer and use it in GitHub Desktop.
Save edbartley/4198359 to your computer and use it in GitHub Desktop.
A Parallax entity for AndEngine that works in both X and Y directions.
import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.entity.shape.IAreaShape;
import org.andengine.opengl.util.GLState;
public class FPGParallaxEntity extends ParallaxEntity
{
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
final float mParallaxXFactor;
final float mParallaxYFactor;
Boolean mParallaxXRepeat = false;
Boolean mParallaxYRepeat = false;
final IAreaShape mShape;
public float mParalaxX = 0;
public float mParalaxY = 0;
// ===========================================================
// Constructors
// ===========================================================
public FPGParallaxEntity(final float pParallaxXFactor, final float pParallaxYFactor,
Boolean pParallaxXRepeat, Boolean pParallaxYRepeat, final IAreaShape pShape)
{
super(pParallaxXFactor, pShape);
this.mParallaxXFactor = pParallaxXFactor;
this.mParallaxYFactor = pParallaxYFactor;
this.mParallaxXRepeat = pParallaxXRepeat;
this.mParallaxYRepeat = pParallaxYRepeat;
this.mShape = pShape;
}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
@Override
public void onDraw(final GLState pGL, final Camera pCamera, final float pParallaxValue)
{
pGL.pushModelViewGLMatrix();
{
final float cameraWidth = pCamera.getWidth();
final float cameraHeight = pCamera.getHeight();
final float shapeWidthScaled = this.mShape.getWidthScaled();
final float shapeHeightScaled = this.mShape.getHeightScaled();
float baseXOffset = (mParalaxX * this.mParallaxXFactor) % shapeWidthScaled;
float baseYOffset = (mParalaxY * this.mParallaxYFactor) % shapeHeightScaled;
while(baseXOffset > 0)
{
baseXOffset -= shapeWidthScaled;
}
while(baseYOffset > 0)
{
baseYOffset -= shapeHeightScaled;
}
pGL.translateModelViewGLMatrixf(baseXOffset, baseYOffset, 0);
float currentMaxX = baseXOffset;
float currentMaxY = baseYOffset;
if(mParallaxXRepeat && mParallaxYRepeat)
{
do
{
do
{
this.mShape.onDraw(pGL, pCamera);
pGL.translateModelViewGLMatrixf(0, shapeHeightScaled, 0);
currentMaxY += shapeHeightScaled;
}
while(currentMaxY < cameraHeight);
pGL.translateModelViewGLMatrixf(0, -(currentMaxY-baseYOffset), 0);
currentMaxY = baseYOffset;
pGL.translateModelViewGLMatrixf(shapeWidthScaled, 0, 0);
currentMaxX += shapeWidthScaled;
}
while(currentMaxX < cameraWidth);
}
else if(mParallaxXRepeat)
{
do
{
this.mShape.onDraw(pGL, pCamera);
pGL.translateModelViewGLMatrixf(shapeWidthScaled, 0, 0);
currentMaxX += shapeWidthScaled;
}
while(currentMaxX < cameraWidth);
}
else if(mParallaxYRepeat)
{
do
{
this.mShape.onDraw(pGL, pCamera);
pGL.translateModelViewGLMatrixf(0, shapeHeightScaled, 0);
currentMaxY += shapeHeightScaled;
}
while(currentMaxY < cameraHeight);
}
}
pGL.popModelViewGLMatrix();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment