Skip to content

Commit

Permalink
Structuring
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Jan 15, 2024
1 parent 75eec6c commit 10fe74a
Show file tree
Hide file tree
Showing 20 changed files with 288 additions and 215 deletions.
11 changes: 2 additions & 9 deletions app-javafx/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
// @MainClass("software.onepiece.javarcade.javafx.Main")
open module software.onepiece.javarcade.app.javafx {
requires software.onepiece.javarcade.lib;
requires software.onepiece.javarcade.logic;
requires software.onepiece.javarcade.model;

requires com.fasterxml.jackson.jakarta.rs.json;
requires jakarta.activation;
requires javafx.base;
requires javafx.controls;
requires javafx.graphics;
requires org.slf4j;

requires /*runtime*/ software.onepiece.javarcade.border.blocks;
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.inhabitant.wall;
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.Inhabitant;
uses software.onepiece.javarcade.model.Level;

exports software.onepiece.javarcade.javafx to
javafx.base,
javafx.graphics,
Expand Down

This file was deleted.

145 changes: 45 additions & 100 deletions app-javafx/src/main/java/software/onepiece/javarcade/javafx/Main.java
Original file line number Diff line number Diff line change
@@ -1,85 +1,65 @@
package software.onepiece.javarcade.javafx;

import com.fasterxml.jackson.jakarta.rs.json.annotation.JSONP;
import jakarta.activation.CommandObject;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.example.lib.Lib;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.onepiece.javarcade.engine.GameLoop;
import software.onepiece.javarcade.engine.GameState;
import software.onepiece.javarcade.model.Inhabitant;
import software.onepiece.javarcade.model.Level;
import software.onepiece.javarcade.logic.GameLoop;
import software.onepiece.javarcade.logic.GameState;

import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

import static java.util.Objects.requireNonNull;
import static software.onepiece.javarcade.model.Spot.MATRIX_HEIGHT;
import static software.onepiece.javarcade.model.Spot.MATRIX_WIDTH;
import static software.onepiece.javarcade.model.Spot.PRECISION;
import static software.onepiece.javarcade.logic.GameParameters.CELL_HEIGHT;
import static software.onepiece.javarcade.logic.GameParameters.CELL_HEIGHT_IN_PIXEL;
import static software.onepiece.javarcade.logic.GameParameters.CELL_WIDTH;
import static software.onepiece.javarcade.logic.GameParameters.CELL_WIDTH_IN_PIXEL;
import static software.onepiece.javarcade.logic.GameParameters.HEIGHT_IN_PIXEL;
import static software.onepiece.javarcade.logic.GameParameters.WIDTH_IN_PIXEL;

public class Main extends Application {
// Services:
// - Render engine
// - Branding/Splash/Background
// - Character
// - Wall
// - Item
// - Level
private static final Logger LOG = LoggerFactory.getLogger(Main.class);

public static void main(String[] args) {
LoggerFactory.getLogger(Main.class).info("App running...");
System.out.println("Module Name: " + Main.class.getModule().getName());
doWork();
private final GameState gameState = new GameState();
private final Map<Character, Image> images = new HashMap<>();

public static void main(String[] args) {
LOG.info("App running [" + Main.class.getModule().getName() + "]");
launch(args);
}


private static final int CELL_WIDTH = 16;
private static final int CELL_HEIGHT = 16;
private static final int GAME_WIDTH = MATRIX_WIDTH * CELL_WIDTH;
private static final int GAME_HEIGHT = MATRIX_HEIGHT * CELL_HEIGHT;

private static final int SCALE = 4;

private static final int WIDTH = SCALE * (GAME_WIDTH + CELL_WIDTH * 2);
private static final int HEIGHT = SCALE * GAME_HEIGHT;


private final GameState gameState = new GameState();

@Override
public void start(Stage stage) {
Canvas canvas = new Canvas(WIDTH, HEIGHT);
Canvas canvas = new Canvas(WIDTH_IN_PIXEL, HEIGHT_IN_PIXEL);
GraphicsContext ctx = canvas.getGraphicsContext2D();
ctx.setImageSmoothing(false);
StackPane pane = new StackPane(canvas);

ServiceLoader.load(Inhabitant.class).forEach(inhabitant -> gameState.inhabitants.put(inhabitant.getRef(), inhabitant));
ServiceLoader.load(Level.class).forEach(level -> level.render().forEach(spot -> {
Inhabitant inhabitant = gameState.inhabitants.get(spot.getInhabitantRef());
images.put(spot.getInhabitantRef(), imageFor(inhabitant.getImage()));
if (inhabitant.controllable()) {
gameState.setPlayer(spot);
gameState.state.addLast(spot);
} else {
gameState.state.addFirst(spot);
}
}));
Scene scene = initScene(pane);
initStage(stage, scene);

StackPane pane = new StackPane(canvas);
Scene scene = new Scene(pane, WIDTH, HEIGHT);
gameState.getImages().forEach((symbol, content) -> {
images.put(symbol, new Image(new ByteArrayInputStream(content), CELL_WIDTH, CELL_HEIGHT, true, false));
});

new GameLoop().run(gameState);
new AnimationTimer() {
@Override
public void handle(long now) {
draw(ctx);
}
}.start();
}

private Scene initScene(StackPane pane) {
Scene scene = new Scene(pane, WIDTH_IN_PIXEL, HEIGHT_IN_PIXEL);
scene.setOnKeyPressed(e -> {
switch (e.getCode()) {
case UP -> gameState.setUp(true);
Expand All @@ -98,62 +78,27 @@ public void start(Stage stage) {
case X -> System.out.println("Boom!");
}
});
return scene;
}


new Label("L1");
private void initStage(Stage stage, Scene scene) {
stage.setTitle("JavArcade");
stage.setScene(scene);
stage.show();
stage.setResizable(false);

new GameLoop().run(gameState);
new AnimationTimer() {
@Override
public void handle(long now) {
draw(ctx);
}
}.start();
stage.show();
}


private final Image leftWall = new Image(requireNonNull(getClass().getResourceAsStream("/wall_left.png")), CELL_WIDTH, CELL_HEIGHT, true, false);
private final Image rightWall = new Image(requireNonNull(getClass().getResourceAsStream("/wall_right.png")), CELL_WIDTH, CELL_HEIGHT, true, false);
private final Map<Character, Image> images = new HashMap<>();

private void draw(GraphicsContext ctx) {
ctx.clearRect(CELL_WIDTH * SCALE, 0, WIDTH, HEIGHT);

for (int y = 0; y < MATRIX_HEIGHT; y++) {
drawSprite(leftWall, 0, y * PRECISION, ctx);
drawSprite(rightWall, (1 + MATRIX_WIDTH) * PRECISION, y * PRECISION, ctx);
}
gameState.state.forEach(s -> drawSprite(images.get(s.getInhabitantRef()), s.getX() + PRECISION, s.getY(), ctx));
ctx.clearRect(0, 0, WIDTH_IN_PIXEL, HEIGHT_IN_PIXEL);
gameState.getSpots().forEach(s -> drawSprite(ctx, images.get(s.getInhabitantRef()),
s.getPixelPositionX(CELL_WIDTH_IN_PIXEL),
s.getPixelPositionY(CELL_HEIGHT_IN_PIXEL)
));
}

private Image imageFor(InputStream stream) {
if (stream == null) {
return null;
}
return new Image(stream, CELL_WIDTH, CELL_HEIGHT, true, false);
}

private void drawSprite(Image img, int x, int y, GraphicsContext ctx) {
if (img == null) {
return;
}
ctx.drawImage(img,
(x * CELL_WIDTH * SCALE) * 1f / PRECISION,
(y * CELL_HEIGHT * SCALE) * 1f / PRECISION,
CELL_WIDTH * SCALE, CELL_HEIGHT * SCALE);
}

@JSONP
@Deprecated
public static void doWork() {
CommandObject o = (s, dataHandler) -> { };
if (o.toString().equals("conf")) {
return;
}
new Lib();
private void drawSprite(GraphicsContext ctx, Image img, double x, double y) {
if (img == null) { return; }
ctx.drawImage(img, x, y, CELL_WIDTH_IN_PIXEL, CELL_HEIGHT_IN_PIXEL);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package software.onepiece.javarcade.javafx;

import org.example.lib.Lib;
import org.junit.jupiter.api.Test;
import software.onepiece.javarcade.model.Spot;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -14,7 +14,7 @@ class MainTest {

@Test
void testApp() throws IOException {
assertEquals("software.onepiece.javarcade.lib", Lib.class.getModule().getName());
assertEquals("software.onepiece.javarcade.model", Spot.class.getModule().getName());
assertEquals("software.onepiece.javarcade.app.javafx", Main.class.getModule().getName());
assertEquals("software.onepiece.javarcade.app.javafx", MainTest.class.getModule().getName());
try (InputStream is = MainTest.class.getResourceAsStream("/data.txt")) {
Expand Down
9 changes: 9 additions & 0 deletions border-blocks/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module software.onepiece.javarcade.border.blocks {
requires transitive software.onepiece.javarcade.model;

exports software.onepiece.javarcade.border.blocks;

provides software.onepiece.javarcade.model.Inhabitant with
software.onepiece.javarcade.border.blocks.WallLeft,
software.onepiece.javarcade.border.blocks.WallRight;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package software.onepiece.javarcade.border.blocks;

import software.onepiece.javarcade.model.Inhabitant;

import java.io.InputStream;

public class WallLeft implements Inhabitant {
@Override
public char getRef() {
return '[';
}

@Override
public InputStream getImage() {
return getClass().getResourceAsStream("assets/wall_left.png");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package software.onepiece.javarcade.border.blocks;

import software.onepiece.javarcade.model.Inhabitant;

import java.io.InputStream;

public class WallRight implements Inhabitant {
@Override
public char getRef() {
return ']';
}

@Override
public InputStream getImage() {
return getClass().getResourceAsStream("assets/wall_right.png");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ public class ClassicLevel extends Level {
@Override
protected String define() {
return """
W.............
.xx.xxxxxx.xx.
.x..........x.
.x.xxxxxxxx.x.
.x.xxxxxxxx.x.
.x.xx....xx.x.
.x.xx.xx.xx.x.
.x.xx.xx.xx.x.
.x.xx....xx.x.
.x.xxxxxxxx.x.
.x.xxxxxxxx.x.
.x..........x.
.xx.xxxxxx.xx.
.............B
[W.............]
[.xx.xxxxxx.xx.]
[.x..........x.]
[.x.xxxxxxxx.x.]
[.x.xxxxxxxx.x.]
[.x.xx....xx.x.]
[.x.xx.xx.xx.x.]
[.x.xx.xx.xx.x.]
[.x.xx....xx.x.]
[.x.xxxxxxxx.x.]
[.x.xxxxxxxx.x.]
[.x..........x.]
[.xx.xxxxxx.xx.]
[.............B]
""";
}
}
Loading

0 comments on commit 10fe74a

Please sign in to comment.