Making optimizations, removing unneeded creation of objects
This commit is contained in:
@@ -7,11 +7,29 @@ import com.badlogic.gdx.math.Vector3;
|
|||||||
import com.mygdx.game.Dimension.Chunks;
|
import com.mygdx.game.Dimension.Chunks;
|
||||||
import com.mygdx.game.Restrictions;
|
import com.mygdx.game.Restrictions;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
import static com.mygdx.game.Main.cam;
|
import static com.mygdx.game.Main.cam;
|
||||||
|
|
||||||
public class InputController implements Restrictions {
|
public class InputController implements Restrictions {
|
||||||
|
|
||||||
|
private Player player;
|
||||||
int i = 0;
|
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() {
|
public void handleInput() {
|
||||||
i++;
|
i++;
|
||||||
@@ -23,19 +41,22 @@ public class InputController implements Restrictions {
|
|||||||
cam.zoom -= 5;
|
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);
|
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
|
||||||
cam.unproject(mousePos);
|
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 > KEY_DELAY) {
|
|
||||||
i = 0;
|
i = 0;
|
||||||
Chunks.placeBlock(x,y,"wood");
|
Chunks.placeBlock(x, y, "wood");
|
||||||
}
|
}
|
||||||
if (Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT) && i > KEY_DELAY) {
|
if (Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT)) {
|
||||||
i = 0;
|
i = 0;
|
||||||
Chunks.removeBlock(x,y);
|
Chunks.removeBlock(x,y);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
directionAnimation("UpLeft");
|
directionAnimation("UpLeft");
|
||||||
@@ -94,6 +115,6 @@ public class InputController implements Restrictions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void directionAnimation(String direction) {
|
public void directionAnimation(String direction) {
|
||||||
Player.setAnimation(new Animation<>(1 / 4f, Player.textureAtlas.findRegions(Player.getSpriteName() + "_" + direction)));
|
player.setAnimation(animations.get(direction));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.mygdx.game.Character;
|
package com.mygdx.game.Character;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||||
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.TextureAtlas;
|
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.Main;
|
||||||
import com.mygdx.game.Restrictions;
|
import com.mygdx.game.Restrictions;
|
||||||
|
|
||||||
public class Player implements Restrictions {
|
import java.util.HashMap;
|
||||||
private static int x,y;
|
|
||||||
private static String spriteName;
|
|
||||||
public static TextureAtlas textureAtlas;
|
|
||||||
private static Animation<TextureAtlas.AtlasRegion> animation;
|
|
||||||
|
|
||||||
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.x = x<<TILE_SHIFT;
|
||||||
Player.y = y<<TILE_SHIFT;
|
Player.y = y<<TILE_SHIFT;
|
||||||
Main.cam.translate(Player.x, Player.y);
|
cam.translate(Player.x, Player.y);
|
||||||
|
|
||||||
spriteName = "man";
|
spriteName = "man";
|
||||||
|
|
||||||
textureAtlas = new TextureAtlas("core/assets/" + spriteName + ".atlas");
|
textureAtlas = new TextureAtlas("core/assets/" + spriteName + ".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;
|
||||||
@@ -31,7 +38,7 @@ public class Player implements Restrictions {
|
|||||||
Main.cam.translate(x,y);
|
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);
|
batch.draw(getAnimation().getKeyFrame(timeSinceLastUpdate, true), getX(), getY(),TILE_SIZE,TILE_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,15 +58,15 @@ public class Player implements Restrictions {
|
|||||||
Player.y += y;
|
Player.y += y;
|
||||||
}
|
}
|
||||||
|
|
||||||
static String getSpriteName() {
|
String getSpriteName() {
|
||||||
return spriteName;
|
return spriteName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Animation<TextureAtlas.AtlasRegion> getAnimation() {
|
public Animation<TextureAtlas.AtlasRegion> getAnimation() {
|
||||||
return animation;
|
return animation;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setAnimation(Animation<TextureAtlas.AtlasRegion> animationTemp) {
|
void setAnimation(Animation<TextureAtlas.AtlasRegion> animationTemp) {
|
||||||
animation = animationTemp;
|
animation = animationTemp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,9 +13,9 @@ public class WorldRenderer implements Restrictions {
|
|||||||
private Mouse mouse;
|
private Mouse mouse;
|
||||||
private InputController inputController;
|
private InputController inputController;
|
||||||
|
|
||||||
public WorldRenderer(Mouse mouse) {
|
public WorldRenderer(Mouse mouse, InputController inputController) {
|
||||||
this.mouse = mouse;
|
this.mouse = mouse;
|
||||||
inputController = new InputController();
|
this.inputController = inputController;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void drawWorld(Batch batch, int layer) {
|
public void drawWorld(Batch batch, int layer) {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.GL30;
|
|||||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
import com.badlogic.gdx.utils.viewport.FitViewport;
|
import com.badlogic.gdx.utils.viewport.FitViewport;
|
||||||
|
import com.mygdx.game.Character.InputController;
|
||||||
import com.mygdx.game.Character.Mouse;
|
import com.mygdx.game.Character.Mouse;
|
||||||
import com.mygdx.game.Character.Player;
|
import com.mygdx.game.Character.Player;
|
||||||
import com.mygdx.game.Dimension.BlockMaterials;
|
import com.mygdx.game.Dimension.BlockMaterials;
|
||||||
@@ -24,25 +25,30 @@ public class Main extends ApplicationAdapter {
|
|||||||
public static float delta = 0;
|
public static float delta = 0;
|
||||||
float timeSinceLastUpdate = 0f; //accumulator
|
float timeSinceLastUpdate = 0f; //accumulator
|
||||||
public static OrthographicCamera cam;
|
public static OrthographicCamera cam;
|
||||||
|
private InputController inputController;
|
||||||
|
private Mouse mouse;
|
||||||
|
private Player player;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
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();
|
||||||
|
gui = new GUI(mouse,fitViewport);
|
||||||
|
inputController = new InputController(player);
|
||||||
|
worldRenderer = new WorldRenderer(mouse,inputController);
|
||||||
|
batch = new SpriteBatch();
|
||||||
|
|
||||||
BlockMaterials.create();
|
BlockMaterials.create();
|
||||||
Chunks.create();
|
Chunks.create();
|
||||||
|
|
||||||
|
|
||||||
//Starting location of the player
|
//Starting location of the player
|
||||||
cam.translate(TILE_SIZE >> 1, TILE_SIZE >> 1);
|
cam.translate(TILE_SIZE >> 1, TILE_SIZE >> 1);
|
||||||
Player.create(0,0);
|
|
||||||
cam.zoom = 25f;
|
cam.zoom = 25f;
|
||||||
|
|
||||||
Mouse mouse = new Mouse();
|
|
||||||
gui = new GUI(mouse,fitViewport);
|
|
||||||
worldRenderer = new WorldRenderer(mouse);
|
|
||||||
batch = new SpriteBatch();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -54,7 +60,7 @@ public class Main extends ApplicationAdapter {
|
|||||||
if(timeSinceLastUpdate > 1/10f){
|
if(timeSinceLastUpdate > 1/10f){
|
||||||
worldRenderer.render(batch);
|
worldRenderer.render(batch);
|
||||||
}
|
}
|
||||||
Player.render(batch,timeSinceLastUpdate);
|
player.render(batch,timeSinceLastUpdate);
|
||||||
|
|
||||||
delta +=Gdx.graphics.getDeltaTime();
|
delta +=Gdx.graphics.getDeltaTime();
|
||||||
if(delta > RENDER_TIME) {
|
if(delta > RENDER_TIME) {
|
||||||
@@ -72,6 +78,6 @@ public class Main extends ApplicationAdapter {
|
|||||||
public void dispose () {
|
public void dispose () {
|
||||||
gui.dispose();
|
gui.dispose();
|
||||||
batch.dispose();
|
batch.dispose();
|
||||||
Player.textureAtlas.dispose();
|
player.textureAtlas.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user