Commit 28bb5972 authored by Thomas Dupriez's avatar Thomas Dupriez

git add src

parent 1ac8010f
package graphics.guiSkeleton.GUIPanel;
import graphics.graphical_abstraction.PanelAbstraction;
import graphics.guiSkeleton.GraphicsMaster;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
* Created by dupriez on 12/11/15.
*
* This abstract class defines the kind of panel the gui can display
* The gui changes what it displays by swapping GUIPanels in the mainFrame.
*/
public abstract class GUIPanel extends JPanel implements PanelAbstraction{
//The following line is commented because KeyListener does not seem to work in a JPanel
//Private KeyListenerForGUIPanel associatedKeyHandler;
//Store the graphicsMaster that created the GUIPanel (there should be only one GraphicsMaster instanciation per application,
// this variable is just there to give the GUIPanel the right to call the method changeGUIStateTo de graphicsMaster)
private GraphicsMaster graphicsMaster;
protected GraphicsMaster getGraphicsMaster() {return graphicsMaster;}
public GUIPanel(GraphicsMaster graphicsMaster) {
this.graphicsMaster = graphicsMaster;
//The following 2 lines are commented because KeyListener does not seem to work in a JPanel
//associatedKeyHandler = new KeyListenerForGUIPanel(this);
setFocusable(true);
}
/**
* This method is called by the gui before displaying the panel
*/
//abstract void initialise();
/**
* This method is called by the gui after this panel is no more displayed
*/
//abstract void finalise();
/**
* Define here what should happen if a key is pressed while this panel has the focus
* @param e
*/
//abstract void keyPressedHandler(KeyEvent e);
//Example of a body one could give to the keyPressedHandler method when implementing a GUIPanel
//if(e.getKeyCode() == 27) { // 27 -> Escape
// System.out.println("Escape has been pressed");
//}
/**
* Define here what should happen if a key is released while this panel has the focus
* @param e
*/
//abstract void keyReleasedHandler(KeyEvent e);
/**
* Define here what should happen if a key is typed while this panel has the focus
* @param e
*/
//abstract void keyTypedHandler(KeyEvent e);
}
// The following code is commented because KeyListener does not seem to work on a JPanel
///**
// * This class detects the KeyEvents coming from the keyboard (KeyPressed, KeyReleased and KeyTyped) when the GUIPanel
// * it is associated with has the focus, and call the corresponding KeyEvent handler of the associated GUIPanel
// */
//class KeyListenerForGUIPanel implements KeyListener {
//
// private GUIPanel associatedGUIPanel;
//
// KeyListenerForGUIPanel(GUIPanel associatedGUIPanel) {
// this.associatedGUIPanel = associatedGUIPanel;
// }
//
// public final void keyPressed(KeyEvent e) {
// associatedGUIPanel.keyPressedHandler(e);
// }
//
// public final void keyReleased(KeyEvent e) {
// associatedGUIPanel.keyReleasedHandler(e);
// }
//
// public final void keyTyped(KeyEvent e) {
// associatedGUIPanel.keyTypedHandler(e);
// }
//}
package graphics.guiSkeleton.GUIPanel;
import core.relayer.Relayer;
import graphics.guiSkeleton.*;
import graphics.guiSkeleton.CoreProcesses.GameContent;
import graphics.guiSkeleton.CoreProcesses.GameContentMapChangeListener;
import graphics.guiSkeleton.DummyForTest.*;
import graphics.ingame_input_listener.Input;
import graphics.sprites.TileSprite;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
/**
* Created by dupriez on 10/11/15.
*
* This class is the panel that displays the game window (what the player sees when he's playing)
*/
//TODO: Replace DummyEntitybis by Entity (or Entity id) (or whatever)
public class GamePanel extends GUIPanel implements GameContentMapChangeListener {
private boolean initialised = false;
private JLabel topLeftLabel;
private boolean debugMapPoint = false;
//If set to true, this class will print information in the console
private boolean debug = false;
//The relayer of the entity that the player plays
private Relayer followedRelayer;
//The place where are stored the information the game panel has to display (entities + map)
private GameContent gameContent;
//The MapImage of the map to display (the one obtained from gameContent)
private MapImage mapImg;
//Defines the portion of the map this panel must display
private VisionBox3 visionBox3;
//React to the key typed
private InputInterpreter MyInputInterpreter;
public GamePanel(GraphicsMaster graphicsMaster) {
super(graphicsMaster);
}
@Override
public void keyTypedHandler(Input e) {
}
/**
* Function to be called when the gui goes into the state GAME_PANEL, to prepare the Game Panel
*/
public void initialise(){
RepaintTimer.getInstance().enterPlayingFramerateMode();
DummyLocalCore dlc = DummyLocalCore.getInstance();
followedRelayer = dlc.startGame();
MyInputInterpreter = new InputInterpreter(followedRelayer);
gameContent = dlc.getLocalGameContent();
//subscribe to the map changes of gameContent
gameContent.addMapChangeListener(this);
//Getting the map, creating a bufferedImage out of it and storing it
mapImg = new MapImage(gameContent.getMap());
GraphicsMapPoint initialTopLeftGraphicsMapPoint = GraphicsMapPoint.produceMapPointFromCoordinatesInCoreSystem(followedRelayer.getEntity().getX(), followedRelayer.getEntity().getY(), mapImg);
visionBox3 = new VisionBox3(initialTopLeftGraphicsMapPoint, getSize().width, getSize().height);
topLeftLabel = new JLabel("Press ESC to go back to main menu");
topLeftLabel.setFont(new Font("Inconsolata", Font.BOLD, 26));
topLeftLabel.setForeground(new Color(255, 185, 15));
topLeftLabel.setHorizontalAlignment(JLabel.LEFT);
topLeftLabel.setOpaque(true);
topLeftLabel.setBackground(new Color(0, 0, 0));
this.add(topLeftLabel, BorderLayout.NORTH);
initialised = true;
}
/**
* Function to be called when the gui leave the state GAME_PANEL, to clean up the GamePanel
*/
public void finalise() {
initialised = false;
RepaintTimer.getInstance().exitPlayingFrameRateMode();
// mapBImg = null;
// followedDummyEntitybis = null;
// vBox2 = null;
this.remove(topLeftLabel);
}
/**
* Draw a PositionedSprite on the GamePanel
* @param positionedSpriteToDraw
* @param g
*/
private void drawPositionedSprite (PositionedSprite positionedSpriteToDraw, GraphicsMapPoint gamePanelTopLeftGraphicsMapPoint, Graphics g) {
GamePanelMapPoint gpmp = new GamePanelMapPoint(positionedSpriteToDraw.getTopLeftGraphicsMapPoint(), gamePanelTopLeftGraphicsMapPoint);
if(debugMapPoint) {
System.out.println("gpmpX=" + gpmp.getX() + ", gpmpY=" + gpmp.getY());
}
g.drawImage(positionedSpriteToDraw.getSpriteImage(), gpmp.getX(), gpmp.getY(), null);
}
/**
* Draw an entity on the gamePanel
* @param entityToDraw
* @param gamePanelTopLeftGraphicsMapPoint
* @param mapImage: the MapImage of the current map
* @param g
*/
private void drawEntity(DummyEntity entityToDraw, GraphicsMapPoint gamePanelTopLeftGraphicsMapPoint, MapImage mapImage, Graphics g) {
/** Get the PositionedSprite of entityToDraw and call the method drawPositionedSprite **/
//TODO: How to protect against possible inconsistencies, where the entity is not on the map
//TODO: the gamePanel uses ?
PositionedSprite entityPositionedSprite = entityToDraw.getEntityDisplayer().getPositionedSprite(mapImage);
if (debugMapPoint) {
System.out.println("PositionedSprite GraphicsMapPoint:");
entityPositionedSprite.getTopLeftGraphicsMapPoint().print();
System.out.print("\n");
}
drawPositionedSprite(entityPositionedSprite, gamePanelTopLeftGraphicsMapPoint, g);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(initialised) {
if (debug) {
System.out.println("gamePanel repaint, width=" + getSize().width + ", height=" + getSize().height);
}
/** Get the GraphicsMapPoint of the position of the entity followed by the gamePanel **/
GraphicsMapPoint followedEntityPositionGraphicsMapPoint = GraphicsMapPoint.produceMapPointFromCoordinatesInCoreSystem(followedRelayer.getEntity().getX(), followedRelayer.getEntity().getY(), mapImg);
if (debugMapPoint) {
System.out.println("followedEntityPositionGraphicsMapPoint:");
followedEntityPositionGraphicsMapPoint.print();
System.out.print("\n");
}
/** Update VisionBox parameters **/
visionBox3.moveVisionBox(followedEntityPositionGraphicsMapPoint, getSize().width, getSize().height);
/** Displaying the map **/
BufferedImage portionOfMapToDisplay = visionBox3.getCutMapImage().getMapBImg();
g.drawImage(portionOfMapToDisplay, 0, 0, visionBox3.getBoxWidth(), visionBox3.getBoxHeight(), null);
//Cool! the following line displays a minimap !
//TODO: fix this minimap (currently, it blocks sight and the entities can go on it -> strange)
g.drawImage(mapImg.getMapBImg(), 0,0,100,100, null);
/** Getting and displaying the entities **/
//Getting all the entities to process in the following loop
ArrayList<DummyEntity> entityList = gameContent.getEntityList();
//The entity currently processed in the following loop
DummyEntity processedEntity;
//The GraphicsMapPoint corresponding to the position of the top left point of the gamePanel
GraphicsMapPoint gamePanelTopLeftGraphicsMapPoint = visionBox3.getTopLeftGraphicsMapPoint();
if (debugMapPoint) {
System.out.println("gamePanelTopLeftGraphicsMapPoint:");
gamePanelTopLeftGraphicsMapPoint.print();
System.out.print("\n");
}
for(int i = 0; i < entityList.size(); i++) {
processedEntity = entityList.get(i);
if(debugMapPoint) {
System.out.println("Entity n°"+i+" X="+processedEntity.getX()+", Y="+processedEntity.getY()+" (Core's coordinates)");
}
drawEntity(processedEntity, gamePanelTopLeftGraphicsMapPoint, mapImg, g);
}
}
}
/** Handlers for KeyEvents **/
@Override
public void keyPressedHandler(Input e) {
if(e == Input.ESCAPE) { // 27 -> Escape
getGraphicsMaster().changeGUIStateTo(GraphicsMaster.GUIStates.MAIN_MENU_PANEL);
}
if (e == Input.LEFT_ARROW) { //<-, left arrow
MyInputInterpreter.setMoveXLeft(1);
}
if (e == Input.UP_ARROW) { // /\, up arrow
MyInputInterpreter.setMoveYTop(1);
}
if (e == Input.RIGHT_ARROW) { // ->, right arrow
MyInputInterpreter.setMoveXRight(1);
}
if (e == Input.DOWN_ARROW) { // \/, down arrow
MyInputInterpreter.setMoveYBottom(1);
}
int tsWidth = TileSprite.TileSpriteWidth();
int tsHeight = TileSprite.TileSpriteHeight();
int stairsX= gameContent.getMap().getPositionStairs().getJ();
int stairsY= gameContent.getMap().getHeight()-1- gameContent.getMap().getPositionStairs().getI();
int posX=followedRelayer.getEntity().getX();
int posY=followedRelayer.getEntity().getY();
if (stairsX*tsWidth<=posX && posX<(stairsX+1)*tsWidth && stairsY*tsHeight<=posY && posY<(stairsY+1)*tsHeight) { // del pressed
initialise();
}
//System.out.print(e.getKeyCode());
}
@Override
public void keyReleasedHandler(Input e) {
if (e == Input.LEFT_ARROW) { //<-, left arrow
MyInputInterpreter.setMoveXLeft(0);
}
if (e == Input.UP_ARROW) { // /\, up arrow
MyInputInterpreter.setMoveYTop(0);
}
if (e == Input.RIGHT_ARROW) { // ->, right arrow
MyInputInterpreter.setMoveXRight(0);
}
if (e == Input.DOWN_ARROW) { // \/, down arrow
MyInputInterpreter.setMoveYBottom(0);
}
}
/** DummyGameContentMapChangeListener implementation **/
@Override
public void mapChange() {
//update the mapImage of the GamePanel and repaint the GamePanel
mapImg = new MapImage(gameContent.getMap());
repaint();
}
}
package graphics.guiSkeleton.GUIPanel;
import gameloop.DummyLocalGameLoop;
import gameloop.DummyServerLoop;
import graphics.guiSkeleton.CoreProcesses.GameType;
import graphics.guiSkeleton.GUIColorsAndFonts;
import graphics.guiSkeleton.GraphicsMaster;
import graphics.ingame_input_listener.Input;
import network.LocalConnection;
import network.NetworkConnection;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
/**
* Created by dupriez on 14/11/15.
*
* A wrapper for the mainMenuPanel I created with intellij ui designer plugin.
* Do not take example of this, in the end, I'll have to rewrite the main menu panel without the plugin
*/
public class MainMenuPanel extends GUIPanel{
//The JPanel created by MainMenuMaker
// private JPanel underlyingMainMenuPanel;
public MainMenuPanel(GraphicsMaster graphicsMaster) {
super(graphicsMaster);
// MainMenuPanelMaker mmpm = new MainMenuPanelMaker(playButtonListener, exitButtonListener);
// underlyingMainMenuPanel = mmpm.getJPanel();
// setLayout(new GridLayout(0,1));
// add(underlyingMainMenuPanel);
/** Creation of the colours used in the following **/
Color darkSlateBlue = new Color(75,51,138);
Color tawnyPort = new Color(106,27,73);
Color gamboge = new Color(217,161,14);
Color darkRaspberry = new Color(138,28,98);
/** Creation of the fonts used in the following **/
Font inconsolata_40_bold = new Font("Inconsolata", Font.BOLD, 40);
Font purisa_30_bold = new Font("Purisa", Font.BOLD, 30);
Font arial_20 = new Font("Arial", Font.PLAIN, 20);
/**##############################################**/
/** Creation of the content of the MainMenuPanel **/
/**##############################################**/
setLayout(new GridBagLayout());
GridBagConstraints c;
/** Creation of the titlePanel **/
JPanel titlePanel = new JPanel();
titlePanel.setOpaque(true);
titlePanel.setBackground(darkSlateBlue);
//TODO: move the titleLabel to the center of the titlePanel
JLabel titleLabel = new JLabel("The Dungeon Project");
titleLabel.setForeground(gamboge);
titleLabel.setFont(inconsolata_40_bold);
titlePanel.add(titleLabel);
// Create the GridBagConstraint for titlePanel in mainMenuPanel
c = new GridBagConstraints();
c.gridx=0; c.gridy=0; c.gridwidth=1; c.gridheight=1;c.weightx=1;c.weighty=0.5;c.fill=GridBagConstraints.BOTH;
// Add the titlePanel to the mainMenuPanel
add(titlePanel, c);
/** Creation of the buttonPanel **/
// Creation of the list of the definition of the buttons of the buttonPanel
// To add a button, just add an element to buttonDefList
// Note: Do not to put more than 15 button
ArrayList<buttonPanelButtonDefinition> buttonDefList = new ArrayList<buttonPanelButtonDefinition>();
buttonDefList.add(new buttonPanelButtonDefinition("single player",singlePlayerButtonListener));
buttonDefList.add(new buttonPanelButtonDefinition("multi player",multiPlayerButtonListener));
buttonDefList.add(new buttonPanelButtonDefinition("exit",exitButtonListener));
JPanel buttonPanel = makeButtonPanel(buttonDefList);
// Create the GridBagConstraint for buttonPanel in mainMenuPanel
c = new GridBagConstraints();
c.gridx=0; c.gridy=1; c.gridwidth=1; c.gridheight=1;c.weightx=1;c.weighty=10;c.fill=GridBagConstraints.BOTH;
// Add the buttonPanel to the mainMenuPanel
add(buttonPanel, c);
/** Creation of the ensLabelPanel **/
JPanel ensLabelPanel = new JPanel();
ensLabelPanel.setLayout(new BorderLayout());
ensLabelPanel.setOpaque(true);
ensLabelPanel.setBackground(tawnyPort);
//ENSLabel
JLabel ensLabel = new JLabel();
ensLabel.setFont(arial_20);
ensLabel.setText("V1.0 - ENS team");
ensLabel.setForeground(gamboge);
ensLabelPanel.add(ensLabel, BorderLayout.LINE_START);
// Create the GridBagConstraint for andLabelPanel in mainMenuPanel
c = new GridBagConstraints();
c.gridx=0; c.gridy=2; c.gridwidth=1; c.gridheight=1;c.weightx=0;c.weighty=0;c.fill=GridBagConstraints.BOTH;
// Add the buttonPanel to the mainMenuPanel
add(ensLabelPanel, c);
}
/**
* This class defines the buttons that can be displayed in the central row of the MainMenuPanel
*/
private class buttonPanelButtonDefinition {
private String buttonText;
private ActionListener buttonActionListener;
buttonPanelButtonDefinition(String buttonText, ActionListener buttonActionListener) {
this.buttonText = buttonText;
this.buttonActionListener = buttonActionListener;
}
public String getButtonText() {
return buttonText;
}
public ActionListener getButtonActionListener() {
return buttonActionListener;
}
}
/**
* This method creates a buttonPanel to be displayed on the MainMenuPanel
* @param buttonDefList: A list of buttonPanelButtonDefinition
* @return A JPanel with all the asked buttons in the central row (buttonPanel)
*/
private JPanel makeButtonPanel(ArrayList<buttonPanelButtonDefinition> buttonDefList) {
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
buttonPanel.setOpaque(true);
buttonPanel.setBackground(GUIColorsAndFonts.backGround1);
ArrayList<Component> verticalBoxesList = new ArrayList<Component>();
ArrayList<Component> horizontalBoxesList = new ArrayList<Component>();
ArrayList<JComponent> buttonList = new ArrayList<JComponent>();
/** Create vertical boxes **/
//Add an initial verticalBox, but do not put it in verticalBoxesList.
Component firstVerticalBox = Box.createVerticalGlue();
//For each buttonDef in buttonDefList, create a verticalBox
for (int i = 0;i < buttonDefList.size();i++) {
verticalBoxesList.add(Box.createVerticalGlue());
}
/** Create horizontal boxes **/
//For each buttonDef in buttonDefList, create 2 horizontallBox (one on each side of the button
for (int i = 0;i < buttonDefList.size(); i++) {
horizontalBoxesList.add(Box.createHorizontalGlue());
horizontalBoxesList.add(Box.createHorizontalGlue());
}
/** Create buttons **/
//Create all the button whose definition are in buttonDefList
JButton buttonUnderConstruction;
for (buttonPanelButtonDefinition buttonDef : buttonDefList) {
buttonUnderConstruction = new JButton();
buttonUnderConstruction.setFont(GUIColorsAndFonts.buttonFont1);
buttonUnderConstruction.setText(buttonDef.getButtonText());
buttonUnderConstruction.setForeground(GUIColorsAndFonts.fontColor1);
buttonUnderConstruction.setOpaque(true);
buttonUnderConstruction.setBackground(GUIColorsAndFonts.buttonBackground1);
buttonUnderConstruction.addActionListener(buttonDef.getButtonActionListener());
buttonList.add(buttonUnderConstruction);
}
/** Compute the weight of the components **/
int buttonWeightX = 1;
int buttonWeightY = 1;
int verticalBoxWeightX = 1;
int verticalBoxWeightY = (15 * (buttonWeightY * buttonDefList.size())) / (buttonDefList.size() + 1);
int horizontalBoxWeightX = 3;
int horizontalBoxWeightY = 1;
/** Create the gridBagConstraints templates **/
//hbc -> horizontalBoxConstraint
GridBagConstraints hbc = new GridBagConstraints();
hbc.gridwidth=1;hbc.gridheight=1;hbc.weightx=horizontalBoxWeightX;hbc.weighty=horizontalBoxWeightY;
//vbc -> verticalBoxConstraint
GridBagConstraints vbc = new GridBagConstraints();
vbc.gridwidth=1;vbc.gridheight=1;vbc.weightx=verticalBoxWeightX;vbc.weighty=verticalBoxWeightY;
//bc -> buttonBoxConstraint
GridBagConstraints bc = new GridBagConstraints();
bc.gridwidth=1;bc.gridheight=1;bc.weightx=buttonWeightX;bc.weighty=buttonWeightY;bc.fill=GridBagConstraints.BOTH;
/** Add all the component to the buttonPanel **/
//Add the first vertical box
vbc.gridx = 1; vbc.gridy = 0;
buttonPanel.add(firstVerticalBox, vbc);
//Add the other components
for (int i = 0; i < buttonDefList.size(); i++) {
hbc.gridx = 0; hbc.gridy = 2*i+1;
buttonPanel.add(horizontalBoxesList.get(2*i), hbc);
hbc.gridx = 2; hbc.gridy = 2*i+1;
buttonPanel.add(horizontalBoxesList.get(2*i + 1), hbc);
bc.gridx = 1; bc.gridy = 2*i+1;
buttonPanel.add(buttonList.get(i), bc);
vbc.gridx = 1; vbc.gridy = 2*i+2;
buttonPanel.add(verticalBoxesList.get(i), vbc);
}
return buttonPanel;
}
private ActionListener singlePlayerButtonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
getGraphicsMaster().changeGUIStateTo(GraphicsMaster.GUIStates.SINGLEPLAYERPANEL);
NetworkConnection localConnection=new LocalConnection();
DummyServerLoop.getInstance().setNetworkConnection(localConnection);
DummyLocalGameLoop.getInstance().setNetworkConnection(localConnection);
DummyLocalGameLoop.getInstance().startGame(GameType.SINGLEPLAYER);
//this is start not run like in the DummyServer, because here we are dealing with a thread
DummyServerLoop.getInstance().start();
}
};
private ActionListener multiPlayerButtonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
getGraphicsMaster().changeGUIStateTo(GraphicsMaster.GUIStates.MULTIPLAYERPANEL);
DummyLocalGameLoop.getInstance().startGame(GameType.MULTIPLAYER);
}
};
private ActionListener exitButtonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
getGraphicsMaster().exitGUI();
}
};
@Override
public void keyPressedHandler(Input e) {
}
@Override
public void keyReleasedHandler(Input e) {
}
@Override
public void keyTypedHandler(Input e) {
}
@Override
public void initialise() {
// underlyingMainMenuPanel.setPreferredSize(new Dimension(getSize().width, getSize().height));
// System.out.println("Dimensions= "+getSize().width+", "+getSize().height);
// setSize(new Dimension(getSize().width, getSize().height));
}