From: fred-boy Date: Thu, 19 Apr 2018 11:33:32 +0000 (+0700) Subject: Gameplay enhancements X-Git-Tag: alpha0.1~4 X-Git-Url: https://deadsoftware.ru/gitweb?p=cavedroid.git;a=commitdiff_plain;h=103459785743a8485af69863d65ad1b1a2410ab0 Gameplay enhancements --- diff --git a/android/assets/gui.png b/android/assets/gui.png index ef76dc0..6542140 100644 Binary files a/android/assets/gui.png and b/android/assets/gui.png differ diff --git a/core/src/ru/deadsoftware/cavecraft/Assets.java b/core/src/ru/deadsoftware/cavecraft/Assets.java index e1fd5c9..593a2c0 100644 --- a/core/src/ru/deadsoftware/cavecraft/Assets.java +++ b/core/src/ru/deadsoftware/cavecraft/Assets.java @@ -2,7 +2,6 @@ package ru.deadsoftware.cavecraft; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.TextureRegion; @@ -23,7 +22,8 @@ public class Assets { public static Texture gui; public static TextureRegion invBar; - public static TextureRegion invCur; + public static TextureRegion invBarCur; + public static TextureRegion guiCur; public static Texture creativeTexture; public static TextureRegion creativeInv; @@ -69,8 +69,9 @@ public class Assets { shade = new Sprite(new Texture(Gdx.files.internal("shade.png"))); gui = new Texture(Gdx.files.internal("gui.png")); - invBar = new TextureRegion(gui,0,0,182,22); - invCur = new TextureRegion(gui,0,22,24,24); + guiCur = new TextureRegion(gui,0,0,16,16); + invBar = new TextureRegion(gui,0,16,182,22); + invBarCur = new TextureRegion(gui,0,38,24,24); creativeTexture = new Texture(Gdx.files.internal("allitems.png")); creativeInv = new TextureRegion(creativeTexture, 0, 0, 176, 208); diff --git a/core/src/ru/deadsoftware/cavecraft/CaveGame.java b/core/src/ru/deadsoftware/cavecraft/CaveGame.java index 50d9447..fc6d0bd 100644 --- a/core/src/ru/deadsoftware/cavecraft/CaveGame.java +++ b/core/src/ru/deadsoftware/cavecraft/CaveGame.java @@ -6,6 +6,8 @@ public class CaveGame extends Game { public static final String VERSION = "alpha 0.0"; + public static GameState STATE; + public static boolean TOUCH; public CaveGame() { @@ -14,6 +16,7 @@ public class CaveGame extends Game { public CaveGame(boolean touch) { TOUCH = touch; + STATE = GameState.GAME_PLAY; } @Override diff --git a/core/src/ru/deadsoftware/cavecraft/GameState.java b/core/src/ru/deadsoftware/cavecraft/GameState.java new file mode 100644 index 0000000..9de340d --- /dev/null +++ b/core/src/ru/deadsoftware/cavecraft/GameState.java @@ -0,0 +1,6 @@ +package ru.deadsoftware.cavecraft; + +public enum GameState { + GAME_PLAY, + GAME_CREATIVE_INV +} diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java index 490960b..61b52e4 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java @@ -3,9 +3,7 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.utils.TimeUtils; -import ru.deadsoftware.cavecraft.Assets; -import ru.deadsoftware.cavecraft.GameScreen; -import ru.deadsoftware.cavecraft.Items; +import ru.deadsoftware.cavecraft.*; public class GameInputHandler { @@ -19,11 +17,11 @@ public class GameInputHandler { if (gameProc.ctrlMode==0) { switch (keyCode) { case Input.Keys.A: - gameProc.player.moveX.add(-GamePhysics.PL_SPEED, 0); + gameProc.player.moveX.x = -GamePhysics.PL_SPEED; gameProc.player.dir = 0; break; case Input.Keys.D: - gameProc.player.moveX.add(GamePhysics.PL_SPEED, 0); + gameProc.player.moveX.x = GamePhysics.PL_SPEED; gameProc.player.dir = 1; break; } @@ -42,14 +40,10 @@ public class GameInputHandler { gameProc.cursorY++; break; } - if (gameProc.cursorX < 0) - gameProc.cursorX = 0; - if (gameProc.cursorX >= gameProc.world.getWidth()) - gameProc.cursorX = gameProc.world.getWidth()-1; - if (gameProc.cursorY < 0) - gameProc.cursorY = 0; - if (gameProc.cursorY >= gameProc.world.getHeight()) - gameProc.cursorY = gameProc.world.getHeight()-1; + if (gameProc.cursorX<(gameProc.player.position.x+gameProc.player.texWidth/2)/16) + gameProc.player.dir=0; + if (gameProc.cursorX>(gameProc.player.position.x+gameProc.player.texWidth/2)/16) + gameProc.player.dir=1; } } @@ -60,8 +54,6 @@ public class GameInputHandler { } else switch (keyCode) { case Input.Keys.ALT_LEFT: gameProc.ctrlMode++; - gameProc.cursorX = (int)(gameProc.player.position.x/16); - gameProc.cursorY = (int)(gameProc.player.position.y/16); if (gameProc.ctrlMode > 1) gameProc.ctrlMode = 0; break; @@ -81,7 +73,8 @@ public class GameInputHandler { break; case Input.Keys.E: - gameProc.renderer.showCreative = !gameProc.renderer.showCreative; + if (CaveGame.STATE == GameState.GAME_PLAY) CaveGame.STATE = GameState.GAME_CREATIVE_INV; + else CaveGame.STATE = GameState.GAME_PLAY; break; } } @@ -102,7 +95,7 @@ public class GameInputHandler { } public void touchDown(int screenX, int screenY, int button) { - if (gameProc.renderer.showCreative && + if (CaveGame.STATE == GameState.GAME_CREATIVE_INV && screenX>gameProc.renderer.camera.viewportWidth/2-Assets.creativeInv.getRegionWidth()/2 && screenXgameProc.renderer.camera.viewportHeight/2-Assets.creativeInv.getRegionHeight()/2 && @@ -118,8 +111,12 @@ public class GameInputHandler { } catch (Exception e) { Gdx.app.error("GameInputHandler", e.toString()); } - } else if (gameProc.renderer.showCreative) { - gameProc.renderer.showCreative = !gameProc.renderer.showCreative; + } else if (CaveGame.STATE == GameState.GAME_CREATIVE_INV) { + CaveGame.STATE = GameState.GAME_PLAY; + } else if (button == Input.Buttons.RIGHT && + !gameProc.player.canJump && !gameProc.player.flyMode) { + gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, + gameProc.player.inventory[gameProc.invSlot]); } else { gameProc.touchDownX = screenX; gameProc.touchDownY = screenY; @@ -132,24 +129,19 @@ public class GameInputHandler { public void touchUp(int screenX, int screenY, int button) { if (gameProc.isTouchDown) { if (button == Input.Buttons.RIGHT){ - if (gameProc.ctrlMode==1) { - gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, + gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, gameProc.player.inventory[gameProc.invSlot]); - } } else if (button == Input.Buttons.LEFT) { - if (gameProc.ctrlMode==1) { - if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY) > 0) { - gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 0); - } else if (gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY) > 0) { - gameProc.world.placeToBackground(gameProc.cursorX, gameProc.cursorY, 0); - } - } else { - if (screenYgameProc.renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 && - screenXgameProc.renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 && + screenX 0) { + gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 0); + } else if (gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY) > 0) { + gameProc.world.placeToBackground(gameProc.cursorX, gameProc.cursorY, 0); } + } } gameProc.isTouchDown = false; diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index d6378d3..e49a405 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -5,6 +5,9 @@ import com.badlogic.gdx.math.RandomXS128; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.TimeUtils; import ru.deadsoftware.cavecraft.Assets; +import ru.deadsoftware.cavecraft.CaveGame; +import ru.deadsoftware.cavecraft.GameState; +import ru.deadsoftware.cavecraft.Items; import ru.deadsoftware.cavecraft.game.mobs.Human; import ru.deadsoftware.cavecraft.game.mobs.Mob; import ru.deadsoftware.cavecraft.game.objects.Player; @@ -31,7 +34,7 @@ public class GameProc { public long touchDownTime; public GameProc() { - world = new GameWorld(4096,256); + world = new GameWorld(1024,256); renderer = new GameRenderer(this); physics = new GamePhysics(this); player = new Player(world.getSpawnPoint()); @@ -43,10 +46,46 @@ public class GameProc { renderer = new GameRenderer(this); } + private boolean isAutoselectable(int x, int y) { + return (world.getForeMap(x,y)>0 && + Items.BLOCKS.getValueAt(world.getForeMap(x,y)).collision); + } + + private void moveCursor() { + if (player.canJump) { + cursorX = (int) (player.position.x + player.texWidth / 2) / 16; + if (player.dir == 0) cursorX--; + else cursorX++; + cursorY = (int) (player.position.y + player.texWidth) / 16; + if (!isAutoselectable(cursorX, cursorY)) { + cursorY++; + } + if (!isAutoselectable(cursorX, cursorY)) { + cursorY++; + } + if (!isAutoselectable(cursorX, cursorY)) { + if (player.dir == 0) cursorX++; + else cursorX--; + } + } else { + cursorX = (int) (player.position.x + player.texWidth / 2) / 16; + cursorY = (int) (player.position.y + player.height+8)/16; + } + } + + private void checkCursorBounds() { + if (cursorX < 0) cursorX = 0; + if (cursorX >= world.getWidth()) cursorX = world.getWidth()-1; + if (cursorY < 0) cursorY = 0; + if (cursorY >= world.getHeight()) cursorY = world.getHeight()-1; + } + public void update(float delta) { RUN_TIME += delta; physics.update(delta); + if (ctrlMode==0) moveCursor(); + checkCursorBounds(); if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) { if (touchDownButton== Input.Buttons.RIGHT) { @@ -56,7 +95,7 @@ public class GameProc { touchDownY< Assets.invBar.getRegionHeight() && touchDownX>renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 && touchDownXgameProc.world.getHeight()) maxY = gameProc.world.getHeight(); for (int y=minY; y0 && + if (gameProc.world.getForeMap(x,y)>0){/* && !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); + x * 16 - camera.position.x,y * 16 - camera.position.y);*/ } else if (gameProc.world.getBackMap(x,y)>0) { spriteBatch.draw( Items.BLOCKS.getValueAt(gameProc.world.getBackMap(x,y)).getTexture(), @@ -95,8 +96,8 @@ public class GameRenderer { 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) { /*&& + 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); @@ -124,24 +125,30 @@ public class GameRenderer { } if (Assets.playerSkin[0][2].getRotation()>=60 || Assets.playerSkin[0][2].getRotation()<=-60) Mob.ANIM_SPEED = -Mob.ANIM_SPEED; + + //back hand Assets.playerSkin[1][2].setPosition( pl.position.x - camera.position.x - 6, pl.position.y - camera.position.y); Assets.playerSkin[1][2].draw(spriteBatch); + //back leg Assets.playerSkin[1][3].setPosition( pl.position.x - camera.position.x - 6, pl.position.y - camera.position.y + 10); Assets.playerSkin[1][3].draw(spriteBatch); + //front leg Assets.playerSkin[0][3].setPosition( pl.position.x - camera.position.x - 6, pl.position.y - camera.position.y + 10); Assets.playerSkin[0][3].draw(spriteBatch); - + //head spriteBatch.draw(Assets.playerSkin[pl.dir][0], - pl.position.x - camera.position.x - 2, pl.position.y - camera.position.y - 2); + pl.position.x - camera.position.x - 2, + pl.position.y - camera.position.y - 2); + //body spriteBatch.draw(Assets.playerSkin[pl.dir][1], pl.position.x - camera.position.x - 2, pl.position.y - camera.position.y + 8); - + //front hand Assets.playerSkin[0][2].setPosition( pl.position.x - camera.position.x - 6, pl.position.y - camera.position.y); @@ -165,6 +172,12 @@ public class GameRenderer { } private void drawGUI() { + if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY)>0 || + gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY)>0 || + gameProc.ctrlMode==1) + spriteBatch.draw(Assets.guiCur, + gameProc.cursorX*16-camera.position.x, + gameProc.cursorY*16-camera.position.y); spriteBatch.draw(Assets.invBar, camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2, 0); for (int i=0; i<9; i++) { if (gameProc.player.inventory[i]>0) { @@ -173,11 +186,9 @@ public class GameRenderer { 3); } } - spriteBatch.draw(Assets.invCur, + spriteBatch.draw(Assets.invBarCur, camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2 - 1 + 20*gameProc.invSlot, -1); - - if (showCreative) drawCreative(); } private void drawTouchGui() { @@ -185,21 +196,32 @@ public class GameRenderer { touchBatch.draw(Assets.touchArrows[1],0,touchCam.viewportHeight-26); touchBatch.draw(Assets.touchArrows[2],26,touchCam.viewportHeight-26); touchBatch.draw(Assets.touchArrows[3],52,touchCam.viewportHeight-26); - //touchBatch.draw(Assets.touchSpace, touchCam.viewportWidth/2-52, touchCam.viewportHeight-26); touchBatch.draw(Assets.touchLMB, touchCam.viewportWidth-52, touchCam.viewportHeight-26); touchBatch.draw(Assets.touchRMB, touchCam.viewportWidth-26, touchCam.viewportHeight-26); touchBatch.draw(Assets.touchToggleMode, 78, touchCam.viewportHeight-26); } - public void render() { - Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - - spriteBatch.begin(); + private void drawGamePlay() { drawWorld(); for (Mob mob : gameProc.mobs) drawMob(mob); drawPlayer(gameProc.player); drawWorldForeground(); drawGUI(); + } + + public void render() { + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + + spriteBatch.begin(); + switch (CaveGame.STATE) { + case GAME_PLAY: + drawGamePlay(); + break; + case GAME_CREATIVE_INV: + drawGamePlay(); + drawCreative(); + break; + } spriteBatch.end(); if (CaveGame.TOUCH) { @@ -208,26 +230,12 @@ public class GameRenderer { touchBatch.end(); } - if (gameProc.ctrlMode==1) { - shapeRenderer.begin(ShapeRenderer.ShapeType.Line); - shapeRenderer.setColor(Color.ORANGE); - shapeRenderer.set(ShapeRenderer.ShapeType.Line); - shapeRenderer.rect(gameProc.cursorX * 16 - camera.position.x, - gameProc.cursorY * 16 - camera.position.y, 16, 16); - shapeRenderer.end(); - } - fontBatch.begin(); setFontColor(255,255,255); drawString("CaveCraft "+CaveGame.VERSION, 0, 0); drawString("FPS: "+GameScreen.FPS, 0, 20); drawString("X: "+(int)(gameProc.player.position.x/16), 0, 40); drawString("Y: "+(int)(gameProc.player.position.y/16), 0, 60); - drawString("Block: "+ - Items.BLOCKS.keys().toArray().get(gameProc.world.getForeMap( - (int)((gameProc.player.position.x+gameProc.player.texWidth/2)/16), - (int)(gameProc.player.position.y/16+2))), - 0, 80); fontBatch.end(); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java index 97d05cb..c4655bf 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java @@ -2,11 +2,13 @@ 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.Items; public class GameWorld { private final int WIDTH, HEIGHT; + private int[][] foreMap; private int[][] backMap; @@ -64,12 +66,15 @@ public class GameWorld { } public void placeToForeground(int x, int y, int value) { - if (getForeMap(x,y) == 0 || value == 0) setForeMap(x,y,value); + if (getForeMap(x,y) == 0 || value == 0) { + setForeMap(x,y,value); + } } public void placeToBackground(int x, int y, int value) { - if (value==0 || (getBackMap(x,y) == 0 && !Items.BLOCKS.getValueAt(value).foreground)) - setBackMap(x,y,value); + if (value==0 || (getBackMap(x,y) == 0 && !Items.BLOCKS.getValueAt(value).foreground)) { + setBackMap(x,y,value); + } } public Vector2 getSpawnPoint() { diff --git a/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java b/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java index d4635fd..8b1b699 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java +++ b/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java @@ -1,7 +1,7 @@ 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 { @@ -61,6 +61,8 @@ public class WorldGen { backMap = new int[width][height]; hMap = genLandscape(width, height/2, height/4, height/4*3); for (int x=0; x2 && x2 && x