Launch game

This commit is contained in:
Yohann D'ANELLO 2020-02-25 02:12:13 +01:00
parent 1c3b887daf
commit 2c1b6a4423
5 changed files with 125 additions and 4 deletions

View File

@ -109,7 +109,7 @@ public class EditorAPI {
return open(file);
}
public static Map open(File f) {
public static RawMap getRawMap(File f) {
String json = null;
try {
GZIPInputStream gis = new GZIPInputStream(new BufferedInputStream(new FileInputStream(f)));
@ -126,9 +126,11 @@ public class EditorAPI {
e.printStackTrace();
}
RawMap rm = createGson().fromJson(json, RawMap.class);
return createGson().fromJson(json, RawMap.class);
}
return open(rm);
public static Map open(File f) {
return open(getRawMap(f));
}
public static Map open(RawMap map) {

View File

@ -4,6 +4,7 @@ import fr.ynerant.leveleditor.api.editor.EditorAPI;
import fr.ynerant.leveleditor.api.editor.RawMap;
import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister;
import fr.ynerant.leveleditor.frame.MainFrame;
import fr.ynerant.leveleditor.game.GameFrame;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
@ -210,6 +211,21 @@ public class Main {
return true;
}
public static boolean launchGameMode() {
System.out.println("Lancement du jeu ...");
JFileChooser jfc = EditorAPI.createJFC();
jfc.showOpenDialog(MainFrame.getInstance());
if (jfc.getSelectedFile() == null)
return false;
RawMap map = EditorAPI.getRawMap(jfc.getSelectedFile());
System.out.println(map);
new GameFrame(map);
return true;
}
/**
* Accesseur disant si le jeu est lancé en développement ou non.
*

View File

@ -69,7 +69,7 @@ public class CollidPanel extends JPanel {
}
}
private boolean isEmpty(BufferedImage image) {
public static boolean isEmpty(BufferedImage image) {
int allrgba = 0;
for (int x = 0; x < image.getWidth(); ++x) {

View File

@ -1,6 +1,8 @@
package fr.ynerant.leveleditor.frame;
import fr.ynerant.leveleditor.api.editor.EditorAPI;
import fr.ynerant.leveleditor.client.main.Main;
import fr.ynerant.leveleditor.editor.EditorFrame;
import fr.ynerant.leveleditor.frame.listeners.ChangeLAFListener;
import fr.ynerant.leveleditor.frame.listeners.CreateMapListener;
import fr.ynerant.leveleditor.frame.listeners.OpenMapListener;
@ -9,6 +11,8 @@ import org.apache.logging.log4j.core.Logger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
/**
@ -86,6 +90,16 @@ public class MainFrame extends JFrame {
menuBar.add(display);
this.setJMenuBar(menuBar);
JButton start = new JButton("Commencer la partie !");
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (Main.launchGameMode())
getInstance().dispose();
}
});
this.setContentPane(start);
}
/**

View File

@ -0,0 +1,89 @@
package fr.ynerant.leveleditor.game;
import fr.ynerant.leveleditor.api.editor.RawCase;
import fr.ynerant.leveleditor.api.editor.RawMap;
import fr.ynerant.leveleditor.api.editor.sprites.Sprite;
import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister;
import fr.ynerant.leveleditor.editor.CollidPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class GameFrame extends JFrame implements WindowListener {
private final RawMap map;
public GameFrame(RawMap map) {
super("Jeu");
this.map = map;
this.setSize(600, 600);
this.setPreferredSize(getSize());
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setLocationRelativeTo(null);
this.addWindowListener(this);
this.setContentPane(new Grid());
setVisible(true);
repaint();
}
public RawMap getMap() {
return map;
}
private class Grid extends JPanel {
@Override
protected void paintComponent(Graphics _g) {
Graphics2D g = (Graphics2D) _g;
if (getMap().getFont() != null)
g.drawImage(getMap().getFont(), null, null);
SpriteRegister.refreshAllSprites();
int SPRITE_SIZE = 32;
for (RawCase c : getMap().getCases()) {
Sprite s1 = SpriteRegister.getCategory(c.getCoucheOne().getCategory()).getSprites().get(c.getCoucheOne().getIndex());
Sprite s2 = SpriteRegister.getCategory(c.getCoucheTwo().getCategory()).getSprites().get(c.getCoucheTwo().getIndex());
Sprite s3 = SpriteRegister.getCategory(c.getCoucheThree().getCategory()).getSprites().get(c.getCoucheThree().getIndex());
g.drawImage(s1.getImage(), SPRITE_SIZE * c.getPosX(), SPRITE_SIZE * c.getPosY(), SPRITE_SIZE, SPRITE_SIZE, Color.white, null);
if (!CollidPanel.isEmpty(s2.getImage()))
g.drawImage(s2.getImage(), SPRITE_SIZE * c.getPosX(), SPRITE_SIZE * c.getPosY(), SPRITE_SIZE, SPRITE_SIZE, null, null);
if (!CollidPanel.isEmpty(s3.getImage()))
g.drawImage(s3.getImage(), SPRITE_SIZE * c.getPosX(), SPRITE_SIZE * c.getPosY(), SPRITE_SIZE, SPRITE_SIZE, null, null);
}
}
}
@Override
public void windowActivated(WindowEvent event) {
}
@Override
public void windowClosed(WindowEvent event) {
}
@Override
public void windowClosing(WindowEvent event) {
dispose();
}
@Override
public void windowDeactivated(WindowEvent event) {
}
@Override
public void windowDeiconified(WindowEvent event) {
}
@Override
public void windowIconified(WindowEvent event) {
}
@Override
public void windowOpened(WindowEvent event) {
}
}