Skip to content

Instantly share code, notes, and snippets.

@Qwark
Created September 19, 2010 19:00
Show Gist options
  • Save Qwark/587019 to your computer and use it in GitHub Desktop.
Save Qwark/587019 to your computer and use it in GitHub Desktop.
public class MainActivity extends BaseGameActivity{
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private TMXTiledMap mTMXTiledMap;
private BoundCamera mBoundChaseCamera;
private Texture mTexture;
private TextureRegion mPlayerTextureRegion;
private Sprite player;
private Texture mOnScreenControlTexture;
private TextureRegion mOnScreenControlBaseTextureRegion;
private TextureRegion mOnScreenControlKnobTextureRegion;
float rotationInRad;
@Override
public void onLoadComplete() {
// TODO Auto-generated method stub
}
@Override
public Engine onLoadEngine() {
this.mBoundChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mBoundChaseCamera));
}
@Override
public void onLoadResources() {
this.mTexture = new Texture(128, 128, TextureOptions.DEFAULT);
//this.mPlayerTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "car.png", 0, 0);
this.mPlayerTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/car.png", 0, 0); // 72x128
this.mOnScreenControlTexture = new Texture(256, 256, TextureOptions.BILINEAR);
this.mOnScreenControlBaseTextureRegion = TextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "gfx/onscreen_control_base.png", 0, 0);
this.mOnScreenControlKnobTextureRegion = TextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "gfx/onscreen_control_knob.png", 128, 0);
}
@Override
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
try {
final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine.getTextureManager(), TextureOptions.BILINEAR, new ITMXTilePropertiesListener() {
@Override
public void onTMXTileWithPropertiesCreated(final TMXTiledMap pTMXTiledMap, final TMXLayer pTMXLayer, final TMXTile pTMXTile, final TMXProperties<TMXTileProperty> pTMXTileProperties) {
}
});
this.mTMXTiledMap = tmxLoader.loadFromAsset(this, "tmx/testtrack.tmx");
} catch (final TMXLoadException tmxle) {
Debug.e(tmxle);
}
final Scene scene = new Scene(2);
final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
scene.getBottomLayer().addEntity(tmxLayer);
this.mBoundChaseCamera.setBounds(0, tmxLayer.getWidth(), 0, tmxLayer.getHeight());
this.mBoundChaseCamera.setBoundsEnabled(true);
/* Calculate the coordinates for the face, so its centered on the camera. */
final int centerX = (CAMERA_WIDTH / 2);
final int centerY = (CAMERA_HEIGHT / 2);
/* Create the sprite and add it to the scene. */
player = new Sprite(centerX, centerY, this.mPlayerTextureRegion);
this.mBoundChaseCamera.setChaseShape(player);
scene.getTopLayer().addEntity(player);
this.initOnScreenControls(scene);
return scene;
}
private void initOnScreenControls(final Scene pScene) {
final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mBoundChaseCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, new IAnalogOnScreenControlListener() {
private Vector2 mVelocityTemp = new Vector2();
@Override
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
player.setAcceleration(pValueY);
rotationInRad += (float)Math.atan2(-pValueX, pValueY);
player.setRotation(rotationInRad);
/*this.mVelocityTemp.set(pValueX * 5, pValueY * 5);
final Body carBody = RacerGameActivity.this.mCarBody;
carBody.setLinearVelocity(this.mVelocityTemp);
final float rotationInRad = (float)Math.atan2(-pValueX, pValueY);
carBody.setTransform(carBody.getWorldCenter(), rotationInRad);
RacerGameActivity.this.mCar.setRotation(MathUtils.radToDeg(rotationInRad));*/
}
@Override
public void onControlClick(AnalogOnScreenControl pAnalogOnScreenControl) {
/* Nothing. */
}
});
analogOnScreenControl.getControlBase().setAlpha(0.5f);
analogOnScreenControl.getControlBase().setScaleCenter(0, 128);
analogOnScreenControl.getControlBase().setScale(0.75f);
analogOnScreenControl.getControlKnob().setScale(0.75f);
analogOnScreenControl.refreshControlKnobPosition();
pScene.setChildScene(analogOnScreenControl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment