Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infdev #732

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ allprojects {
apply plugin: "java"
apply plugin: "application"

version = "2.3.0-dev1"
version = "2.3.0-dev2"

sourceCompatibility = 8
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
Expand Down
2 changes: 1 addition & 1 deletion src/client/java/minicraft/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected Game() {

public static final String NAME = "Minicraft Plus"; // This is the name on the application window.

public static final Version VERSION = new Version("2.3.0-dev1");
public static final Version VERSION = new Version("2.3.0-dev2");

public static InputHandler input; // Input used in Game, Player, and just about all the *Menu classes.
public static Player player;
Expand Down
8 changes: 4 additions & 4 deletions src/client/java/minicraft/core/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ private static void renderLevel() {
int yScroll = player.y - (Screen.h - 8) / 2; // Scrolls the screen in the y axis.

// Stop scrolling if the screen is at the ...
if (xScroll < 0) xScroll = 0; // ...Left border.
if (yScroll < 0) yScroll = 0; // ...Top border.
if (xScroll > (level.w << 4) - Screen.w) xScroll = (level.w << 4) - Screen.w; // ...Right border.
if (yScroll > (level.h << 4) - Screen.h) yScroll = (level.h << 4) - Screen.h; // ...Bottom border.
// if (xScroll < 0) xScroll = 0; // ...Left border.
// if (yScroll < 0) yScroll = 0; // ...Top border.
// if (xScroll > (level.w << 4) - Screen.w) xScroll = (level.w << 4) - Screen.w; // ...Right border.
// if (yScroll > (level.h << 4) - Screen.h) yScroll = (level.h << 4) - Screen.h; // ...Bottom border.
if (currentLevel > 3) { // If the current level is higher than 3 (which only the sky level (and dungeon) is)
MinicraftImage cloud = spriteLinker.getSheet(SpriteType.Tile, "cloud_background");
for (int y = 0; y < 28; y++)
Expand Down
11 changes: 7 additions & 4 deletions src/client/java/minicraft/entity/mob/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ public void tick() {
return;
}

// Ensure chunks generated around player
level.loadChunksAround(x >> 4, y >> 4);

super.tick(); // Ticks Mob.java

tickMultiplier();
Expand Down Expand Up @@ -652,7 +655,7 @@ protected void attack() {
Point t = getInteractionTile();

// If the target coordinates are a valid tile.
if (t.x >= 0 && t.y >= 0 && t.x < level.w && t.y < level.h) {
// if (t.x >= 0 && t.y >= 0 && t.x < level.w && t.y < level.h) {

// Get any entities (except dropped items and particles) on the tile.
List<Entity> tileEntities = level.getEntitiesInTiles(t.x, t.y, t.x, t.y, false, ItemEntity.class, Particle.class);
Expand All @@ -679,7 +682,7 @@ protected void attack() {
fishingTicks = maxFishingTicks;
}
}
}
// }
if (done) return; // Skip the rest if interaction was handled
}

Expand All @@ -692,10 +695,10 @@ protected void attack() {
Point t = getInteractionTile();

// Check if tile is in bounds of the map.
if (t.x >= 0 && t.y >= 0 && t.x < level.w && t.y < level.h) {
// if (t.x >= 0 && t.y >= 0 && t.x < level.w && t.y < level.h) {
Tile tile = level.getTile(t.x, t.y);
used = tile.hurt(level, t.x, t.y, this, random.nextInt(3) + 1, attackDir) || used;
}
// }

if (used && activeItem instanceof ToolItem)
((ToolItem) activeItem).payDurability();
Expand Down
113 changes: 113 additions & 0 deletions src/client/java/minicraft/level/ChunkManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package minicraft.level;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import minicraft.gfx.Point;
import minicraft.level.tile.Tile;
import minicraft.level.tile.Tiles;

public class ChunkManager {

public static final int CHUNK_SIZE = 64;

public static final int CHUNK_STAGE_UNFINISHED_STAIRS = 3;
public static final int CHUNK_STAGE_DONE = 4;

/**
* A data structure where
* [x][y] input points to CHUNK_SIZE x CHUNK_SIZE list of TileDat
*/
public Map<Integer, Map<Integer, Chunk>> chunks;

public ChunkManager() {
chunks = new HashMap<>();
}

public Set<Point> getAllChunks() {
HashSet<Point> out = new HashSet<>();
for(int x : chunks.keySet())
for(int y : chunks.get(x).keySet())
out.add(new Point(x, y));
return out;
}

/**
* Return the chunk in which the tileX and tileY land
*/
private Chunk getChunk(int tileX, int tileY) {
int cX = Math.floorDiv(tileX, CHUNK_SIZE), cY = Math.floorDiv(tileY, CHUNK_SIZE);
// If [cX][cY] are not keys in chunks, put them there
if(!chunks.containsKey(cX))
chunks.put(cX, new HashMap<>());
if(!chunks.get(cX).containsKey(cY))
chunks.get(cX).put(cY, new Chunk());
return chunks.get(cX).get(cY);
}

/**
* Returns a tile. After finding the right chunk, mods x and y to the range 0-CHUNK_SIZE as to never be out of bounds
*/
public Tile getTile(int x, int y) {
return Tiles.get(getChunk(x, y).getTileDat(x, y).id);
}

/**
* Updates a tile. After finding the right chunk, mods x and y to the range 0-CHUNK_SIZE as to never be out of bounds
*/
public void setTile(int x, int y, Tile t, int dataVal) {
TileDat dat = getChunk(x, y).getTileDat(x, y);
dat.id = t.id;
dat.data = (short) dataVal;
}

public int getData(int x, int y) {
return getChunk(x, y).getTileDat(x, y).data;
}

public void setData(int x, int y, int val) {
getChunk(x, y).getTileDat(x, y).data = (short) val;
}

public int getChunkStage(int chunkX, int chunkY) {
// If [cX][cY] are not keys in chunks, stage must be 0
if(!chunks.containsKey(chunkX) || !chunks.get(chunkX).containsKey(chunkY))
return 0;
return chunks.get(chunkX).get(chunkY).stage;
}

public void setChunkStage(int chunkX, int chunkY, int stage) {
// If [chunkX][chunkY] are not keys in chunks, put them there
if(!chunks.containsKey(chunkX))
chunks.put(chunkX, new HashMap<>());
if(!chunks.get(chunkX).containsKey(chunkY))
chunks.get(chunkX).put(chunkY, new Chunk());
chunks.get(chunkX).get(chunkY).stage = (short)stage;
}

private static class Chunk {
protected TileDat[] tiles;
protected short stage = 0;
public Chunk() {
tiles = new TileDat[CHUNK_SIZE * CHUNK_SIZE];
}

public TileDat getTileDat(int tileX, int tileY) {
int index = Math.floorMod(tileX, CHUNK_SIZE) + Math.floorMod(tileY, CHUNK_SIZE) * CHUNK_SIZE;
if(tiles[index] == null)
tiles[index] = new TileDat((short)0);
return tiles[index];
}
}

private static class TileDat {
protected short id, data;

public TileDat(short id) {
this.id = id;
this.data = 0;
}
}
}
Loading
Loading