/*
* Created on Jun 26, 2005
*/
package edu.trinity.cs.gamecore.basic;
import java.util.Vector;
import javax.swing.*;
import edu.trinity.cs.gamecore.GameSetup;
import edu.trinity.cs.gamecore.ListBasedPriorityQueue;
import edu.trinity.cs.gamecore.Location;
import edu.trinity.cs.gamecore.MainFrame;
import edu.trinity.cs.gamecore.Player;
import edu.trinity.cs.gamecore.PriorityQueue;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Iterator;
/**
*
This is a simple implementation of the GameSetup interface. It tells the game
* infrastructure how to render things and is a good place to put things that you need
* to get to from anywhere.
*
* Students are given this code with assignment #2. The class itself uses the singleton
* pattern not only so that only one is created, but also so that you can easily get to that
* one instance from anywhere in your code. For the singleton pattern, the constructor is
* made private and we put in a static method called instance that checks to see if we have
* created an instance, creates one if not, and returns the instance.
*
* Feel free to add whatever other methods or members you want into your implementation
* of GameSetup. This can be a simple way to keep track of information that lots of different
* entities or blocks need to get hold of.
*/
public class BasicGameSetup implements GameSetup {
/**
* This method returns the singleton of the BasicGameSetup class.
*/
public static BasicGameSetup instance() {
if(bgsInstance==null) {
bgsInstance=new BasicGameSetup();
}
return bgsInstance;
}
/**
* This should initialize all of your variables that are related to the general
* state of the game.
*/
private BasicGameSetup() {
// Set this to whatever works well for your game.
Location.setPartialsInWhole(5);
Vector vect=null;
// Load in the map.
// You can uncomment this code when you want to have your game load a map
// that you made with the ScreenEditor.
/*
try {
vect=ScreenEditor.readScreenVector("BasicMap.bin");
firstScreen=(Screen)vect.get(0);
} catch(IOException e) {
firstScreen=new BasicScreen();
} catch(ClassNotFoundException e) {
firstScreen=new BasicScreen();
}
*/
// This builds a screen using the default constructor. I put it in the
// vector so that it works with the code below that was written for the
// loading.
vect=new Vector();
firstScreen=new BasicScreen(30,20);
vect.add(firstScreen);
// Setup basic variables.
localPlayer=new BasicPlayer(new Location(firstScreen,10,10));
firstScreen.addEntity(localPlayer);
// Set up the original queue and add all entities.
priorityQueue=new ListBasedPriorityQueue();
for(int i=0; i iter=scr.createEntityIterator(); iter.hasNext(); ) {
BasicEntity ge=iter.next();
priorityQueue.add(ge);
}
}
}
/**
* This method is called in the constructor of a main frame so that the
* GameSetup class will be able to communicate with the frame that the
* game is being played in.
* @param mf The MainFrame object the game is being played in.
*/
public void setMainFrame(MainFrame mf) {
mainFrame = mf;
}
/**
* Returns the instance of your Player subclass that should be used in this game.
*/
public BasicPlayer getLocalPlayer() {
return localPlayer;
}
/**
* Return the priority queue that you are using for the game. You will write
* two of these during the semester.
*/
public PriorityQueue getPriorityQueue() {
return priorityQueue;
}
/**
* Tells the display class is this game has a scrolling background in the x direction.
*
* @return This value tells the program how many squares across it should draw around the player. Return -1 for no scrolling.
*/
public int getScrollingX() {
return -1;
}
/**
* Tells the display class is this game has a scrolling background in the y direction.
*
* @return This value tells the program how many squares vertically it should draw around the player. Return -1 for no scrolling.
*/
public int getScrollingY() {
return -1;
}
/**
* If this returns true, the drawing routine will use an optimization of
* drawing the images for all the blocks once to a large image and just
* putting up that image as long as the player hasn't changed screens. This
* can be significantly faster. However, if it is true, it means that changes
* to blocks won't normally be drawn so you can't have animated blocks, or
* moving blocks, or blocks that disappear (doors). This flag is checked
* at every step so you could have it return true most of the time, but return
* false when some event has occured that should require the blocks to be
* redrawn.
* @return Whether or not the framework should double-buffer the blocks as a background image.
*/
public boolean useDrawOptimization() {
return true;
}
/**
* This function returns a JMenuBar object that will be added to the main
* display window. All of the functions that you want the menus to be able
* to doo will need to be connected from here.
*/
public JMenuBar getMenuBar() {
JMenuBar menuBar=new JMenuBar();
JMenu fileMenu=new JMenu("File");
JMenuItem startItem=new JMenuItem("Start Game");
startItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { startGame(); }
});
fileMenu.add(startItem);
JMenuItem item=new JMenuItem("Exit");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { System.exit(0); }
});
fileMenu.add(item);
menuBar.add(fileMenu);
return menuBar;
}
/**
* This function will automatically be called when the player begins to return
* something other than GAME_RUNNING.
*/
public void stopGame() {
if(getLocalPlayer().gameStatus()==Player.GameStatus.GAME_SUCCESS) {
JOptionPane.showMessageDialog(mainFrame,"You won the game!","Congratulations!",JOptionPane.OK_OPTION);
} else if(getLocalPlayer().gameStatus()==Player.GameStatus.PLAYER_DEAD) {
JOptionPane.showMessageDialog(mainFrame,"Sorry, you lost.","Oops",JOptionPane.OK_OPTION);
}
}
/**
* This method is called from the event handler for the start option on the
* menu. It can be replaced if you want to use different menu options. You
* will add other similar functions for all the menu options that you add.
*/
private void startGame() {
mainFrame.getDisplay().requestFocus();
mainFrame.getTimer().start();
}
private BasicPlayer localPlayer = null;
private PriorityQueue priorityQueue = null;
private BasicScreen firstScreen;
private MainFrame mainFrame;
private static BasicGameSetup bgsInstance;
}