Moved the properties of Blocks into the class Block. Also made Blocks and Superchunks singletons, to prevent the creation of unnecessary objects
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.mygdx.game.Character;
|
||||
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.Animation;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
||||
@@ -12,9 +13,12 @@ public class Player implements Restrictions {
|
||||
private TextureAtlas textureAtlas;
|
||||
private Animation<TextureAtlas.AtlasRegion> animation;
|
||||
|
||||
public Player(int x, int y) {
|
||||
this.x = 0;
|
||||
this.y = 0;
|
||||
public Player(int x, int y, OrthographicCamera cam) {
|
||||
this.x = x*TILE_SIZE;
|
||||
this.y = y*TILE_SIZE;
|
||||
|
||||
cam.translate(this.x,this.y);
|
||||
|
||||
this.spriteName = "man";
|
||||
this.textureAtlas = new TextureAtlas("core/assets/" + spriteName + ".atlas");
|
||||
Array<TextureAtlas.AtlasRegion> keyFrames = textureAtlas.findRegions(spriteName + "_Down");
|
||||
|
||||
63
core/src/com/mygdx/game/Dimension/Block.java
Normal file
63
core/src/com/mygdx/game/Dimension/Block.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package com.mygdx.game.Dimension;
|
||||
|
||||
public class Block {
|
||||
private String name;
|
||||
private Boolean floor, wall, harvestable, breakable, passable;
|
||||
|
||||
public Block(String name, Boolean floor, Boolean wall, Boolean harvestable, Boolean breakable, Boolean passable) {
|
||||
this.name = name;
|
||||
this.floor = floor;
|
||||
this.wall = wall;
|
||||
this.harvestable = harvestable;
|
||||
this.breakable = breakable;
|
||||
this.passable = passable;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Boolean getFloor() {
|
||||
return floor;
|
||||
}
|
||||
|
||||
public void setFloor(Boolean floor) {
|
||||
this.floor = floor;
|
||||
}
|
||||
|
||||
public Boolean getWall() {
|
||||
return wall;
|
||||
}
|
||||
|
||||
public void setWall(Boolean wall) {
|
||||
this.wall = wall;
|
||||
}
|
||||
|
||||
public Boolean getHarvestable() {
|
||||
return harvestable;
|
||||
}
|
||||
|
||||
public void setHarvestable(Boolean harvestable) {
|
||||
this.harvestable = harvestable;
|
||||
}
|
||||
|
||||
public Boolean getBreakable() {
|
||||
return breakable;
|
||||
}
|
||||
|
||||
public void setBreakable(Boolean breakable) {
|
||||
this.breakable = breakable;
|
||||
}
|
||||
|
||||
public Boolean getPassable() {
|
||||
return passable;
|
||||
}
|
||||
|
||||
public void setPassable(Boolean passable) {
|
||||
this.passable = passable;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,25 @@
|
||||
package com.mygdx.game.Dimension;
|
||||
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import net.dermetfan.utils.Pair;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Blocks {
|
||||
private static HashMap<String,Texture> textures = new HashMap<>();
|
||||
public static HashMap<String,Block> materials = new HashMap<>();
|
||||
public static HashMap<String,Texture> textures = new HashMap<>();
|
||||
|
||||
//Private so the singleton can't be instantiated
|
||||
private Blocks() {}
|
||||
|
||||
public Texture getTexture(String material) {
|
||||
return textures.get(material);
|
||||
}
|
||||
|
||||
public static void create(){
|
||||
textures.put("grass",new Texture("core/assets/grass.png"));
|
||||
textures.put("wood",new Texture("core/assets/wood.png"));
|
||||
materials.put("grass",new Block("grass",true,false,true,true,true));
|
||||
materials.put("wood",new Block("wood",false,true,true,true,false));
|
||||
|
||||
textures.put("grass", new Texture("core/assets/grass.png"));
|
||||
textures.put("wood", new Texture("core/assets/wood.png"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ import java.util.HashMap;
|
||||
import static com.mygdx.game.Restrictions.CHUNK_SIZE;
|
||||
|
||||
public class Chunks {
|
||||
public static HashMap<Pair<Integer,Integer>, Blocks> blocks = new HashMap<>();
|
||||
public HashMap<Pair<Integer, Integer>, Block> blocks = new HashMap<>();
|
||||
|
||||
public Chunks(int x, int y) {
|
||||
public void generateChunk(int x, int y){
|
||||
int startX = x*CHUNK_SIZE;
|
||||
int startY = y*CHUNK_SIZE;
|
||||
int endX = startX + CHUNK_SIZE;
|
||||
@@ -18,20 +18,16 @@ public class Chunks {
|
||||
//Going from start of selected chunk to end of selected chunk in x and y
|
||||
for (int i = startX; i != endX; i++){
|
||||
for (int j = startY; j != endY; j++) {
|
||||
blocks.put(new Pair<>(i, j), new Blocks());
|
||||
blocks.put(new Pair<>(i, j), Blocks.materials.get("grass"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void create(){
|
||||
Blocks.create();
|
||||
}
|
||||
|
||||
public HashMap<Pair<Integer, Integer>, Blocks> getBlocks() {
|
||||
public HashMap<Pair<Integer, Integer>, Block> getBlocks() {
|
||||
return blocks;
|
||||
}
|
||||
|
||||
public void setBlocks(HashMap<Pair<Integer, Integer>, Blocks> blocks) {
|
||||
public void setBlocks(HashMap<Pair<Integer, Integer>, Block> blocks) {
|
||||
this.blocks = blocks;
|
||||
}
|
||||
}
|
||||
|
||||
38
core/src/com/mygdx/game/Dimension/Superchunks.java
Normal file
38
core/src/com/mygdx/game/Dimension/Superchunks.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.mygdx.game.Dimension;
|
||||
|
||||
import net.dermetfan.utils.Pair;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import static com.mygdx.game.Restrictions.RENDER_DISTANCE;
|
||||
import static com.mygdx.game.Restrictions.SUPER_CHUNK_SIZE;
|
||||
|
||||
public class Superchunks {
|
||||
public static HashMap<Pair, Chunks> overworld = new HashMap<>();
|
||||
|
||||
private Superchunks() {}
|
||||
|
||||
public static void generateSuperchunk(int x, int y) {
|
||||
int startX = x*SUPER_CHUNK_SIZE;
|
||||
int startY = y*SUPER_CHUNK_SIZE;
|
||||
int endX = startX + SUPER_CHUNK_SIZE;
|
||||
int endY = startY + SUPER_CHUNK_SIZE;
|
||||
|
||||
for (int i = startX; i != endX; i++){
|
||||
for (int j = startY; j != endY; j++) {
|
||||
Chunks chunks = new Chunks();
|
||||
Blocks.create();
|
||||
chunks.generateChunk(i, j);
|
||||
overworld.put(new Pair<>(i, j), chunks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void generateWithPlayer(){
|
||||
for (int i = -RENDER_DISTANCE; i < RENDER_DISTANCE; i++) {
|
||||
for (int j = -RENDER_DISTANCE; j < RENDER_DISTANCE; j++) {
|
||||
generateSuperchunk(i,j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,29 +9,35 @@ import com.mygdx.game.Character.Player;
|
||||
import com.mygdx.game.Restrictions;
|
||||
import net.dermetfan.utils.Pair;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class WorldRenderer implements Restrictions {
|
||||
private Mouse mouse;
|
||||
private OrthographicCamera cam;
|
||||
private Player player;
|
||||
private InputController inputController;
|
||||
private Worlds worlds;
|
||||
|
||||
public WorldRenderer(Player player, Mouse mouse, OrthographicCamera cam, Worlds worlds) {
|
||||
public WorldRenderer(Player player, Mouse mouse, OrthographicCamera cam) {
|
||||
this.player = player;
|
||||
this.mouse = mouse;
|
||||
this.cam = cam;
|
||||
this.worlds = worlds;
|
||||
inputController = new InputController(this.cam, this.player);
|
||||
}
|
||||
|
||||
public void create(){
|
||||
worlds.create();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void drawWorld(Batch batch) {
|
||||
for (Pair worldpair: Worlds.overworld.keySet()) {
|
||||
for (Pair<Integer,Integer> chunkpair:Chunks.blocks.keySet()) {
|
||||
batch.draw( Worlds.overworld.get(worldpair).getBlocks().get(chunkpair).getTexture("grass"), chunkpair.getKey() * TILE_SIZE, chunkpair.getValue()* TILE_SIZE);
|
||||
for (Pair<Integer, Integer> worldpair: Superchunks.overworld.keySet()) {
|
||||
Chunks chunks = Superchunks.overworld.get(worldpair);
|
||||
for (Pair<Integer,Integer> chunkpair: chunks.blocks.keySet()) {
|
||||
|
||||
HashMap<Pair<Integer, Integer>, Block> blocks = chunks.getBlocks();
|
||||
Block chunkBlock = blocks.get(chunkpair);
|
||||
String name = chunkBlock.getName();
|
||||
|
||||
int drawingLocationX = chunkpair.getKey() * TILE_SIZE;
|
||||
int drawingLocationY = chunkpair.getValue()* TILE_SIZE;
|
||||
|
||||
batch.draw(Blocks.textures.get(name),drawingLocationX ,drawingLocationY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
package com.mygdx.game.Dimension;
|
||||
|
||||
import net.dermetfan.utils.Pair;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import static com.mygdx.game.Restrictions.CHUNK_SIZE;
|
||||
import static com.mygdx.game.Restrictions.SUPER_CHUNK_SIZE;
|
||||
|
||||
public class Worlds {
|
||||
public static HashMap<Pair, Chunks> overworld = new HashMap<>();
|
||||
|
||||
public Worlds(int x, int y) {
|
||||
int startX = x*SUPER_CHUNK_SIZE;
|
||||
int startY = y*SUPER_CHUNK_SIZE;
|
||||
int endX = startX + SUPER_CHUNK_SIZE;
|
||||
int endY = startY + SUPER_CHUNK_SIZE;
|
||||
|
||||
for (int i = startX; i != endX; i++){
|
||||
for (int j = startY; j != endY; j++) {
|
||||
overworld.put(new Pair<>(i, j), new Chunks(i,j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void create() {
|
||||
Chunks.create();
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,14 @@ import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.GL30;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.scenes.scene2d.Stage;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Label;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Table;
|
||||
import com.mygdx.game.Character.Mouse;
|
||||
import com.mygdx.game.Character.Player;
|
||||
import com.mygdx.game.Dimension.Worlds;
|
||||
import com.mygdx.game.UI.GUI;
|
||||
import com.mygdx.game.Dimension.Superchunks;
|
||||
import com.mygdx.game.Dimension.WorldRenderer;
|
||||
import com.mygdx.game.UI.GUI;
|
||||
|
||||
import static com.mygdx.game.Restrictions.*;
|
||||
|
||||
@@ -19,7 +22,9 @@ public class Main extends ApplicationAdapter {
|
||||
private GUI gui;
|
||||
private Mouse mouse;
|
||||
private OrthographicCamera cam;
|
||||
private Worlds worlds;
|
||||
private Stage stage;
|
||||
private Table table;
|
||||
private Label label;
|
||||
float physicsUpdateSpeed = 1/60f; //how often you want to do a physics-update, in this case every 1/60th of a sec
|
||||
float timeSinceLastUpdate = 0f; //accumulator
|
||||
|
||||
@@ -27,19 +32,31 @@ public class Main extends ApplicationAdapter {
|
||||
@Override
|
||||
public void create () {
|
||||
Gdx.graphics.setWindowedMode(1024,576);
|
||||
Player player = new Player(0, 0);
|
||||
mouse = new Mouse(player);
|
||||
cam = new OrthographicCamera(VIEWPORT_WIDTH,VIEWPORT_HEIGHT);
|
||||
cam.translate(TILE_SIZE/2,TILE_SIZE/2);
|
||||
cam.zoom = 25f;
|
||||
worlds = new Worlds(0,0);
|
||||
worlds = new Worlds(-1,0);
|
||||
worlds = new Worlds(0,-1);
|
||||
worlds = new Worlds(-1,-1);
|
||||
|
||||
worlds.create();
|
||||
worldRenderer = new WorldRenderer(player, mouse,cam, worlds);
|
||||
worldRenderer.create();
|
||||
//
|
||||
stage = new Stage();
|
||||
Gdx.input.setInputProcessor(stage);
|
||||
|
||||
table = new Table();
|
||||
table.add(label);
|
||||
table.setFillParent(true);
|
||||
stage.addActor(table);
|
||||
|
||||
table.setDebug(true);
|
||||
|
||||
|
||||
|
||||
//Starting location of the player
|
||||
Player player = new Player(0,0, cam);
|
||||
|
||||
cam.translate((float) TILE_SIZE / 2, (float) TILE_SIZE / 2);
|
||||
cam.zoom = 25f;
|
||||
|
||||
Superchunks.generateWithPlayer();
|
||||
|
||||
mouse = new Mouse(player);
|
||||
worldRenderer = new WorldRenderer(player, mouse,cam);
|
||||
batch = new SpriteBatch();
|
||||
gui = new GUI(mouse,cam);
|
||||
}
|
||||
@@ -48,16 +65,19 @@ public class Main extends ApplicationAdapter {
|
||||
public void render () {
|
||||
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
timeSinceLastUpdate = Gdx.graphics.getDeltaTime(); //Accumulate delta time
|
||||
while(timeSinceLastUpdate >= physicsUpdateSpeed){ //If the accumulated delta-time is greater than, do a physics update - this can happen multiple times in a single frame, depending on fps/lag/physicsUpdateSpeed
|
||||
timeSinceLastUpdate -= physicsUpdateSpeed; //Subtract physicsUpdateSpeed from timeSinceLastUpdate - if timeSinceLastUpdate is STILL >= physicsUpdateSpeed, repeat, if not, the loop breaks
|
||||
}
|
||||
fixedStep();
|
||||
|
||||
batch.begin();
|
||||
worldRenderer.render(batch);
|
||||
gui.render(batch);
|
||||
batch.end();
|
||||
System.gc();
|
||||
}
|
||||
|
||||
public void fixedStep() {
|
||||
timeSinceLastUpdate = Gdx.graphics.getDeltaTime(); //Accumulate delta time
|
||||
while(timeSinceLastUpdate >= physicsUpdateSpeed){ //If the accumulated delta-time is greater than, do a physics update - this can happen multiple times in a single frame, depending on fps/lag/physicsUpdateSpeed
|
||||
timeSinceLastUpdate -= physicsUpdateSpeed; //Subtract physicsUpdateSpeed from timeSinceLastUpdate - if timeSinceLastUpdate is STILL >= physicsUpdateSpeed, repeat, if not, the loop breaks
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -8,4 +8,5 @@ public interface Restrictions {
|
||||
int KEY_DELAY = 10;
|
||||
int CHUNK_SIZE = 16;
|
||||
int SUPER_CHUNK_SIZE = 3;
|
||||
int RENDER_DISTANCE = 3;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user