From: fred-boy Date: Fri, 6 Apr 2018 11:07:28 +0000 (+0700) Subject: Add assets loader and player skin X-Git-Tag: alpha0.1~25 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=7895e461428b3cd6db4c606373081f063a9e0a2b;p=cavedroid.git Add assets loader and player skin --- diff --git a/core/src/ru/deadsoftware/cavecraft/Assets.java b/core/src/ru/deadsoftware/cavecraft/Assets.java new file mode 100644 index 0000000..17395b3 --- /dev/null +++ b/core/src/ru/deadsoftware/cavecraft/Assets.java @@ -0,0 +1,21 @@ +package ru.deadsoftware.cavecraft; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Sprite; +import com.badlogic.gdx.graphics.g2d.TextureRegion; + +public class Assets { + + public static Texture charTexture; + public static Sprite[] playerSprite = new Sprite[2]; + + public static void load() { + charTexture = new Texture(Gdx.files.internal("char.png")); + playerSprite[0] = new Sprite(new TextureRegion(charTexture, 0,0,8,30)); + playerSprite[0].flip(false,true); + playerSprite[1] = new Sprite(new TextureRegion(charTexture, 8,0,8,30)); + playerSprite[1].flip(false,true); + } + +} diff --git a/core/src/ru/deadsoftware/cavecraft/GameScreen.java b/core/src/ru/deadsoftware/cavecraft/GameScreen.java index 451f2a2..9ad88f6 100644 --- a/core/src/ru/deadsoftware/cavecraft/GameScreen.java +++ b/core/src/ru/deadsoftware/cavecraft/GameScreen.java @@ -13,9 +13,9 @@ public class GameScreen implements Screen { private GameInputHandler gameInput; public GameScreen() { + Assets.load(); gameProc = new GameProc(); gameInput = new GameInputHandler(gameProc); - Gdx.input.setInputProcessor(new InputHandler()); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java index c3aa564..26b4b6a 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java @@ -20,7 +20,8 @@ public class GameInputHandler { gameProc.player.moveX.add(GamePhysics.PL_SPEED,0); gameProc.player.dir = 1; } - if (keyCode == Input.Keys.UP) gameProc.player.moveY.add(0,-12); + if (keyCode == Input.Keys.UP && + gameProc.player.canJump) gameProc.player.moveY.add(0,-8); } public void keyUp(int keyCode) { diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java index 1ed455b..631e9d1 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java @@ -1,6 +1,7 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavecraft.game.objects.Player; @@ -15,17 +16,17 @@ public class GamePhysics { public GamePhysics(GameProc gameProc) { this.gameProc = gameProc; - gravity = new Vector2(0,2); + gravity = new Vector2(0,1); } private boolean checkColl(Rectangle rect) { int[] bl = new int [6]; - bl[0] = gameProc.world.getForeMap(((int)rect.x/16), ((int)rect.y/16)); - bl[1] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)rect.y/16)); - bl[2] = gameProc.world.getForeMap(((int)rect.x/16), ((int)(rect.y+rect.height/2)/16)); - bl[3] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+rect.height/2)/16)); - bl[4] = gameProc.world.getForeMap(((int)rect.x/16), ((int)(rect.y+rect.height-1)/16)); - bl[5] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+(rect.height-1))/16)); + bl[0] = gameProc.world.getForeMap(((int)(rect.x+2)/16), ((int)rect.y/16)); + bl[1] = gameProc.world.getForeMap(((int)(rect.x+rect.width-2)/16), ((int)rect.y/16)); + bl[2] = gameProc.world.getForeMap(((int)(rect.x+2)/16), ((int)(rect.y+rect.height/2)/16)); + bl[3] = gameProc.world.getForeMap(((int)(rect.x+rect.width-2)/16), ((int)(rect.y+rect.height/2)/16)); + bl[4] = gameProc.world.getForeMap(((int)(rect.x+2)/16), ((int)(rect.y+rect.height-1)/16)); + bl[5] = gameProc.world.getForeMap(((int)(rect.x+rect.width-2)/16), ((int)(rect.y+(rect.height-1))/16)); for (int b: bl) if (b>0) { return true; } @@ -34,18 +35,25 @@ public class GamePhysics { private void movePlayer(Player pl) { pl.position.add(pl.moveX); + if (pl.position.x<0 || + pl.position.x+pl.width>gameProc.world.getWidth()*16) + pl.position.sub(pl.moveX); if (checkColl(pl.getRect())) { int d = 0; if (pl.moveX.x<0) d=1; else if (pl.moveX.x>0) d=-1; + pl.position.x = MathUtils.round(pl.position.x); while (checkColl(pl.getRect())) pl.position.x+=d; - //pl.moveX.setZero(); } pl.position.add(pl.moveY); if (checkColl(pl.getRect())) { - int d = 0; + pl.canJump = true; + float d = 0; if (pl.moveY.y<0) d=1; else if (pl.moveY.y>0) d=-1; + pl.position.y = MathUtils.round(pl.position.y); while (checkColl(pl.getRect())) pl.position.y+=d; pl.moveY.setZero(); + } else { + pl.canJump = false; } pl.moveY.add(gravity); switch (pl.dir) { @@ -56,6 +64,10 @@ public class GamePhysics { gameProc.renderer.camTargetPos.x = pl.position.x-100; break; } + if (gameProc.renderer.camTargetPos.x < 0) gameProc.renderer.camTargetPos.x = 0; + if (gameProc.renderer.camTargetPos.x + gameProc.renderer.camera.viewportWidth > + gameProc.world.getWidth()*16) + gameProc.renderer.camTargetPos.x = gameProc.world.getWidth()*16-gameProc.renderer.camera.viewportWidth; } public void update(float delta) { diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java index 563f095..b93de89 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -3,10 +3,12 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector3; +import ru.deadsoftware.cavecraft.Assets; import ru.deadsoftware.cavecraft.GameScreen; import ru.deadsoftware.cavecraft.game.objects.Player; @@ -17,19 +19,22 @@ public class GameRenderer { public Vector3 camTargetPos; public OrthographicCamera camera; ShapeRenderer shapeRenderer; + SpriteBatch spriteBatch; public GameRenderer(GameProc gameProc) { Gdx.gl.glClearColor(0f,.6f,.6f,1f); this.gameProc = gameProc; camera = new OrthographicCamera(); - camera.setToOrtho(true, 320, - 320*((float)GameScreen.getHeight()/GameScreen.getWidth())); + camera.setToOrtho(true, 360, + 360*((float)GameScreen.getHeight()/GameScreen.getWidth())); camera.position.x=0; camera.position.y=0; camTargetPos = camera.position.cpy(); shapeRenderer = new ShapeRenderer(); shapeRenderer.setProjectionMatrix(camera.combined); shapeRenderer.setAutoShapeType(true); + spriteBatch = new SpriteBatch(); + spriteBatch.setProjectionMatrix(camera.combined); } private void setColor(int r, int g, int b) { @@ -53,8 +58,8 @@ public class GameRenderer { 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()-1; - if (maxY>=gameProc.world.getHeight()) maxY = gameProc.world.getHeight()-1; + if (maxX>gameProc.world.getWidth()) maxX = gameProc.world.getWidth(); + if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight(); for (int y=minY; y0) { @@ -83,17 +88,19 @@ public class GameRenderer { } private void drawPlayer(Player pl) { - setColor(0,128,0); - fillRect(pl.position.x - camera.position.x, - pl.position.y - camera.position.y, pl.width, pl.height); + spriteBatch.begin(); + Assets.playerSprite[pl.dir].setPosition(pl.position.x - camera.position.x, + pl.position.y - camera.position.y); + Assets.playerSprite[pl.dir].draw(spriteBatch); + spriteBatch.end(); } public void render() { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); shapeRenderer.begin(); drawWorld(); - drawPlayer(gameProc.player); shapeRenderer.end(); + drawPlayer(gameProc.player); } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java index c63e059..0c5581a 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java @@ -8,6 +8,7 @@ public class Player { public Vector2 position; public Vector2 moveX, moveY; public int width, height, dir; + public boolean canJump; public Player() { position = new Vector2(0, 0); diff --git a/desktop/src/ru/deadsoftware/cavecraft/desktop/DesktopLauncher.java b/desktop/src/ru/deadsoftware/cavecraft/desktop/DesktopLauncher.java index 8afb2b9..2f0d7b0 100644 --- a/desktop/src/ru/deadsoftware/cavecraft/desktop/DesktopLauncher.java +++ b/desktop/src/ru/deadsoftware/cavecraft/desktop/DesktopLauncher.java @@ -7,6 +7,10 @@ import ru.deadsoftware.cavecraft.CaveGame; public class DesktopLauncher { public static void main (String[] arg) { LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); + config.foregroundFPS = 60; + config.title = "CaveCraft"; + config.width = 960; + config.height = 720; new LwjglApplication(new CaveGame(), config); } }