Making optimizations, removing unneeded creation of objects

This commit is contained in:
2020-04-11 05:21:07 -06:00
parent 890f38b6b4
commit c61f8ae6cb
4 changed files with 67 additions and 33 deletions

View File

@@ -7,11 +7,29 @@ import com.badlogic.gdx.math.Vector3;
import com.mygdx.game.Dimension.Chunks;
import com.mygdx.game.Restrictions;
import java.util.HashMap;
import static com.mygdx.game.Main.cam;
public class InputController implements Restrictions {
private Player player;
int i = 0;
private final HashMap<String, Animation<com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion>> animations;
public InputController(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")));
animations.put("Down", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Down")));
animations.put("DownRight", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "DownRight")));
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")));
this.player = player;
}
public void handleInput() {
i++;
@@ -23,19 +41,22 @@ public class InputController implements Restrictions {
cam.zoom -= 5;
}
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
cam.unproject(mousePos);
int x = Mouse.getSelectedX(mousePos) >> TILE_SHIFT;
int y = Mouse.getSelectedY(mousePos) >> TILE_SHIFT;
if (Gdx.input.isButtonJustPressed(Input.Buttons.LEFT) && i > KEY_DELAY) {
i = 0;
Chunks.placeBlock(x,y,"wood");
}
if (Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT) && i > KEY_DELAY) {
i = 0;
Chunks.removeBlock(x,y);
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);
int x = Mouse.getSelectedX(mousePos) >> TILE_SHIFT;
int y = Mouse.getSelectedY(mousePos) >> TILE_SHIFT;
if(Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) {
i = 0;
Chunks.placeBlock(x, y, "wood");
}
if (Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT)) {
i = 0;
Chunks.removeBlock(x,y);
}
}
if (Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
i = 0;
directionAnimation("UpLeft");
@@ -94,6 +115,6 @@ public class InputController implements Restrictions {
}
public void directionAnimation(String direction) {
Player.setAnimation(new Animation<>(1 / 4f, Player.textureAtlas.findRegions(Player.getSpriteName() + "_" + direction)));
player.setAnimation(animations.get(direction));
}
}

View File

@@ -1,5 +1,6 @@
package com.mygdx.game.Character;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
@@ -7,18 +8,24 @@ import com.badlogic.gdx.utils.Array;
import com.mygdx.game.Main;
import com.mygdx.game.Restrictions;
public class Player implements Restrictions {
private static int x,y;
private static String spriteName;
public static TextureAtlas textureAtlas;
private static Animation<TextureAtlas.AtlasRegion> animation;
import java.util.HashMap;
public static void create(int x, int y) {
public class Player implements Restrictions {
private static int x;
private static int y;
private OrthographicCamera cam;
private String spriteName;
public TextureAtlas textureAtlas;
private Animation<TextureAtlas.AtlasRegion> animation;
private HashMap<String, Animation> animations = new HashMap<>();
public Player(OrthographicCamera cam, int x, int y) {
Player.x = x<<TILE_SHIFT;
Player.y = y<<TILE_SHIFT;
Main.cam.translate(Player.x, Player.y);
cam.translate(Player.x, Player.y);
spriteName = "man";
textureAtlas = new TextureAtlas("core/assets/" + spriteName + ".atlas");
Array<TextureAtlas.AtlasRegion> keyFrames = textureAtlas.findRegions(spriteName + "_Down");
float frameDuration = 1 / 4f;
@@ -31,7 +38,7 @@ public class Player implements Restrictions {
Main.cam.translate(x,y);
}
public static void render(Batch batch, float timeSinceLastUpdate){
public void render(Batch batch, float timeSinceLastUpdate){
batch.draw(getAnimation().getKeyFrame(timeSinceLastUpdate, true), getX(), getY(),TILE_SIZE,TILE_SIZE);
}
@@ -51,15 +58,15 @@ public class Player implements Restrictions {
Player.y += y;
}
static String getSpriteName() {
String getSpriteName() {
return spriteName;
}
public static Animation<TextureAtlas.AtlasRegion> getAnimation() {
public Animation<TextureAtlas.AtlasRegion> getAnimation() {
return animation;
}
static void setAnimation(Animation<TextureAtlas.AtlasRegion> animationTemp) {
void setAnimation(Animation<TextureAtlas.AtlasRegion> animationTemp) {
animation = animationTemp;
}

View File

@@ -13,9 +13,9 @@ public class WorldRenderer implements Restrictions {
private Mouse mouse;
private InputController inputController;
public WorldRenderer(Mouse mouse) {
public WorldRenderer(Mouse mouse, InputController inputController) {
this.mouse = mouse;
inputController = new InputController();
this.inputController = inputController;
}
public void drawWorld(Batch batch, int layer) {

View File

@@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.mygdx.game.Character.InputController;
import com.mygdx.game.Character.Mouse;
import com.mygdx.game.Character.Player;
import com.mygdx.game.Dimension.BlockMaterials;
@@ -24,25 +25,30 @@ public class Main extends ApplicationAdapter {
public static float delta = 0;
float timeSinceLastUpdate = 0f; //accumulator
public static OrthographicCamera cam;
private InputController inputController;
private Mouse mouse;
private Player player;
@Override
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(mouse,fitViewport);
inputController = new InputController(player);
worldRenderer = new WorldRenderer(mouse,inputController);
batch = new SpriteBatch();
BlockMaterials.create();
Chunks.create();
//Starting location of the player
cam.translate(TILE_SIZE >> 1, TILE_SIZE >> 1);
Player.create(0,0);
cam.zoom = 25f;
Mouse mouse = new Mouse();
gui = new GUI(mouse,fitViewport);
worldRenderer = new WorldRenderer(mouse);
batch = new SpriteBatch();
}
@Override
@@ -54,7 +60,7 @@ public class Main extends ApplicationAdapter {
if(timeSinceLastUpdate > 1/10f){
worldRenderer.render(batch);
}
Player.render(batch,timeSinceLastUpdate);
player.render(batch,timeSinceLastUpdate);
delta +=Gdx.graphics.getDeltaTime();
if(delta > RENDER_TIME) {
@@ -72,6 +78,6 @@ public class Main extends ApplicationAdapter {
public void dispose () {
gui.dispose();
batch.dispose();
Player.textureAtlas.dispose();
player.textureAtlas.dispose();
}
}