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.jmd.asteroidshooter.entities; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Body; import com.jmd.asteroidshooter.screens.AsteroidShooter; import com.jmd.asteroidshooter.handlers.Vars; import static com.jmd.asteroidshooter.handlers.Vars.PPM; /** * * @author jared */ public class Player extends Sprite { protected Body body; protected Sprite image; private AsteroidShooter game; public short colorMask = Vars.RED_BIT; private float gravity_scale; private Vector2 velocity; private final float maxVelocityX = 10f; private final float maxVelocityY = 10f; public Player(AsteroidShooter game) { super(new Sprite(new Texture("res/images/spaceship.png"))); this.velocity = new Vector2(); this.game = game; } // public Vector2 setVelocity(float x, float y) { // this.velocity.x += Math.min(x, maxVelocityX); // this.velocity.y += Math.min(y, maxVelocityY); // return this.velocity; // } // // public Vector2 alterVelocity(float x, float y) { // this.velocity.x += x; // this.velocity.y += y; // return this.velocity; // } // // public Vector2 setVelocity(Vector2 _velocity) { // this.velocity = _velocity; // return this.velocity; // } // public void applyForceToCenter(Vector2 _velocity) { this.body.applyForceToCenter(_velocity, true); } public void update() { // set sprite position based on physics body position this.setPosition(body.getPosition().x * Vars.PPM, body.getPosition().y * Vars.PPM); //check for bounds //scale gravity based on distance from bottom of screen if (!game.finished) { gravity_scale = (game.maxY - (body.getPosition().y * Vars.PPM)) / game.maxY * .6f; } body.setGravityScale(gravity_scale); } public void render(SpriteBatch sb) { sb.begin(); draw(sb); sb.end(); } public Body getBody() { return body; } /** * * @return the position vector of the physics body */ public Vector2 getPosition() { return body.getPosition(); } /** * * @return the position vector of the sprite */ public Vector2 getScaledPosition() { Vector2 pos = new Vector2(); pos.x = body.getPosition().x * PPM; pos.y = body.getPosition().y * PPM; return pos; } public String toString() { return "Player body at: [" + body.getPosition().x * PPM + ", " + body.getPosition().y * PPM + "]" + body.getLinearVelocity(); } public void setBody(Body body) { this.body = body; setX(body.getPosition().x / PPM); setY(body.getPosition().y / PPM); } public void toggleColor() { if (colorMask == Vars.RED_BIT) { colorMask = Vars.BLUE_BIT; } else if (colorMask == Vars.BLUE_BIT) { colorMask = Vars.GREEN_BIT; } else if (colorMask == Vars.GREEN_BIT) { colorMask = Vars.RED_BIT; } } public void setGravityScale(float gravityScale) { gravity_scale = gravityScale; } }