package oop13.space.testing;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import oop13.space.model.Alien;
import oop13.space.model.AlienMotherShip;
import oop13.space.model.AlienShot;
import oop13.space.model.AlienType;
import oop13.space.model.Individual;
import oop13.space.model.Ship;
import oop13.space.model.ShipShot;
import oop13.space.model.Wall;
import oop13.space.utilities.GameStrings;
import oop13.space.views.GamePanel;
import oop13.space.views.MainFrame;
//Class used to test collisions ShipShot -> Individuals
public class TestCollisionsInFrame {
private static final int POSITION_Y = 50;
private static final int INITIAL_POSITION_X = 40;
private static final int DELTA_X = 90;
private List<Individual> listIndividuals;
private Ship spaceShip;
private Alien alien1;
private Alien alien2;
private Alien alien3;
private AlienMotherShip alien4;
private AlienShot alienShot;
private Wall wall;
//Creates new TestCollisionsFrame
public TestCollisionsInFrame() {
this.initIndividuals();
}
/*Initialize all the individuals that will collide with the ship shot and add them
in a list*/
public void initIndividuals() {
this.listIndividuals = new CopyOnWriteArrayList<Individual>();
int startPositionX = INITIAL_POSITION_X;
this.spaceShip = new Ship();
this.alien1 = new Alien(new AlienType(1), startPositionX, POSITION_Y);
startPositionX += DELTA_X;
this.alien2 = new Alien(new AlienType(2), startPositionX, POSITION_Y);
startPositionX += DELTA_X;
this.alien3 = new Alien(new AlienType(3), startPositionX, POSITION_Y);
startPositionX += DELTA_X;
this.alien4 = new AlienMotherShip();
this.alien4.moveTo(startPositionX, POSITION_Y);
startPositionX += DELTA_X;
this.alienShot = new AlienShot(startPositionX, POSITION_Y);
startPositionX += DELTA_X;
this.wall = new Wall(startPositionX);
this.wall.moveTo(startPositionX, POSITION_Y);
this.listIndividuals.add(spaceShip);
this.listIndividuals.add(alien1);
this.listIndividuals.add(alien2);
this.listIndividuals.add(alien3);
this.listIndividuals.add(alien4);
this.listIndividuals.add(alienShot);
this.listIndividuals.add(wall);
}
//Returns the list of individuals
public List<Individual> getList() {
return this.listIndividuals;
}
/*Thread permitting the test collisions ShipShot -> Individuals.
* It ends when all the individuals are killed.
*/
public static class TestCollisions extends Thread {
private static final int INDIVIDUALS_KILLED_TO_STOP_THREAD = 6;
private static final int SLEEP_TIME = 15;
private GamePanel game;
private List<Individual> list;
//Creates new TestCollisions
public TestCollisions(List<Individual> list, GamePanel game) {
this.list = list;
this.game = game;
}
@Override
public void run() {
int individualsKilled = 0;
while (individualsKilled != INDIVIDUALS_KILLED_TO_STOP_THREAD) {
for (Individual ind : this.list) {
if (ind instanceof ShipShot) {
ind.moveIndividual(ind.getPositionX(), ind.getPositionY());
}
ind.collideWith(this.list);
if (ind.isDead()) {
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.list.remove(ind);
if (!(ind instanceof ShipShot)) {
individualsKilled++;
}
}
}
this.game.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);
}
}
/*
* Class that, given a list, permitted to handle the interaction with the ship
* that the list contains.
*/
public static class Control implements KeyListener {
private static final int DELTA_MOVEMENT = 10;
private List<Individual> list;
public Control(List<Individual> list) {
this.list = list;
}
//Returns the ship from the list
public Ship getShip() {
for (Individual ind : list) {
if (ind instanceof Ship) {
return (Ship) ind;
}
}
return null;
}
@Override
public void keyPressed(KeyEvent e) {
if (this.getShip() != null) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
this.getShip().moveIndividual(this.getShip().getPositionX() - DELTA_MOVEMENT, this.getShip().getPositionY());
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
this.getShip().moveIndividual(this.getShip().getPositionX() + DELTA_MOVEMENT, this.getShip().getPositionY());
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
ShipShot shot = new ShipShot(this.getShip().getPositionX() + DELTA_MOVEMENT, this.getShip().getPositionY() - (DELTA_MOVEMENT * 2));
this.list.add(shot);
}
}
}
@Override
public void keyReleased(KeyEvent e) {
// Do nothing
}
@Override
public void keyTyped(KeyEvent e) {
// Do nothing
}
}
public static void main(String[] args) {
MainFrame mainFrame = new MainFrame();
TestCollisionsInFrame f = new TestCollisionsInFrame();
List<Individual> list = f.getList();
GamePanel game = new GamePanel(list);
Control cont = new Control(list);
game.addKeyListener(cont);
mainFrame.replacePanel(game);
TestCollisions thread = new TestCollisions(list, game);
thread.start();
mainFrame.setVisible(true);
}
}