Added terrain generation and also diagonal movement

This commit is contained in:
2020-04-05 04:32:41 -06:00
parent 060b5f51e8
commit 3101d508e0
9 changed files with 2198 additions and 24 deletions

BIN
core/assets/water.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@@ -27,19 +27,49 @@ public class InputController implements Restrictions {
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;
directionAnimation("Left");
player.addX(-MOVEMENT_SPEED);
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;
directionAnimation("Right");
player.addX(MOVEMENT_SPEED);
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;
directionAnimation("Down");
player.addY(-MOVEMENT_SPEED);

View File

@@ -34,9 +34,8 @@ public class Player implements Restrictions {
player.cam.translate(x,y);
}
public void render(Batch batch){
float elapsedTime = 1f;
batch.draw(getAnimation().getKeyFrame(elapsedTime, true), getX(), getY(), TILE_SIZE, TILE_SIZE);
public void render(Batch batch, float timeSinceLastUpdate){
batch.draw(getAnimation().getKeyFrame(timeSinceLastUpdate, true), getX(), getY(),TILE_SIZE,TILE_SIZE);
}
public int getX() {
@@ -59,11 +58,11 @@ public class Player implements Restrictions {
return spriteName;
}
TextureAtlas getTextureAtlas() {
public TextureAtlas getTextureAtlas() {
return textureAtlas;
}
Animation<TextureAtlas.AtlasRegion> getAnimation() {
public Animation<TextureAtlas.AtlasRegion> getAnimation() {
return animation;
}

View File

@@ -18,8 +18,10 @@ public class BlockMaterials {
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));
textures.put("grass", new Texture("core/assets/grass.png"));
textures.put("wood", new Texture("core/assets/wood.png"));
textures.put("water", new Texture("core/assets/water.png"));
}
}

View File

@@ -1,5 +1,6 @@
package com.mygdx.game.Dimension;
import com.mygdx.game.OpenSimplexNoise;
import net.dermetfan.utils.Pair;
import java.util.HashMap;
@@ -8,6 +9,7 @@ import static com.mygdx.game.Restrictions.CHUNK_SIZE;
public class Chunks {
public HashMap<Pair<Integer, Integer>, Block> blocks = new HashMap<>();
private long seed = 10000;
public void generateChunk(int x, int y){
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
for (int i = startX; i != endX; i++){
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() {
return blocks;
}

View File

@@ -41,7 +41,7 @@ public class WorldRenderer implements Restrictions {
}
}
public void render(SpriteBatch batch) {
public void render(SpriteBatch batch, float elapsedTime) {
try {
drawWorld(batch);
}catch (Exception e){}
@@ -50,7 +50,7 @@ public class WorldRenderer implements Restrictions {
batch.setProjectionMatrix(cam.combined);
cam.update();
mouse.render(batch, cam);
player.render(batch);
player.render(batch,elapsedTime);
}
public void dispose() {

View File

@@ -49,7 +49,7 @@ public class Main extends ApplicationAdapter {
public void render () {
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
fixedStep();
timeSinceLastUpdate += Gdx.graphics.getDeltaTime(); //Accumulate delta time
delta +=Gdx.graphics.getDeltaTime();
if(delta > RENDER_TIME) {
@@ -58,27 +58,18 @@ public class Main extends ApplicationAdapter {
Superchunks.loadChunks(player);
}
batch.begin();
worldRenderer.render(batch);
worldRenderer.render(batch,timeSinceLastUpdate);
gui.render(batch);
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
public void dispose () {
worldRenderer.dispose();
gui.dispose();
batch.dispose();
player.getTextureAtlas().dispose();
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -5,7 +5,7 @@ public interface Restrictions {
float VIEWPORT_HEIGHT = 9/2f;
float VIEWPORT_WIDTH = 16/2f;
int MOVEMENT_SPEED = 16;
int KEY_DELAY = 1;
int KEY_DELAY = 4;
int CHUNK_SIZE = 16;
int SUPER_CHUNK_SIZE = 3;
int RENDER_DISTANCE = 2;