Added the camera object to the player class and added the teleport method. Also renamed Blocks.java to BlockMaterials.java. Also added Player object to the Main class

This commit is contained in:
2020-04-01 16:18:19 -06:00
parent 2476be3808
commit 952991862f
6 changed files with 24 additions and 12 deletions

View File

@@ -12,12 +12,14 @@ public class Player implements Restrictions {
private String spriteName; private String spriteName;
private TextureAtlas textureAtlas; private TextureAtlas textureAtlas;
private Animation<TextureAtlas.AtlasRegion> animation; private Animation<TextureAtlas.AtlasRegion> animation;
private OrthographicCamera cam;
public Player(int x, int y, OrthographicCamera cam) { public Player(int x, int y, OrthographicCamera cam) {
this.cam = cam;
this.x = x*TILE_SIZE; this.x = x*TILE_SIZE;
this.y = y*TILE_SIZE; this.y = y*TILE_SIZE;
cam.translate(this.x,this.y); this.cam.translate(this.x,this.y);
this.spriteName = "man"; this.spriteName = "man";
this.textureAtlas = new TextureAtlas("core/assets/" + spriteName + ".atlas"); this.textureAtlas = new TextureAtlas("core/assets/" + spriteName + ".atlas");
@@ -26,6 +28,12 @@ public class Player implements Restrictions {
this.animation = new Animation<>(frameDuration, keyFrames); this.animation = new Animation<>(frameDuration, keyFrames);
} }
public static void teleport(Player player, int x, int y){
player.x = x*TILE_SIZE;
player.y = y*TILE_SIZE;
player.cam.translate(x,y);
}
public void render(Batch batch){ public void render(Batch batch){
float elapsedTime = 1f; float elapsedTime = 1f;
batch.draw(getAnimation().getKeyFrame(elapsedTime, true), getX(), getY(), TILE_SIZE, TILE_SIZE); batch.draw(getAnimation().getKeyFrame(elapsedTime, true), getX(), getY(), TILE_SIZE, TILE_SIZE);

View File

@@ -4,12 +4,12 @@ import com.badlogic.gdx.graphics.Texture;
import java.util.HashMap; import java.util.HashMap;
public class Blocks { public class BlockMaterials {
public static HashMap<String,Block> materials = new HashMap<>(); public static HashMap<String,Block> materials = new HashMap<>();
public static HashMap<String,Texture> textures = new HashMap<>(); public static HashMap<String,Texture> textures = new HashMap<>();
//Private so the singleton can't be instantiated //Private so the singleton can't be instantiated
private Blocks() {} private BlockMaterials() {}
public Texture getTexture(String material) { public Texture getTexture(String material) {
return textures.get(material); return textures.get(material);

View File

@@ -18,7 +18,7 @@ public class Chunks {
//Going from start of selected chunk to end of selected chunk in x and y //Going from start of selected chunk to end of selected chunk in x and y
for (int i = startX; i != endX; i++){ for (int i = startX; i != endX; i++){
for (int j = startY; j != endY; j++) { for (int j = startY; j != endY; j++) {
blocks.put(new Pair<>(i, j), Blocks.materials.get("grass")); blocks.put(new Pair<>(i, j), BlockMaterials.materials.get("grass"));
} }
} }
} }

View File

@@ -1,10 +1,11 @@
package com.mygdx.game.Dimension; package com.mygdx.game.Dimension;
import com.mygdx.game.Character.Player;
import net.dermetfan.utils.Pair; import net.dermetfan.utils.Pair;
import java.util.HashMap; import java.util.HashMap;
import static com.mygdx.game.Restrictions.RENDER_DISTANCE; import static com.mygdx.game.Restrictions.CHUNK_SIZE;
import static com.mygdx.game.Restrictions.SUPER_CHUNK_SIZE; import static com.mygdx.game.Restrictions.SUPER_CHUNK_SIZE;
public class Superchunks { public class Superchunks {
@@ -21,16 +22,16 @@ public class Superchunks {
for (int i = startX; i != endX; i++){ for (int i = startX; i != endX; i++){
for (int j = startY; j != endY; j++) { for (int j = startY; j != endY; j++) {
Chunks chunks = new Chunks(); Chunks chunks = new Chunks();
Blocks.create(); BlockMaterials.create();
chunks.generateChunk(i, j); chunks.generateChunk(i, j);
overworld.put(new Pair<>(i, j), chunks); overworld.put(new Pair<>(i, j), chunks);
} }
} }
} }
public static void generateWithPlayer(){ public static void generateWithPlayer(Player player){
for (int i = -RENDER_DISTANCE; i < RENDER_DISTANCE; i++) { for (int i = -1+player.getX()/CHUNK_SIZE*SUPER_CHUNK_SIZE; i < 1+player.getX()/CHUNK_SIZE*SUPER_CHUNK_SIZE; i++) {
for (int j = -RENDER_DISTANCE; j < RENDER_DISTANCE; j++) { for (int j = -1+player.getY()/CHUNK_SIZE*SUPER_CHUNK_SIZE; j < 1+player.getY()/CHUNK_SIZE*SUPER_CHUNK_SIZE; j++) {
generateSuperchunk(i,j); generateSuperchunk(i,j);
} }
} }

View File

@@ -37,7 +37,7 @@ public class WorldRenderer implements Restrictions {
int drawingLocationX = chunkpair.getKey() * TILE_SIZE; int drawingLocationX = chunkpair.getKey() * TILE_SIZE;
int drawingLocationY = chunkpair.getValue()* TILE_SIZE; int drawingLocationY = chunkpair.getValue()* TILE_SIZE;
batch.draw(Blocks.textures.get(name),drawingLocationX ,drawingLocationY); batch.draw(BlockMaterials.textures.get(name),drawingLocationX ,drawingLocationY);
} }
} }
} }

View File

@@ -27,6 +27,7 @@ public class Main extends ApplicationAdapter {
private Label label; 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 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 float timeSinceLastUpdate = 0f; //accumulator
private Player player;
@Override @Override
@@ -48,12 +49,13 @@ public class Main extends ApplicationAdapter {
//Starting location of the player //Starting location of the player
Player player = new Player(0,0, cam); player = new Player(0,0, cam);
cam.translate((float) TILE_SIZE / 2, (float) TILE_SIZE / 2); cam.translate((float) TILE_SIZE / 2, (float) TILE_SIZE / 2);
cam.zoom = 25f; cam.zoom = 25f;
Superchunks.generateWithPlayer(); Superchunks.generateWithPlayer(player);
mouse = new Mouse(player); mouse = new Mouse(player);
worldRenderer = new WorldRenderer(player, mouse,cam); worldRenderer = new WorldRenderer(player, mouse,cam);
@@ -67,6 +69,7 @@ public class Main extends ApplicationAdapter {
fixedStep(); fixedStep();
batch.begin(); batch.begin();
worldRenderer.render(batch); worldRenderer.render(batch);
gui.render(batch); gui.render(batch);