Fixed issue with crosshair being missing. Also moved crosshair logic into WorldRenderer
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package Collector.Character;
|
package Collector.Character;
|
||||||
|
|
||||||
|
import Collector.Main;
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.Input;
|
import com.badlogic.gdx.Input;
|
||||||
import com.badlogic.gdx.graphics.g2d.Animation;
|
import com.badlogic.gdx.graphics.g2d.Animation;
|
||||||
@@ -9,16 +10,16 @@ import Collector.Restrictions;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import static Collector.Main.cam;
|
|
||||||
|
|
||||||
public class InputController implements Restrictions {
|
public class InputController implements Restrictions {
|
||||||
|
|
||||||
private final Player player;
|
private final Player player;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
private final HashMap<String, Animation<com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion>> animations;
|
private final HashMap<String, Animation<com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion>> animations;
|
||||||
|
private Mouse mouse;
|
||||||
|
|
||||||
public InputController(Player player) {
|
public InputController(Player player, Mouse mouse) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
|
this.mouse = mouse;
|
||||||
animations = new HashMap<>();
|
animations = new HashMap<>();
|
||||||
animations.put("Up", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Up")));
|
animations.put("Up", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Up")));
|
||||||
animations.put("UpRight", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "UpRight")));
|
animations.put("UpRight", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "UpRight")));
|
||||||
@@ -33,16 +34,16 @@ public class InputController implements Restrictions {
|
|||||||
public void handleInput() {
|
public void handleInput() {
|
||||||
i++;
|
i++;
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
|
if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
|
||||||
cam.zoom += 5;
|
Main.cam.zoom += 5;
|
||||||
}
|
}
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.E)) {
|
if (Gdx.input.isKeyPressed(Input.Keys.E)) {
|
||||||
cam.zoom -= 5;
|
Main.cam.zoom -= 5;
|
||||||
}
|
}
|
||||||
if ((Gdx.input.isButtonJustPressed(Input.Buttons.LEFT) || Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT) )&& i > KEY_DELAY) {
|
if ((Gdx.input.isButtonJustPressed(Input.Buttons.LEFT) || Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT) )&& i > KEY_DELAY) {
|
||||||
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
|
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
|
||||||
cam.unproject(mousePos);
|
Main.cam.unproject(mousePos);
|
||||||
int x = Mouse.getSelectedX(mousePos) >> TILE_SHIFT;
|
int x = mouse.getSelectedX(mousePos) >> TILE_SHIFT;
|
||||||
int y = Mouse.getSelectedY(mousePos) >> TILE_SHIFT;
|
int y = mouse.getSelectedY(mousePos) >> TILE_SHIFT;
|
||||||
if(Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) {
|
if(Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) {
|
||||||
i = 0;
|
i = 0;
|
||||||
Chunks.placeBlock(x, y, "wood");
|
Chunks.placeBlock(x, y, "wood");
|
||||||
@@ -58,21 +59,21 @@ public class InputController implements Restrictions {
|
|||||||
player.setAnimation(animations.get("UpLeft"));
|
player.setAnimation(animations.get("UpLeft"));
|
||||||
Player.addX(-MOVEMENT_SPEED);
|
Player.addX(-MOVEMENT_SPEED);
|
||||||
Player.addY(MOVEMENT_SPEED);
|
Player.addY(MOVEMENT_SPEED);
|
||||||
cam.translate(-MOVEMENT_SPEED, MOVEMENT_SPEED);
|
Main.cam.translate(-MOVEMENT_SPEED, MOVEMENT_SPEED);
|
||||||
}
|
}
|
||||||
else if (Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
|
else if (Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
|
||||||
i = 0;
|
i = 0;
|
||||||
player.setAnimation(animations.get("UpRight"));
|
player.setAnimation(animations.get("UpRight"));
|
||||||
Player.addX(MOVEMENT_SPEED);
|
Player.addX(MOVEMENT_SPEED);
|
||||||
Player.addY(MOVEMENT_SPEED);
|
Player.addY(MOVEMENT_SPEED);
|
||||||
cam.translate(MOVEMENT_SPEED, MOVEMENT_SPEED);
|
Main.cam.translate(MOVEMENT_SPEED, MOVEMENT_SPEED);
|
||||||
}
|
}
|
||||||
else if (Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
|
else if (Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
|
||||||
i = 0;
|
i = 0;
|
||||||
player.setAnimation(animations.get("DownLeft"));
|
player.setAnimation(animations.get("DownLeft"));
|
||||||
Player.addX(-MOVEMENT_SPEED);
|
Player.addX(-MOVEMENT_SPEED);
|
||||||
Player.addY(-MOVEMENT_SPEED);
|
Player.addY(-MOVEMENT_SPEED);
|
||||||
cam.translate(-MOVEMENT_SPEED, -MOVEMENT_SPEED);
|
Main.cam.translate(-MOVEMENT_SPEED, -MOVEMENT_SPEED);
|
||||||
}
|
}
|
||||||
else if (Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
|
else if (Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
|
||||||
i = 0;
|
i = 0;
|
||||||
@@ -80,33 +81,32 @@ public class InputController implements Restrictions {
|
|||||||
Player.addX(MOVEMENT_SPEED);
|
Player.addX(MOVEMENT_SPEED);
|
||||||
Player.addY(-MOVEMENT_SPEED);
|
Player.addY(-MOVEMENT_SPEED);
|
||||||
|
|
||||||
cam.translate(MOVEMENT_SPEED, -MOVEMENT_SPEED);
|
Main.cam.translate(MOVEMENT_SPEED, -MOVEMENT_SPEED);
|
||||||
}
|
}
|
||||||
else if (Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
|
else if (Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
|
||||||
i = 0;
|
i = 0;
|
||||||
player.setAnimation(animations.get("Left"));
|
player.setAnimation(animations.get("Left"));
|
||||||
Player.addX(-MOVEMENT_SPEED);
|
Player.addX(-MOVEMENT_SPEED);
|
||||||
cam.translate(-MOVEMENT_SPEED, 0);
|
Main.cam.translate(-MOVEMENT_SPEED, 0);
|
||||||
}
|
}
|
||||||
else if (Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
|
else if (Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
|
||||||
i = 0;
|
i = 0;
|
||||||
player.setAnimation(animations.get("Right"));
|
player.setAnimation(animations.get("Right"));
|
||||||
Player.addX(MOVEMENT_SPEED);
|
Player.addX(MOVEMENT_SPEED);
|
||||||
cam.translate(MOVEMENT_SPEED, 0);
|
Main.cam.translate(MOVEMENT_SPEED, 0);
|
||||||
}
|
}
|
||||||
else if (Gdx.input.isKeyPressed(Input.Keys.S) && i > KEY_DELAY) {
|
else if (Gdx.input.isKeyPressed(Input.Keys.S) && i > KEY_DELAY) {
|
||||||
i = 0;
|
i = 0;
|
||||||
player.setAnimation(animations.get("Down"));
|
player.setAnimation(animations.get("Down"));
|
||||||
Player.addY(-MOVEMENT_SPEED);
|
Player.addY(-MOVEMENT_SPEED);
|
||||||
cam.translate(0, -MOVEMENT_SPEED);
|
Main.cam.translate(0, -MOVEMENT_SPEED);
|
||||||
}
|
}
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.W) && i > KEY_DELAY) {
|
if (Gdx.input.isKeyPressed(Input.Keys.W) && i > KEY_DELAY) {
|
||||||
i = 0;
|
i = 0;
|
||||||
player.setAnimation(animations.get("Up"));
|
player.setAnimation(animations.get("Up"));
|
||||||
Player.addY(MOVEMENT_SPEED);
|
Player.addY(MOVEMENT_SPEED);
|
||||||
cam.translate(0, MOVEMENT_SPEED);
|
Main.cam.translate(0, MOVEMENT_SPEED);
|
||||||
}
|
}
|
||||||
cam.update();
|
Main.cam.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
package Collector.Character;
|
package Collector.Character;
|
||||||
|
|
||||||
import Collector.Main;
|
|
||||||
import Collector.Restrictions;
|
import Collector.Restrictions;
|
||||||
import com.badlogic.gdx.Gdx;
|
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
import com.badlogic.gdx.graphics.Texture;
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
|
||||||
import com.badlogic.gdx.math.Vector3;
|
import com.badlogic.gdx.math.Vector3;
|
||||||
|
|
||||||
public class Mouse implements Restrictions {
|
public class Mouse implements Restrictions {
|
||||||
@@ -14,25 +11,15 @@ public class Mouse implements Restrictions {
|
|||||||
this.crosshair = new Texture("assets/crosshair.png");
|
this.crosshair = new Texture("assets/crosshair.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void selectedTile(SpriteBatch batch) {
|
public Texture getCrosshair() {
|
||||||
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
|
return crosshair;
|
||||||
Main.cam.unproject(mousePos);
|
|
||||||
int x = getSelectedX(mousePos);
|
|
||||||
int y = getSelectedY(mousePos);
|
|
||||||
batch.draw(crosshair, x, y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getSelectedX(Vector3 mousePos) {
|
public int getSelectedX(Vector3 mousePos) {
|
||||||
return ((int)(mousePos.x)>>4)<<4;
|
return ((int)(mousePos.x)>>4)<<4;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getSelectedY(Vector3 mousePos) {
|
public int getSelectedY(Vector3 mousePos) {
|
||||||
return ((int)(mousePos.y)>>4)<<4;
|
return ((int)(mousePos.y)>>4)<<4;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render(SpriteBatch batch) {
|
|
||||||
selectedTile(batch);
|
|
||||||
//System.out.println("X = " + getTileX(cam) + " Y = "+ getTileY(cam));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
package Collector.Dimension;
|
package Collector.Dimension;
|
||||||
|
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
public class Block {
|
public class Block {
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
|
|||||||
@@ -2,24 +2,29 @@ package Collector.Dimension;
|
|||||||
|
|
||||||
import Collector.Character.Player;
|
import Collector.Character.Player;
|
||||||
import Collector.Main;
|
import Collector.Main;
|
||||||
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
|
import com.badlogic.gdx.math.Vector3;
|
||||||
import com.github.czyzby.kiwi.util.tuple.immutable.Triple;
|
import com.github.czyzby.kiwi.util.tuple.immutable.Triple;
|
||||||
import Collector.Character.InputController;
|
import Collector.Character.InputController;
|
||||||
import Collector.Character.Mouse;
|
import Collector.Character.Mouse;
|
||||||
import Collector.Restrictions;
|
import Collector.Restrictions;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
public class WorldRenderer implements Restrictions {
|
public class WorldRenderer implements Restrictions {
|
||||||
private final Mouse mouse;
|
private final Mouse mouse;
|
||||||
private final InputController inputController;
|
private final InputController inputController;
|
||||||
|
|
||||||
public WorldRenderer(Mouse mouse, InputController inputController) {
|
public WorldRenderer(InputController inputController, Mouse mouse) {
|
||||||
this.mouse = mouse;
|
|
||||||
this.inputController = inputController;
|
this.inputController = inputController;
|
||||||
|
this.mouse = mouse;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void drawWorld(Batch batch, int layer) {
|
public void drawWorld(Batch batch, int layer) {
|
||||||
for (Triple<Integer, Integer, Integer> chunkpair : Chunks.blocks.keySet()) {
|
for (Iterator<Triple<Integer, Integer, Integer>> iterator = Chunks.blocks.keySet().iterator(); iterator.hasNext(); ) {
|
||||||
|
Triple<Integer, Integer, Integer> chunkpair = iterator.next();
|
||||||
if (chunkpair.getThird() == layer) {
|
if (chunkpair.getThird() == layer) {
|
||||||
batch.draw(
|
batch.draw(
|
||||||
BlockMaterials.textures.get(Chunks.blocks.get(chunkpair).getName()),
|
BlockMaterials.textures.get(Chunks.blocks.get(chunkpair).getName()),
|
||||||
@@ -30,13 +35,21 @@ public class WorldRenderer implements Restrictions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void mouseCrosshair(SpriteBatch batch) {
|
||||||
|
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
|
||||||
|
Main.cam.unproject(mousePos);
|
||||||
|
int x = mouse.getSelectedX(mousePos);
|
||||||
|
int y = mouse.getSelectedY(mousePos);
|
||||||
|
batch.draw(mouse.getCrosshair(), x, y);
|
||||||
|
}
|
||||||
|
|
||||||
public void render(SpriteBatch batch, float timeSinceLastUpdate) {
|
public void render(SpriteBatch batch, float timeSinceLastUpdate) {
|
||||||
inputController.handleInput();
|
//Higher means draws in a lower layer
|
||||||
drawWorld(batch,0);
|
drawWorld(batch,0);
|
||||||
drawWorld(batch,1);
|
|
||||||
batch.setProjectionMatrix(Main.cam.combined);
|
|
||||||
Main.cam.update();
|
|
||||||
batch.draw(Player.getAnimation().getKeyFrame(timeSinceLastUpdate, true), Player.x, Player.y,TILE_SIZE,TILE_SIZE);
|
batch.draw(Player.getAnimation().getKeyFrame(timeSinceLastUpdate, true), Player.x, Player.y,TILE_SIZE,TILE_SIZE);
|
||||||
mouse.render(batch);
|
drawWorld(batch,1);
|
||||||
|
mouseCrosshair(batch);
|
||||||
|
inputController.handleInput();
|
||||||
|
batch.setProjectionMatrix(Main.cam.combined);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,14 +37,14 @@ public class Main extends ApplicationAdapter {
|
|||||||
mouse = new Mouse();
|
mouse = new Mouse();
|
||||||
gui = new GUI(fitViewport);
|
gui = new GUI(fitViewport);
|
||||||
player = new Player(cam, 0, 0);
|
player = new Player(cam, 0, 0);
|
||||||
inputController = new InputController(player);
|
inputController = new InputController(player,mouse);
|
||||||
worldRenderer = new WorldRenderer(mouse, inputController);
|
worldRenderer = new WorldRenderer(inputController,mouse);
|
||||||
batch = new SpriteBatch();
|
batch = new SpriteBatch();
|
||||||
|
|
||||||
BlockMaterials.create();
|
BlockMaterials.create();
|
||||||
Chunks.create();
|
Chunks.create();
|
||||||
|
|
||||||
cam.translate(TILE_SIZE >> 1, TILE_SIZE >> 1);
|
//cam.translate(TILE_SIZE, TILE_SIZE);
|
||||||
cam.zoom = 25f;
|
cam.zoom = 25f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,9 +54,7 @@ public class Main extends ApplicationAdapter {
|
|||||||
|
|
||||||
timeSinceLastUpdate += Gdx.graphics.getDeltaTime(); //Accumulate delta time
|
timeSinceLastUpdate += Gdx.graphics.getDeltaTime(); //Accumulate delta time
|
||||||
batch.begin();
|
batch.begin();
|
||||||
if(timeSinceLastUpdate > 1/10f){
|
|
||||||
worldRenderer.render(batch,timeSinceLastUpdate);
|
worldRenderer.render(batch,timeSinceLastUpdate);
|
||||||
}
|
|
||||||
|
|
||||||
delta += Gdx.graphics.getDeltaTime();
|
delta += Gdx.graphics.getDeltaTime();
|
||||||
if (delta > RENDER_TIME) {
|
if (delta > RENDER_TIME) {
|
||||||
@@ -64,7 +62,7 @@ public class Main extends ApplicationAdapter {
|
|||||||
World.loadChunks();
|
World.loadChunks();
|
||||||
World.unloadChunks();
|
World.unloadChunks();
|
||||||
}
|
}
|
||||||
|
cam.update();
|
||||||
gui.render();
|
gui.render();
|
||||||
batch.end();
|
batch.end();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user