Add mobs
This commit is contained in:
parent
2c1b6a4423
commit
07221a09f2
|
@ -26,4 +26,8 @@ public class Category {
|
||||||
return sprites;
|
return sprites;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,32 +19,27 @@ public class SpriteRegister {
|
||||||
private static Map<String, List<List<Double>>> nameToCoords;
|
private static Map<String, List<List<Double>>> nameToCoords;
|
||||||
private static final Map<String, Category> sprites = new HashMap<>();
|
private static final Map<String, Category> sprites = new HashMap<>();
|
||||||
|
|
||||||
public static void unpack() throws IOException, URISyntaxException {
|
public static void unpack() throws IOException {
|
||||||
if (Main.isInDevelopmentMode()) {
|
@SuppressWarnings("deprecation")
|
||||||
File dir = new File(SpriteRegister.class.getResource("/assets").toURI());
|
String path = URLDecoder.decode(SpriteRegister.class.getProtectionDomain().getCodeSource().getLocation().getPath());
|
||||||
unpackDir(dir);
|
path = path.substring(1);
|
||||||
} else {
|
File jarFile = new File(path);
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
String path = URLDecoder.decode(SpriteRegister.class.getProtectionDomain().getCodeSource().getLocation().getPath());
|
|
||||||
path = path.substring(1);
|
|
||||||
File jarFile = new File(path);
|
|
||||||
|
|
||||||
if (jarFile.isFile()) {
|
if (jarFile.isFile()) {
|
||||||
JarFile jar = new JarFile(jarFile);
|
JarFile jar = new JarFile(jarFile);
|
||||||
Enumeration<JarEntry> entries = jar.entries();
|
Enumeration<JarEntry> entries = jar.entries();
|
||||||
while (entries.hasMoreElements()) {
|
while (entries.hasMoreElements()) {
|
||||||
JarEntry je = entries.nextElement();
|
JarEntry je = entries.nextElement();
|
||||||
String name = je.getName();
|
String name = je.getName();
|
||||||
if (name.startsWith("assets/")) {
|
if (name.startsWith("assets/")) {
|
||||||
File f = new File(name);
|
File f = new File(name);
|
||||||
if (name.endsWith("/"))
|
if (name.endsWith("/"))
|
||||||
assert f.mkdirs();
|
assert f.mkdirs();
|
||||||
else if (!f.isFile())
|
else if (!f.isFile())
|
||||||
Files.copy(jar.getInputStream(je), Paths.get(f.toURI()));
|
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()) {
|
for (String key : nameToCoords.keySet()) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File(f, key + ".png")));
|
BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File(f, key + ".png")));
|
||||||
BufferedImage img = ImageIO.read(is);
|
BufferedImage img = ImageIO.read(is);
|
||||||
Category cat = Category.create(key, new ArrayList<>());
|
Category cat = Category.create(key, new ArrayList<>());
|
||||||
|
|
|
@ -92,7 +92,7 @@ public class Main {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
SpriteRegister.unpack();
|
SpriteRegister.unpack();
|
||||||
} catch (IOException | URISyntaxException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,7 +219,6 @@ public class Main {
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
RawMap map = EditorAPI.getRawMap(jfc.getSelectedFile());
|
RawMap map = EditorAPI.getRawMap(jfc.getSelectedFile());
|
||||||
System.out.println(map);
|
|
||||||
|
|
||||||
new GameFrame(map);
|
new GameFrame(map);
|
||||||
|
|
||||||
|
|
|
@ -10,10 +10,17 @@ import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
import java.awt.event.WindowListener;
|
import java.awt.event.WindowListener;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class GameFrame extends JFrame implements WindowListener {
|
public class GameFrame extends JFrame implements WindowListener {
|
||||||
|
private final Random RANDOM = new Random();
|
||||||
private final RawMap map;
|
private final RawMap map;
|
||||||
|
|
||||||
|
private int round = 0;
|
||||||
|
private List<Mob> mobs = new ArrayList<>();
|
||||||
|
|
||||||
public GameFrame(RawMap map) {
|
public GameFrame(RawMap map) {
|
||||||
super("Jeu");
|
super("Jeu");
|
||||||
|
|
||||||
|
@ -55,6 +62,22 @@ public class GameFrame extends JFrame implements WindowListener {
|
||||||
if (!CollidPanel.isEmpty(s3.getImage()))
|
if (!CollidPanel.isEmpty(s3.getImage()))
|
||||||
g.drawImage(s3.getImage(), SPRITE_SIZE * c.getPosX(), SPRITE_SIZE * c.getPosY(), SPRITE_SIZE, SPRITE_SIZE, null, null);
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 |
|
@ -318,5 +318,14 @@
|
||||||
80,
|
80,
|
||||||
192
|
192
|
||||||
]
|
]
|
||||||
|
],
|
||||||
|
"mob1": [
|
||||||
|
[0, 0]
|
||||||
|
],
|
||||||
|
"mob2": [
|
||||||
|
[0, 0]
|
||||||
|
],
|
||||||
|
"mobcancer": [
|
||||||
|
[0, 0]
|
||||||
]
|
]
|
||||||
}
|
}
|
Loading…
Reference in New Issue