Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.github.skittishSloth.openSkies.battles.ships; import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Disposable; import com.github.skittishSloth.openSkies.battles.weapons.Laser; /** * * @author mcory01 */ public class PlayerShip implements Disposable { private static final float FRAME_RATE = 1 / 15f; private static final float MAX_VELOCITY = 400f; private static final float ACCEL_RATE = 5f; public PlayerShip(final String packFilePath) { atlas = new TextureAtlas(packFilePath); final TextureAtlas.AtlasRegion baseRegion = atlas.findRegion("base"); baseAnimation = new Animation(FRAME_RATE, baseRegion); final Array<TextureAtlas.AtlasRegion> bankLeftRegion = atlas.findRegions("left"); bankLeft = new Animation(FRAME_RATE, bankLeftRegion); final Array<TextureAtlas.AtlasRegion> bankRightRegion = atlas.findRegions("right"); bankRight = new Animation(FRAME_RATE, bankRightRegion); } public void render(final SpriteBatch batch, final float delta) { final TextureRegion curFrame = getCurrentFrame(delta); final int width = curFrame.getRegionWidth(); final int height = curFrame.getRegionHeight(); final float originX = width / 2; final float originY = height / 2; final Vector2 origin = new Vector2(originX, originY); origin.add(position); final Vector2 lo = laserOffset.cpy(); lo.setAngle(rotation + 90); lo.add(origin); laserPosition.set(lo); batch.draw(curFrame, position.x, position.y, originX, originY, width, height, 1.0f, 1.0f, getRotation()); } public TextureRegion getCurrentFrame(final float deltaTime) { final Animation curAnim; switch (bankState) { case NONE: curAnim = baseAnimation; break; case LEFT: curAnim = bankLeft; break; case RIGHT: curAnim = bankRight; break; default: throw new UnsupportedOperationException("Unknown state: " + bankState); } if (movementTime > curAnim.getAnimationDuration()) { movementTime = curAnim.getAnimationDuration(); } return curAnim.getKeyFrame(movementTime); } public void setBankState(final BankState bankState) { this.bankState = bankState; movementTime = 0f; } public float getRotation() { return rotation; } public void rotateLeft(final float delta) { this.rotation -= (200 * delta); } public void rotateRight(final float delta) { this.rotation += (200 * delta); } public void accelerateForward(final float delta) { if (velocity.y >= MAX_VELOCITY) { velocity.y = MAX_VELOCITY; } else { velocity.y += ACCEL_RATE * delta; } } public void moveForward(final float delta) { velocity.setAngle(rotation + 90); final Vector2 dv = velocity.cpy(); dv.scl(delta); position.add(dv); } public void moveBackward(final float delta) { velocity.setAngle(rotation + 90); final Vector2 dv = velocity.cpy(); dv.scl(delta); position.sub(dv); } public void bankLeft(final float delta) { bankVelocity.setAngle(rotation); final Vector2 dv = bankVelocity.cpy(); dv.scl(delta); position.sub(dv); } public void bankRight(final float delta) { bankVelocity.setAngle(rotation); final Vector2 dv = bankVelocity.cpy(); dv.scl(delta); position.add(dv); } public Laser fireLaser() { lastFireTime = System.currentTimeMillis(); final Laser laser = new Laser(rotation, laserPosition); return laser; } public boolean canFire() { return ((System.currentTimeMillis() - lastFireTime) > getLaserCoolDownTime()); } @Override public void dispose() { atlas.dispose(); } protected float getLaserCoolDownTime() { return 400f; } private final Vector2 position = new Vector2(); private final Vector2 laserPosition = new Vector2(); private final Vector2 laserOffset = new Vector2(0, 32); private final Vector2 velocity = new Vector2(0, 200); private final Vector2 bankVelocity = new Vector2(200, 0); private float movementTime = 0f; private float rotation = 0f; private long lastFireTime; private BankState bankState = BankState.NONE; private final TextureAtlas atlas; private final Animation baseAnimation; private final Animation bankLeft; private final Animation bankRight; }