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) {
this.player = player;
animations = new HashMap<>();
/*
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("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("Left", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Left")));
animations.put("Right", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Right")));
*/
}
public void handleInput() {
i++;
if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
cam.zoom += 5;
}
if (Gdx.input.isKeyPressed(Input.Keys.E)) {
cam.zoom -= 5;
}
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);
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) {
i = 0;
//player.setAnimation(animations.get("UpLeft"));
player.setAnimation(animations.get("UpLeft"));
Player.addX(-MOVEMENT_SPEED);
Player.addY(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) {
i = 0;
//player.setAnimation(animations.get("UpRight"));
player.setAnimation(animations.get("UpRight"));
Player.addX(MOVEMENT_SPEED);
Player.addY(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) {
i = 0;
//player.setAnimation(animations.get("DownLeft"));
player.setAnimation(animations.get("DownLeft"));
Player.addX(-MOVEMENT_SPEED);
Player.addY(-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) {
i = 0;
//player.setAnimation(animations.get("DownRight"));
player.setAnimation(animations.get("DownRight"));
Player.addX(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) {
i = 0;
//player.setAnimation(animations.get("Left"));
player.setAnimation(animations.get("Left"));
Player.addX(-MOVEMENT_SPEED);
cam.translate(-MOVEMENT_SPEED, 0);
}
else if (Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
i = 0;
//player.setAnimation(animations.get("Right"));
player.setAnimation(animations.get("Right"));
Player.addX(MOVEMENT_SPEED);
cam.translate(MOVEMENT_SPEED, 0);
}
else if (Gdx.input.isKeyPressed(Input.Keys.S) && i > KEY_DELAY) {
i = 0;
//player.setAnimation(animations.get("Down"));
player.setAnimation(animations.get("Down"));
Player.addY(-MOVEMENT_SPEED);
cam.translate(0, -MOVEMENT_SPEED);
}
if (Gdx.input.isKeyPressed(Input.Keys.W) && i > KEY_DELAY) {
i = 0;
//player.setAnimation(animations.get("Up"));
player.setAnimation(animations.get("Up"));
Player.addY(MOVEMENT_SPEED);
cam.translate(0, MOVEMENT_SPEED);
}

View File

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

View File

@@ -1,5 +1,8 @@
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.SpriteBatch;
import com.github.czyzby.kiwi.util.tuple.immutable.Triple;
@@ -7,7 +10,7 @@ import Collector.Character.InputController;
import Collector.Character.Mouse;
import Collector.Restrictions;
import static Collector.Main.cam;
import static Collector.Character.Player.getX;
public class WorldRenderer implements Restrictions {
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();
drawWorld(batch,0);
drawWorld(batch,1);
batch.setProjectionMatrix(cam.combined);
cam.update();
batch.setProjectionMatrix(Main.cam.combined);
Main.cam.update();
batch.draw(Player.getAnimation().getKeyFrame(timeSinceLastUpdate, true), Player.x, Player.y,TILE_SIZE,TILE_SIZE);
mouse.render(batch);
}
}

View File

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

View File

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