-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removes tile and data arrays from Level, and disperses it among Chunks. These are managed in ChunkManager, used by Level and LevelGen in order to access Chunks and Tiles.
- Loading branch information
Showing
8 changed files
with
254 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package minicraft.level; | ||
|
||
import minicraft.level.tile.Tile; | ||
import minicraft.level.tile.Tiles; | ||
|
||
public class Chunk { | ||
public static final int SIZE = 32; | ||
|
||
public short[] data, tiles; | ||
|
||
public Chunk() { | ||
data = new short[SIZE * SIZE]; | ||
tiles = new short[SIZE * SIZE]; | ||
} | ||
|
||
/** | ||
* Returns a tile. Mods x and y to the range 0-SIZE as to never be out of bounds | ||
*/ | ||
public Tile getTile(int x, int y) { | ||
return Tiles.get(tiles[(x % SIZE) + (y % SIZE) * SIZE]); | ||
} | ||
|
||
/** | ||
* Updates a tile. Mods x and y to the range 0-SIZE as to never be out of bounds | ||
*/ | ||
public void setTile(int x, int y, Tile t, int dataVal) { | ||
int index = (x % SIZE) + (y % SIZE) * SIZE; | ||
tiles[index] = t.id; | ||
data[index] = (short) dataVal; | ||
} | ||
|
||
public int getData(int x, int y) { | ||
return data[(x % SIZE) + (y % SIZE) * SIZE] & 0xFFFF; | ||
} | ||
|
||
public void setData(int x, int y, int val) { | ||
data[(x % SIZE) + (y % SIZE) * SIZE] = (short) val; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package minicraft.level; | ||
|
||
import minicraft.level.tile.Tile; | ||
import minicraft.level.tile.Tiles; | ||
|
||
public class ChunkManager { | ||
public Chunk[] chunks; | ||
public int levelWidth, levelHeight; | ||
private int widthInChunks, heightInChunks; | ||
|
||
public ChunkManager(int levelWidth, int levelHeight) { | ||
this.levelWidth = levelWidth; | ||
this.levelHeight = levelHeight; | ||
// Create enough chunks to cover the number of tiles provided | ||
widthInChunks = (int)Math.ceil((double)levelWidth / Chunk.SIZE); | ||
heightInChunks = (int)Math.ceil((double)levelHeight / Chunk.SIZE); | ||
chunks = new Chunk[widthInChunks * heightInChunks]; | ||
for(int i = 0; i < widthInChunks * heightInChunks; i++) | ||
chunks[i] = new Chunk(); | ||
} | ||
|
||
/** | ||
* Return the chunk in which the tileX and tileY land, or null if out of bounds | ||
*/ | ||
public Chunk getChunk(int tileX, int tileY) { | ||
if(tileX < 0 || tileY < 0 || tileX >= levelWidth || tileY >= levelHeight /* || (tileX + tileY * levelWidth / Chunk.SIZE) >= chunks.length */) | ||
return null; | ||
int cInd = tileX / Chunk.SIZE + (tileY / Chunk.SIZE) * widthInChunks; | ||
return chunks[cInd]; | ||
} | ||
|
||
public Tile getTile(int x, int y) { | ||
Chunk c = getChunk(x, y); | ||
if(c == null) return Tiles.get("connector tile"); | ||
return c.getTile(x, y); | ||
} | ||
|
||
public void setTile(int x, int y, Tile t, int dataVal) { | ||
Chunk c = getChunk(x, y); | ||
if(c == null) return; | ||
c.setTile(x, y, t, dataVal); | ||
} | ||
|
||
public int getData(int x, int y) { | ||
Chunk c = getChunk(x, y); | ||
if(c == null) return 0; | ||
return c.getData(x, y); | ||
} | ||
|
||
public void setData(int x, int y, int val) { | ||
Chunk c = getChunk(x, y); | ||
if(c == null) return; | ||
c.setData(x, y, val); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.