Fixed the player sprite issue

This commit is contained in:
2020-04-11 08:34:57 -06:00
parent 57f806983e
commit fabd9bcb82
5 changed files with 28 additions and 38 deletions

View File

@@ -20,7 +20,6 @@ public class InputController implements Restrictions {
public InputController(Player player) { public InputController(Player player) {
this.player = player; this.player = player;
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")));
animations.put("UpLeft", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "UpLeft"))); animations.put("UpLeft", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "UpLeft")));
@@ -29,21 +28,16 @@ public class InputController implements Restrictions {
animations.put("DownLeft", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "DownLeft"))); animations.put("DownLeft", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "DownLeft")));
animations.put("Left", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Left"))); animations.put("Left", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Left")));
animations.put("Right", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Right"))); animations.put("Right", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Right")));
*/
} }
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; cam.zoom += 5;
} }
if (Gdx.input.isKeyPressed(Input.Keys.E)) { if (Gdx.input.isKeyPressed(Input.Keys.E)) {
cam.zoom -= 5; 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); cam.unproject(mousePos);
@@ -61,28 +55,28 @@ public class InputController implements Restrictions {
if (Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) { if (Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
i = 0; i = 0;
//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); 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); 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); 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;
//player.setAnimation(animations.get("DownRight")); player.setAnimation(animations.get("DownRight"));
Player.addX(MOVEMENT_SPEED); Player.addX(MOVEMENT_SPEED);
Player.addY(-MOVEMENT_SPEED); Player.addY(-MOVEMENT_SPEED);
@@ -90,25 +84,25 @@ public class InputController implements Restrictions {
} }
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); 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); 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); 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); cam.translate(0, MOVEMENT_SPEED);
} }

View File

@@ -1,6 +1,8 @@
package Collector.Character; package Collector.Character;
import Collector.Dimension.BlockMaterials;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.Animation;
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.Sprite;
@@ -13,21 +15,21 @@ public class Player implements Restrictions {
public static int y; public static int y;
private final String spriteName; private final String spriteName;
public TextureAtlas textureAtlas; public TextureAtlas textureAtlas;
private Animation<TextureAtlas.AtlasRegion> animation; private static Animation<TextureAtlas.AtlasRegion> animation;
public static Sprite sprite; public static Sprite sprite;
public Player(OrthographicCamera cam, int x, int y) { public Player(OrthographicCamera cam, int x, int y) {
Player.x = x<<TILE_SHIFT; Player.x = x<<TILE_SHIFT;
Player.y = y<<TILE_SHIFT; Player.y = y<<TILE_SHIFT;
cam.translate(Player.x, Player.y);
spriteName = "man"; spriteName = "man";
sprite = new Sprite(new Texture("assets/wood.png"),TILE_SIZE,TILE_SIZE);
//textureAtlas = new TextureAtlas("man.atlas"); textureAtlas = new TextureAtlas("man.atlas");
//Array<TextureAtlas.AtlasRegion> keyFrames = textureAtlas.findRegions(spriteName + "_Down"); Array<TextureAtlas.AtlasRegion> keyFrames = textureAtlas.findRegions(spriteName + "_Down");
float frameDuration = 1 / 4f; float frameDuration = 1 / 4f;
//animation = new Animation<>(frameDuration, keyFrames); animation = new Animation<>(frameDuration, keyFrames);
} }
public static int getX() { public static int getX() {
@@ -50,8 +52,7 @@ public class Player implements Restrictions {
return spriteName; return spriteName;
} }
/* public static Animation<TextureAtlas.AtlasRegion> getAnimation() {
public Animation<TextureAtlas.AtlasRegion> getAnimation() {
return animation; return animation;
} }
@@ -59,6 +60,4 @@ public class Player implements Restrictions {
animation = animationTemp; animation = animationTemp;
} }
*/
} }

View File

@@ -1,5 +1,8 @@
package Collector.Dimension; package Collector.Dimension;
import Collector.Character.Player;
import Collector.Main;
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.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.github.czyzby.kiwi.util.tuple.immutable.Triple; import com.github.czyzby.kiwi.util.tuple.immutable.Triple;
@@ -7,7 +10,7 @@ import Collector.Character.InputController;
import Collector.Character.Mouse; import Collector.Character.Mouse;
import Collector.Restrictions; import Collector.Restrictions;
import static Collector.Main.cam; import static Collector.Character.Player.getX;
public class WorldRenderer implements Restrictions { public class WorldRenderer implements Restrictions {
private final Mouse mouse; private final Mouse mouse;
@@ -30,12 +33,13 @@ public class WorldRenderer implements Restrictions {
} }
} }
public void render(SpriteBatch batch) { public void render(SpriteBatch batch, float timeSinceLastUpdate) {
inputController.handleInput(); inputController.handleInput();
drawWorld(batch,0); drawWorld(batch,0);
drawWorld(batch,1); drawWorld(batch,1);
batch.setProjectionMatrix(cam.combined); batch.setProjectionMatrix(Main.cam.combined);
cam.update(); Main.cam.update();
batch.draw(Player.getAnimation().getKeyFrame(timeSinceLastUpdate, true), Player.x, Player.y,TILE_SIZE,TILE_SIZE);
mouse.render(batch); mouse.render(batch);
} }
} }

View File

@@ -1,5 +1,6 @@
package Collector; package Collector;
import Collector.Dimension.*;
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.GL30; import com.badlogic.gdx.graphics.GL30;
@@ -9,10 +10,6 @@ import com.badlogic.gdx.utils.viewport.FitViewport;
import Collector.Character.InputController; import Collector.Character.InputController;
import Collector.Character.Mouse; import Collector.Character.Mouse;
import Collector.Character.Player; import Collector.Character.Player;
import Collector.Dimension.BlockMaterials;
import Collector.Dimension.Chunks;
import Collector.Dimension.World;
import Collector.Dimension.WorldRenderer;
import Collector.UI.GUI; import Collector.UI.GUI;
import static Collector.Restrictions.*; import static Collector.Restrictions.*;
@@ -34,10 +31,10 @@ public class Main extends ApplicationAdapter {
public void create () { public void create () {
Gdx.graphics.setWindowedMode(1024, 576); Gdx.graphics.setWindowedMode(1024, 576);
cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT); cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
player = new Player(cam, 0, 0);
fitViewport = new FitViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, cam); fitViewport = new FitViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, cam);
mouse = new Mouse(); mouse = new Mouse();
gui = new GUI(fitViewport); gui = new GUI(fitViewport);
player = new Player(cam, 0, 0);
inputController = new InputController(player); inputController = new InputController(player);
worldRenderer = new WorldRenderer(mouse, inputController); worldRenderer = new WorldRenderer(mouse, inputController);
batch = new SpriteBatch(); batch = new SpriteBatch();
@@ -45,8 +42,6 @@ public class Main extends ApplicationAdapter {
BlockMaterials.create(); BlockMaterials.create();
Chunks.create(); Chunks.create();
//Starting location of the player
cam.translate(TILE_SIZE >> 1, TILE_SIZE >> 1); cam.translate(TILE_SIZE >> 1, TILE_SIZE >> 1);
cam.zoom = 25f; cam.zoom = 25f;
} }
@@ -55,11 +50,10 @@ public class Main extends ApplicationAdapter {
public void render () { public void render () {
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
timeSinceLastUpdate += Gdx.graphics.getDeltaTime(); //Accumulate delta time timeSinceLastUpdate += Gdx.graphics.getDeltaTime(); //Accumulate delta time
batch.begin(); batch.begin();
if(timeSinceLastUpdate > 1/10f){ if(timeSinceLastUpdate > 1/10f){
worldRenderer.render(batch); worldRenderer.render(batch,timeSinceLastUpdate);
} }
delta += Gdx.graphics.getDeltaTime(); delta += Gdx.graphics.getDeltaTime();
@@ -70,7 +64,6 @@ public class Main extends ApplicationAdapter {
} }
gui.render(); gui.render();
batch.draw(BlockMaterials.materials.get("wood").getTexture(), Player.x, Player.y,TILE_SIZE,TILE_SIZE);
batch.end(); batch.end();
} }

View File

@@ -16,7 +16,7 @@ public class Lwjgl3Launcher {
private static Lwjgl3ApplicationConfiguration getDefaultConfiguration() { private static Lwjgl3ApplicationConfiguration getDefaultConfiguration() {
Lwjgl3ApplicationConfiguration configuration = new Lwjgl3ApplicationConfiguration(); Lwjgl3ApplicationConfiguration configuration = new Lwjgl3ApplicationConfiguration();
configuration.setTitle("Collevtor"); configuration.setTitle("Collector");
configuration.setWindowedMode(640, 480); configuration.setWindowedMode(640, 480);
configuration.setWindowIcon("libgdx128.png", "libgdx64.png", "libgdx32.png", "libgdx16.png"); configuration.setWindowIcon("libgdx128.png", "libgdx64.png", "libgdx32.png", "libgdx16.png");
return configuration; return configuration;