Skip to content

Commit

Permalink
Some more implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Jan 11, 2024
1 parent 0e3cc5f commit 0415a16
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 28 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
75 changes: 53 additions & 22 deletions app/src/main/java/software/onepiece/javarcade/bomb/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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<SpritePosition> 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<Spot> 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();
}
}
8 changes: 8 additions & 0 deletions inhabitant-floor/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions level-classic/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module software.onepiece.javarcade.level.classic {
requires transitive software.onepiece.javarcade.model;

exports software.onepiece.javarcade.level.classic;

provides software.onepiece.javarcade.model.Level
with software.onepiece.javarcade.level.classic.ClassicLevel;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package software.onepiece.javarcade.level.classic;

import software.onepiece.javarcade.model.Level;

public class ClassicLevel extends Level {
@Override
protected String define() {
return """
..............
.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.
..............
""";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package software.onepiece.javarcade.model;

import java.io.InputStream;

public interface Inhabitant {
Inhabitant EMPTY = new Inhabitant() {
@Override
public char getRef() {
return ' ';
}

@Override
public InputStream getImage() {
return null;
}
};

char getRef();

InputStream getImage();

}
20 changes: 20 additions & 0 deletions model/src/main/java/software/onepiece/javarcade/model/Level.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package software.onepiece.javarcade.model;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public abstract class Level {

protected abstract String define();

public List<Spot> render() {
List<Integer> 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());
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -20,31 +33,31 @@ public int getY() {
return y;
}

public SpritePosition moveRight() {
public Spot moveRight() {
x++;
if (x >= MATRIX_WIDTH) {
x = MATRIX_WIDTH - 1;
}
return this;
}

public SpritePosition moveLeft() {
public Spot moveLeft() {
x--;
if (x < 0) {
x = 0;
}
return this;
}

public SpritePosition moveDown() {
public Spot moveDown() {
y++;
if (y >= MATRIX_HEIGHT) {
y = MATRIX_HEIGHT - 1;
}
return this;
}

public SpritePosition moveUp() {
public Spot moveUp() {
y--;
if (y < 0) {
y = 0;
Expand Down

0 comments on commit 0415a16

Please sign in to comment.