/*
* This file is part of jSpaceWar.
*
* jSpaceWar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* jSpaceWar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with jSpaceWar. If not, see <http://www.gnu.org/licenses/>.
*/
package com.mlarktar.spacewar.commands;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import com.mlarktar.spacewar.KeyInterpreter;
import com.mlarktar.spacewar.Labels;
import com.mlarktar.spacewar.NewGame;
import com.mlarktar.spacewar.SoundEvents;
import com.mlarktar.spacewar.SoundServer;
import com.mlarktar.spacewar.Space;
import com.mlarktar.spacewar.SpaceWar;
import com.mlarktar.spacewar.sprites.DeffensiveRepresentation;
import com.mlarktar.spacewar.sprites.NullShip;
import com.mlarktar.spacewar.sprites.OffensiveRepresentation;
import com.mlarktar.spacewar.sprites.Planet;
import com.mlarktar.spacewar.sprites.Ship;
import com.mlarktar.spacewar.sprites.ShipAI;
import com.mlarktar.spacewar.sprites.ShipObserver;
/**
* @author malar
*
* This command sets up and plays a SW game.
*/
public class PlaySpaceWar extends SWCommand implements ActionListener {
private SpaceWar app;
private ShipObserver so1, so2;
private Ship ship1, ship2;
private Space space;
private KeyInterpreter controller;
private Timer timer;
public PlaySpaceWar(SpaceWar app) {
super();
this.app = app;
this.timer = new Timer(500, this);
space = new Space();
panel = space;
}
/* (non-Javadoc)
* @see com.mlarktar.spacewar.SWCommand#init()
*/
public void init() {
NewGame ngf = app.ngf;
Rectangle bounds = space.getBounds();
int unity = bounds.height / 10;
int unitx = bounds.width / 10;
// Add the two ships to space
if (ngf.getShip1Pilot() == NewGame.PILOTHUMAN) {
ship1 = new Ship(unitx * 2, unity * 2, space);
} else {
ship1 = new ShipAI(unitx * 2, unity * 2, space);
}
if (ngf.getShip2Pilot() == NewGame.PILOTHUMAN) {
ship2 =
new Ship(
bounds.width - unitx * 2,
bounds.height - unity * 2,
space);
} else {
ship2 =
new ShipAI(
bounds.width - unitx * 2,
bounds.height - unity * 2,
space);
}
ship1.setEnemy(ship2);
ship2.setEnemy(ship1);
ship1.setSounds(SoundServer.getSoundEvents());
ship2.setSounds(SoundServer.getSoundEvents());
ship1.setRepresentation(new DeffensiveRepresentation());
ship2.setRepresentation(new OffensiveRepresentation());
space.addSpaceItem(ship1);
space.addSpaceItem(ship2);
// Add observers that show ships energy levels
so1 = new ShipObserver(unitx, unity, ship1);
so2 = new ShipObserver(unitx, bounds.height - unity, ship2);
space.addSpaceItem(so1);
space.addSpaceItem(so2);
// Send all keystrokes to the KeyInterpreter object
if (ngf.getShip2Pilot() == ngf.getShip1Pilot()) {
if (ngf.getShip2Pilot() == NewGame.PILOTHUMAN) {
controller = new KeyInterpreter(ship1, ship2);
} else {
controller = null;
}
} else {
if (ngf.getShip1Pilot() == NewGame.PILOTHUMAN) {
controller = new KeyInterpreter(ship1, new NullShip());
} else {
controller = new KeyInterpreter(new NullShip(), ship2);
}
}
// Adds a planet in the middle of the screen
if (ngf.getUsePlanet() || ngf.getUseGravity()) {
Planet planet =
new Planet(
(int) bounds.getCenterX(),
(int) bounds.getCenterY(),
ngf.getUsePlanet());
space.addSpaceItem(planet, ngf.getUseGravity());
}
space.fillWithStars();
keyListener = controller;
}
/* Starts all the timers and kicks off the game music
* @see com.mlarktar.spacewar.SWCommand#start()
*/
public void start() {
ship1.start();
ship2.start();
space.start();
timer.start();
if (!SoundServer.getSoundEvents().isLooping()) {
SoundServer.getSoundEvents().loopEvent(SoundEvents.AMB_DEF);
}
}
/* Stops all timers and releases all references
* @see com.mlarktar.spacewar.SWCommand#stop()
*/
public void stop() {
timer.stop();
SoundServer.getSoundEvents().stopLooping();
if (controller != null) {
controller.dispose();
}
space.dispose();
ship1.dispose();
ship2.dispose();
so1.dispose();
so2.dispose();
}
private void checkOnShips() {
if (so1.isDead() || so2.isDead()) {
boolean s1dead = false;
boolean s2dead = false;
if (so1.isDead()) {
app.increaseShipVictories(1);
s1dead = true;
}
if (so2.isDead()) {
app.increaseShipVictories(0);
s2dead = true;
}
app.setTitle(
"SpaceWar - Ship #1: "
+ app.getShipVictories(0)
+ " Ship #2: "
+ app.getShipVictories(1));
stop();
SoundServer.getSoundEvents().playEvent(SoundEvents.AMB_SUM);
if (s1dead && s2dead) {
JOptionPane.showMessageDialog(
app,
Labels.getString("EndGame.2ShipKilled"));
} else {
if (s1dead) {
JOptionPane.showMessageDialog(
app,
Labels.getString("EndGame.Ship2Wins"));
}
if (s2dead) {
JOptionPane.showMessageDialog(
app,
Labels.getString("EndGame.Ship1Wins"));
}
}
app.doCommand(new WellcomeSpaceWar(app));
}
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
checkOnShips();
}
/* (non-Javadoc)
* @see com.mlarktar.spacewar.commands.SWCommand#pause()
*/
public void pause() {
timer.stop();
space.stop();
ship1.stop();
ship2.stop();
}
}