Changed a bunch of variables to use bitwise shift instead of multiplication and division
This commit is contained in:
@@ -2,20 +2,15 @@ package com.mygdx.game.Character;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.Animation;
|
||||
import com.mygdx.game.Restrictions;
|
||||
|
||||
import static com.mygdx.game.Main.cam;
|
||||
|
||||
public class InputController implements Restrictions {
|
||||
|
||||
private OrthographicCamera cam;
|
||||
private Player player;
|
||||
int i = 0;
|
||||
|
||||
public InputController(OrthographicCamera cam, Player player) {
|
||||
this.cam = cam;
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
public void handleInput() {
|
||||
i++;
|
||||
@@ -30,29 +25,29 @@ public class InputController implements Restrictions {
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.UP) && Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) {
|
||||
i = 0;
|
||||
directionAnimation("UpLeft");
|
||||
player.addX(-MOVEMENT_SPEED);
|
||||
player.addY(MOVEMENT_SPEED);
|
||||
Player.addX(-MOVEMENT_SPEED);
|
||||
Player.addY(MOVEMENT_SPEED);
|
||||
cam.translate(-MOVEMENT_SPEED,MOVEMENT_SPEED);
|
||||
}
|
||||
else if (Gdx.input.isKeyPressed(Input.Keys.UP)&& Gdx.input.isKeyPressed(Input.Keys.RIGHT) &&i > KEY_DELAY) {
|
||||
i = 0;
|
||||
directionAnimation("UpRight");
|
||||
player.addX(MOVEMENT_SPEED);
|
||||
player.addY(MOVEMENT_SPEED);
|
||||
Player.addX(MOVEMENT_SPEED);
|
||||
Player.addY(MOVEMENT_SPEED);
|
||||
cam.translate(MOVEMENT_SPEED,MOVEMENT_SPEED);
|
||||
}
|
||||
else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)&& Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) {
|
||||
i = 0;
|
||||
directionAnimation("DownLeft");
|
||||
player.addX(-MOVEMENT_SPEED);
|
||||
player.addY(-MOVEMENT_SPEED);
|
||||
Player.addX(-MOVEMENT_SPEED);
|
||||
Player.addY(-MOVEMENT_SPEED);
|
||||
cam.translate(-MOVEMENT_SPEED,-MOVEMENT_SPEED);
|
||||
}
|
||||
else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)&& Gdx.input.isKeyPressed(Input.Keys.RIGHT) && i > KEY_DELAY) {
|
||||
i = 0;
|
||||
directionAnimation("DownRight");
|
||||
player.addX(MOVEMENT_SPEED);
|
||||
player.addY(-MOVEMENT_SPEED);
|
||||
Player.addX(MOVEMENT_SPEED);
|
||||
Player.addY(-MOVEMENT_SPEED);
|
||||
|
||||
cam.translate(MOVEMENT_SPEED,-MOVEMENT_SPEED);
|
||||
}
|
||||
@@ -60,33 +55,34 @@ public class InputController implements Restrictions {
|
||||
else if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) {
|
||||
i = 0;
|
||||
directionAnimation("Left");
|
||||
player.addX(-MOVEMENT_SPEED);
|
||||
Player.addX(-MOVEMENT_SPEED);
|
||||
cam.translate(-MOVEMENT_SPEED,0);
|
||||
}
|
||||
else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)&& i > KEY_DELAY) {
|
||||
i = 0;
|
||||
directionAnimation("Right");
|
||||
player.addX(MOVEMENT_SPEED);
|
||||
Player.addX(MOVEMENT_SPEED);
|
||||
cam.translate(MOVEMENT_SPEED,0);
|
||||
}
|
||||
else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)&& i > KEY_DELAY) {
|
||||
i = 0;
|
||||
directionAnimation("Down");
|
||||
player.addY(-MOVEMENT_SPEED);
|
||||
Player.addY(-MOVEMENT_SPEED);
|
||||
cam.translate(0,-MOVEMENT_SPEED);
|
||||
}
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.UP)&& i > KEY_DELAY) {
|
||||
i = 0;
|
||||
directionAnimation("Up");
|
||||
player.addY(MOVEMENT_SPEED);
|
||||
Player.addY(MOVEMENT_SPEED);
|
||||
|
||||
cam.translate(0,MOVEMENT_SPEED);
|
||||
}
|
||||
|
||||
cam.update();
|
||||
}
|
||||
|
||||
|
||||
public void directionAnimation(String direction) {
|
||||
player.setAnimation(new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + direction)));
|
||||
Player.setAnimation(new Animation<>(1 / 4f, Player.textureAtlas.findRegions(Player.getSpriteName() + "_" + direction)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,45 +7,43 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.mygdx.game.Restrictions;
|
||||
|
||||
import static java.lang.Math.round;
|
||||
import static com.mygdx.game.Main.cam;
|
||||
|
||||
public class Mouse implements Restrictions {
|
||||
Player player;
|
||||
Texture crosshair = new Texture("core/assets/crosshair.png");
|
||||
|
||||
public Mouse(Player player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
public void selectedTile(SpriteBatch batch, OrthographicCamera cam){
|
||||
public void selectedTile(SpriteBatch batch){
|
||||
Vector3 mousePos = new Vector3( Gdx.input.getX(), Gdx.input.getY(),0);
|
||||
cam.unproject(mousePos);
|
||||
batch.draw(crosshair, getSelectedX(mousePos), getSelectedY(mousePos));
|
||||
int x = getSelectedX(mousePos);
|
||||
int y = getSelectedY(mousePos);
|
||||
batch.draw(crosshair, x, y);
|
||||
}
|
||||
|
||||
public int getSelectedX(Vector3 mousePos) {
|
||||
return round((mousePos.x-8)/16)*16;
|
||||
return ((int)(mousePos.x)>>4)<<4;
|
||||
}
|
||||
|
||||
public int getSelectedY(Vector3 mousePos) {
|
||||
return round((mousePos.y-8)/16)*16;
|
||||
return ((int)(mousePos.y)>>4)<<4;
|
||||
}
|
||||
|
||||
public int getTileX(OrthographicCamera cam){
|
||||
Vector3 mousePos = new Vector3( Gdx.input.getX(), Gdx.input.getY(),0);
|
||||
cam.unproject(mousePos);
|
||||
return (getSelectedX(mousePos) + player.getX()/2)/TILE_SIZE;
|
||||
return (getSelectedX(mousePos) + Player.getX()>>1)/TILE_SIZE;
|
||||
}
|
||||
|
||||
public int getTileY(OrthographicCamera cam){
|
||||
Vector3 mousePos = new Vector3( Gdx.input.getX(), Gdx.input.getY(),0);
|
||||
cam.unproject(mousePos);
|
||||
return (getSelectedY(mousePos) + player.getY()/2)/TILE_SIZE;
|
||||
return (getSelectedY(mousePos) + Player.getY()>>1)/TILE_SIZE;
|
||||
}
|
||||
|
||||
|
||||
public void render(SpriteBatch batch, OrthographicCamera cam) {
|
||||
selectedTile(batch,cam);
|
||||
public void render(SpriteBatch batch) {
|
||||
selectedTile(batch);
|
||||
//System.out.println("X = " + getTileX(cam) + " Y = "+ getTileY(cam));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,72 +1,66 @@
|
||||
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;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.mygdx.game.Main;
|
||||
import com.mygdx.game.Restrictions;
|
||||
|
||||
public class Player implements Restrictions {
|
||||
private int x,y;
|
||||
private String spriteName;
|
||||
private TextureAtlas textureAtlas;
|
||||
private Animation<TextureAtlas.AtlasRegion> animation;
|
||||
private OrthographicCamera cam;
|
||||
private static int x,y;
|
||||
private static String spriteName;
|
||||
public static TextureAtlas textureAtlas;
|
||||
private static Animation<TextureAtlas.AtlasRegion> animation;
|
||||
|
||||
public Player(int x, int y, OrthographicCamera cam) {
|
||||
this.cam = cam;
|
||||
this.x = x*TILE_SIZE;
|
||||
this.y = y*TILE_SIZE;
|
||||
public static void create(int x, int y) {
|
||||
Player.x = x<<TILE_SHIFT;
|
||||
Player.y = y<<TILE_SHIFT;
|
||||
Main.cam.translate(Player.x, Player.y);
|
||||
|
||||
this.cam.translate(this.x,this.y);
|
||||
|
||||
this.spriteName = "man";
|
||||
this.textureAtlas = new TextureAtlas("core/assets/" + spriteName + ".atlas");
|
||||
spriteName = "man";
|
||||
textureAtlas = new TextureAtlas("core/assets/" + spriteName + ".atlas");
|
||||
Array<TextureAtlas.AtlasRegion> keyFrames = textureAtlas.findRegions(spriteName + "_Down");
|
||||
float frameDuration = 1 / 4f;
|
||||
this.animation = new Animation<>(frameDuration, keyFrames);
|
||||
animation = new Animation<>(frameDuration, keyFrames);
|
||||
}
|
||||
|
||||
public static void teleport(Player player, int x, int y){
|
||||
player.x = x*TILE_SIZE;
|
||||
player.y = y*TILE_SIZE;
|
||||
player.cam.translate(x,y);
|
||||
public static void teleport(int x, int y){
|
||||
Player.x = x<<TILE_SHIFT;
|
||||
Player.y = y<<TILE_SHIFT;
|
||||
Main.cam.translate(x,y);
|
||||
}
|
||||
|
||||
public void render(Batch batch, float timeSinceLastUpdate){
|
||||
public static void render(Batch batch, float timeSinceLastUpdate){
|
||||
batch.draw(getAnimation().getKeyFrame(timeSinceLastUpdate, true), getX(), getY(),TILE_SIZE,TILE_SIZE);
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
public static int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
public static int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void addX(int x){
|
||||
this.x += x;
|
||||
public static void addX(int x){
|
||||
Player.x += x;
|
||||
}
|
||||
|
||||
public void addY(int y){
|
||||
this.y += y;
|
||||
public static void addY(int y){
|
||||
Player.y += y;
|
||||
}
|
||||
|
||||
String getSpriteName() {
|
||||
static String getSpriteName() {
|
||||
return spriteName;
|
||||
}
|
||||
|
||||
public TextureAtlas getTextureAtlas() {
|
||||
return textureAtlas;
|
||||
}
|
||||
|
||||
public Animation<TextureAtlas.AtlasRegion> getAnimation() {
|
||||
public static Animation<TextureAtlas.AtlasRegion> getAnimation() {
|
||||
return animation;
|
||||
}
|
||||
|
||||
void setAnimation(Animation<TextureAtlas.AtlasRegion> animation) {
|
||||
this.animation = animation;
|
||||
static void setAnimation(Animation<TextureAtlas.AtlasRegion> animationTemp) {
|
||||
animation = animationTemp;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@ package com.mygdx.game.Dimension;
|
||||
|
||||
public class Block {
|
||||
private String name;
|
||||
private Boolean floor, wall, harvestable, breakable, passable;
|
||||
private Boolean harvestable;
|
||||
private Boolean breakable;
|
||||
private Boolean passable;
|
||||
|
||||
public Block(String name, Boolean floor, Boolean wall, Boolean harvestable, Boolean breakable, Boolean passable) {
|
||||
public Block(String name, Boolean harvestable, Boolean breakable, Boolean passable) {
|
||||
this.name = name;
|
||||
this.floor = floor;
|
||||
this.wall = wall;
|
||||
this.harvestable = harvestable;
|
||||
this.breakable = breakable;
|
||||
this.passable = passable;
|
||||
@@ -21,22 +21,6 @@ public class Block {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Boolean getFloor() {
|
||||
return floor;
|
||||
}
|
||||
|
||||
public void setFloor(Boolean floor) {
|
||||
this.floor = floor;
|
||||
}
|
||||
|
||||
public Boolean getWall() {
|
||||
return wall;
|
||||
}
|
||||
|
||||
public void setWall(Boolean wall) {
|
||||
this.wall = wall;
|
||||
}
|
||||
|
||||
public Boolean getHarvestable() {
|
||||
return harvestable;
|
||||
}
|
||||
|
||||
@@ -11,17 +11,17 @@ public class BlockMaterials {
|
||||
//Private so the singleton can't be instantiated
|
||||
private BlockMaterials() {}
|
||||
|
||||
public Texture getTexture(String material) {
|
||||
public static Texture getTexture(String material) {
|
||||
return textures.get(material);
|
||||
}
|
||||
|
||||
public static void create(){
|
||||
materials.put("grass",new Block("grass",true,false,true,true,true));
|
||||
materials.put("wood",new Block("wood",false,true,true,true,false));
|
||||
materials.put("water",new Block("water",true,false,false,false,false));
|
||||
materials.put("stone",new Block("stone",true,false,false,false,false));
|
||||
materials.put("snow",new Block("snow",true,false,false,false,false));
|
||||
materials.put("sand",new Block("sand",true,false,false,false,false));
|
||||
materials.put("grass",new Block("grass",true,false,true));
|
||||
materials.put("wood",new Block("wood",false,true,true));
|
||||
materials.put("water",new Block("water",true,false,false));
|
||||
materials.put("stone",new Block("stone",true,false,false));
|
||||
materials.put("snow",new Block("snow",true,false,false));
|
||||
materials.put("sand",new Block("sand",true,false,false));
|
||||
|
||||
textures.put("grass", new Texture("core/assets/grass.png"));
|
||||
textures.put("wood", new Texture("core/assets/wood.png"));
|
||||
|
||||
@@ -1,28 +1,30 @@
|
||||
//https://www.redblobgames.com/maps/terrain-from-noise/
|
||||
package com.mygdx.game.Dimension;
|
||||
|
||||
import com.github.czyzby.kiwi.util.tuple.immutable.Triple;
|
||||
import com.mygdx.game.OpenSimplexNoise;
|
||||
import net.dermetfan.utils.Pair;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import static com.mygdx.game.Restrictions.CHUNK_SIZE;
|
||||
import static com.mygdx.game.Restrictions.*;
|
||||
|
||||
public class Chunks {
|
||||
public HashMap<Pair<Integer, Integer>, Block> blocks = new HashMap<>();
|
||||
public static HashMap<Triple<Integer, Integer, Integer>, Block> blocks = new HashMap<>();
|
||||
private long seed = 1;
|
||||
|
||||
|
||||
public void generateChunk(int x, int y){
|
||||
int startX = x*CHUNK_SIZE;
|
||||
int startY = y*CHUNK_SIZE;
|
||||
int startX = x << CHUNK_SHIFT;
|
||||
int startY = y << CHUNK_SHIFT;
|
||||
int startZ = 0;
|
||||
int endX = startX + CHUNK_SIZE;
|
||||
int endY = startY + CHUNK_SIZE;
|
||||
int endZ = startY + CHUNK_HEIGHT;
|
||||
|
||||
//Going from start of selected chunk to end of selected chunk in x and y
|
||||
for (int i = startX; i != endX; i++){
|
||||
for (int j = startY; j != endY; j++) {
|
||||
blocks.put(new Pair<>(i, j), getTerrain(i,j));
|
||||
blocks.put(new Triple<Integer, Integer, Integer>(i, j, 0), getTerrain(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,12 +55,12 @@ public class Chunks {
|
||||
elevation /= (0.17+0.42+0.16+0.00+0.00+0.03);
|
||||
elevation = Math.pow(elevation, 3.00);
|
||||
moisture = (1.00 * noise2( 1 * nx, 1 * ny)
|
||||
+ 0.75 * noise2( 2 * nx, 2 * ny)
|
||||
+ 0.33 * noise2( 4 * nx, 4 * ny)
|
||||
+ 0.33 * noise2( 8 * nx, 8 * ny)
|
||||
+ 0.33 * noise2(16 * nx, 16 * ny)
|
||||
+ 0.50 * noise2(32 * nx, 32 * ny));
|
||||
moisture /= (1.00+0.75+0.33+0.33+0.33+0.50);
|
||||
+ 0.00 * noise2( 2 * nx, 2 * ny)
|
||||
+ 1.00 * noise2( 4 * nx, 4 * ny)
|
||||
+ 0.00 * noise2( 8 * nx, 8 * ny)
|
||||
+ 0.00 * noise2(16 * nx, 16 * ny)
|
||||
+ 0.00 * noise2(32 * nx, 32 * ny));
|
||||
moisture /= (0.00+1.00+0.00+0.00+0.00+0.00);
|
||||
|
||||
return getBiomeBlock(moisture, elevation);
|
||||
}
|
||||
@@ -68,18 +70,23 @@ public class Chunks {
|
||||
if (biome.equals("Tundra")) return BlockMaterials.materials.get("snow");
|
||||
if (biome.equals("Taiga")) return BlockMaterials.materials.get("snow");
|
||||
if (biome.equals("Snow")) return BlockMaterials.materials.get("snow");
|
||||
|
||||
if (biome.equals("Grassland")) return BlockMaterials.materials.get("grass");
|
||||
if (biome.equals("Shrubland")) return BlockMaterials.materials.get("grass");
|
||||
if (biome.equals("TemperateDeciduousForest")) return BlockMaterials.materials.get("grass");
|
||||
|
||||
if (biome.equals("TemperateRainForest")) return BlockMaterials.materials.get("grass");
|
||||
if (biome.equals("TropicalSeasonalForest")) return BlockMaterials.materials.get("grass");
|
||||
if (biome.equals("TropicalForest")) return BlockMaterials.materials.get("grass");
|
||||
if (biome.equals("TropicalRainForest")) return BlockMaterials.materials.get("grass");
|
||||
|
||||
if (biome.equals("Beach")) return BlockMaterials.materials.get("sand");
|
||||
if (biome.equals("Bare")) return BlockMaterials.materials.get("sand");
|
||||
if (biome.equals("Scorched")) return BlockMaterials.materials.get("sand");
|
||||
if (biome.equals("TemperateDesert")) return BlockMaterials.materials.get("sand");
|
||||
if (biome.equals("SubtropicalDesert")) return BlockMaterials.materials.get("sand");
|
||||
else return BlockMaterials.materials.get("water");
|
||||
|
||||
return BlockMaterials.materials.get("water");
|
||||
}
|
||||
|
||||
|
||||
@@ -113,8 +120,4 @@ public class Chunks {
|
||||
return "TropicalRainForest";
|
||||
}
|
||||
|
||||
public HashMap<Pair<Integer, Integer>, Block> getBlocks() {
|
||||
return blocks;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,29 +42,29 @@ public class Superchunks {
|
||||
}
|
||||
|
||||
|
||||
public static void loadChunks(Player player) {
|
||||
public static void loadChunks() {
|
||||
|
||||
int chunkY = 0;
|
||||
int chunkX = 0;
|
||||
//Top-right
|
||||
if (0 <= player.getX() && 0 <= player.getY()) {
|
||||
chunkY = (player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
chunkX = (player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
if (0 <= Player.getX() && 0 <= Player.getY()) {
|
||||
chunkY = (Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
chunkX = (Player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
}
|
||||
//Top-left
|
||||
else if (0 > player.getX() && 0 < player.getY()) {
|
||||
chunkY = (player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
chunkX = ((player.getX() - TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
else if (0 > Player.getX() && 0 < Player.getY()) {
|
||||
chunkY = (Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
chunkX = ((Player.getX() - TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
}
|
||||
//Bottom-left
|
||||
else if (0 >= player.getX() && 0 >= player.getY()) {
|
||||
chunkY = ((player.getY() - TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
chunkX = ((player.getX() - TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
else if (0 >= Player.getX()) {
|
||||
chunkY = ((Player.getY() - TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
chunkX = ((Player.getX() - TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
}
|
||||
//Bottom-right
|
||||
else if (0 < player.getX() && 0 > player.getY()) {
|
||||
chunkY = ((player.getY() - TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
chunkX = ((player.getX()) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
else {
|
||||
chunkY = ((Player.getY() - TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
chunkX = ((Player.getX()) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,56 +1,43 @@
|
||||
package com.mygdx.game.Dimension;
|
||||
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.github.czyzby.kiwi.util.tuple.immutable.Triple;
|
||||
import com.mygdx.game.Character.InputController;
|
||||
import com.mygdx.game.Character.Mouse;
|
||||
import com.mygdx.game.Character.Player;
|
||||
import com.mygdx.game.Restrictions;
|
||||
import net.dermetfan.utils.Pair;
|
||||
|
||||
import java.util.HashMap;
|
||||
import static com.mygdx.game.Main.cam;
|
||||
|
||||
public class WorldRenderer implements Restrictions {
|
||||
private Mouse mouse;
|
||||
private OrthographicCamera cam;
|
||||
private Player player;
|
||||
private InputController inputController;
|
||||
|
||||
public WorldRenderer(Player player, Mouse mouse, OrthographicCamera cam) {
|
||||
this.player = player;
|
||||
public WorldRenderer(Mouse mouse) {
|
||||
this.mouse = mouse;
|
||||
this.cam = cam;
|
||||
inputController = new InputController(this.cam, this.player);
|
||||
inputController = new InputController();
|
||||
}
|
||||
|
||||
public void drawWorld(Batch batch) {
|
||||
for (Pair<Integer, Integer> worldpair: Superchunks.loadedChunks.keySet()) {
|
||||
Chunks chunks = Superchunks.loadedChunks.get(worldpair);
|
||||
for (Pair<Integer,Integer> chunkpair: chunks.blocks.keySet()) {
|
||||
for (Triple<Integer, Integer, Integer> chunkpair : Chunks.blocks.keySet()) {
|
||||
|
||||
HashMap<Pair<Integer, Integer>, Block> blocks = chunks.getBlocks();
|
||||
Block chunkBlock = blocks.get(chunkpair);
|
||||
String name = chunkBlock.getName();
|
||||
String name = Chunks.blocks.get(chunkpair).getName();
|
||||
int drawingLocationX = chunkpair.getFirst() << TILE_SHIFT;
|
||||
int drawingLocationY = chunkpair.getSecond() << TILE_SHIFT;
|
||||
|
||||
int drawingLocationX = chunkpair.getKey() * TILE_SIZE;
|
||||
int drawingLocationY = chunkpair.getValue()* TILE_SIZE;
|
||||
|
||||
batch.draw(BlockMaterials.textures.get(name),drawingLocationX ,drawingLocationY);
|
||||
}
|
||||
batch.draw(BlockMaterials.getTexture(name), drawingLocationX, drawingLocationY);
|
||||
}
|
||||
}
|
||||
|
||||
public void render(SpriteBatch batch, float elapsedTime) {
|
||||
public void render(SpriteBatch batch) {
|
||||
try {
|
||||
drawWorld(batch);
|
||||
}catch (Exception e){}
|
||||
}catch (Exception ignored){}
|
||||
|
||||
inputController.handleInput();
|
||||
batch.setProjectionMatrix(cam.combined);
|
||||
cam.update();
|
||||
mouse.render(batch, cam);
|
||||
player.render(batch,elapsedTime);
|
||||
mouse.render(batch);
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
|
||||
@@ -22,26 +22,25 @@ public class Main extends ApplicationAdapter {
|
||||
public static float delta = 0;
|
||||
float physicsUpdateSpeed = 1/60f; //how often you want to do a physics-update, in this case every 1/60th of a sec
|
||||
float timeSinceLastUpdate = 0f; //accumulator
|
||||
private Player player;
|
||||
public static OrthographicCamera cam;
|
||||
|
||||
|
||||
@Override
|
||||
public void create () {
|
||||
Gdx.graphics.setWindowedMode(1024,576);
|
||||
OrthographicCamera cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
|
||||
cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
|
||||
fitViewport = new FitViewport(VIEWPORT_WIDTH,VIEWPORT_HEIGHT, cam);
|
||||
Superchunks.create();
|
||||
|
||||
//Starting location of the player
|
||||
player = new Player(0, 0, cam);
|
||||
|
||||
cam.translate((float) TILE_SIZE / 2, (float) TILE_SIZE / 2);
|
||||
cam.translate(TILE_SIZE >> 1, TILE_SIZE >> 1);
|
||||
Player.create(0,0);
|
||||
cam.zoom = 25f;
|
||||
|
||||
Mouse mouse = new Mouse(player);
|
||||
Mouse mouse = new Mouse();
|
||||
gui = new GUI(mouse,fitViewport);
|
||||
|
||||
worldRenderer = new WorldRenderer(player, mouse, cam);
|
||||
worldRenderer = new WorldRenderer(mouse);
|
||||
batch = new SpriteBatch();
|
||||
}
|
||||
|
||||
@@ -50,16 +49,18 @@ public class Main extends ApplicationAdapter {
|
||||
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
timeSinceLastUpdate += Gdx.graphics.getDeltaTime(); //Accumulate delta time
|
||||
batch.begin();
|
||||
if(timeSinceLastUpdate > 1/10f){
|
||||
worldRenderer.render(batch);
|
||||
}
|
||||
Player.render(batch,timeSinceLastUpdate);
|
||||
|
||||
delta +=Gdx.graphics.getDeltaTime();
|
||||
if(delta > RENDER_TIME) {
|
||||
delta -= RENDER_TIME;
|
||||
Superchunks.loadedChunks.clear();
|
||||
Superchunks.loadChunks(player);
|
||||
Superchunks.loadChunks();
|
||||
}
|
||||
|
||||
batch.begin();
|
||||
worldRenderer.render(batch,timeSinceLastUpdate);
|
||||
gui.render(batch);
|
||||
batch.end();
|
||||
}
|
||||
@@ -70,6 +71,6 @@ public class Main extends ApplicationAdapter {
|
||||
worldRenderer.dispose();
|
||||
gui.dispose();
|
||||
batch.dispose();
|
||||
player.getTextureAtlas().dispose();
|
||||
Player.textureAtlas.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,21 @@
|
||||
package com.mygdx.game;
|
||||
|
||||
public interface Restrictions {
|
||||
int TILE_SIZE = 16;
|
||||
float VIEWPORT_HEIGHT = 9/2f;
|
||||
float VIEWPORT_WIDTH = 16/2f;
|
||||
int MOVEMENT_SPEED = 16;
|
||||
int MOVEMENT_SPEED = 2;
|
||||
int KEY_DELAY = 0;
|
||||
int CHUNK_SIZE = 16;
|
||||
int SUPER_CHUNK_SIZE = 3;
|
||||
|
||||
int TILE_SIZE = 16;
|
||||
int CHUNK_SIZE = 8;
|
||||
int CHUNK_HEIGHT = 2;
|
||||
int SUPER_CHUNK_SIZE = 1;
|
||||
|
||||
int TILE_SHIFT = 4;
|
||||
int CHUNK_SHIFT = 3;
|
||||
int CHUNK_HEIGHT_SHIFT = 2;
|
||||
int SUPER_CHUNK_SHIFT = 0;
|
||||
|
||||
int RENDER_DISTANCE = 2;
|
||||
float RENDER_TIME = 4f;
|
||||
float RENDER_TIME = 1f;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user