Package oop13.space.testing

Source Code of oop13.space.testing.TestCollisionsToShip

package oop13.space.testing;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import oop13.space.model.AlienShot;
import oop13.space.model.Individual;
import oop13.space.model.Ship;
import oop13.space.utilities.AudioPlayer;
import oop13.space.utilities.GameStrings;
import oop13.space.views.GamePanel;
import oop13.space.views.MainFrame;
import oop13.space.testing.TestCollisionsInFrame.Control;

/*
* Class used to test the collisions AlienShot -> Ship.
* It is a thread that stops when the ship has not more lives.
*/
public class TestCollisionsToShip extends Thread {
 
  private static final int TIME_AFTER_SHOT = 1000;
  private static final int MAX_RANDOM_NUMBER = 110;
  private static final int MULTIPLE = 5;
  private static final int SLEEP_TIME = 90;
  private static final int ALIENSHOT_DEAD_LINE = 550;
 
  private GamePanel testPanel;
  private Ship spaceShip;
  private List<Individual> listIndividuals;
  private long time;
 
  //Creates new TestCollisionsToShip
  public TestCollisionsToShip() {
    this.listIndividuals = new CopyOnWriteArrayList<>();
    this.spaceShip = new ShipTest();
    this.listIndividuals.add(this.spaceShip);
    this.testPanel = new GamePanel(this.listIndividuals);
    this.testPanel.addKeyListener(new Control(this.listIndividuals));
    this.testPanel.setFocusable(true);
  }
 

  @Override
  public void run() {
    while (!this.spaceShip.isDead()) {
      if (System.currentTimeMillis() - time >= TIME_AFTER_SHOT) {
        time = System.currentTimeMillis();
        int x = ((int) (Math.random() * MAX_RANDOM_NUMBER)) * MULTIPLE;
        AlienShot alShot = new AlienShot((int) x, 0);
        this.listIndividuals.add(alShot);
      }
      for (Individual ind : this.listIndividuals) {
        ind.moveIndividual(ind.getPositionX(), ind.getPositionY());
        ind.collideWith(this.listIndividuals);
        if (ind.isDead()) {
          if (ind instanceof AlienShot && ind.getPositionY() < ALIENSHOT_DEAD_LINE) {
            System.out.println(GameStrings.IND_NAME + ind.getClass().getSimpleName());
            System.out.println(GameStrings.IND_POSITION_X + ind.getPositionX());
            System.out.println(GameStrings.IND_POSITION_Y + ind.getPositionY());
            System.out.println("")
          }
          this.listIndividuals.remove(ind);
        }
      }
      this.testPanel.repaint();
      try {
        Thread.sleep(SLEEP_TIME);
      } catch (InterruptedException e) {
        System.err.println(GameStrings.THREAD_INTERRUPTED_ERROR);
      }
    }
    System.out.println(GameStrings.TEST_COMPLETED);
    System.exit(0);
  }
 
  //Returns the panel of the test
  public GamePanel getTestPanel() {
    return this.testPanel;
  }
 
  /*
   * A ship that prints some informations in the console when it collides with
   * other individuals.
   */
  private class ShipTest extends Ship {
   
    private static final long serialVersionUID = -3093904319708051729L;
    private static final int DEFAULT_X_POSITION = 270;
    private static final int DEFAULT_Y_POSITION = 500;
   
    @Override
    public void collideWith(List<Individual> list) {
      for (Individual ind : list) {
        if (ind instanceof AlienShot) {
          if (ind.getPositionX() > this.getPositionX() - DELTA_MOVEMENT
              && ind.getPositionX() <= this.getPositionX() + (DELTA_MOVEMENT * 3)
              && ind.getPositionY() > this.getPositionY() - (DELTA_MOVEMENT * 2)
              && ind.getPositionY() < this.getPositionY() + (DELTA_MOVEMENT * 2)) {
            this.decreaseLives();
            System.out.println(GameStrings.IND_NAME + this.getClass().getSimpleName());
            System.out.println(GameStrings.IND_POSITION_X + this.getPositionX());
            System.out.println(GameStrings.IND_POSITION_Y + this.getPositionY());
            System.out.println("");
            AudioPlayer.getAudioPlayer().playOnce(AudioPlayer.SHIP_HITTED_SOUND);
            this.moveTo(DEFAULT_X_POSITION, DEFAULT_Y_POSITION);
            ind.setDeath();
            if (this.getLives() == 0) {
              this.setDeath();
            }
          }   
        }
      }
    }
  }
 
  public static void main(String[] args) {
    MainFrame mainFrame = new MainFrame();
    TestCollisionsToShip test = new TestCollisionsToShip();
    mainFrame.replacePanel(test.getTestPanel());
    test.start();
    mainFrame.setVisible(true);
  }

}
TOP

Related Classes of oop13.space.testing.TestCollisionsToShip

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.