Changed a bunch of variables to use bitwise shift instead of multiplication and division

This commit is contained in:
2020-04-08 06:43:02 -06:00
parent 4e38fd9da5
commit 5ffa837f50
21 changed files with 241 additions and 166 deletions

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

View File

@@ -0,0 +1,2 @@
cd /d %~dp0
start javaw -Xms64m -Xmx1024m -XX:+UseG1GC -XX:MinHeapFreeRatio=15 -XX:MaxHeapFreeRatio=30 -jar gdx-texturepacker.jar %*

View File

@@ -0,0 +1,3 @@
SCRIPTDIR=$(dirname "$0")
cd $SCRIPTDIR
java -Xms64m -Xmx1024m -XX:+UseG1GC -XX:MinHeapFreeRatio=15 -XX:MaxHeapFreeRatio=30 -jar ./gdx-texturepacker.jar

View File

@@ -0,0 +1,7 @@
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c launcher.bat "
For Each arg In Wscript.Arguments
strArgs = strArgs + arg
Next
oShell.Run strArgs, 0, false

View File

@@ -0,0 +1,27 @@
Gdx Texture Packer GUI
Simple utility to help you pack and manage texture atlases for LibGDX game framework. It's mostly just visual wrapper over LibGDX TexturePacker classes and offers more convenient way of using it.
For further details visit GitHub project page:
https://github.com/crashinvaders/gdx-texture-packer-gui
Requirements:
- Java 7 JRE (or higher) (can be donwnloaded here http://www.oracle.com/technetwork/java/javase/downloads/index.html)
- OpenGL support
How to launch:
Application is packed into JAR executable file. To launch it, issue next command
java -jar gdx-texturepacker.jar
MacOS and UNIX
You may execute launch.sh bash script to launch application
Windows
launch.bat - starts application
launcher_no_cmd.vbs - does exactly what launch.bat does, but CMD window won't appear in that case
If you got any questions or would like to share some ideas, reach me out at anton@crashinvaders.com
Hope you will enjoy it!
Anton Chekulaev

View File

@@ -0,0 +1,3 @@
You can define/override you own keyboard shortcuts for some actions.
For the details look at the default hotkey configuration file.
https://github.com/crashinvaders/gdx-texture-packer-gui/blob/master/assets/hotkeys_default.txt

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

BIN
core/assets/jungleGrass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@@ -0,0 +1,62 @@
world.png
size: 128,16
format: RGBA8888
filter: Nearest,Nearest
repeat: none
crosshair
rotate: false
xy: 48, 0
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
grass
rotate: false
xy: 96, 0
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
jungleGrass
rotate: false
xy: 112, 0
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
sand
rotate: false
xy: 16, 0
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
snow
rotate: false
xy: 0, 0
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
stone
rotate: false
xy: 64, 0
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
water
rotate: false
xy: 32, 0
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
wood
rotate: false
xy: 80, 0
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@@ -2,20 +2,15 @@ package com.mygdx.game.Character;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input; import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.Animation;
import com.mygdx.game.Restrictions; import com.mygdx.game.Restrictions;
import static com.mygdx.game.Main.cam;
public class InputController implements Restrictions { public class InputController implements Restrictions {
private OrthographicCamera cam;
private Player player;
int i = 0; int i = 0;
public InputController(OrthographicCamera cam, Player player) {
this.cam = cam;
this.player = player;
}
public void handleInput() { public void handleInput() {
i++; 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) { if (Gdx.input.isKeyPressed(Input.Keys.UP) && Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) {
i = 0; i = 0;
directionAnimation("UpLeft"); directionAnimation("UpLeft");
player.addX(-MOVEMENT_SPEED); Player.addX(-MOVEMENT_SPEED);
player.addY(MOVEMENT_SPEED); Player.addY(MOVEMENT_SPEED);
cam.translate(-MOVEMENT_SPEED,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) { else if (Gdx.input.isKeyPressed(Input.Keys.UP)&& Gdx.input.isKeyPressed(Input.Keys.RIGHT) &&i > KEY_DELAY) {
i = 0; i = 0;
directionAnimation("UpRight"); directionAnimation("UpRight");
player.addX(MOVEMENT_SPEED); Player.addX(MOVEMENT_SPEED);
player.addY(MOVEMENT_SPEED); Player.addY(MOVEMENT_SPEED);
cam.translate(MOVEMENT_SPEED,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) { else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)&& Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) {
i = 0; i = 0;
directionAnimation("DownLeft"); directionAnimation("DownLeft");
player.addX(-MOVEMENT_SPEED); Player.addX(-MOVEMENT_SPEED);
player.addY(-MOVEMENT_SPEED); Player.addY(-MOVEMENT_SPEED);
cam.translate(-MOVEMENT_SPEED,-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) { else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)&& Gdx.input.isKeyPressed(Input.Keys.RIGHT) && i > KEY_DELAY) {
i = 0; i = 0;
directionAnimation("DownRight"); directionAnimation("DownRight");
player.addX(MOVEMENT_SPEED); Player.addX(MOVEMENT_SPEED);
player.addY(-MOVEMENT_SPEED); Player.addY(-MOVEMENT_SPEED);
cam.translate(MOVEMENT_SPEED,-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) { else if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) {
i = 0; i = 0;
directionAnimation("Left"); directionAnimation("Left");
player.addX(-MOVEMENT_SPEED); Player.addX(-MOVEMENT_SPEED);
cam.translate(-MOVEMENT_SPEED,0); cam.translate(-MOVEMENT_SPEED,0);
} }
else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)&& i > KEY_DELAY) { else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)&& i > KEY_DELAY) {
i = 0; i = 0;
directionAnimation("Right"); directionAnimation("Right");
player.addX(MOVEMENT_SPEED); Player.addX(MOVEMENT_SPEED);
cam.translate(MOVEMENT_SPEED,0); cam.translate(MOVEMENT_SPEED,0);
} }
else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)&& i > KEY_DELAY) { else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)&& i > KEY_DELAY) {
i = 0; i = 0;
directionAnimation("Down"); directionAnimation("Down");
player.addY(-MOVEMENT_SPEED); Player.addY(-MOVEMENT_SPEED);
cam.translate(0,-MOVEMENT_SPEED); cam.translate(0,-MOVEMENT_SPEED);
} }
if (Gdx.input.isKeyPressed(Input.Keys.UP)&& i > KEY_DELAY) { if (Gdx.input.isKeyPressed(Input.Keys.UP)&& i > KEY_DELAY) {
i = 0; i = 0;
directionAnimation("Up"); directionAnimation("Up");
player.addY(MOVEMENT_SPEED); Player.addY(MOVEMENT_SPEED);
cam.translate(0,MOVEMENT_SPEED); cam.translate(0,MOVEMENT_SPEED);
} }
cam.update(); cam.update();
} }
public void directionAnimation(String direction) { 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)));
} }
} }

View File

@@ -7,45 +7,43 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
import com.mygdx.game.Restrictions; import com.mygdx.game.Restrictions;
import static java.lang.Math.round; import static com.mygdx.game.Main.cam;
public class Mouse implements Restrictions { public class Mouse implements Restrictions {
Player player;
Texture crosshair = new Texture("core/assets/crosshair.png"); 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); Vector3 mousePos = new Vector3( Gdx.input.getX(), Gdx.input.getY(),0);
cam.unproject(mousePos); 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) { public int getSelectedX(Vector3 mousePos) {
return round((mousePos.x-8)/16)*16; return ((int)(mousePos.x)>>4)<<4;
} }
public int getSelectedY(Vector3 mousePos) { public int getSelectedY(Vector3 mousePos) {
return round((mousePos.y-8)/16)*16; return ((int)(mousePos.y)>>4)<<4;
} }
public int getTileX(OrthographicCamera cam){ public int getTileX(OrthographicCamera cam){
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);
return (getSelectedX(mousePos) + player.getX()/2)/TILE_SIZE; return (getSelectedX(mousePos) + Player.getX()>>1)/TILE_SIZE;
} }
public int getTileY(OrthographicCamera cam){ public int getTileY(OrthographicCamera cam){
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);
return (getSelectedY(mousePos) + player.getY()/2)/TILE_SIZE; return (getSelectedY(mousePos) + Player.getY()>>1)/TILE_SIZE;
} }
public void render(SpriteBatch batch, OrthographicCamera cam) { public void render(SpriteBatch batch) {
selectedTile(batch,cam); selectedTile(batch);
//System.out.println("X = " + getTileX(cam) + " Y = "+ getTileY(cam)); //System.out.println("X = " + getTileX(cam) + " Y = "+ getTileY(cam));
} }
} }

View File

@@ -1,72 +1,66 @@
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;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
import com.mygdx.game.Main;
import com.mygdx.game.Restrictions; import com.mygdx.game.Restrictions;
public class Player implements Restrictions { public class Player implements Restrictions {
private int x,y; private static int x,y;
private String spriteName; private static String spriteName;
private TextureAtlas textureAtlas; public static TextureAtlas textureAtlas;
private Animation<TextureAtlas.AtlasRegion> animation; private static Animation<TextureAtlas.AtlasRegion> animation;
private OrthographicCamera cam;
public Player(int x, int y, OrthographicCamera cam) { public static void create(int x, int y) {
this.cam = cam; Player.x = x<<TILE_SHIFT;
this.x = x*TILE_SIZE; Player.y = y<<TILE_SHIFT;
this.y = y*TILE_SIZE; Main.cam.translate(Player.x, Player.y);
this.cam.translate(this.x,this.y); spriteName = "man";
textureAtlas = new TextureAtlas("core/assets/" + spriteName + ".atlas");
this.spriteName = "man";
this.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;
this.animation = new Animation<>(frameDuration, keyFrames); animation = new Animation<>(frameDuration, keyFrames);
} }
public static void teleport(Player player, int x, int y){ public static void teleport(int x, int y){
player.x = x*TILE_SIZE; Player.x = x<<TILE_SHIFT;
player.y = y*TILE_SIZE; Player.y = y<<TILE_SHIFT;
player.cam.translate(x,y); 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); batch.draw(getAnimation().getKeyFrame(timeSinceLastUpdate, true), getX(), getY(),TILE_SIZE,TILE_SIZE);
} }
public int getX() { public static int getX() {
return x; return x;
} }
public int getY() { public static int getY() {
return y; return y;
} }
public void addX(int x){ public static void addX(int x){
this.x += x; Player.x += x;
} }
public void addY(int y){ public static void addY(int y){
this.y += y; Player.y += y;
} }
String getSpriteName() { static String getSpriteName() {
return spriteName; return spriteName;
} }
public TextureAtlas getTextureAtlas() { public static Animation<TextureAtlas.AtlasRegion> getAnimation() {
return textureAtlas;
}
public Animation<TextureAtlas.AtlasRegion> getAnimation() {
return animation; return animation;
} }
void setAnimation(Animation<TextureAtlas.AtlasRegion> animation) { static void setAnimation(Animation<TextureAtlas.AtlasRegion> animationTemp) {
this.animation = animation; animation = animationTemp;
} }
} }

View File

@@ -2,12 +2,12 @@ package com.mygdx.game.Dimension;
public class Block { public class Block {
private String name; 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.name = name;
this.floor = floor;
this.wall = wall;
this.harvestable = harvestable; this.harvestable = harvestable;
this.breakable = breakable; this.breakable = breakable;
this.passable = passable; this.passable = passable;
@@ -21,22 +21,6 @@ public class Block {
this.name = name; 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() { public Boolean getHarvestable() {
return harvestable; return harvestable;
} }

View File

@@ -11,17 +11,17 @@ public class BlockMaterials {
//Private so the singleton can't be instantiated //Private so the singleton can't be instantiated
private BlockMaterials() {} private BlockMaterials() {}
public Texture getTexture(String material) { public static Texture getTexture(String material) {
return textures.get(material); return textures.get(material);
} }
public static void create(){ public static void create(){
materials.put("grass",new Block("grass",true,false,true,true,true)); materials.put("grass",new Block("grass",true,false,true));
materials.put("wood",new Block("wood",false,true,true,true,false)); materials.put("wood",new Block("wood",false,true,true));
materials.put("water",new Block("water",true,false,false,false,false)); materials.put("water",new Block("water",true,false,false));
materials.put("stone",new Block("stone",true,false,false,false,false)); materials.put("stone",new Block("stone",true,false,false));
materials.put("snow",new Block("snow",true,false,false,false,false)); materials.put("snow",new Block("snow",true,false,false));
materials.put("sand",new Block("sand",true,false,false,false,false)); materials.put("sand",new Block("sand",true,false,false));
textures.put("grass", new Texture("core/assets/grass.png")); textures.put("grass", new Texture("core/assets/grass.png"));
textures.put("wood", new Texture("core/assets/wood.png")); textures.put("wood", new Texture("core/assets/wood.png"));

View File

@@ -1,28 +1,30 @@
//https://www.redblobgames.com/maps/terrain-from-noise/ //https://www.redblobgames.com/maps/terrain-from-noise/
package com.mygdx.game.Dimension; package com.mygdx.game.Dimension;
import com.github.czyzby.kiwi.util.tuple.immutable.Triple;
import com.mygdx.game.OpenSimplexNoise; import com.mygdx.game.OpenSimplexNoise;
import net.dermetfan.utils.Pair;
import java.util.HashMap; import java.util.HashMap;
import static com.mygdx.game.Restrictions.CHUNK_SIZE; import static com.mygdx.game.Restrictions.*;
public class Chunks { 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; private long seed = 1;
public void generateChunk(int x, int y){ public void generateChunk(int x, int y){
int startX = x*CHUNK_SIZE; int startX = x << CHUNK_SHIFT;
int startY = y*CHUNK_SIZE; int startY = y << CHUNK_SHIFT;
int startZ = 0;
int endX = startX + CHUNK_SIZE; int endX = startX + CHUNK_SIZE;
int endY = startY + 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 //Going from start of selected chunk to end of selected chunk in x and y
for (int i = startX; i != endX; i++){ for (int i = startX; i != endX; i++){
for (int j = startY; j != endY; j++) { 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 /= (0.17+0.42+0.16+0.00+0.00+0.03);
elevation = Math.pow(elevation, 3.00); elevation = Math.pow(elevation, 3.00);
moisture = (1.00 * noise2( 1 * nx, 1 * ny) moisture = (1.00 * noise2( 1 * nx, 1 * ny)
+ 0.75 * noise2( 2 * nx, 2 * ny) + 0.00 * noise2( 2 * nx, 2 * ny)
+ 0.33 * noise2( 4 * nx, 4 * ny) + 1.00 * noise2( 4 * nx, 4 * ny)
+ 0.33 * noise2( 8 * nx, 8 * ny) + 0.00 * noise2( 8 * nx, 8 * ny)
+ 0.33 * noise2(16 * nx, 16 * ny) + 0.00 * noise2(16 * nx, 16 * ny)
+ 0.50 * noise2(32 * nx, 32 * ny)); + 0.00 * noise2(32 * nx, 32 * ny));
moisture /= (1.00+0.75+0.33+0.33+0.33+0.50); moisture /= (0.00+1.00+0.00+0.00+0.00+0.00);
return getBiomeBlock(moisture, elevation); return getBiomeBlock(moisture, elevation);
} }
@@ -68,18 +70,23 @@ public class Chunks {
if (biome.equals("Tundra")) return BlockMaterials.materials.get("snow"); if (biome.equals("Tundra")) return BlockMaterials.materials.get("snow");
if (biome.equals("Taiga")) 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("Snow")) return BlockMaterials.materials.get("snow");
if (biome.equals("Grassland")) return BlockMaterials.materials.get("grass"); if (biome.equals("Grassland")) return BlockMaterials.materials.get("grass");
if (biome.equals("Shrubland")) 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("TemperateDeciduousForest")) return BlockMaterials.materials.get("grass");
if (biome.equals("TemperateRainForest")) 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("TropicalSeasonalForest")) return BlockMaterials.materials.get("grass");
if (biome.equals("TropicalForest")) 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("Beach")) return BlockMaterials.materials.get("sand");
if (biome.equals("Bare")) 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("Scorched")) return BlockMaterials.materials.get("sand");
if (biome.equals("TemperateDesert")) return BlockMaterials.materials.get("sand"); if (biome.equals("TemperateDesert")) return BlockMaterials.materials.get("sand");
if (biome.equals("SubtropicalDesert")) 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"; return "TropicalRainForest";
} }
public HashMap<Pair<Integer, Integer>, Block> getBlocks() {
return blocks;
}
} }

View File

@@ -42,29 +42,29 @@ public class Superchunks {
} }
public static void loadChunks(Player player) { public static void loadChunks() {
int chunkY = 0; int chunkY = 0;
int chunkX = 0; int chunkX = 0;
//Top-right //Top-right
if (0 <= player.getX() && 0 <= player.getY()) { if (0 <= Player.getX() && 0 <= Player.getY()) {
chunkY = (player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)); chunkY = (Player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
chunkX = (player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)); chunkX = (Player.getX() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
} }
//Top-left //Top-left
else if (0 > player.getX() && 0 < player.getY()) { else if (0 > Player.getX() && 0 < Player.getY()) {
chunkY = (player.getY() / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)); 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)); chunkX = ((Player.getX() - TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
} }
//Bottom-left //Bottom-left
else if (0 >= player.getX() && 0 >= player.getY()) { else if (0 >= Player.getX()) {
chunkY = ((player.getY() - TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)); 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)); chunkX = ((Player.getX() - TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
} }
//Bottom-right //Bottom-right
else if (0 < player.getX() && 0 > player.getY()) { else {
chunkY = ((player.getY() - TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE)); 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)); chunkX = ((Player.getX()) / (TILE_SIZE * SUPER_CHUNK_SIZE * CHUNK_SIZE));
} }

View File

@@ -1,56 +1,43 @@
package com.mygdx.game.Dimension; package com.mygdx.game.Dimension;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 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.InputController;
import com.mygdx.game.Character.Mouse; import com.mygdx.game.Character.Mouse;
import com.mygdx.game.Character.Player;
import com.mygdx.game.Restrictions; 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 { public class WorldRenderer implements Restrictions {
private Mouse mouse; private Mouse mouse;
private OrthographicCamera cam;
private Player player;
private InputController inputController; private InputController inputController;
public WorldRenderer(Player player, Mouse mouse, OrthographicCamera cam) { public WorldRenderer(Mouse mouse) {
this.player = player;
this.mouse = mouse; this.mouse = mouse;
this.cam = cam; inputController = new InputController();
inputController = new InputController(this.cam, this.player);
} }
public void drawWorld(Batch batch) { public void drawWorld(Batch batch) {
for (Pair<Integer, Integer> worldpair: Superchunks.loadedChunks.keySet()) { for (Triple<Integer, Integer, Integer> chunkpair : Chunks.blocks.keySet()) {
Chunks chunks = Superchunks.loadedChunks.get(worldpair);
for (Pair<Integer,Integer> chunkpair: chunks.blocks.keySet()) {
HashMap<Pair<Integer, Integer>, Block> blocks = chunks.getBlocks(); String name = Chunks.blocks.get(chunkpair).getName();
Block chunkBlock = blocks.get(chunkpair); int drawingLocationX = chunkpair.getFirst() << TILE_SHIFT;
String name = chunkBlock.getName(); int drawingLocationY = chunkpair.getSecond() << TILE_SHIFT;
int drawingLocationX = chunkpair.getKey() * TILE_SIZE; batch.draw(BlockMaterials.getTexture(name), drawingLocationX, drawingLocationY);
int drawingLocationY = chunkpair.getValue()* TILE_SIZE;
batch.draw(BlockMaterials.textures.get(name),drawingLocationX ,drawingLocationY);
}
} }
} }
public void render(SpriteBatch batch, float elapsedTime) { public void render(SpriteBatch batch) {
try { try {
drawWorld(batch); drawWorld(batch);
}catch (Exception e){} }catch (Exception ignored){}
inputController.handleInput(); inputController.handleInput();
batch.setProjectionMatrix(cam.combined); batch.setProjectionMatrix(cam.combined);
cam.update(); cam.update();
mouse.render(batch, cam); mouse.render(batch);
player.render(batch,elapsedTime);
} }
public void dispose() { public void dispose() {

View File

@@ -22,26 +22,25 @@ public class Main extends ApplicationAdapter {
public static float delta = 0; 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 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 float timeSinceLastUpdate = 0f; //accumulator
private Player player; public static OrthographicCamera cam;
@Override @Override
public void create () { public void create () {
Gdx.graphics.setWindowedMode(1024,576); 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); fitViewport = new FitViewport(VIEWPORT_WIDTH,VIEWPORT_HEIGHT, cam);
Superchunks.create(); Superchunks.create();
//Starting location of the player //Starting location of the player
player = new Player(0, 0, cam); cam.translate(TILE_SIZE >> 1, TILE_SIZE >> 1);
Player.create(0,0);
cam.translate((float) TILE_SIZE / 2, (float) TILE_SIZE / 2);
cam.zoom = 25f; cam.zoom = 25f;
Mouse mouse = new Mouse(player); Mouse mouse = new Mouse();
gui = new GUI(mouse,fitViewport); gui = new GUI(mouse,fitViewport);
worldRenderer = new WorldRenderer(player, mouse, cam); worldRenderer = new WorldRenderer(mouse);
batch = new SpriteBatch(); batch = new SpriteBatch();
} }
@@ -50,16 +49,18 @@ public class Main extends ApplicationAdapter {
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
timeSinceLastUpdate += Gdx.graphics.getDeltaTime(); //Accumulate delta time timeSinceLastUpdate += Gdx.graphics.getDeltaTime(); //Accumulate delta time
batch.begin();
if(timeSinceLastUpdate > 1/10f){
worldRenderer.render(batch);
}
Player.render(batch,timeSinceLastUpdate);
delta +=Gdx.graphics.getDeltaTime(); delta +=Gdx.graphics.getDeltaTime();
if(delta > RENDER_TIME) { if(delta > RENDER_TIME) {
delta -= RENDER_TIME; delta -= RENDER_TIME;
Superchunks.loadedChunks.clear(); Superchunks.loadChunks();
Superchunks.loadChunks(player);
} }
batch.begin();
worldRenderer.render(batch,timeSinceLastUpdate);
gui.render(batch); gui.render(batch);
batch.end(); batch.end();
} }
@@ -70,6 +71,6 @@ public class Main extends ApplicationAdapter {
worldRenderer.dispose(); worldRenderer.dispose();
gui.dispose(); gui.dispose();
batch.dispose(); batch.dispose();
player.getTextureAtlas().dispose(); Player.textureAtlas.dispose();
} }
} }

View File

@@ -1,13 +1,21 @@
package com.mygdx.game; package com.mygdx.game;
public interface Restrictions { public interface Restrictions {
int TILE_SIZE = 16;
float VIEWPORT_HEIGHT = 9/2f; float VIEWPORT_HEIGHT = 9/2f;
float VIEWPORT_WIDTH = 16/2f; float VIEWPORT_WIDTH = 16/2f;
int MOVEMENT_SPEED = 16; int MOVEMENT_SPEED = 2;
int KEY_DELAY = 0; 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; int RENDER_DISTANCE = 2;
float RENDER_TIME = 4f; float RENDER_TIME = 1f;
} }