Android Open Source - AndriyVoronaSpaceAttack Assets






From Project

Back to project page AndriyVoronaSpaceAttack.

License

The source code is released under:

GNU General Public License

If you think the Android project AndriyVoronaSpaceAttack listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.vorona.game;
/*w ww . ja v  a2s.c o  m*/
import java.util.ArrayList;
import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;

public class Assets {

  final VoronaGame game;

  FontWriter f;

  Stage s;

  boolean started;

  private static final Rand r = new Rand();

  int countOfAsteroids;

  private static Timer t;
  private static Timer tA;
  private Timer tB;

  int scrWidth;
  int scrHeight;

  private StartButton startButton;
  private ExitButton exitButton;
  private AboutButton aboutButton;

  public Group buttonGroup;

  int startXStart;
  int startXExit;

  private ArrayList<Asteroid> asteroids;
  public SpaceShip spaceShip;
  private ArrayList<Bullet> bullets;

  private Texture logoScreenTex;

  private LogoScreen logoScreen;
  private GameScreen gameScreen;
  private MenuScreen menuScreen;

  private Texture aboutButtonNormalTex;
  private Texture aboutButtonPressedTex;
  private Texture exitButtonNormalTex;
  private Texture exitButtonPressedTex;
  private Texture startButtonNormalTex;
  private Texture startButtonPressedTex;
  private Texture gameScreenTex;
  private Texture spaceShipTex;
  private Texture bulletTex;
  private Texture menuScreenTex;

  private TextureRegion startButtonTR;
  private TextureRegion exitButtonTR;
  private TextureRegion gameScreenTR;
  private TextureRegion aboutButtonTR;
  private TextureRegion spaceShipTR;
  private TextureRegion menuScreenTR;
  private TextureRegion bulletTR;
  private TextureRegion logoScreenTR;
  private ArrayList<TextureRegion> asteroidTR;

  Asteroid a;
  float aX;
  float aWidth;
  float aY;
  float aHeight;
  float ssX;
  float ssY;
  float ssWidth;
  float ssHeight;
  float ssSpeed;
  int sSpeed;

  private Texture aboutScreenTex;

  private TextureRegion aboutScreenTR;

  private AboutScreen aboutScreen;

  public Assets(Stage s, VoronaGame game) {

    started = false;
    countOfAsteroids = 2;
    this.s = s;
    this.game = game;
    scrWidth = Gdx.graphics.getWidth();
    scrHeight = Gdx.graphics.getHeight();
    asteroids = new ArrayList<Asteroid>();
    asteroidTR = new ArrayList<TextureRegion>();
    bullets = new ArrayList<Bullet>();
    tA = new Timer();
    t = new Timer();
    tB = new Timer();

  }

  private void loadMenuScreen() {

    menuScreenTex = new Texture("icons/MenuScreen.png");
    menuScreenTex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    menuScreenTR = new TextureRegion(menuScreenTex);
    menuScreenTR.flip(false, true);

    startButtonNormalTex = new Texture("icons/StartNormal.png");
    startButtonNormalTex.setFilter(TextureFilter.Linear,
        TextureFilter.Linear);
    startButtonPressedTex = new Texture("icons/StartPressed.png");
    startButtonPressedTex.setFilter(TextureFilter.Linear,
        TextureFilter.Linear);

    startButtonTR = new TextureRegion(startButtonNormalTex);
    startButtonTR.flip(false, true);

    exitButtonNormalTex = new Texture("icons/ExitNormal.png");
    exitButtonNormalTex.setFilter(TextureFilter.Linear,
        TextureFilter.Linear);
    exitButtonPressedTex = new Texture("icons/ExitPressed.png");
    exitButtonPressedTex.setFilter(TextureFilter.Linear,
        TextureFilter.Linear);

    exitButtonTR = new TextureRegion(exitButtonNormalTex);
    exitButtonTR.flip(false, true);

    aboutButtonNormalTex = new Texture("icons/AboutNormal.png");
    aboutButtonNormalTex.setFilter(TextureFilter.Linear,
        TextureFilter.Linear);
    aboutButtonPressedTex = new Texture("icons/AboutPressed.png");
    aboutButtonPressedTex.setFilter(TextureFilter.Linear,
        TextureFilter.Linear);

    aboutButtonTR = new TextureRegion(aboutButtonNormalTex);
    aboutButtonTR.flip(false, true);

  }

  private void loadAsteroids() {

    for (int i = 0; i < countOfAsteroids; i++) {
      asteroidTR.add(new TextureRegion(new Texture("icons/Asteroid" + i
          + ".png")));
      asteroidTR.get(i).flip(false, true);
    }

  }

  // ================================Creators==========================================

  public void createAsteroid() {
    asteroids.add(new Asteroid());
    s.addActor(asteroids.get(asteroids.size() - 1));
  }

  public void createMenuScreen() {

    buttonGroup = new Group();

    int posX = 0;
    float sizeX = scrWidth * 340 / 540;
    float sizeY = scrHeight * 100 / 960;

    loadMenuScreen();

    menuScreen = new MenuScreen();
    menuScreen.setPosition(0, 0);
    menuScreen.setSize(scrWidth, scrHeight);

    startButton = new StartButton();
    startButton.setPosition(posX, 0);
    startButton.setSize(sizeX, sizeY);
    startButton.addListener(new StartListener());

    exitButton = new ExitButton();
    exitButton.setPosition(0, sizeY + 10);
    exitButton.setSize(sizeX, sizeY);
    exitButton.addListener(new ExitListener());

    aboutButton = new AboutButton();
    aboutButton.setPosition(0, 2 * sizeY + 2 * 10);
    aboutButton.setSize(sizeX, sizeY);
    aboutButton.addListener(new AboutListener());

    buttonGroup.addActor(startButton);
    buttonGroup.addActor(exitButton);
    buttonGroup.addActor(aboutButton);
    buttonGroup.setSize(sizeX, 2 * sizeY + 2 * 10 + sizeY);
    buttonGroup.setPosition((scrWidth / 2 - buttonGroup.getWidth() / 2),
        (scrHeight / 2 - buttonGroup.getHeight() / 2));

    showMenuScreen();

  }

  public void createLogoScreen() {

    logoScreenTex = new Texture("icons/LogoScreen.png");
    logoScreenTex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    logoScreenTR = new TextureRegion(logoScreenTex);
    logoScreenTR.flip(false, true);

    logoScreen = new LogoScreen();
    logoScreen.setBounds(0, 0, scrWidth, scrHeight);
    logoScreen.addListener(new LogoListener());

  }

  public void createGameScreen() {

    gameScreenTex = new Texture("icons/GameScreen.png");
    gameScreenTex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    gameScreenTR = new TextureRegion(gameScreenTex);
    gameScreenTR.flip(false, true);

    gameScreen = new GameScreen();
    gameScreen.setBounds(0, 0, scrWidth, scrHeight);

    spaceShipTex = new Texture("icons/SpaceShip.png");
    spaceShipTex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    spaceShipTR = new TextureRegion(spaceShipTex);
    spaceShipTR.flip(false, true);

    spaceShip = new SpaceShip();
    spaceShip.setSize(scrWidth * 80 / 540, scrWidth * 80 / 540);
    spaceShip.setY(scrHeight - spaceShip.getHeight());

    bulletTex = new Texture("icons/Shot.png");
    bulletTex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    bulletTR = new TextureRegion(bulletTex, 5, 0, 7, 16);
    bulletTR.flip(false, true);
    spaceShip.setZIndex(1);

    showGameScreen();
    s.addListener(new GameListener());

  }

  public void createAboutScreen() {

    aboutScreenTex = new Texture("icons/AboutScreen.png");
    aboutScreenTex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    aboutScreenTR = new TextureRegion(aboutScreenTex);
    aboutScreenTR.flip(false, true);

    aboutScreen = new AboutScreen();
    aboutScreen.setBounds(0, 0, scrWidth, scrHeight);
    aboutScreen.addListener(new LogoListener());

  }

  // ================================Listeners==========================================

  class LogoListener extends InputListener {

    @Override
    public boolean touchDown(InputEvent event, float x, float y,
        int pointer, int button) {
      // TODO Auto-generated method
      s.clear();
      tA.clear();
      tB.clear();
      t.clear();
      game.setMenuScreen();
      return true;
    }

  }

  class StartListener extends InputListener {

    @Override
    public boolean touchDown(InputEvent event, float x, float y,
        int pointer, int button) {
      // TODO Auto-generated method stub
      startButtonTR.setTexture(startButtonPressedTex);
      t.scheduleTask(new Task() {

        @Override
        public void run() {
          // TODO Auto-generated method stub
          s.clear();
          game.setGameScreen();
          newGame();
        }
      }, 0.2f);
      return true;
    }
  
    @Override
    public void touchUp(InputEvent event, float x, float y, int pointer,
        int button) {
      // TODO Auto-generated method stub
      startButtonTR.setTexture(startButtonNormalTex);
      //super.touchUp(event, x, y, pointer, button);
    }  
  }

  class ExitListener extends InputListener {

    @Override
    public boolean touchDown(InputEvent event, float x, float y,
        int pointer, int button) {
      // TODO Auto-generated method stub
      exitButtonTR.setTexture(exitButtonPressedTex);
      t.scheduleTask(new Task() {

        @Override
        public void run() {
          // TODO Auto-generated method stub
          System.gc();
          pool();
          Gdx.app.exit();
        }
      }, 0.1f);

      return true;
    }

  }

  class AboutListener extends InputListener {

    @Override
    public boolean touchDown(InputEvent event, float x, float y,
        int pointer, int button) {
      // TODO Auto-generated method stub
      aboutButtonTR.setTexture(aboutButtonPressedTex);
      tA.clear();
      tB.clear();
      t.clear();
      game.setAboutScreen();
      return true;
    }

    @Override
    public void touchUp(InputEvent event, float x, float y, int pointer,
        int button) {
      // TODO Auto-generated method stub
      aboutButtonTR.setTexture(aboutButtonNormalTex);
      super.touchUp(event, x, y, pointer, button);
    }

  }

  class GameListener extends InputListener {

    @Override
    public boolean touchDown(InputEvent event, float x, float y,
        int pointer, int button) {
      // TODO Auto-generated method stub
      if (x < scrWidth / 2)
        spaceShip.moveLeft();
      else
        spaceShip.moveRight();

      if (!started) {
        startAsteroidTimer();
        startBulletTimer();
        started = true;
      }

      return true;
    }

    @Override
    public void touchUp(InputEvent event, float x, float y, int pointer,
        int button) {
      // TODO Auto-generated method stub
      t.clear();
    }

    @Override
    public boolean keyDown(InputEvent event, int keycode) {
      // TODO Auto-generated method stub

      if (keycode == Keys.BACK)
        game.setMenuScreen();
      if (keycode == Keys.HOME) {
        Gdx.app.exit();
      }
      return super.keyDown(event, keycode);
    }
  }

  // =================================Actors============================================

  class StartButton extends Actor {

    @Override
    public void draw(Batch batch, float parentAlpha) {
      // TODO Auto-generated method stub
      batch.draw(startButtonTR, getX(), getY(), getOriginX(),
          getOriginY(), getWidth(), getHeight(), getScaleX(),
          getScaleY(), getRotation());
    }
  }

  class ExitButton extends Actor {

    @Override
    public void draw(Batch batch, float parentAlpha) {
      // TODO Auto-generated method stub
      batch.draw(exitButtonTR, getX(), getY(), getOriginX(),
          getOriginY(), getWidth(), getHeight(), getScaleX(),
          getScaleY(), getRotation());
    }
  }

  class AboutButton extends Actor {

    @Override
    public void draw(Batch batch, float parentAlpha) {
      // TODO Auto-generated method stub
      batch.draw(aboutButtonTR, getX(), getY(), getOriginX(),
          getOriginY(), getWidth(), getHeight(), getScaleX(),
          getScaleY(), getRotation());
    }
  }

  class MenuScreen extends Actor {

    @Override
    public void draw(Batch batch, float parentAlpha) {
      // TODO Auto-generated method stub
      batch.draw(menuScreenTR, getX(), getY(), getOriginX(),
          getOriginY(), getWidth(), getHeight(), getScaleX(),
          getScaleY(), getRotation());
    }
  }

  class LogoScreen extends Actor {

    @Override
    public void draw(Batch batch, float parentAlpha) {
      // TODO Auto-generated method stub
      batch.draw(logoScreenTR, getX(), getY(), getWidth(), getHeight());
    }
  }

  class AboutScreen extends Actor {

    @Override
    public void draw(Batch batch, float parentAlpha) {
      // TODO Auto-generated method stub
      batch.draw(aboutScreenTR, getX(), getY(), getWidth(), getHeight());
    }
  }

  class GameScreen extends Actor {

    @Override
    public void draw(Batch batch, float parentAlpha) {
      // TODO Auto-generated method stub
      batch.draw(gameScreenTR, getX(), getY(), getWidth(), getHeight());
    }
  }

  class SpaceShip extends Actor {

    public int lives;
    private int speedX;
    public int score;

    public SpaceShip() {
      // TODO Auto-generated constructor stub
      lives = 3;
      speedX = 7;
      score = 0;
      setX(scrWidth / 2 - this.getWidth() / 2);
    }

    public void moveRight() {
      t.scheduleTask(new Task() {

        @Override
        public void run() {
          // TODO Auto-generated method stub
          if (getX() + getWidth() < scrWidth - speedX)
            setX(getX() + speedX);
        }
      }, 0, 0.02f);
    }

    public void moveLeft() {
      t.scheduleTask(new Task() {

        @Override
        public void run() {
          // TODO Auto-generated method stub

          if (getX() > speedX)
            setX(getX() - speedX);
        }
      }, 0, 0.02f);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
      // TODO Auto-generated method stub
      batch.draw(spaceShipTR, getX(), getY(), getOriginX(), getOriginY(),
          getWidth(), getHeight(), getScaleX(), getScaleY(),
          getRotation());
    }

    public void dead() {

      setX(scrWidth / 2 - getWidth() / 2);

      tA.scheduleTask(new Task() {

        @Override
        public void run() {
          // TODO Auto-generated method stub
          setVisible(!isVisible());
        }
      }, 0, 0.2f, 5);

      tA.scheduleTask(new Task() {

        @Override
        public void run() {
          // TODO Auto-generated method stub
          setVisible(true);
        }
      }, 2.01f);

      if (lives > 0)
        lives--;
      else {
        tA.clear();
        t.clear();
        tB.clear();
        s.clear();
        s = null;
        started = false;
        game.setDeadScreen();

      }

    };

  }

  class Asteroid extends Actor {

    public final Vector2 speed;
    TextureRegion t;
    public int rotationDegree;

    public Asteroid() {

      int posX = 0;
      int posY = 0;
      boolean clockWise = (r.nextInt(2) == 1) ? true : false;

      speed = new Vector2();

      speed.x = r.nextIntBounds(1, 3);
      speed.y = r.nextIntBounds(4, 7);

      int rt = r.nextIntBounds(1, 7);
      rotationDegree = (clockWise) ? -rt : rt;

      t = asteroidTR.get(r.nextInt(2));

      setSize(r.nextIntBounds(5, 8) * 10, r.nextIntBounds(5, 8) * 10);
      setOrigin(getCenterX(), getCenterY());

      switch (r.nextInt(3)) {
      case 0:
        posX = r.nextIntBounds(0, (int) (scrWidth - getWidth()));
        break;

      case 1:
        posX = (int) (-getWidth() / 2);
        break;

      case 2:
        posX = (int) (scrWidth + getWidth() / 2);
        break;

      }

      posY = (int) (((posX < 0) || (posX > scrWidth)) ? r.nextIntBounds(
          0, scrHeight / 2) : -getHeight() / 2);
      speed.x = (posX < (scrWidth / 2)) ? speed.x : -speed.x;
      setPosition(posX, posY);

    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
      // TODO Auto-generated method stub
      batch.draw(t, getX(), getY(), getOriginX(), getOriginY(),
          getWidth(), getHeight(), getScaleX(), getScaleY(),
          getRotation());
    }

  }

  class Bullet extends Actor {

    int speed;

    public Bullet() {
      setSize(scrWidth * 6 / 540, scrHeight * 19.2f / 960);
      setPosition(spaceShip.getX() + spaceShip.getWidth() / 2,
          spaceShip.getY() + 6);
      speed = 10;
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
      // TODO Auto-generated method stub
      batch.draw(bulletTR, getX(), getY(), getOriginX(), getOriginY(),
          getWidth(), getHeight(), getScaleX(), getScaleY(),
          getRotation());
    }

  }

  // Random variables for this game
  static class Rand extends Random {

    public int nextIntBounds(int l, int r) {
      // TODO Auto-generated method stub
      int t;
      do {
        t = super.nextInt(r + 1);
      } while ((t < l));
      return t;
    }
  }

  // Timer's schedule for asteroids
  public void startAsteroidTimer() {

    tA.scheduleTask(new Task() {

      @Override
      public void run() {
        createAsteroid();
      }
    }, 0, 0.5f);

    tA.scheduleTask(new Task() {

      @Override
      public void run() {
        // TODO Auto-generated method stub
        asteroidRotation();
        asteroidFlie();
      }
    }, 0, 0.05f);

  }

  private void asteroidRotation() {
    // TODO Auto-generated method stub
    for (int i = 0; i < asteroids.size(); i++) {
      Asteroid a = asteroids.get(i);
      a.rotateBy(a.rotationDegree);
    }
  }

  private void asteroidFlie() {

    for (int i = 0; i < asteroids.size(); i++) {

      a = asteroids.get(i);
      aX = a.getX();
      aWidth = a.getWidth();
      aY = a.getY();
      aHeight = a.getHeight();

      if ((aX > scrWidth || aX < -aWidth) || (aY > scrHeight)) {
        s.getRoot().removeActor(a);
        asteroids.remove(a);
        a = null;
      } else if ((aY + aHeight > spaceShip.getY())
          && ((spaceShip.getX() > a.getX() && spaceShip.getX() < a
              .getX() + a.getWidth()) || (spaceShip.getX()
              + spaceShip.getWidth() > a.getX() && spaceShip
              .getX() + spaceShip.getWidth() < a.getX()
              + a.getWidth()))) {
        s.getRoot().removeActor(a);
        asteroids.remove(a);
        a = null;
        spaceShip.dead();
      } else
        a.setPosition(a.speed.x + aX, a.speed.y + aY);
    }
  }

  private void startBulletTimer() {

    tB.scheduleTask(new Task() {
      @Override
      public void run() {
        // TODO Auto-generated method stub
        Bullet b1 = new Bullet();
        b1.setX(spaceShip.getX() + 5);

        Bullet b2 = new Bullet();
        b2.setX(spaceShip.getX() + spaceShip.getWidth() - 5);

        bullets.add(b2);
        s.addActor(bullets.get(bullets.size() - 1));

        bullets.add(b1);
        s.addActor(bullets.get(bullets.size() - 1));

        b1 = null;
        b2 = null;
      }
    }, 0, 0.8f);
    tB.scheduleTask(new Task() {

      @Override
      public void run() {
        // TODO Auto-generated method stub
        bulletFlie();
      }
    }, 0, 0.006f);
  }

  private void bulletFlie() {

    for (int j = 0; j < bullets.size(); j++) {
      Bullet b = bullets.get(j);
      for (int i = 0; i < asteroids.size(); i++) {
        Asteroid a = asteroids.get(i);
        if ((a.getY() + a.getHeight() >= b.getY())
            && ((a.getX() < b.getX()) && (a.getX() + a.getWidth() > b
                .getX()))) {
          s.getRoot().removeActor(a);
          s.getRoot().removeActor(b);
          asteroids.remove(a);
          bullets.remove(b);
          if (spaceShip.score < 50)
            spaceShip.score++;
          else {
            started = false;
            tA.clear();
            t.clear();
            tB.clear();
            s.clear();
            s = null;
            game.setWinScreen();
          }
        }
      }
      if (b.getY() < -b.getHeight()) {
        s.getRoot().removeActor(b);
        bullets.remove(b);
      } else
        b.setPosition(b.getX(), b.getY() - b.speed);
    }

  }

  public void pool() {
    aboutButtonNormalTex.dispose();
    aboutButtonPressedTex.dispose();
    exitButtonNormalTex.dispose();
    exitButtonPressedTex.dispose();
    startButtonNormalTex.dispose();
    startButtonPressedTex.dispose();
    t.clear();
    tA.clear();
    tB.clear();
    s.clear();
    bullets.clear();
    asteroids.clear();
    asteroidTR.clear();
    menuScreen.clear();
    game.dispose();
    System.gc();
  }

  public void newGame() {

    loadAsteroids();
    createGameScreen();

    spaceShip.lives = 3;
    spaceShip.score = 0;
    spaceShip.setX(scrWidth / 2 - spaceShip.getWidth() / 2);
    bullets.clear();
    asteroids.clear();
    ssX = spaceShip.getX();
    ssY = spaceShip.getY();
    ssWidth = spaceShip.getWidth();
    ssHeight = spaceShip.getHeight();

  }

  public void addStage(Stage s) {
    this.s = s;
  }

  public void showLogoScreen() {
    s.clear();
    s.addActor(logoScreen);
  }

  public void showAboutScreen() {
    s.clear();
    s.addActor(aboutScreen);
  }

  public void showMenuScreen() {
    s.clear();
    s.addActor(menuScreen);
    s.addActor(buttonGroup);
  }

  public void showGameScreen() {
    s.clear();
    s.addActor(gameScreen);
    s.addActor(spaceShip);
  }

}




Java Source Code List

com.vorona.game.AScreen.java
com.vorona.game.Assets.java
com.vorona.game.DScreen.java
com.vorona.game.FontWriter.java
com.vorona.game.GScreen.java
com.vorona.game.LScreen.java
com.vorona.game.MScreen.java
com.vorona.game.VoronaGame.java
com.vorona.game.WScreen.java
com.vorona.game.WaitTimer.java
com.vorona.game.android.AndroidLauncher.java
com.vorona.game.desktop.DesktopLauncher.java