public Game(final MainFrame mainFrame, final GameManager gameManager, ImageProvider imageProvider) {
this.setLayout(null);
dragX=0;
dragY=0;
direction=-1;
setPress= false;
drawBackground=false;
this.mainFrame = mainFrame;
this.gameManager = gameManager;
this.imageProvider = imageProvider;
this.wManager = gameManager.getWorldManager();
this.currentWorld= gameManager.getCurrentWorld();
inserts = this.getInsets();
worldHeight = gameManager.getCurrentWorld().getData().length;
worldWidth = gameManager.getCurrentWorld().getData().length;
width = imageProvider.getImage().getWidth(this);
height = imageProvider.getImage().getHeight(this);
paintPauseButton();
this.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {}
@Override
public void mouseDragged(MouseEvent e)
{
if(setPress == false){
dragY = e.getX()/width;
dragX = e.getY()/height;
setPress=true ;
}
else{
lastY = e.getX()/width;
lastX = e.getY()/height;
}
repaint();
e.consume();
}
});
this.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
int clickY = e.getX()/width;
int clickX = e.getY()/height;
JOptionPane.showMessageDialog(mainFrame, "ERROR", "Message", JOptionPane.CLOSED_OPTION);
repaint();
e.consume();
}
@Override
public void mouseReleased(MouseEvent e){
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(lastX < pressX){ //UP
checkMoves();
direction = 0;
wManager.move(Game.this.gameManager.set, direction, wManager.worldTmp, dragX, dragY);
setPress = false;
drawBackground=false;
else if(lastX > pressX){ //DOWN
checkMoves();
direction = 1;
wManager.move(Game.this.gameManager.set, direction, wManager.worldTmp, dragX, dragY);
setPress = false;
drawBackground=false;
}
else if(lastY < pressY ){ //LEFT
checkMoves();
direction = 2;
wManager.move(Game.this.gameManager.set, direction, wManager.worldTmp, dragX, dragY);
setPress = false;
drawBackground=false;
}
else{
checkMoves();
direction = 3;
wManager.move(Game.this.gameManager.set, direction, wManager.worldTmp, dragX, dragY);
setPress=false;
drawBackground=false;
}
repaint();
e.consume();
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e){
pressX = e.getY()/height;
pressY = e.getX()/width;
drawBackground = true;
repaint();
e.consume();
}
});
}
private void paintTime(Graphics g, int worldHeight2, int worldWidth2) {
g.drawImage(imageProvider.getTime(), worldWidth2 + 1000, worldHeight2 + 300, this);
f = new Font("Cooper Black", Font.BOLD, 90);
g.setFont(f);
g.setColor(Color.RED);
//Here I have to draw the string that will represent the passing of time
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(imageProvider.getBackgroundGame(), 0, 0, getWidth(), getHeight(), this);
//this function draws a map of my game
paintMap(g, wManager.worldTmp, worldHeight, worldWidth);
paintTime(g, worldHeight, worldWidth);
}