This commit is contained in:
Yohann D'ANELLO 2020-02-25 03:22:32 +01:00
parent 2c1b6a4423
commit 07221a09f2
10 changed files with 101 additions and 26 deletions

View File

@ -26,4 +26,8 @@ public class Category {
return sprites;
}
@Override
public String toString() {
return name;
}
}

View File

@ -19,32 +19,27 @@ public class SpriteRegister {
private static Map<String, List<List<Double>>> nameToCoords;
private static final Map<String, Category> 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<JarEntry> 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<JarEntry> 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<>());

View File

@ -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);

View File

@ -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<Mob> 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();
}
}

View File

@ -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<String> 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 +
'}';
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 760 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 731 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 732 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

View File

@ -318,5 +318,14 @@
80,
192
]
],
"mob1": [
[0, 0]
],
"mob2": [
[0, 0]
],
"mobcancer": [
[0, 0]
]
}