From c61f8ae6cb5690db3fef4e8eba083d2430027c68 Mon Sep 17 00:00:00 2001 From: Solargale Date: Sat, 11 Apr 2020 05:21:07 -0600 Subject: [PATCH] Making optimizations, removing unneeded creation of objects --- .../mygdx/game/Character/InputController.java | 45 ++++++++++++++----- core/src/com/mygdx/game/Character/Player.java | 29 +++++++----- .../mygdx/game/Dimension/WorldRenderer.java | 4 +- core/src/com/mygdx/game/Main.java | 22 +++++---- 4 files changed, 67 insertions(+), 33 deletions(-) diff --git a/core/src/com/mygdx/game/Character/InputController.java b/core/src/com/mygdx/game/Character/InputController.java index 15b6fb0..16a681f 100644 --- a/core/src/com/mygdx/game/Character/InputController.java +++ b/core/src/com/mygdx/game/Character/InputController.java @@ -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> 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)); } } diff --git a/core/src/com/mygdx/game/Character/Player.java b/core/src/com/mygdx/game/Character/Player.java index a5538e3..a4eee04 100644 --- a/core/src/com/mygdx/game/Character/Player.java +++ b/core/src/com/mygdx/game/Character/Player.java @@ -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 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 animation; + private HashMap animations = new HashMap<>(); + + public Player(OrthographicCamera cam, int x, int y) { Player.x = x< 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 getAnimation() { + public Animation getAnimation() { return animation; } - static void setAnimation(Animation animationTemp) { + void setAnimation(Animation animationTemp) { animation = animationTemp; } diff --git a/core/src/com/mygdx/game/Dimension/WorldRenderer.java b/core/src/com/mygdx/game/Dimension/WorldRenderer.java index 10323eb..33b62d3 100644 --- a/core/src/com/mygdx/game/Dimension/WorldRenderer.java +++ b/core/src/com/mygdx/game/Dimension/WorldRenderer.java @@ -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) { diff --git a/core/src/com/mygdx/game/Main.java b/core/src/com/mygdx/game/Main.java index fcc2d53..7df775d 100644 --- a/core/src/com/mygdx/game/Main.java +++ b/core/src/com/mygdx/game/Main.java @@ -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(); } }