diff --git a/src/main/java/fr/ynerant/leveleditor/api/editor/sprites/Category.java b/src/main/java/fr/ynerant/leveleditor/api/editor/sprites/Category.java index 5f336fe..6fd66e6 100644 --- a/src/main/java/fr/ynerant/leveleditor/api/editor/sprites/Category.java +++ b/src/main/java/fr/ynerant/leveleditor/api/editor/sprites/Category.java @@ -26,4 +26,8 @@ public class Category { return sprites; } + @Override + public String toString() { + return name; + } } diff --git a/src/main/java/fr/ynerant/leveleditor/api/editor/sprites/SpriteRegister.java b/src/main/java/fr/ynerant/leveleditor/api/editor/sprites/SpriteRegister.java index d75f759..6539db0 100644 --- a/src/main/java/fr/ynerant/leveleditor/api/editor/sprites/SpriteRegister.java +++ b/src/main/java/fr/ynerant/leveleditor/api/editor/sprites/SpriteRegister.java @@ -19,32 +19,27 @@ public class SpriteRegister { private static Map>> nameToCoords; private static final Map sprites = new HashMap<>(); - public static void unpack() throws IOException, URISyntaxException { - if (Main.isInDevelopmentMode()) { - File dir = new File(SpriteRegister.class.getResource("/assets").toURI()); - unpackDir(dir); - } else { - @SuppressWarnings("deprecation") - String path = URLDecoder.decode(SpriteRegister.class.getProtectionDomain().getCodeSource().getLocation().getPath()); - path = path.substring(1); - File jarFile = new File(path); + public static void unpack() throws IOException { + @SuppressWarnings("deprecation") + String path = URLDecoder.decode(SpriteRegister.class.getProtectionDomain().getCodeSource().getLocation().getPath()); + path = path.substring(1); + File jarFile = new File(path); - if (jarFile.isFile()) { - JarFile jar = new JarFile(jarFile); - Enumeration entries = jar.entries(); - while (entries.hasMoreElements()) { - JarEntry je = entries.nextElement(); - String name = je.getName(); - if (name.startsWith("assets/")) { - File f = new File(name); - if (name.endsWith("/")) - assert f.mkdirs(); - else if (!f.isFile()) - Files.copy(jar.getInputStream(je), Paths.get(f.toURI())); - } + if (jarFile.isFile()) { + JarFile jar = new JarFile(jarFile); + Enumeration entries = jar.entries(); + while (entries.hasMoreElements()) { + JarEntry je = entries.nextElement(); + String name = je.getName(); + if (name.startsWith("assets/")) { + File f = new File(name); + if (name.endsWith("/")) + assert f.mkdirs(); + else if (!f.isFile()) + Files.copy(jar.getInputStream(je), Paths.get(f.toURI())); } - jar.close(); } + jar.close(); } } @@ -86,7 +81,6 @@ public class SpriteRegister { for (String key : nameToCoords.keySet()) { try { - BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File(f, key + ".png"))); BufferedImage img = ImageIO.read(is); Category cat = Category.create(key, new ArrayList<>()); diff --git a/src/main/java/fr/ynerant/leveleditor/client/main/Main.java b/src/main/java/fr/ynerant/leveleditor/client/main/Main.java index 61de694..5e5a996 100644 --- a/src/main/java/fr/ynerant/leveleditor/client/main/Main.java +++ b/src/main/java/fr/ynerant/leveleditor/client/main/Main.java @@ -92,7 +92,7 @@ public class Main { try { SpriteRegister.unpack(); - } catch (IOException | URISyntaxException e) { + } catch (IOException e) { e.printStackTrace(); } @@ -219,7 +219,6 @@ public class Main { return false; RawMap map = EditorAPI.getRawMap(jfc.getSelectedFile()); - System.out.println(map); new GameFrame(map); diff --git a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java index 90c85f6..6bdcf5b 100644 --- a/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java +++ b/src/main/java/fr/ynerant/leveleditor/game/GameFrame.java @@ -10,10 +10,17 @@ import javax.swing.*; import java.awt.*; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; +import java.util.List; +import java.util.ArrayList; +import java.util.Random; public class GameFrame extends JFrame implements WindowListener { + private final Random RANDOM = new Random(); private final RawMap map; + private int round = 0; + private List mobs = new ArrayList<>(); + public GameFrame(RawMap map) { super("Jeu"); @@ -55,6 +62,22 @@ public class GameFrame extends JFrame implements WindowListener { if (!CollidPanel.isEmpty(s3.getImage())) g.drawImage(s3.getImage(), SPRITE_SIZE * c.getPosX(), SPRITE_SIZE * c.getPosY(), SPRITE_SIZE, SPRITE_SIZE, null, null); } + + if (mobs.isEmpty() && round < 4) { + ++round; + for (int i = 1; i <= RANDOM.nextInt(16) + 1; ++i) { + Mob mob = new Mob(); + mob.move(RANDOM.nextInt(getMap().getWidth() / 16), RANDOM.nextInt(getMap().getHeight() / 16)); + mobs.add(mob); + } + } + + for (Mob mob : mobs) { + Sprite s = mob.getSprite(); + g.drawImage(s.getImage(), SPRITE_SIZE * mob.getX(), SPRITE_SIZE * mob.getY(), SPRITE_SIZE, SPRITE_SIZE, null, null); + } + + repaint(); } } diff --git a/src/main/java/fr/ynerant/leveleditor/game/Mob.java b/src/main/java/fr/ynerant/leveleditor/game/Mob.java new file mode 100644 index 0000000..1249ac7 --- /dev/null +++ b/src/main/java/fr/ynerant/leveleditor/game/Mob.java @@ -0,0 +1,46 @@ +package fr.ynerant.leveleditor.game; + +import fr.ynerant.leveleditor.api.editor.sprites.Sprite; +import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister; + +import java.util.Arrays; +import java.util.List; +import java.util.Random; + +public class Mob { + private static final Random RANDOM = new Random(); + private static final List MOB_SPRITES = Arrays.asList("mob1", "mob2", "mobcancer"); + private Sprite sprite; + private int x; + private int y; + + public Mob() { + this.sprite = SpriteRegister.getCategory(MOB_SPRITES.get(RANDOM.nextInt(3))).getSprites().get(0); + } + + public Sprite getSprite() { + return sprite; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public void move(int x, int y) { + this.x = x; + this.y = y; + } + + @Override + public String toString() { + return "Mob{" + + "sprite=" + sprite + + ", x=" + x + + ", y=" + y + + '}'; + } +} diff --git a/src/main/resources/assets/leveleditor/textures/sprites/mob1.png b/src/main/resources/assets/leveleditor/textures/sprites/mob1.png new file mode 100644 index 0000000..3874744 Binary files /dev/null and b/src/main/resources/assets/leveleditor/textures/sprites/mob1.png differ diff --git a/src/main/resources/assets/leveleditor/textures/sprites/mob2.png b/src/main/resources/assets/leveleditor/textures/sprites/mob2.png new file mode 100644 index 0000000..1f0471f Binary files /dev/null and b/src/main/resources/assets/leveleditor/textures/sprites/mob2.png differ diff --git a/src/main/resources/assets/leveleditor/textures/sprites/mobcancer.png b/src/main/resources/assets/leveleditor/textures/sprites/mobcancer.png new file mode 100644 index 0000000..2f3e8a2 Binary files /dev/null and b/src/main/resources/assets/leveleditor/textures/sprites/mobcancer.png differ diff --git a/src/main/resources/assets/leveleditor/textures/sprites/mobs.png b/src/main/resources/assets/leveleditor/textures/sprites/mobs.png new file mode 100644 index 0000000..658f896 Binary files /dev/null and b/src/main/resources/assets/leveleditor/textures/sprites/mobs.png differ diff --git a/src/main/resources/assets/leveleditor/textures/sprites/sprites.json b/src/main/resources/assets/leveleditor/textures/sprites/sprites.json index 2ddaef4..7f2ac5d 100644 --- a/src/main/resources/assets/leveleditor/textures/sprites/sprites.json +++ b/src/main/resources/assets/leveleditor/textures/sprites/sprites.json @@ -318,5 +318,14 @@ 80, 192 ] + ], + "mob1": [ + [0, 0] + ], + "mob2": [ + [0, 0] + ], + "mobcancer": [ + [0, 0] ] } \ No newline at end of file