Skip to content

Commit

Permalink
Update OJ | more implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Jan 13, 2024
1 parent 0415a16 commit afdb148
Show file tree
Hide file tree
Showing 23 changed files with 130 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
distribution: temurin
java-version-file: .oj/jdk-version.txt
- uses: gradle/[email protected]
- run: "./oj ${{ matrix.os }}Build"
- run: "./oj assemble${{ matrix.os }"
env:
BUILD_CACHE_USER: ${{ secrets.BUILD_CACHE_USER }}
BUILD_CACHE_PWD: ${{ secrets.BUILD_CACHE_PWD }}
Expand Down
File renamed without changes
Binary file added app/resources/macos/icon-background-darkAqua.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/resources/macos/icon-background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Binary file added app/resources/macos/icon.icns
Binary file not shown.
File renamed without changes.
2 changes: 1 addition & 1 deletion app/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
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.Character;
uses software.onepiece.javarcade.model.Inhabitant;
uses software.onepiece.javarcade.model.Level;

Expand Down
82 changes: 47 additions & 35 deletions app/src/main/java/software/onepiece/javarcade/bomb/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
import org.apache.commons.configuration2.Configuration;
import org.example.lib.Lib;
import org.slf4j.LoggerFactory;
import software.onepiece.javarcade.model.Character;
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.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -57,75 +59,85 @@ public static void main(String[] args) {
private static final int WIDTH = SCALE * (GAME_WIDTH + CELL_WIDTH * 2);
private static final int HEIGHT = SCALE * GAME_HEIGHT;

private final Map<Character, Inhabitant> inhabitants = new LinkedHashMap<>();
private final List<Spot> state = new LinkedList<>();
private Spot player;

@Override
public void start(Stage stage) {
Canvas canvas = new Canvas(WIDTH, HEIGHT);
GraphicsContext ctx = canvas.getGraphicsContext2D();
ctx.setImageSmoothing(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 -> {
Spot spot = characterStartPositions.removeFirst();
drawSprite(imageFor(c.getImage()), 1 + spot.getX(), spot.getY(), ctx);
});
ServiceLoader.load(Inhabitant.class).forEach(inhabitant -> inhabitants.put(inhabitant.getRef(), inhabitant));
ServiceLoader.load(Level.class).forEach(level -> level.render().forEach(spot -> {
Inhabitant inhabitant = inhabitants.get(spot.getInhabitantRef());
images.put(spot.getInhabitantRef(), imageFor(inhabitant.getImage()));
if (inhabitant.controllable()) {
player = spot;
state.addLast(spot);
} else {
state.addFirst(spot);
}
}));

ServiceLoader.load(Level.class).forEach(level -> {
level.render().forEach(spot -> drawSprite(imageFor(inhabitantForRef(spot).getImage()), 1 + spot.getX(), spot.getY(), ctx));
});
draw(ctx);

StackPane pane = new StackPane(canvas);
Scene scene = new Scene(pane, WIDTH, HEIGHT);

scene.setOnKeyPressed(e -> {
switch (e.getCode()) {
case UP -> player.moveUp(state, inhabitants);
case DOWN -> player.moveDown(state, inhabitants);
case LEFT -> player.moveLeft(state, inhabitants);
case RIGHT -> player.moveRight(state, inhabitants);
case X -> System.out.println("Boom!");
}
draw(ctx);
});

new Label("L1");
stage.setTitle("JavArcade");
stage.setScene(scene);
stage.show();
stage.setResizable(false);
}


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, ctx);
drawSprite(rightWall, 1 + MATRIX_WIDTH, y, ctx);
}
state.forEach(s -> drawSprite(images.get(s.getInhabitantRef()), s.getX() + 1, s.getY(), ctx));
}

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<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) {
}
};
CommandObject o = (s, dataHandler) -> { };
if (o.toString().equals(conf.toString())) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion character-bigs/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

exports software.onepiece.javarcade.character.bigs;

provides software.onepiece.javarcade.model.Character
provides software.onepiece.javarcade.model.Inhabitant
with software.onepiece.javarcade.character.bigs.Bigs;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package software.onepiece.javarcade.character.bigs;

import software.onepiece.javarcade.model.Character;
import software.onepiece.javarcade.model.Inhabitant;

import java.io.InputStream;

public class Bigs implements Character {
public class Bigs implements Inhabitant {
@Override
public char getRef() {
return 'B';
}

@Override
public InputStream getImage() {
return getClass().getResourceAsStream("assets/bigs.png");
Expand Down
2 changes: 1 addition & 1 deletion character-wedge/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

exports software.onepiece.javarcade.character.wedge;

provides software.onepiece.javarcade.model.Character
provides software.onepiece.javarcade.model.Inhabitant
with software.onepiece.javarcade.character.wedge.Wedge;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package software.onepiece.javarcade.character.wedge;

import software.onepiece.javarcade.model.Character;
import software.onepiece.javarcade.model.Inhabitant;

import java.io.InputStream;

public class Wedge implements Character {
public class Wedge implements Inhabitant {
@Override
public char getRef() {
return 'W';
}

@Override
public InputStream getImage() {
return getClass().getResourceAsStream("assets/wedge.png");
}

@Override
public boolean controllable() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class Floor implements Inhabitant {
@Override
public char getRef() {
return 'x';
return '.';
}

@Override
Expand Down
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 inhabitant-wall/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.wall {
requires transitive software.onepiece.javarcade.model;

exports software.onepiece.javarcade.inhabitant.wall;

provides software.onepiece.javarcade.model.Inhabitant
with software.onepiece.javarcade.inhabitant.wall.Wall;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package software.onepiece.javarcade.inhabitant.wall;

import software.onepiece.javarcade.model.Inhabitant;

import java.io.InputStream;

public class Wall implements Inhabitant {
@Override
public char getRef() {
return 'x';
}

@Override
public InputStream getImage() {
return getClass().getResourceAsStream("assets/wall.png");
}

@Override
public boolean blocks() {
return true;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class ClassicLevel extends Level {
@Override
protected String define() {
return """
..............
W.............
.xx.xxxxxx.xx.
.x..........x.
.x.xxxxxxxx.x.
Expand All @@ -19,7 +19,7 @@ protected String define() {
.x.xxxxxxxx.x.
.x..........x.
.xx.xxxxxx.xx.
..............
.............B
""";
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ public InputStream getImage() {

InputStream getImage();

default boolean controllable() {
return false;
}

default boolean blocks() {
return false;
}
}
27 changes: 19 additions & 8 deletions model/src/main/java/software/onepiece/javarcade/model/Spot.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package software.onepiece.javarcade.model;

import java.util.List;
import java.util.Map;

public class Spot {
public static final int MATRIX_WIDTH = 14;
public static final int MATRIX_HEIGHT = 14;
Expand Down Expand Up @@ -33,35 +36,43 @@ public int getY() {
return y;
}

public Spot moveRight() {
public void moveRight(List<Spot> state, Map<Character, Inhabitant> inhabitants) {
if (state.stream().anyMatch(s -> s.getX() == x + 1 && s.getY() == y && inhabitants.get(s.getInhabitantRef()).blocks())) {
return;
}
x++;
if (x >= MATRIX_WIDTH) {
x = MATRIX_WIDTH - 1;
}
return this;
}

public Spot moveLeft() {
public void moveLeft(List<Spot> state, Map<Character, Inhabitant> inhabitants) {
if (state.stream().anyMatch(s -> s.getX() == x - 1 && s.getY() == y && inhabitants.get(s.getInhabitantRef()).blocks())) {
return;
}
x--;
if (x < 0) {
x = 0;
}
return this;
}

public Spot moveDown() {
public void moveDown(List<Spot> state, Map<Character, Inhabitant> inhabitants) {
if (state.stream().anyMatch(s -> s.getX() == x && s.getY() == y + 1 && inhabitants.get(s.getInhabitantRef()).blocks())) {
return;
}
y++;
if (y >= MATRIX_HEIGHT) {
y = MATRIX_HEIGHT - 1;
}
return this;
}

public Spot moveUp() {
public void moveUp(List<Spot> state, Map<Character, Inhabitant> inhabitants) {
if (state.stream().anyMatch(s -> s.getX() == x && s.getY() == y - 1 && inhabitants.get(s.getInhabitantRef()).blocks())) {
return;
}
y--;
if (y < 0) {
y = 0;
}
return this;
}
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pluginManagement {
repositories.gradlePluginPortal()
repositories.maven { url = 'https://gradle.onepiece.software:1443/releases' }
}
plugins { id 'software.onepiece.j' version '0.0.11' }
plugins { id 'software.onepiece.j' version '0.0.13' }

0 comments on commit afdb148

Please sign in to comment.