Added terrain generation and also diagonal movement
This commit is contained in:
BIN
core/assets/water.png
Normal file
BIN
core/assets/water.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
@@ -27,19 +27,49 @@ public class InputController implements Restrictions {
|
|||||||
cam.zoom -= 5;
|
cam.zoom -= 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (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;
|
||||||
|
directionAnimation("UpLeft");
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
|
||||||
|
cam.translate(MOVEMENT_SPEED,-MOVEMENT_SPEED);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
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);
|
||||||
}
|
}
|
||||||
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);
|
||||||
|
|||||||
@@ -34,9 +34,8 @@ public class Player implements Restrictions {
|
|||||||
player.cam.translate(x,y);
|
player.cam.translate(x,y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render(Batch batch){
|
public void render(Batch batch, float timeSinceLastUpdate){
|
||||||
float elapsedTime = 1f;
|
batch.draw(getAnimation().getKeyFrame(timeSinceLastUpdate, true), getX(), getY(),TILE_SIZE,TILE_SIZE);
|
||||||
batch.draw(getAnimation().getKeyFrame(elapsedTime, true), getX(), getY(), TILE_SIZE, TILE_SIZE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getX() {
|
public int getX() {
|
||||||
@@ -59,11 +58,11 @@ public class Player implements Restrictions {
|
|||||||
return spriteName;
|
return spriteName;
|
||||||
}
|
}
|
||||||
|
|
||||||
TextureAtlas getTextureAtlas() {
|
public TextureAtlas getTextureAtlas() {
|
||||||
return textureAtlas;
|
return textureAtlas;
|
||||||
}
|
}
|
||||||
|
|
||||||
Animation<TextureAtlas.AtlasRegion> getAnimation() {
|
public Animation<TextureAtlas.AtlasRegion> getAnimation() {
|
||||||
return animation;
|
return animation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,10 @@ public class BlockMaterials {
|
|||||||
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,true,true));
|
||||||
materials.put("wood",new Block("wood",false,true,true,true,false));
|
materials.put("wood",new Block("wood",false,true,true,true,false));
|
||||||
|
materials.put("water",new Block("water",true,false,false,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"));
|
||||||
|
textures.put("water", new Texture("core/assets/water.png"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.mygdx.game.Dimension;
|
package com.mygdx.game.Dimension;
|
||||||
|
|
||||||
|
import com.mygdx.game.OpenSimplexNoise;
|
||||||
import net.dermetfan.utils.Pair;
|
import net.dermetfan.utils.Pair;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -8,6 +9,7 @@ import static com.mygdx.game.Restrictions.CHUNK_SIZE;
|
|||||||
|
|
||||||
public class Chunks {
|
public class Chunks {
|
||||||
public HashMap<Pair<Integer, Integer>, Block> blocks = new HashMap<>();
|
public HashMap<Pair<Integer, Integer>, Block> blocks = new HashMap<>();
|
||||||
|
private long seed = 10000;
|
||||||
|
|
||||||
public void generateChunk(int x, int y){
|
public void generateChunk(int x, int y){
|
||||||
int startX = x*CHUNK_SIZE;
|
int startX = x*CHUNK_SIZE;
|
||||||
@@ -18,11 +20,24 @@ public class Chunks {
|
|||||||
//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), BlockMaterials.materials.get("grass"));
|
blocks.put(new Pair<>(i, j), getTerrain(i,j));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Block getTerrain(int i, int j) {
|
||||||
|
|
||||||
|
OpenSimplexNoise openSimplexNoise = new OpenSimplexNoise(seed);
|
||||||
|
double terrainType = openSimplexNoise.eval(i*0.01,j*0.01);
|
||||||
|
if(terrainType < 0.3) {
|
||||||
|
return BlockMaterials.materials.get("grass");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return BlockMaterials.materials.get("water");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public HashMap<Pair<Integer, Integer>, Block> getBlocks() {
|
public HashMap<Pair<Integer, Integer>, Block> getBlocks() {
|
||||||
return blocks;
|
return blocks;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public class WorldRenderer implements Restrictions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render(SpriteBatch batch) {
|
public void render(SpriteBatch batch, float elapsedTime) {
|
||||||
try {
|
try {
|
||||||
drawWorld(batch);
|
drawWorld(batch);
|
||||||
}catch (Exception e){}
|
}catch (Exception e){}
|
||||||
@@ -50,7 +50,7 @@ public class WorldRenderer implements Restrictions {
|
|||||||
batch.setProjectionMatrix(cam.combined);
|
batch.setProjectionMatrix(cam.combined);
|
||||||
cam.update();
|
cam.update();
|
||||||
mouse.render(batch, cam);
|
mouse.render(batch, cam);
|
||||||
player.render(batch);
|
player.render(batch,elapsedTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dispose() {
|
public void dispose() {
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ public class Main extends ApplicationAdapter {
|
|||||||
public void render () {
|
public void render () {
|
||||||
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
|
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
fixedStep();
|
timeSinceLastUpdate += Gdx.graphics.getDeltaTime(); //Accumulate delta time
|
||||||
|
|
||||||
delta +=Gdx.graphics.getDeltaTime();
|
delta +=Gdx.graphics.getDeltaTime();
|
||||||
if(delta > RENDER_TIME) {
|
if(delta > RENDER_TIME) {
|
||||||
@@ -58,27 +58,18 @@ public class Main extends ApplicationAdapter {
|
|||||||
Superchunks.loadChunks(player);
|
Superchunks.loadChunks(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
batch.begin();
|
batch.begin();
|
||||||
worldRenderer.render(batch);
|
worldRenderer.render(batch,timeSinceLastUpdate);
|
||||||
gui.render(batch);
|
gui.render(batch);
|
||||||
batch.end();
|
batch.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void fixedStep() {
|
|
||||||
timeSinceLastUpdate = Gdx.graphics.getDeltaTime(); //Accumulate delta time
|
|
||||||
while(timeSinceLastUpdate >= physicsUpdateSpeed){ //If the accumulated delta-time is greater than, do a physics update - this can happen multiple times in a single frame, depending on fps/lag/physicsUpdateSpeed
|
|
||||||
timeSinceLastUpdate -= physicsUpdateSpeed; //Subtract physicsUpdateSpeed from timeSinceLastUpdate - if timeSinceLastUpdate is STILL >= physicsUpdateSpeed, repeat, if not, the loop breaks
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dispose () {
|
public void dispose () {
|
||||||
worldRenderer.dispose();
|
worldRenderer.dispose();
|
||||||
gui.dispose();
|
gui.dispose();
|
||||||
batch.dispose();
|
batch.dispose();
|
||||||
|
player.getTextureAtlas().dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2137
core/src/com/mygdx/game/OpenSimplexNoise.java
Normal file
2137
core/src/com/mygdx/game/OpenSimplexNoise.java
Normal file
File diff suppressed because it is too large
Load Diff
@@ -5,7 +5,7 @@ public interface Restrictions {
|
|||||||
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 = 16;
|
||||||
int KEY_DELAY = 1;
|
int KEY_DELAY = 4;
|
||||||
int CHUNK_SIZE = 16;
|
int CHUNK_SIZE = 16;
|
||||||
int SUPER_CHUNK_SIZE = 3;
|
int SUPER_CHUNK_SIZE = 3;
|
||||||
int RENDER_DISTANCE = 2;
|
int RENDER_DISTANCE = 2;
|
||||||
|
|||||||
Reference in New Issue
Block a user