Added a movable camera

This commit is contained in:
2020-03-27 21:21:45 -06:00
parent 4e7e8ac99c
commit 26e4a8ab42
5 changed files with 77 additions and 40 deletions

View File

@@ -1,10 +1,18 @@
package com.mygdx.game; package com.mygdx.game;
import com.badlogic.gdx.graphics.Texture;
public class Block { public class Block {
private String material; private String material;
private Texture texture;
public Block(){
material = null;
}
public Block(String tempMaterial) { public Block(String tempMaterial) {
material = tempMaterial; material = tempMaterial;
texture = new Texture("core/assets/"+tempMaterial+".png");
} }
public String getMaterial() { public String getMaterial() {
@@ -14,4 +22,12 @@ public class Block {
public void setMaterial(String material) { public void setMaterial(String material) {
this.material = material; this.material = material;
} }
public Texture getTexture() {
return texture;
}
public void setTexture(Texture texture) {
this.texture = texture;
}
} }

View File

@@ -0,0 +1,44 @@
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.MathUtils;
public class InputController {
private OrthographicCamera cam;
public InputController(OrthographicCamera cam) {
this.cam = cam;
}
public void handleInput() {
if (Gdx.input.isKeyPressed(Input.Keys.A)) {
cam.zoom += 0.02;
}
if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
cam.zoom -= 0.02;
}
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
cam.translate(-3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
cam.translate(3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
cam.translate(0, -3, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
cam.translate(0, 3, 0);
}
cam.zoom = MathUtils.clamp(cam.zoom, 0.1f, 100/cam.viewportWidth);
float effectiveViewportWidth = cam.viewportWidth * cam.zoom;
float effectiveViewportHeight = cam.viewportHeight * cam.zoom;
cam.position.x = MathUtils.clamp(cam.position.x, effectiveViewportWidth / 2f, 100 - effectiveViewportWidth / 2f);
cam.position.y = MathUtils.clamp(cam.position.y, effectiveViewportHeight / 2f, 100 - effectiveViewportHeight / 2f);
}
}

View File

@@ -2,15 +2,8 @@ package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import javafx.util.Pair;
import java.util.HashMap;
public class Main extends ApplicationAdapter { public class Main extends ApplicationAdapter {
private SpriteBatch batch; private SpriteBatch batch;

View File

@@ -1,5 +1,6 @@
package com.mygdx.game; package com.mygdx.game;
import com.badlogic.gdx.graphics.Texture;
import javafx.util.Pair; import javafx.util.Pair;
import java.util.HashMap; import java.util.HashMap;
@@ -17,20 +18,21 @@ public class World {
fillWorld(); fillWorld();
} }
public Map<Pair<Integer, Integer>, Block> getBlocks() {
return blocks;
}
public void addTile(int x, int y, Block block){ public void addTile(int x, int y, Block block){
this.blocks.put(new Pair<>(x,y),block); this.blocks.put(new Pair<>(x,y),block);
} }
public Block getTile(int x, int y){
return this.blocks.get(new Pair<>(x,y));
}
public Texture getTileTexture(int x, int y){
return this.blocks.get(new Pair<>(x,y)).getTexture();
}
public void fillWorld(){ public void fillWorld(){
this.addTile(1,1,new Block("grass")); this.addTile(1,1,new Block("grass"));
this.addTile(1,2,new Block("grass")); this.addTile(0,0,new Block("grass"));
this.addTile(1,3,new Block("grass"));
this.addTile(-1,-2,new Block("grass"));
} }
} }

View File

@@ -3,45 +3,25 @@ package com.mygdx.game;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import javafx.util.Pair;
import java.util.HashMap;
import java.util.Map;
public class WorldRenderer { public class WorldRenderer {
private Texture texture; private Texture texture;
private OrthographicCamera cam; private OrthographicCamera cam;
private World world; private World world;
private Player player; private Player player;
private InputController inputController;
public WorldRenderer(World world, Player player) { public WorldRenderer(World world, Player player) {
this.player = player; this.player = player;
this.world = world; this.world = world;
texture = new Texture("core/assets/grass.png");
cam = new OrthographicCamera(10, 7); cam = new OrthographicCamera(10, 7);
cam.position.set(this.player.getX(), this.player.getY(),0); inputController = new InputController(cam);
}
public WorldRenderer(OrthographicCamera cam, World world, Player player) {
this.cam = cam;
this.world = world;
this.player = player;
texture = new Texture("core/assets/grass.png");
cam.zoom = 1;
cam = new OrthographicCamera(10, 7);
cam.position.set(player.getX(), player.getY(),0);
} }
public void render(Batch batch){ public void render(Batch batch){
cam.position.x = player.getX(); inputController.handleInput();
cam.position.y = player.getY(); batch.setProjectionMatrix(cam.combined);
drawWorld(batch, player); drawWorld(batch, player);
cam.update(); cam.update();
} }
@@ -49,7 +29,9 @@ public class WorldRenderer {
private void drawWorld(Batch batch,Player player) { private void drawWorld(Batch batch,Player player) {
for(int x = player.getX()-10; x<player.getX()+10; x++) { for(int x = player.getX()-10; x<player.getX()+10; x++) {
for(int y = player.getY()-10; y<player.getY()+10; y++) { for(int y = player.getY()-10; y<player.getY()+10; y++) {
batch.draw(texture, x, y, 1, 1); try {
batch.draw(world.getTile(x,y).getTexture(), (x * 16), (y * 16));
} catch (Exception e){}
} }
} }
} }