Back to project page RubeLoader.
The source code is released under:
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUC...
If you think the Android project RubeLoader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.gushikustudios.rube; /* w w w . jav a 2 s . c o m*/ import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Body; /** * Simple class for spatial (image) rendering. If a body reference is included, it's position will serve as the local coordinate system. * * @author tescott * */ public class SimpleSpatial { private Sprite mSprite; private Body mBody; private final Vector2 mCenter = new Vector2(); private final Vector2 mHalfSize = new Vector2(); private float mRotation; private static final Vector2 mTmp = new Vector2(); public SimpleSpatial(Texture texture, boolean flip, Body body, Color color, Vector2 size, Vector2 center, float rotationInDegrees) { mSprite = new Sprite(texture); defineSpatial(flip,body,color,size,center,rotationInDegrees); } public SimpleSpatial(TextureRegion region, boolean flip, Body body, Color color, Vector2 size, Vector2 center, float rotationInDegrees) { mSprite = new Sprite(region); defineSpatial(flip,body,color,size,center,rotationInDegrees); } public void defineSpatial(boolean flip, Body body, Color color, Vector2 size, Vector2 center, float rotationInDegrees) { mBody = body; mSprite.flip(flip, false); mSprite.setColor(color); mRotation = rotationInDegrees; mSprite.setSize(size.x, size.y); mSprite.setOrigin(size.x / 2, size.y / 2); mHalfSize.set(size.x / 2, size.y / 2); mCenter.set(center); if (body != null) { mTmp.set(body.getPosition()); mSprite.setPosition(mTmp.x - size.x / 2, mTmp.y - size.y / 2); float angle = mBody.getAngle() * MathUtils.radiansToDegrees; mTmp.set(mCenter).rotate(angle).add(mBody.getPosition()) .sub(mHalfSize); mSprite.setRotation(mRotation + angle); } else { mTmp.set(center.x - size.x / 2, center.y - size.y / 2); mSprite.setRotation(rotationInDegrees); } mSprite.setPosition(mTmp.x, mTmp.y); } public void render(SpriteBatch batch, float delta) { // if this is a dynamic spatial... if (mBody != null) { // use body information to render it... float angle = mBody.getAngle() * MathUtils.radiansToDegrees; mTmp.set(mCenter).rotate(angle).add(mBody.getPosition()) .sub(mHalfSize); mSprite.setPosition(mTmp.x, mTmp.y); mSprite.setRotation(mRotation + angle); mSprite.draw(batch); } else { // else just draw it wherever it was defined at initialization mSprite.draw(batch); } } }