From 524d5abc0b76b25a4cf2b31e6ba4f41a107a5ab6 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Sat, 21 Apr 2018 23:48:22 +0700 Subject: [PATCH] Ciclic worlds --- .../cavecraft/game/GamePhysics.java | 29 ++++----- .../deadsoftware/cavecraft/game/GameProc.java | 12 ++-- .../cavecraft/game/GameRenderer.java | 28 +++------ .../cavecraft/game/GameWorld.java | 61 +++++++++---------- .../deadsoftware/cavecraft/game/WorldGen.java | 8 ++- 5 files changed, 62 insertions(+), 76 deletions(-) diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java index 3bcbc9b..2fdc29b 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java @@ -1,6 +1,9 @@ package ru.deadsoftware.cavecraft.game; -import com.badlogic.gdx.math.*; +import com.badlogic.gdx.math.Intersector; +import com.badlogic.gdx.math.MathUtils; +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavecraft.CaveGame; import ru.deadsoftware.cavecraft.Items; import ru.deadsoftware.cavecraft.game.mobs.Mob; @@ -23,18 +26,18 @@ public class GamePhysics { int bl = 0; switch (dir) { case 0: - if ((int)((rect.x+(rect.width/2))/16) - 1>=0) - bl = gameProc.world.getForeMap( - (int)((rect.x+(rect.width/2))/16) - 1, - (int)(rect.y/16)+1); + bl = gameProc.world.getForeMap( + (int)((rect.x+(rect.width/2))/16) - 1, + (int)(rect.y/16)+1); if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)-1,(int)(rect.y/16))>0) bl=0; + if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)-1,(int)(rect.y/16)-1)>0) bl=0; break; case 1: - if ((int)((rect.x+(rect.width/2))/16) + 10) bl=0; + if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)+1,(int)(rect.y/16)-1)>0) bl=0; break; default: bl=0; @@ -48,9 +51,7 @@ public class GamePhysics { int minY = (int) ((rect.y+rect.height/2)/16)-4; int maxX = (int) ((rect.x+rect.width/2)/16)+4; int maxY = (int) ((rect.y+rect.height/2)/16)+4; - if (minX<0) minX=0; if (minY<0) minY=0; - if (maxX>gameProc.world.getWidth()) maxX = gameProc.world.getWidth(); if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight(); for (int y=minY; y=gameProc.world.getWidth()*16) - pl.position.sub(pl.moveX); if (checkColl(pl.getRect())) { if (pl.canJump && !pl.flyMode) pl.position.y-=8; if (checkColl(pl.getRect())) { @@ -119,9 +117,6 @@ public class GamePhysics { } mob.moveY.add(gravity); mob.position.add(mob.moveX); - if (mob.position.x<32 || - mob.position.x+mob.width>gameProc.world.getWidth()*16-32) - mob.position.sub(mob.moveX); if (checkColl(mob.getRect())) { int d = 0; if (mob.moveX.x<0) d=1; else if (mob.moveX.x>0) d=-1; diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index 6a2bf04..6c532ac 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -2,7 +2,6 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; -import com.badlogic.gdx.math.RandomXS128; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.TimeUtils; import ru.deadsoftware.cavecraft.*; @@ -37,8 +36,8 @@ public class GameProc { physics = new GamePhysics(this); player = new Player(world.getSpawnPoint()); mobs = new Array(); - for (int i=0; i<1024/64; i++) { - mobs.add(new Pig(i*16*64, 0, world)); + for (int i=0; i= world.getWidth()) cursorX = world.getWidth()-1; if (cursorY < 0) cursorY = 0; if (cursorY >= world.getHeight()) cursorY = world.getHeight()-1; if (cursorX<(player.position.x+player.texWidth/2)/16) diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java index b8b5324..9440938 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -1,7 +1,6 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.SpriteBatch; @@ -9,8 +8,8 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavecraft.Assets; import ru.deadsoftware.cavecraft.CaveGame; -import ru.deadsoftware.cavecraft.Items; import ru.deadsoftware.cavecraft.GameScreen; +import ru.deadsoftware.cavecraft.Items; import ru.deadsoftware.cavecraft.game.mobs.Mob; import ru.deadsoftware.cavecraft.game.objects.Player; @@ -53,22 +52,16 @@ public class GameRenderer { Assets.minecraftFont.draw(fontBatch, str, x, y); } - private void drawWorld() { - int minX = (int) (camera.position.x/16); - int minY = (int) (camera.position.y/16); + private void drawWorldBackground() { + int minX = (int) (camera.position.x/16)-1; + int minY = (int) (camera.position.y/16)-1; int maxX = (int) ((camera.position.x+camera.viewportWidth)/16)+1; int maxY = (int) ((camera.position.y+camera.viewportHeight)/16)+1; - if (minX<0) minX=0; if (minY<0) minY=0; - if (maxX>gameProc.world.getWidth()) maxX = gameProc.world.getWidth(); if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight(); for (int y=minY; y0){/* && - !Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).foreground) { - spriteBatch.draw( - Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).getTexture(), - x * 16 - camera.position.x,y * 16 - camera.position.y);*/ + if (gameProc.world.getForeMap(x,y)>0){ } else if (gameProc.world.getBackMap(x,y)>0) { spriteBatch.draw( Items.BLOCKS.getValueAt(gameProc.world.getBackMap(x,y)).getTexture(), @@ -81,18 +74,15 @@ public class GameRenderer { } private void drawWorldForeground(){ - int minX = (int) (camera.position.x/16); - int minY = (int) (camera.position.y/16); + int minX = (int) (camera.position.x/16)-1; + int minY = (int) (camera.position.y/16)-1; int maxX = (int) ((camera.position.x+camera.viewportWidth)/16)+1; int maxY = (int) ((camera.position.y+camera.viewportHeight)/16)+1; - if (minX<0) minX=0; if (minY<0) minY=0; - if (maxX>gameProc.world.getWidth()) maxX = gameProc.world.getWidth(); if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight(); for (int y=minY; y0) { /*&& - Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).foreground) {*/ + if (gameProc.world.getForeMap(x,y)>0) { spriteBatch.draw( Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).getTexture(), x * 16 - camera.position.x,y * 16 - camera.position.y); @@ -197,7 +187,7 @@ public class GameRenderer { } private void drawGamePlay() { - drawWorld(); + drawWorldBackground(); Mob.animateMobs(); for (Mob mob : gameProc.mobs) drawMob(mob); drawPlayer(gameProc.player); diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java index e6b0b00..ab89dd7 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java @@ -2,9 +2,6 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.math.Vector2; -import com.badlogic.gdx.utils.ArrayMap; -import ru.deadsoftware.cavecraft.CaveGame; -import ru.deadsoftware.cavecraft.GameState; import ru.deadsoftware.cavecraft.Items; public class GameWorld { @@ -32,17 +29,29 @@ public class GameWorld { } public int getForeMap(int x, int y) { - int ret = 0; + int map = 0; try { - ret = foreMap[x][y]; + if (x<0) { + x = x % (getWidth()-1)-1; + x = getWidth()- Math.abs(x); + } else if (x>0) { + x = x % (getWidth()-1)+1; + } + map = foreMap[x][y]; } catch (ArrayIndexOutOfBoundsException e) { Gdx.app.error("GameWorld",e.toString()); } - return ret; + return map; } public void setForeMap(int x, int y, int value) { try { + if (x<0) { + x = x % (getWidth()-1)-1; + x = getWidth()- Math.abs(x); + } else if (x>0) { + x = x % (getWidth()-1)+1; + } foreMap[x][y] = value; } catch (ArrayIndexOutOfBoundsException e) { Gdx.app.error("GameWorld", e.toString()); @@ -50,17 +59,29 @@ public class GameWorld { } public int getBackMap(int x, int y) { - int ret = 0; + int map = 0; try { - ret = backMap[x][y]; + if (x<0) { + x = x % (getWidth()-1)-1; + x = getWidth()- Math.abs(x); + } else if (x>0) { + x = x % (getWidth()-1)+1; + } + map = backMap[x][y]; } catch (ArrayIndexOutOfBoundsException e) { Gdx.app.error("GameWorld",e.toString()); } - return ret; + return map; } public void setBackMap(int x, int y, int value) { try { + if (x<0) { + x = x % (getWidth()-1)-1; + x = getWidth()- Math.abs(x); + } else if (x>0) { + x = x % (getWidth()-1)+1; + } backMap[x][y] = value; } catch (ArrayIndexOutOfBoundsException e) { Gdx.app.error("GameWorld", e.toString()); @@ -81,28 +102,6 @@ public class GameWorld { public Vector2 getSpawnPoint() { float x=0, y=0; - boolean found = false; - x = getWidth()/2; - while (!found) { - for (int i = 0; i < getHeight(); i++) { - if (getForeMap((int)x, i)>0 && - Items.BLOCKS.getValueAt(getForeMap((int)x, i)).collision && - getForeMap((int)x, i-1)==0 && getForeMap((int)x, i-1)==0) { - y = i-2; - found = true; - break; - } - } - if (!found) { - x--; - if (x<0) x=getWidth()-1; - if ((int)x == getWidth()/2+1) { - x--; - y=0; - break; - } - } - } x = x*16 + 4; y *= 16; return new Vector2(x,y); diff --git a/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java b/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java index 954bdff..ccbe50a 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java +++ b/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java @@ -1,8 +1,6 @@ package ru.deadsoftware.cavecraft.game; -import com.badlogic.gdx.Gdx; import com.badlogic.gdx.math.RandomXS128; -import com.badlogic.gdx.utils.ArrayMap; import com.badlogic.gdx.utils.TimeUtils; public class WorldGen { @@ -23,6 +21,10 @@ public class WorldGen { res[0] = mid; for (int i=1; iwidth-(max-min)) { + if (res[i-1]+tres[0]) t=-Math.abs(t); + } res[i] = res[i-1] + t; if (res[i]max) res[i] = max; @@ -60,7 +62,7 @@ public class WorldGen { rand = new RandomXS128(seed); foreMap = new int[width][height]; backMap = new int[width][height]; - hMap = genLandscape(width, height/4, height/8, height/2); + hMap = genLandscape(width, height/8*3, height/8, height/2); for (int x=0; x