Skip to content
This repository has been archived by the owner on Mar 11, 2023. It is now read-only.

Commit

Permalink
200926-pa-build.0
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Sep 26, 2020
1 parent 3baeedf commit 9ebb761
Show file tree
Hide file tree
Showing 40 changed files with 194 additions and 172 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# idea
.idea/
out/
*.iml
*.iml

# vscode
.vscode/
Binary file not shown.
Binary file not shown.
7 changes: 7 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
...

200926-pa-build.0 2020/09/20~2020/09/26 (a week)
1. Improve performance
2. press 'a' to move left (world move right)
3. press 'd' to move right (world move left)
4. 'Identifier' class now can construct by a string array
6 changes: 3 additions & 3 deletions src/io/github/overrun/mc2d/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

/**
* @author squid233
* @date 2020/9/14
* @since 2020/09/14
*/
public class Main {
public static void main(String[] args) {
Expand All @@ -49,9 +49,9 @@ public static void main(String[] args) {
}
/////
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("saves/save.dat"))) {
Worlds.OVERWORLD.setStorageBlock(((Overworld) ois.readObject()).getStorageBlock());
Worlds.overworld = (Overworld) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
for (Chunk chunk : Worlds.OVERWORLD.getStorageBlock().chunks) {
for (Chunk chunk : Worlds.overworld.getStorageBlock().chunks) {
for (int i = 0; i < 16; i++) {
chunk.setBlock(i, 0, Blocks.BEDROCK);
for (int j = 0; j < 4; j++) {
Expand Down
4 changes: 2 additions & 2 deletions src/io/github/overrun/mc2d/Minecraft2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

/**
* @author squid233
* @date 2020/9/14
* @since 2020/09/14
*/
public class Minecraft2D {
public static final String VERSION = "200919-pa-build.0";
public static final String VERSION = "200926-pa-build.0";
public static final String NAMESPACE = "minecraft2d";
public static final Logger LOGGER = new Logger();
}
2 changes: 1 addition & 1 deletion src/io/github/overrun/mc2d/asset/AssetManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

/**
* @author squid233
* @date 2020/9/15
* @since 2020/09/15
*/
public class AssetManager {
public static Path getAssets(String namespace, String... more) {
Expand Down
9 changes: 4 additions & 5 deletions src/io/github/overrun/mc2d/block/AbstractBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@

import io.github.overrun.mc2d.client.Mc2dClient;
import io.github.overrun.mc2d.registry.IRegistrable;
import io.github.overrun.mc2d.util.factory.Mc2dFactories;

import java.awt.Graphics;
import java.io.Serializable;
import java.util.Properties;

/**
* @author squid233
* @date 2020/9/14
* @since 2020/09/14
*/
public abstract class AbstractBlock implements IRegistrable, Serializable {
///private static final long serialVersionUID = -4198693747044134268L;
private static final long serialVersionUID = 3268494809155234202L;
public int x;
public int y;

Expand Down Expand Up @@ -77,12 +76,12 @@ public AbstractBlock setY(int y) {
public abstract AbstractBlock setPos(BlockPos pos);

public AbstractBlock setPos(int x, int y) {
setPos(Mc2dFactories.getBlockPos().get(x, y));
setPos(BlockPos.of(x, y));
return this;
}

public BlockPos getPos() {
return Mc2dFactories.getBlockPos().get(x, y);
return BlockPos.of(x, y);
}

public int getPreviewX(int x) {
Expand Down
12 changes: 9 additions & 3 deletions src/io/github/overrun/mc2d/block/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package io.github.overrun.mc2d.block;

import io.github.overrun.mc2d.asset.AssetManager;
import io.github.overrun.mc2d.client.Mc2dClient;
import io.github.overrun.mc2d.image.Images;
import io.github.overrun.mc2d.util.Highlight;
import io.github.overrun.mc2d.util.Identifier;
Expand All @@ -36,7 +37,7 @@

/**
* @author squid233
* @date 2020/9/14
* @since 2020/09/14
*/
public class Block extends AbstractBlock {
private static final long serialVersionUID = 4041316942709149977L;
Expand All @@ -48,8 +49,13 @@ public Block(Settings settings) { }

@Override
public void draw(Graphics g, int x) {
g.drawImage(Images.getBlockTexture(this), getPreviewX(x), getPreviewY(), 16, 16, null);
Highlight.block(g, getPreviewX(x), getPreviewY());
if (
getPreviewX() > -8 && getPreviewX() < Mc2dClient.getInstance().getWidth()
&& getPreviewY() > 14 && getPreviewY() < Mc2dClient.getInstance().getHeight()
) {
g.drawImage(Images.getBlockTexture(this), getPreviewX(x), getPreviewY(), 16, 16, null);
Highlight.block(g, getPreviewX(x), getPreviewY());
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/io/github/overrun/mc2d/block/BlockAir.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

/**
* @author squid233
* @date 2020/9/15
* @since 2020/09/15
*/
public class BlockAir extends Block {
private static final long serialVersionUID = -1997162368270314669L;
Expand Down
36 changes: 17 additions & 19 deletions src/io/github/overrun/mc2d/block/BlockPos.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,36 @@

package io.github.overrun.mc2d.block;

import io.github.overrun.mc2d.util.StringAppender;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Objects;
import java.util.StringJoiner;

/**
* @author squid233
* @date 2020/9/15
* @since 2020/09/15
*/
public class BlockPos implements Serializable {
private static final long serialVersionUID = -805306762075532340L;
private static final long serialVersionUID = -2544644015817653957L;
private final int x;
private final int y;
private static final HashMap<String, BlockPos> CACHE = new HashMap<>();

/**
* Use {@linkplain io.github.overrun.mc2d.util.factory.BlockPosFactory#get(int, int) BlockPosFactory.get(int, int)}
*
* @param x pos x
* @param y pos y
*/
public BlockPos(int x, int y) {
private BlockPos(int x, int y) {
this.x = x;
this.y = y;
}

public static BlockPos of(int x, int y) {
String st = new StringAppender(x, ", ", y).toString();
if (!CACHE.containsKey(st)) {
CACHE.put(st, new BlockPos(x, y));
}
return CACHE.get(st);
}

public int getX() {
return x;
}
Expand All @@ -58,23 +64,15 @@ public int getY() {

@Override
public String toString() {
return new StringJoiner(", ", BlockPos.class.getSimpleName() + "[", "]")
return new StringJoiner(", ", "{", "}")
.add("x=" + x)
.add("y=" + y)
.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BlockPos blockPos = (BlockPos) o;
return getX() == blockPos.getX() &&
getY() == blockPos.getY();
return o instanceof BlockPos && ((BlockPos) o).x == x && ((BlockPos) o).y == y;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/io/github/overrun/mc2d/block/Blocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@

/**
* @author squid233
* @date 2020/9/15
* @since 2020/09/15
*/
public class Blocks {
private static final ArrayList<AbstractBlock> BLOCKS = new ArrayList<>(5);
private static final ArrayList<AbstractBlock> BLOCKS = new ArrayList<>(6);

private static Block register(String name, Block block) {
Minecraft2D.LOGGER.debug("Registered block: " + Registry.register(Registry.BLOCK, name, block));
Expand Down
36 changes: 20 additions & 16 deletions src/io/github/overrun/mc2d/client/Mc2dClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,34 @@

/**
* @author squid233
* @date 2020/9/14
* @since 2020/09/14
*/
public class Mc2dClient extends JFrame implements Screen {
private static Mc2dClient instance;
public static final Point NULL_POINT = new Point();
private static boolean initialized = false;

private Mc2dClient() {
super("Minecraft 2D " + Minecraft2D.VERSION + " [Esc:Quit Game] - Made by OverRun Organization");
setSize(Options.getI(Options.WIDTH, 1296), Options.getI(Options.HEIGHT, 486));
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setIconImage(new ImageIcon("icon.png").getImage());
addKeyListener(new KeyAdapter());
var mouse = new MouseAdapter();
addMouseListener(mouse);
addMouseWheelListener(mouse);
System.out.println("Max memory: " + ((Runtime.getRuntime().maxMemory() >> 10 >> 10) >= 1024
? (Runtime.getRuntime().maxMemory() >> 10 >> 10 >> 10) + "GB"
: (Runtime.getRuntime().maxMemory() >> 10 >> 10) + "MB"));
new RenderThread().start();
setVisible(true);
if (!initialized) {
initialized = true;
setSize(Options.getI(Options.WIDTH, 1296), Options.getI(Options.HEIGHT, 486));
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setIconImage(new ImageIcon("icon.png").getImage());
addKeyListener(new KeyAdapter());
var mouse = new MouseAdapter();
addMouseListener(mouse);
addMouseWheelListener(mouse);
Minecraft2D.LOGGER.debug("Max memory: " + ((Runtime.getRuntime().maxMemory() >> 10 >> 10) >= 1024
? (Runtime.getRuntime().maxMemory() >> 10 >> 10 >> 10) + "GB"
: (Runtime.getRuntime().maxMemory() >> 10 >> 10) + "MB"));
new RenderThread(this).start();
setVisible(true);
}
}

public static synchronized Mc2dClient getInstance() {
public static Mc2dClient getInstance() {
if (instance == null) {
instance = new Mc2dClient();
}
Expand All @@ -82,7 +86,7 @@ public void paint(Graphics g) {
/////
drawBackground(gg, Colors.decode(Colors.skyBlue));
/////
Worlds.OVERWORLD.getStorageBlock().draw(gg);
Worlds.overworld.getStorageBlock().draw(gg);
/////
drawImage(gg, Images.getBlockTexture(Blocks.getByRawId(Player.handledBlock)), getWidth() - 49, 1, 32, 32);
LiteralText handledBlockId = Mc2dFactories.getText().getLiteralText(Blocks.getByRawId(Player.handledBlock).getRegistryName().toString());
Expand Down
23 changes: 13 additions & 10 deletions src/io/github/overrun/mc2d/client/RenderThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,49 @@

package io.github.overrun.mc2d.client;

import io.github.overrun.mc2d.Minecraft2D;
import io.github.overrun.mc2d.option.Options;

/**
* @author squid233
* @date 2020/9/15
* @since 2020/09/15
*/
public class RenderThread implements Runnable {
private Thread thread;
private boolean exited = false;
private final int interval;
private final Mc2dClient client;
public int fps;

public RenderThread() {
public RenderThread(Mc2dClient client) {
this.client = client;
fps = Options.getI(Options.FPS_OPT);
interval = 1000 / fps;
System.out.println("[RenderThread]Created");
System.out.println("[RenderThread]FPS: " + fps);
System.out.println("[RenderThread]Render interval: " + interval + "ms");
Minecraft2D.LOGGER.debug("Created render thread");
Minecraft2D.LOGGER.debug("FPS: " + fps);
Minecraft2D.LOGGER.debug("Render interval: " + interval + "ms");
}

@Override
public void run() {
System.out.println(thread.getName() + "Start rendering");
Minecraft2D.LOGGER.info("Start rendering");
while (!exited) {
Mc2dClient.getInstance().repaint();
client.repaint();
try {
//noinspection BusyWait
Thread.sleep(interval);
} catch (InterruptedException e) {
System.out.println(thread.getName() + "Error: " + e.toString());
Minecraft2D.LOGGER.error(thread.getName() + "Error: " + e.toString());
exited = true;
}
}
System.out.println(thread.getName() + "Stop rendering");
Minecraft2D.LOGGER.info(thread.getName() + "Stop rendering");
System.exit(0);
}

public void start() {
if (thread == null) {
thread = new Thread(this, "[RenderThread]");
thread = new Thread(this, "RenderThread");
thread.start();
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/io/github/overrun/mc2d/game/Camera.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@

/**
* @author squid233
* @date 2020/9/15
* @since 2020/09/15
*/
public class Camera {
public static int view = 0;
public static byte view = 0;

public static void plus() {
if (view < Integer.MAX_VALUE) {
if (view < Byte.MAX_VALUE) {
view++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/io/github/overrun/mc2d/game/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

/**
* @author squid233
* @date 2020/9/17
* @since 2020/09/17
*/
public class Player {
public static int handledBlock = 1;
Expand Down
Loading

0 comments on commit 9ebb761

Please sign in to comment.