Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.badlogic.gdx.ApplicationAdapter;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.InputAdapter;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- 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.math.Vector3;
- public class SBGame extends ApplicationAdapter {
- Vector3 temp = new Vector3();
- OrthographicCamera camera;
- SpriteBatch batch;
- Texture texture, textureTwo;
- Sprite sprite, spriteTwo, spriteThree;
- Ball ball = new Ball(100, 100);
- Enemy enemy = new Enemy(150, 150);
- @Override
- public void create () {
- camera = new OrthographicCamera();
- camera.setToOrtho(false);
- batch = new SpriteBatch();
- texture = new Texture(Gdx.files.internal("ball.png"));
- textureTwo = new Texture(Gdx.files.internal("enemy.png"));
- sprite = new Sprite(texture);
- spriteTwo = new Sprite(textureTwo);
- Gdx.input.setInputProcessor(new InputAdapter() {
- @Override
- public boolean touchDown (int screenX, int screenY, int pointer, int button) {
- camera.unproject(temp.set(screenX, screenY, 0));
- ball.moveTo(temp.x, temp.y);
- return true;
- }
- });
- }
- @Override
- public void render () {
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- ball.update(Gdx.graphics.getDeltaTime());
- enemy.update(Gdx.graphics.getDeltaTime());
- sprite.setPosition(ball.position.x, ball.position.y);
- sprite.setSize(ball.getHeight(), ball.getWidth());
- spriteTwo.setPosition(enemy.positionTwo.x, enemy.positionTwo.y);
- spriteTwo.setSize(enemy.getHeight(), enemy.getWidth());
- batch.begin();
- sprite.draw(batch);
- spriteTwo.draw(batch);
- batch.end();
- }
- @Override
- public void dispose () {
- texture.dispose();
- textureTwo.dispose();
- batch.dispose();
- }
- }
- class Ball {
- Vector2 position = new Vector2();
- Vector2 velocity = new Vector2();
- Vector2 movement = new Vector2();
- Vector2 target = new Vector2();
- Vector2 dir = new Vector2();
- private float width;
- private float height;
- float speed = 100;
- public Ball (float x, float y) {
- position.set(x, y);
- target.set(x,y);
- this.width = 10;
- this.height = 10;
- }
- public float getWidth() {
- return width;
- }
- public float getHeight() {
- return height;
- }
- public void update (float deltaTime) {
- movement.set(velocity).scl(deltaTime);
- if (position.dst2(target) > movement.len2()) {
- position.add(movement);
- } else {
- position.set(target);
- }
- }
- public void moveTo (float x, float y) {
- dir.set(target.set(x,y)).sub(position).nor();
- velocity.set(dir).scl(speed);
- }
- }
- class Enemy {
- private Ball ball;
- Vector2 positionTwo = new Vector2();
- Vector2 velocityTwo = new Vector2();
- Vector2 movementTwo = new Vector2();
- Vector2 targetTwo = new Vector2();
- Vector2 dirTwo = new Vector2();
- private float width;
- private float height;
- float speed = 50;
- public Enemy (float x, float y)
- {
- positionTwo.set(x, y);
- targetTwo.set(x,y);
- this.width = 5;
- this.height = 5;
- }
- public float getWidth() {
- return width;
- }
- public float getHeight() {
- return height;
- }
- public void update (float deltaTime) {
- movementTwo.set(velocityTwo).scl(deltaTime);
- if (positionTwo.dst2(targetTwo) > movementTwo.len2()) {
- positionTwo.add(movementTwo);
- } else {
- positionTwo.set(targetTwo);
- }
- }
- public void moveTo (float x, float y) {
- dirTwo.set(targetTwo.set(x,y)).sub(positionTwo).nor();
- velocityTwo.set(dirTwo).scl(speed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement