From 0415a16bf33663c1e321f8c34ed8591cd8758b9f Mon Sep 17 00:00:00 2001 From: Jendrik Johannes Date: Thu, 11 Jan 2024 15:27:12 +0100 Subject: [PATCH] Some more implementation --- app/src/main/java/module-info.java | 4 + .../onepiece/javarcade/bomb/Main.java | 75 +++++++++++++----- .../src/main/java/module-info.java | 8 ++ .../javarcade/inhabitant/floor/Floor.java | 17 ++++ .../inhabitant/floor/assets/floor.png | Bin 0 -> 143 bytes level-classic/src/main/java/module-info.java | 8 ++ .../javarcade/level/classic/ClassicLevel.java | 25 ++++++ .../onepiece/javarcade/model/Inhabitant.java | 22 +++++ .../onepiece/javarcade/model/Level.java | 20 +++++ .../model/{SpritePosition.java => Spot.java} | 25 ++++-- 10 files changed, 176 insertions(+), 28 deletions(-) create mode 100644 inhabitant-floor/src/main/java/module-info.java create mode 100644 inhabitant-floor/src/main/java/software/onepiece/javarcade/inhabitant/floor/Floor.java create mode 100644 inhabitant-floor/src/main/resources/software/onepiece/javarcade/inhabitant/floor/assets/floor.png create mode 100644 level-classic/src/main/java/module-info.java create mode 100644 level-classic/src/main/java/software/onepiece/javarcade/level/classic/ClassicLevel.java create mode 100644 model/src/main/java/software/onepiece/javarcade/model/Inhabitant.java create mode 100644 model/src/main/java/software/onepiece/javarcade/model/Level.java rename model/src/main/java/software/onepiece/javarcade/model/{SpritePosition.java => Spot.java} (57%) diff --git a/app/src/main/java/module-info.java b/app/src/main/java/module-info.java index 9ef5d4d..39277de 100644 --- a/app/src/main/java/module-info.java +++ b/app/src/main/java/module-info.java @@ -13,12 +13,16 @@ requires /*runtime*/ software.onepiece.javarcade.character.bigs; requires /*runtime*/ software.onepiece.javarcade.character.wedge; + requires /*runtime*/ software.onepiece.javarcade.inhabitant.floor; + requires /*runtime*/ software.onepiece.javarcade.level.classic; requires /*runtime*/ io.netty.transport.epoll.linux.aarch_64; requires /*runtime*/ io.netty.transport.epoll.linux.x86_64; requires /*runtime*/ org.slf4j.simple; uses software.onepiece.javarcade.model.Character; + uses software.onepiece.javarcade.model.Inhabitant; + uses software.onepiece.javarcade.model.Level; exports software.onepiece.javarcade.bomb to javafx.base, diff --git a/app/src/main/java/software/onepiece/javarcade/bomb/Main.java b/app/src/main/java/software/onepiece/javarcade/bomb/Main.java index 42a6929..81fb467 100644 --- a/app/src/main/java/software/onepiece/javarcade/bomb/Main.java +++ b/app/src/main/java/software/onepiece/javarcade/bomb/Main.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.jakarta.rs.json.annotation.JSONP; import jakarta.activation.CommandObject; +import jakarta.activation.DataHandler; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; @@ -15,14 +16,18 @@ import org.example.lib.Lib; import org.slf4j.LoggerFactory; import software.onepiece.javarcade.model.Character; -import software.onepiece.javarcade.model.SpritePosition; +import software.onepiece.javarcade.model.Inhabitant; +import software.onepiece.javarcade.model.Level; +import software.onepiece.javarcade.model.Spot; +import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.ServiceLoader; -import static software.onepiece.javarcade.model.SpritePosition.MATRIX_HEIGHT; -import static software.onepiece.javarcade.model.SpritePosition.MATRIX_WIDTH; +import static java.util.Objects.requireNonNull; +import static software.onepiece.javarcade.model.Spot.MATRIX_HEIGHT; +import static software.onepiece.javarcade.model.Spot.MATRIX_WIDTH; public class Main extends Application { // Services: @@ -33,16 +38,7 @@ public class Main extends Application { // - Item // - Level - - @JSONP - @Deprecated - public static void doWork() { - CommandObject o = null; - new Lib(); - } - public static void main(String[] args) { - Configuration conf = new CombinedConfiguration(); LoggerFactory.getLogger(Main.class).info("App running..."); System.out.println("Module Name: " + Main.class.getModule().getName()); doWork(); @@ -67,17 +63,21 @@ public void start(Stage stage) { GraphicsContext ctx = canvas.getGraphicsContext2D(); ctx.setImageSmoothing(false); - Image leftWall = new Image(getClass().getResourceAsStream("/wall_left.png"), CELL_WIDTH, CELL_HEIGHT, true, false); - Image rightWall = new Image(getClass().getResourceAsStream("/wall_right.png"), CELL_WIDTH, CELL_HEIGHT, true, false); + Image leftWall = new Image(requireNonNull(getClass().getResourceAsStream("/wall_left.png")), CELL_WIDTH, CELL_HEIGHT, true, false); + Image rightWall = new Image(requireNonNull(getClass().getResourceAsStream("/wall_right.png")), CELL_WIDTH, CELL_HEIGHT, true, false); for (int y = 0; y < MATRIX_HEIGHT; y++) { drawSprite(leftWall, 0, y, ctx); drawSprite(rightWall, 1 + MATRIX_WIDTH, y, ctx); } ServiceLoader.load(Character.class).forEach(c -> { - SpritePosition pos = characterStartPositions.removeFirst(); - drawSprite(new Image(c.getImage(), CELL_WIDTH, CELL_HEIGHT, true, false), 1 + pos.getX(), pos.getY(), ctx); - } ); + Spot spot = characterStartPositions.removeFirst(); + drawSprite(imageFor(c.getImage()), 1 + spot.getX(), spot.getY(), ctx); + }); + + ServiceLoader.load(Level.class).forEach(level -> { + level.render().forEach(spot -> drawSprite(imageFor(inhabitantForRef(spot).getImage()), 1 + spot.getX(), spot.getY(), ctx)); + }); StackPane pane = new StackPane(canvas); Scene scene = new Scene(pane, WIDTH, HEIGHT); @@ -89,15 +89,46 @@ public void start(Stage stage) { stage.setResizable(false); } + private Image imageFor(InputStream stream) { + if (stream == null) { + return null; + } + return new Image(stream, CELL_WIDTH, CELL_HEIGHT, true, false); + } + + private Inhabitant inhabitantForRef(Spot spot) { + return ServiceLoader.load(Inhabitant.class).stream().map(ServiceLoader.Provider::get).filter(i -> + i.getRef() == spot.getInhabitantRef() + ).findFirst().orElse(Inhabitant.EMPTY); + } + private void drawSprite(Image img, int x, int y, GraphicsContext ctx) { + if (img == null) { + return; + } ctx.drawImage(img, x * CELL_WIDTH * SCALE, y * CELL_HEIGHT * SCALE, CELL_WIDTH * SCALE, CELL_HEIGHT * SCALE); } - private final List characterStartPositions = new ArrayList<>(List.of( - new SpritePosition(0, 0), - new SpritePosition(MATRIX_WIDTH - 1, MATRIX_HEIGHT - 1), - new SpritePosition(0, MATRIX_HEIGHT - 1), - new SpritePosition(MATRIX_WIDTH - 1, 0) + private final List characterStartPositions = new ArrayList<>(List.of( + new Spot(' ', 0, 0), + new Spot(' ', MATRIX_WIDTH - 1, MATRIX_HEIGHT - 1), + new Spot(' ', 0, MATRIX_HEIGHT - 1), + new Spot(' ', MATRIX_WIDTH - 1, 0) )); + + @JSONP + @Deprecated + public static void doWork() { + Configuration conf = new CombinedConfiguration(); + CommandObject o = new CommandObject() { + @Override + public void setCommandContext(String s, DataHandler dataHandler) { + } + }; + if (o.toString().equals(conf.toString())) { + return; + } + new Lib(); + } } \ No newline at end of file diff --git a/inhabitant-floor/src/main/java/module-info.java b/inhabitant-floor/src/main/java/module-info.java new file mode 100644 index 0000000..6ed70da --- /dev/null +++ b/inhabitant-floor/src/main/java/module-info.java @@ -0,0 +1,8 @@ +module software.onepiece.javarcade.inhabitant.floor { + requires transitive software.onepiece.javarcade.model; + + exports software.onepiece.javarcade.inhabitant.floor; + + provides software.onepiece.javarcade.model.Inhabitant + with software.onepiece.javarcade.inhabitant.floor.Floor; +} \ No newline at end of file diff --git a/inhabitant-floor/src/main/java/software/onepiece/javarcade/inhabitant/floor/Floor.java b/inhabitant-floor/src/main/java/software/onepiece/javarcade/inhabitant/floor/Floor.java new file mode 100644 index 0000000..7a22f52 --- /dev/null +++ b/inhabitant-floor/src/main/java/software/onepiece/javarcade/inhabitant/floor/Floor.java @@ -0,0 +1,17 @@ +package software.onepiece.javarcade.inhabitant.floor; + +import software.onepiece.javarcade.model.Inhabitant; + +import java.io.InputStream; + +public class Floor implements Inhabitant { + @Override + public char getRef() { + return 'x'; + } + + @Override + public InputStream getImage() { + return getClass().getResourceAsStream("assets/floor.png"); + } +} diff --git a/inhabitant-floor/src/main/resources/software/onepiece/javarcade/inhabitant/floor/assets/floor.png b/inhabitant-floor/src/main/resources/software/onepiece/javarcade/inhabitant/floor/assets/floor.png new file mode 100644 index 0000000000000000000000000000000000000000..bec979d97eeeeb13fdf35a178da78f81a4875151 GIT binary patch literal 143 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!93?!50ihlx9Ea{HEjtmSN`?>!lvI6;>0X`wF z2TtD!N~r0cy~WD^WCl>s(9^{+gyVWL2P;!sXt49*1xMC|Zk9NZl(l-Ap@B~H)*R6m nhVE831D{lm-Y8Si render() { + List symbols = Arrays.stream(define().split("\n")).flatMap(line -> line.chars().boxed()).toList(); + + return IntStream.range(0, Spot.MATRIX_WIDTH * Spot.MATRIX_HEIGHT).mapToObj(p -> new Spot( + p < symbols.size() ? (char) symbols.get(p).intValue() : ' ', p) + ).collect(Collectors.toList()); + } + +} diff --git a/model/src/main/java/software/onepiece/javarcade/model/SpritePosition.java b/model/src/main/java/software/onepiece/javarcade/model/Spot.java similarity index 57% rename from model/src/main/java/software/onepiece/javarcade/model/SpritePosition.java rename to model/src/main/java/software/onepiece/javarcade/model/Spot.java index 57a64bd..887395d 100644 --- a/model/src/main/java/software/onepiece/javarcade/model/SpritePosition.java +++ b/model/src/main/java/software/onepiece/javarcade/model/Spot.java @@ -1,17 +1,30 @@ package software.onepiece.javarcade.model; -public class SpritePosition { +public class Spot { public static final int MATRIX_WIDTH = 14; public static final int MATRIX_HEIGHT = 14; + private final char inhabitantRef; + private int x; private int y; - public SpritePosition(int x, int y) { + public Spot(char inhabitantRef, int x, int y) { + this.inhabitantRef = inhabitantRef; this.x = x; this.y = y; } + public Spot(char inhabitantRef, int posInStream) { + this.inhabitantRef = inhabitantRef; + this.y = posInStream / MATRIX_HEIGHT; + this.x = posInStream - y * MATRIX_WIDTH; + } + + public char getInhabitantRef() { + return inhabitantRef; + } + public int getX() { return x; } @@ -20,7 +33,7 @@ public int getY() { return y; } - public SpritePosition moveRight() { + public Spot moveRight() { x++; if (x >= MATRIX_WIDTH) { x = MATRIX_WIDTH - 1; @@ -28,7 +41,7 @@ public SpritePosition moveRight() { return this; } - public SpritePosition moveLeft() { + public Spot moveLeft() { x--; if (x < 0) { x = 0; @@ -36,7 +49,7 @@ public SpritePosition moveLeft() { return this; } - public SpritePosition moveDown() { + public Spot moveDown() { y++; if (y >= MATRIX_HEIGHT) { y = MATRIX_HEIGHT - 1; @@ -44,7 +57,7 @@ public SpritePosition moveDown() { return this; } - public SpritePosition moveUp() { + public Spot moveUp() { y--; if (y < 0) { y = 0;