DEADSOFTWARE

Add assets loader and player skin
authorfred-boy <fred-boy@protonmail.com>
Fri, 6 Apr 2018 11:07:28 +0000 (18:07 +0700)
committerfred-boy <fred-boy@protonmail.com>
Fri, 6 Apr 2018 11:07:28 +0000 (18:07 +0700)
core/src/ru/deadsoftware/cavecraft/Assets.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavecraft/GameScreen.java
core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java
core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java
core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java
core/src/ru/deadsoftware/cavecraft/game/objects/Player.java
desktop/src/ru/deadsoftware/cavecraft/desktop/DesktopLauncher.java

diff --git a/core/src/ru/deadsoftware/cavecraft/Assets.java b/core/src/ru/deadsoftware/cavecraft/Assets.java
new file mode 100644 (file)
index 0000000..17395b3
--- /dev/null
@@ -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);
+    }
+
+}
index 451f2a2e241520d25e193263083e783b00fb6a86..9ad88f6eb4517c61f7c437ce202a6e5c49158d9c 100644 (file)
@@ -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());
     }
 
index c3aa564c9b25c0ad45c104eaa6e0effe537db918..26b4b6acdc9bfba26fd494d123ef7929c0ffcab2 100644 (file)
@@ -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) {
index 1ed455b75ce04cd450e7c6c5b407dfe31b205b8b..631e9d128c4c8b8a55ceab3cd033f8f55a5e16ac 100644 (file)
@@ -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) {
index 563f095203bd418c6822cb9039b907a70c48318d..b93de89b8d756f9e42470882954f262fa0a03e2d 100644 (file)
@@ -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; y<maxY; y++) {
             for (int x=minX; x<maxX; x++) {
                 if (gameProc.world.getForeMap(x,y)>0) {
@@ -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);
     }
 
 }
index c63e0595449265aba12da75e3cf947424ddbd167..0c5581a7ee2711999f526e52bbc77cafa4385a2e 100644 (file)
@@ -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);
index 8afb2b9f50ee6f8cae9650d105146b1b324ed53a..2f0d7b020ac0c6da2679d44251655adb0c13a5c6 100644 (file)
@@ -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);
        }
 }