From: fred-boy Date: Sat, 28 Sep 2019 15:08:41 +0000 (+0700) Subject: Refactor X-Git-Tag: alpha0.4~39 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=59d07dd8a43cfb159045e6313cca4d519f67a6fc;p=cavedroid.git Refactor Rewrite input handling Move textures to json --- diff --git a/android/assets/json/game_items.json b/android/assets/json/game_items.json new file mode 100644 index 0000000..12cad82 --- /dev/null +++ b/android/assets/json/game_items.json @@ -0,0 +1,610 @@ +{ + "blocks": { + "none": { + "collision": false, + "transparent": true + }, + "stone": { + "hp": 450, + "drop": "cobblestone" + }, + "grass": { + "hp": 54, + "drop": "dirt" + }, + "dirt": { + "hp": 45 + }, + "cobblestone": { + "hp": 600 + }, + "planks": { + "hp": 180 + }, + "sapling": { + "hp": 0, + "collision": false, + "background": false, + "transparent": true, + "block_required": true + }, + "bedrock": { + "hp": -1 + }, + "water": { + "hp": -1, + "collision": false, + "background": false, + "transparent": true, + "fluid": true, + "meta": "water" + }, + "lava": { + "hp": -1, + "collision": false, + "background": false, + "fluid": true, + "meta": "lava" + }, + "sand": { + "hp": 45 + }, + "gravel": { + "hp": 54 + }, + "gold_ore": { + "hp": 900 + }, + "iron_ore": { + "hp": 900 + }, + "coal_ore": { + "hp": 900, + "drop": "coal" + }, + "log": { + "hp": 180 + }, + "leaves": { + "hp": 21 + }, + "sponge": { + "hp": 54 + }, + "glass": { + "hp": 27, + "drop": "none", + "background": false, + "transparent": true + }, + "lapis_ore": { + "hp": 900, + "drop": "lapis" + }, + "lapis_block": { + "hp": 900 + }, + "sandstone": { + "hp": 240 + }, + "noteblock": { + "hp": 75 + }, + "bed_l": { + "hp": 21, + "drop": "none", + "collision": false, + "background": true, + "transparent": true + }, + "bed_r": { + "hp": 21, + "drop": "none", + "collision": false, + "background": true, + "transparent": true + }, + "web": { + "hp": 1200, + "collision": false, + "background": false, + "transparent": true + }, + "tallgrass": { + "hp": 0, + "collision": false, + "background": false, + "transparent": true, + "block_required": true + }, + "deadbush": { + "hp": 0, + "collision": false, + "background": false, + "transparent": true, + "block_required": true + }, + "bricks": { + "hp": 600 + }, + "dandelion": { + "hp": 0, + "collision": false, + "background": false, + "transparent": true, + "block_required": true + }, + "rose": { + "hp": 0, + "collision": false, + "background": false, + "transparent": true, + "block_required": true + }, + "mushroom_brown": { + "hp": 0, + "collision": false, + "background": false, + "transparent": true, + "block_required": true + }, + "mushroom_red": { + "hp": 0, + "collision": false, + "background": false, + "transparent": true, + "block_required": true + }, + "wool": { + "hp": 75 + }, + "wool_orange": { + "hp": 75 + }, + "wool_magenta": { + "hp": 75 + }, + "wool_lightblue": { + "hp": 75 + }, + "wool_yellow": { + "hp": 75 + }, + "wool_lime": { + "hp": 75 + }, + "wool_pink": { + "hp": 75 + }, + "wool_gray": { + "hp": 75 + }, + "wool_lightgray": { + "hp": 75 + }, + "wool_cyan": { + "hp": 75 + }, + "wool_purple": { + "hp": 75 + }, + "wool_blue": { + "hp": 75 + }, + "wool_brown": { + "hp": 75 + }, + "wool_green": { + "hp": 75 + }, + "wool_red": { + "hp": 75 + }, + "wool_black": { + "hp": 75 + }, + "gold_block": { + "hp": 900 + }, + "iron_block": { + "hp": 1500 + }, + "stone_slab": { + "top": 8, + "hp": 600, + "transparent": true + }, + "double_stone_slab": { + "hp": 600, + "drop": "stone_slab" + }, + "sandstone_slab": { + "top": 8, + "hp": 600, + "transparent": true + }, + "wooden_slab": { + "top": 8, + "hp": 180, + "transparent": true + }, + "cobblestone_slab": { + "top": 8, + "hp": 600, + "transparent": true + }, + "brick_slab": { + "top": 8, + "hp": 600, + "transparent": true + }, + "stonebrick": { + "hp": 450 + }, + "stonebrick_slab": { + "top": 8, + "hp": 450, + "transparent": true + }, + "cactus": { + "left": 1, + "right": 1, + "hp": 39, + "transparent": true, + "block_required": true + }, + "water_16": { + "hp": -1, + "collision": false, + "background": false, + "transparent": true, + "texture": "water", + "fluid": true, + "meta": "water" + }, + "water_12": { + "hp": -1, + "collision": false, + "background": false, + "transparent": true, + "fluid": true, + "meta": "water" + }, + "water_8": { + "hp": -1, + "collision": false, + "background": false, + "transparent": true, + "fluid": true, + "meta": "water" + }, + "water_4": { + "hp": -1, + "collision": false, + "background": false, + "transparent": true, + "fluid": true, + "meta": "water" + }, + "lava_16": { + "hp": -1, + "collision": false, + "background": false, + "transparent": true, + "texture": "lava", + "fluid": true, + "meta": "lava" + }, + "lava_12": { + "hp": -1, + "collision": false, + "background": false, + "transparent": true, + "fluid": true, + "meta": "lava" + }, + "lava_8": { + "hp": -1, + "collision": false, + "background": false, + "transparent": true, + "fluid": true, + "meta": "lava" + }, + "lava_4": { + "hp": -1, + "collision": false, + "background": false, + "transparent": true, + "fluid": true, + "meta": "lava" + }, + "obsidian": { + "hp": 1500 + } + }, + "items": { + "none": { + "name": "", + "type": "block" + }, + "stone": { + "name": "Stone", + "type": "block" + }, + "grass": { + "name": "Grass", + "type": "block" + }, + "dirt": { + "name": "Dirt", + "type": "block" + }, + "cobblestone": { + "name": "Cobblestone", + "type": "block" + }, + "planks": { + "name": "Planks", + "type": "block" + }, + "sapling": { + "name": "Sapling", + "type": "block" + }, + "bedrock": { + "name": "Bedrock", + "type": "block" + }, + "water": { + "name": "Water", + "type": "block" + }, + "lava": { + "name": "Lava", + "type": "block" + }, + "sand": { + "name": "Sand", + "type": "block" + }, + "gravel": { + "name": "Gravel", + "type": "block" + }, + "gold_ore": { + "name": "Golden Ore", + "type": "block" + }, + "iron_ore": { + "name": "Iron Ore", + "type": "block" + }, + "coal_ore": { + "name": "Coal Ore", + "type": "block" + }, + "log": { + "name": "Wood", + "type": "block" + }, + "leaves": { + "name": "Leaves", + "type": "block" + }, + "glass": { + "name": "Glass", + "type": "block" + }, + "lapis_ore": { + "name": "Lapis Ore", + "type": "block" + }, + "lapis_block": { + "name": "Lapis Block", + "type": "block" + }, + "sandstone": { + "name": "Sandstone", + "type": "block" + }, + "web": { + "name": "Cobweb", + "type": "block" + }, + "tallgrass": { + "name": "Tall Grass", + "type": "block" + }, + "deadbush": { + "name": "Dead Bush", + "type": "block" + }, + "bricks": { + "name": "Bricks", + "type": "block" + }, + "dandelion": { + "name": "Dandelion", + "type": "block" + }, + "rose": { + "name": "Rose", + "type": "block" + }, + "mushroom_brown": { + "name": "Mushroom", + "type": "block" + }, + "mushroom_red": { + "name": "Mushroom", + "type": "block" + }, + "wool": { + "name": "Wool", + "type": "block" + }, + "wool_orange": { + "name": "Wool", + "type": "block" + }, + "wool_magenta": { + "name": "Wool", + "type": "block" + }, + "wool_lightblue": { + "name": "Wool", + "type": "block" + }, + "wool_yellow": { + "name": "Wool", + "type": "block" + }, + "wool_lime": { + "name": "Wool", + "type": "block" + }, + "wool_pink": { + "name": "Wool", + "type": "block" + }, + "wool_gray": { + "name": "Wool", + "type": "block" + }, + "wool_lightgray": { + "name": "Wool", + "type": "block" + }, + "wool_cyan": { + "name": "Wool", + "type": "block" + }, + "wool_purple": { + "name": "Wool", + "type": "block" + }, + "wool_blue": { + "name": "Wool", + "type": "block" + }, + "wool_brown": { + "name": "Wool", + "type": "block" + }, + "wool_green": { + "name": "Wool", + "type": "block" + }, + "wool_red": { + "name": "Wool", + "type": "block" + }, + "wool_black": { + "name": "Wool", + "type": "block" + }, + "gold_block": { + "name": "Gold Block", + "type": "block" + }, + "iron_block": { + "name": "Iron Block", + "type": "block" + }, + "stone_slab": { + "name": "Stone Slab", + "type": "block", + "meta": "slab" + }, + "sandstone_slab": { + "name": "Sandstone Slab", + "type": "block", + "meta": "slab" + }, + "wooden_slab": { + "name": "Wooden Slab", + "type": "block", + "meta": "slab" + }, + "cobblestone_slab": { + "name": "Cobblestone Slab", + "type": "block", + "meta": "slab" + }, + "brick_slab": { + "name": "Brick Slab", + "type": "block", + "meta": "slab" + }, + "stonebrick": { + "name": "Stone Brick", + "type": "block" + }, + "stonebrick_slab": { + "name": "Stone Brick Slab", + "type": "block", + "meta": "slab" + }, + "cactus": { + "name": "Cactus", + "type": "block" + }, + "obsidian": { + "name": "Obsidian", + "type": "block" + }, + "wood_sword": { + "name": "Wooden Sword", + "type": "tool" + }, + "stone_sword": { + "name": "Stone Sword", + "type": "tool" + }, + "iron_sword": { + "name": "Iron Sword", + "type": "tool" + }, + "diamond_sword": { + "name": "Diamond Sword", + "type": "tool" + }, + "gold_sword": { + "name": "Golden Sword", + "type": "tool" + }, + "wood_shovel": { + "name": "Wooden Shovel", + "type": "tool" + }, + "stone_shovel": { + "name": "Stone Shovel", + "type": "tool" + }, + "iron_shovel": { + "name": "Iron Shovel", + "type": "tool" + }, + "diamond_shovel": { + "name": "Diamond Shovel", + "type": "tool" + }, + "gold_shovel": { + "name": "Golden Shovel", + "type": "tool" + }, + "bucket_empty": { + "name": "Empty Bucket", + "type": "tool" + }, + "bucket_water": { + "name": "Water Bucket", + "type": "tool" + }, + "bucket_lava": { + "name": "Lava Bucket", + "type": "tool" + } + } +} \ No newline at end of file diff --git a/android/assets/json/texture_regions.json b/android/assets/json/texture_regions.json new file mode 100644 index 0000000..d032497 --- /dev/null +++ b/android/assets/json/texture_regions.json @@ -0,0 +1,141 @@ +{ + "touch_gui": { + "up": { + "w": 26, + "h": 26 + }, + "down": { + "x": 52, + "w": 26, + "h": 26 + }, + "left": { + "x": 26, + "w": 26, + "h": 26 + }, + "right": { + "x": 78, + "w": 26, + "h": 26 + }, + "lmb": { + "y": 26, + "w": 26, + "h": 26 + }, + "alt": { + "x": 26, + "y": 26, + "w": 26, + "h": 26 + }, + "rmb": { + "x": 52, + "y": 26, + "w": 26, + "h": 26 + } + }, + "break": { + "break_0": { + "w": 16, + "h": 16 + }, + "break_1": { + "x": 16, + "w": 16, + "h": 16 + }, + "break_2": { + "x": 32, + "w": 16, + "h": 16 + }, + "break_3": { + "x": 48, + "w": 16, + "h": 16 + }, + "break_4": { + "x": 64, + "w": 16, + "h": 16 + }, + "break_5": { + "x": 80, + "w": 16, + "h": 16 + }, + "break_6": { + "x": 96, + "w": 16, + "h": 16 + }, + "break_7": { + "x": 112, + "w": 16, + "h": 16 + }, + "break_8": { + "x": 128, + "w": 16, + "h": 16 + }, + "break_9": { + "x": 144, + "w": 16, + "h": 16 + } + }, + "allitems": { + "creative": { + "w": 176, + "h": 136 + }, + "handle": { + "x": 3, + "y": 137, + "w": 12, + "h": 15 + } + }, + "buttons": { + "button_0": { + "w": 200, + "h": 20 + }, + "button_1": { + "y": 20, + "w": 200, + "h": 20 + }, + "button_2": { + "y": 40, + "w": 200, + "h": 20 + } + }, + "gui": { + "hotbar": { + "y": 16, + "w": 182, + "h": 22 + }, + "hotbar_selector": { + "y": 38, + "w": 24, + "h": 24 + }, + "cursor": { + "w": 16, + "h": 16 + } + }, + "shade": { + "shade": {} + }, + "gamelogo": { + "gamelogo": {} + } +} \ No newline at end of file diff --git a/android/assets/json/touch_buttons.json b/android/assets/json/touch_buttons.json new file mode 100644 index 0000000..ef5c42e --- /dev/null +++ b/android/assets/json/touch_buttons.json @@ -0,0 +1,44 @@ +{ + "up": { + "x": 26, + "y": -52, + "w": 26, + "h": 26 + }, + "down": { + "x": 26, + "y": -26, + "w": 26, + "h": 26 + }, + "left": { + "x": 0, + "y": -26, + "w": 26, + "h": 26 + }, + "right": { + "x": 52, + "y": -26, + "w": 26, + "h": 26 + }, + "alt": { + "x": 78, + "y": -26, + "w": 26, + "h": 26 + }, + "lmb": { + "x": -52, + "y": -26, + "w": 26, + "h": 26 + }, + "rmb": { + "x": -26, + "y": -26, + "w": 26, + "h": 26 + } +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/GameScreen.java b/core/src/ru/deadsoftware/cavedroid/GameScreen.java index f741dbd..fc82bf2 100644 --- a/core/src/ru/deadsoftware/cavedroid/GameScreen.java +++ b/core/src/ru/deadsoftware/cavedroid/GameScreen.java @@ -6,7 +6,10 @@ import ru.deadsoftware.cavedroid.game.GameItems; import ru.deadsoftware.cavedroid.game.GameProc; import ru.deadsoftware.cavedroid.game.GameSaver; import ru.deadsoftware.cavedroid.menu.MenuRenderer; -import ru.deadsoftware.cavedroid.misc.*; +import ru.deadsoftware.cavedroid.misc.Assets; +import ru.deadsoftware.cavedroid.misc.InputHandlerGame; +import ru.deadsoftware.cavedroid.misc.InputHandlerMenu; +import ru.deadsoftware.cavedroid.misc.Renderer; import ru.deadsoftware.cavedroid.misc.states.AppState; import ru.deadsoftware.cavedroid.misc.states.GameState; import ru.deadsoftware.cavedroid.misc.states.MenuState; @@ -14,21 +17,24 @@ import ru.deadsoftware.cavedroid.misc.states.MenuState; public class GameScreen implements Screen { public static GameProc GP; - public static Renderer RENDERER; public static int FPS; - public static boolean SHOW_DEBUG = true; + public static boolean SHOW_DEBUG = false; public static boolean SHOW_MAP = false; - public static int NEW_GAME_MODE = 0; + private Renderer renderer; private MenuRenderer menuRenderer; + private InputHandlerGame inputHandlerGame; + private InputHandlerMenu inputHandlerMenu; + public GameScreen() { Assets.load(); GameItems.load(); menuRenderer = new MenuRenderer(CaveGame.TOUCH ? 320 : 480); - RENDERER = menuRenderer; - Gdx.input.setInputProcessor(new InputHandlerMenu(menuRenderer)); + inputHandlerMenu = new InputHandlerMenu(menuRenderer); + renderer = menuRenderer; + Gdx.input.setInputProcessor(inputHandlerMenu); } public static float getWidth() { @@ -63,10 +69,12 @@ public class GameScreen implements Screen { break; case LOAD: - GP = GameSaver.load(); GP.resetRenderer(); - RENDERER = GP.renderer; - Gdx.input.setInputProcessor(new InputHandlerGame()); + renderer = GP.renderer; + if (inputHandlerGame == null) { + inputHandlerGame = new InputHandlerGame(); + } + Gdx.input.setInputProcessor(inputHandlerGame); CaveGame.APP_STATE = AppState.GAME; CaveGame.GAME_STATE = GameState.PLAY; break; @@ -76,12 +84,11 @@ public class GameScreen implements Screen { CaveGame.APP_STATE = AppState.MENU; CaveGame.MENU_STATE = MenuState.MAIN; GP.dispose(); - menuRenderer = new MenuRenderer(CaveGame.TOUCH ? 320 : 480); - RENDERER = menuRenderer; - Gdx.input.setInputProcessor(new InputHandlerMenu(menuRenderer)); + renderer = menuRenderer; + Gdx.input.setInputProcessor(inputHandlerMenu); break; } - RENDERER.render(); + renderer.render(); } @Override @@ -89,12 +96,12 @@ public class GameScreen implements Screen { switch (CaveGame.APP_STATE) { case MENU: menuRenderer = new MenuRenderer(CaveGame.TOUCH ? 320 : 480); - Gdx.input.setInputProcessor(new InputHandlerMenu(menuRenderer)); - RENDERER = menuRenderer; + Gdx.input.setInputProcessor(inputHandlerMenu); + renderer = menuRenderer; break; case GAME: GP.resetRenderer(); - RENDERER = GP.renderer; + renderer = GP.renderer; break; } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index 13b2fbf..32672ed 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -1,13 +1,14 @@ package ru.deadsoftware.cavedroid.game; import com.badlogic.gdx.Input; +import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.utils.TimeUtils; import ru.deadsoftware.cavedroid.CaveGame; import ru.deadsoftware.cavedroid.GameScreen; import ru.deadsoftware.cavedroid.game.mobs.Pig; +import ru.deadsoftware.cavedroid.misc.Assets; import ru.deadsoftware.cavedroid.misc.ControlMode; import ru.deadsoftware.cavedroid.misc.states.AppState; -import ru.deadsoftware.cavedroid.misc.Assets; import ru.deadsoftware.cavedroid.misc.states.GameState; import static ru.deadsoftware.cavedroid.GameScreen.GP; @@ -18,11 +19,13 @@ public class GameInput { return GameItems.isFluid(GP.world.getForeMap(GP.player.getMapX(), GP.player.getLowerMapY())); } - private boolean insideCreativeInv(int screenX, int screenY) { - return (screenX > GP.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 && - screenX < GP.renderer.getWidth() / 2 + Assets.creativeInv.getRegionWidth() / 2 && - screenY > GP.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 && - screenY < GP.renderer.getHeight() / 2 + Assets.creativeInv.getRegionHeight() / 2); + @SuppressWarnings("IntegerDivisionInFloatingPointContext") + private boolean insideCreativeInv(float screenX, float screenY) { + TextureRegion creative = Assets.textureRegions.get("creative"); + return (screenX > GP.renderer.getWidth() / 2 - creative.getRegionWidth() / 2 && + screenX < GP.renderer.getWidth() / 2 + creative.getRegionWidth() / 2 && + screenY > GP.renderer.getHeight() / 2 - creative.getRegionHeight() / 2 && + screenY < GP.renderer.getHeight() / 2 + creative.getRegionHeight() / 2); } private void wasdPressed(int keycode) { @@ -143,23 +146,25 @@ public class GameInput { } } - public void touchDown(int screenX, int screenY, int button) { + public void touchDown(float touchX, float touchY, int button) { GP.touchDownTime = TimeUtils.millis(); GP.isTouchDown = true; GP.touchDownBtn = button; - GP.touchDownX = screenX; - GP.touchDownY = screenY; + GP.touchDownX = touchX; + GP.touchDownY = touchY; } - public void touchUp(int screenX, int screenY, int button) { + public void touchUp(float screenX, float screenY, int button) { if (CaveGame.TOUCH && GP.isKeyDown) { keyUp(GP.keyDownCode); GP.isKeyDown = false; } + TextureRegion hotbar = Assets.textureRegions.get("hotbar"); + TextureRegion creative = Assets.textureRegions.get("creative"); if (GP.isTouchDown) { if (CaveGame.GAME_STATE == GameState.CREATIVE_INV && insideCreativeInv(screenX, screenY)) { - int ix = (int) (screenX - (GP.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 + 8)) / 18; - int iy = (int) (screenY - (GP.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 + 18)) / 18; + int ix = (int) (screenX - (GP.renderer.getWidth() / 2 - creative.getRegionWidth() / 2 + 8)) / 18; + int iy = (int) (screenY - (GP.renderer.getHeight() / 2 - creative.getRegionHeight() / 2 + 18)) / 18; int item = GP.creativeScroll * 8 + (ix + iy * 8); if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) item = -1; if (item >= 0 && item < GameItems.getItemsSize()) { @@ -168,10 +173,10 @@ public class GameInput { } } else if (CaveGame.GAME_STATE == GameState.CREATIVE_INV) { CaveGame.GAME_STATE = GameState.PLAY; - } else if (screenY < Assets.invBar.getRegionHeight() && - screenX > GP.renderer.getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 && - screenX < GP.renderer.getWidth() / 2 + (float) Assets.invBar.getRegionWidth() / 2) { - GP.player.slot = (int) ((screenX - (GP.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2)) / 20); + } else if (screenY < hotbar.getRegionHeight() && + screenX > GP.renderer.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 && + screenX < GP.renderer.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2) { + GP.player.slot = (int) ((screenX - (GP.renderer.getWidth() / 2 - hotbar.getRegionWidth() / 2)) / 20); } else if (button == Input.Buttons.RIGHT) { GP.useItem(GP.curX, GP.curY, GP.player.inventory[GP.player.slot], false); @@ -182,7 +187,7 @@ public class GameInput { GP.isTouchDown = false; } - public void touchDragged(int screenX, int screenY) { + public void touchDragged(float screenX, float screenY) { if (CaveGame.GAME_STATE == GameState.CREATIVE_INV && Math.abs(screenY - GP.touchDownY) > 16) { if (insideCreativeInv(screenX, screenY)) { GP.creativeScroll -= (screenY - GP.touchDownY) / 16; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java index da43077..82c4b33 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java @@ -4,10 +4,10 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.utils.ArrayMap; -import com.badlogic.gdx.utils.JsonReader; import com.badlogic.gdx.utils.JsonValue; import ru.deadsoftware.cavedroid.game.objects.Block; import ru.deadsoftware.cavedroid.game.objects.Item; +import ru.deadsoftware.cavedroid.misc.Assets; import java.util.HashMap; @@ -19,87 +19,85 @@ public class GameItems { private static ArrayMap blocks = new ArrayMap<>(); private static ArrayMap items = new ArrayMap<>(); - public static boolean isFluid(int id) { + static boolean isFluid(int id) { return getBlock(id).isFluid(); } - public static boolean isWater(int id) { + static boolean isWater(int id) { return getBlock(id).getMeta().equals("water"); } - public static boolean isLava(int id) { + static boolean isLava(int id) { return getBlock(id).getMeta().equals("lava"); } - public static boolean isSlab(int id) { + static boolean isSlab(int id) { return getBlock(id).getMeta().equals("slab"); } - public static boolean fluidCanFlowThere(int thisId, int thatId) { + static boolean fluidCanFlowThere(int thisId, int thatId) { return thatId == 0 || (!getBlock(thatId).hasCollision() && !isFluid(thatId)) || (isWater(thisId) && isWater(thatId) && thisId < thatId) || (isLava(thisId) && isLava(thatId) && thisId < thatId); } - public static Block getBlock(int id) { + static Block getBlock(int id) { return blocks.getValueAt(id); } - public static Item getItem(int id) { + static Item getItem(int id) { return items.getValueAt(id); } - public static Block getBlock(String key) { + static Block getBlock(String key) { return blocks.getValueAt(blocksIds.get(key)); } - public static Item getItem(String key) { + static Item getItem(String key) { return items.getValueAt(itemsIds.get(key)); } - public static int getBlockId(String key) { + static int getBlockId(String key) { return blocksIds.get(key); } - public static int getItemId(String key) { + static int getItemId(String key) { return itemsIds.get(key); } - public static String getBlockKey(int id) { + static String getBlockKey(int id) { return blocks.getKeyAt(id); } - public static String getItemKey(int id) { + static String getItemKey(int id) { return items.getKeyAt(id); } - public static int getBlockIdByItemId(int id) { + static int getBlockIdByItemId(int id) { return getBlockId(items.getKeyAt(id)); } - public static int getBlocksSize() { + static int getBlocksSize() { return blocks.size; } - public static int getItemsSize() { + static int getItemsSize() { return items.size; } - public static Sprite getBlockTex(int id) { + static Sprite getBlockTex(int id) { return getBlock(id).getTex(); } - public static Sprite getItemTex(int id) { + static Sprite getItemTex(int id) { if (items.getValueAt(id).getType().equals("block")) return getBlockTex(id); else return getItem(id).getTex(); } public static void load() { - JsonValue json = new JsonReader().parse(Gdx.files.internal("game_items.json")); - JsonValue block = json.child.child; - JsonValue item = json.child.next.child; - while (block != null) { - String key = block.name; + JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/game_items.json")); + for (JsonValue block = json.get("blocks").child(); block != null; block = block.next()) { + String key = block.name(); int left = block.has("left") ? block.getInt("left") : 0; int right = block.has("right") ? block.getInt("right") : 0; int top = block.has("top") ? block.getInt("top") : 0; @@ -133,10 +131,9 @@ public class GameItems { blocksIds.put(key, blocks.size); blocks.put(key, newBlock); - block = block.next(); } - while (item != null) { - String key = item.name; + for (JsonValue item = json.get("items").child(); item != null; item = item.next()) { + String key = item.name(); String name = item.has("name") ? item.getString("name") : key; String type = item.has("type") ? item.getString("type") : "item"; String texture = item.has("texture") ? item.getString("texture") : key; @@ -144,7 +141,6 @@ public class GameItems { new Sprite(new Texture(Gdx.files.internal("textures/items/" + texture + ".png"))); itemsIds.put(key, items.size); items.put(key, new Item(name, type, sprite)); - item = item.next(); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java index 4de9790..fdfef63 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java @@ -2,8 +2,10 @@ package ru.deadsoftware.cavedroid.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; +import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.utils.TimeUtils; +import com.google.common.collect.Range; import ru.deadsoftware.cavedroid.CaveGame; import ru.deadsoftware.cavedroid.GameScreen; import ru.deadsoftware.cavedroid.game.mobs.FallingGravel; @@ -11,8 +13,8 @@ import ru.deadsoftware.cavedroid.game.mobs.FallingSand; import ru.deadsoftware.cavedroid.game.mobs.Mob; import ru.deadsoftware.cavedroid.game.objects.Drop; import ru.deadsoftware.cavedroid.game.objects.Player; -import ru.deadsoftware.cavedroid.misc.ControlMode; import ru.deadsoftware.cavedroid.misc.Assets; +import ru.deadsoftware.cavedroid.misc.ControlMode; import ru.deadsoftware.cavedroid.misc.states.GameState; import java.io.Serializable; @@ -41,8 +43,11 @@ public class GameProc implements Serializable, Disposable { public ArrayList mobs; ArrayList drops; - public boolean isTouchDown, isKeyDown; - public int touchDownX, touchDownY, touchDownBtn, keyDownCode; + public boolean isKeyDown; + public int keyDownCode; + boolean isTouchDown; + float touchDownX, touchDownY; + int touchDownBtn; long touchDownTime; int curX, curY; @@ -211,21 +216,22 @@ public class GameProc implements Serializable, Disposable { } } + private boolean insideHotbar(float x, float y) { + TextureRegion hotbar = Assets.textureRegions.get("hotbar"); + return y < hotbar.getRegionHeight() && + Range.open(renderer.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2, + renderer.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2).contains(x); + } + private void holdMB() { - switch (touchDownBtn) { - case Input.Buttons.RIGHT: - useItem(curX, curY, player.inventory[player.slot], true); + if (touchDownBtn == Input.Buttons.RIGHT) { + useItem(curX, curY, player.inventory[player.slot], true); + isTouchDown = false; + } else { + if (insideHotbar(touchDownX, touchDownY)) { + CaveGame.GAME_STATE = GameState.CREATIVE_INV; isTouchDown = false; - break; - case Input.Buttons.LEFT: - break; - default: - if (touchDownY < Assets.invBar.getRegionHeight() && - touchDownX > renderer.getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 && - touchDownX < renderer.getWidth() / 2 + (float) Assets.invBar.getRegionWidth() / 2) { - CaveGame.GAME_STATE= GameState.CREATIVE_INV; - isTouchDown = false; - } + } } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index c128d85..3899946 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -3,16 +3,19 @@ package ru.deadsoftware.cavedroid.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Rectangle; import ru.deadsoftware.cavedroid.CaveGame; import ru.deadsoftware.cavedroid.GameScreen; import ru.deadsoftware.cavedroid.game.mobs.Mob; import ru.deadsoftware.cavedroid.game.objects.Drop; -import ru.deadsoftware.cavedroid.misc.Assets; import ru.deadsoftware.cavedroid.misc.ControlMode; import ru.deadsoftware.cavedroid.misc.Renderer; import static ru.deadsoftware.cavedroid.GameScreen.GP; +import static ru.deadsoftware.cavedroid.misc.Assets.guiMap; +import static ru.deadsoftware.cavedroid.misc.Assets.textureRegions; public class GameRenderer extends Renderer { @@ -31,11 +34,9 @@ public class GameRenderer extends Renderer { private void drawWreck(int bl) { if (GP.blockDmg > 0) { - spriter.draw(Assets.wreck[ - 10 * GP.blockDmg / - GameItems.getBlock(bl).getHp()], - GP.curX * 16 - getCamX(), - GP.curY * 16 - getCamY()); + int index = 10 * GP.blockDmg / GameItems.getBlock(bl).getHp(); + String key = "break_" + index; + spriter.draw(textureRegions.get(key), GP.curX * 16 - getCamX(), GP.curY * 16 - getCamY()); } } @@ -105,10 +106,11 @@ public class GameRenderer extends Renderer { @SuppressWarnings("IntegerDivisionInFloatingPointContext") private void drawCreative() { - float x = getWidth() / 2 - (float) Assets.creativeInv.getRegionWidth() / 2; - float y = getHeight() / 2 - (float) Assets.creativeInv.getRegionHeight() / 2; - spriter.draw(Assets.creativeInv, x, y); - spriter.draw(Assets.creativeScr, x + 156, + TextureRegion creative = textureRegions.get("creative"); + float x = getWidth() / 2 - (float) creative.getRegionWidth() / 2; + float y = getHeight() / 2 - (float) creative.getRegionHeight() / 2; + spriter.draw(creative, x, y); + spriter.draw(textureRegions.get("handle"), x + 156, y + 18 + (GP.creativeScroll * (72f / GameProc.MAX_CREATIVE_SCROLL))); for (int i = GP.creativeScroll * 8; i < GP.creativeScroll * 8 + 40; i++) { if (i > 0 && i < GameItems.getItemsSize()) @@ -126,52 +128,54 @@ public class GameRenderer extends Renderer { if (GP.player.inventory[i] > 0) if (GameItems.getItem(GP.player.inventory[i]).isBlock()) { spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(GP.player.inventory[i])).getTex(), - x + 8 + i * 18, y + Assets.creativeInv.getRegionHeight() - 24); + x + 8 + i * 18, y + creative.getRegionHeight() - 24); } else { spriter.draw(GameItems.getItem(GP.player.inventory[i]).getTex(), - x + 8 + i * 18, y + Assets.creativeInv.getRegionHeight() - 24); + x + 8 + i * 18, y + creative.getRegionHeight() - 24); } } } private void drawGUI() { + TextureRegion cursor = textureRegions.get("cursor"); + TextureRegion hotbar = textureRegions.get("hotbar"); + TextureRegion hotbarSelector = textureRegions.get("hotbar_selector"); + if (GP.world.hasForeAt(GP.curX, GP.curY) || GP.world.hasBackAt(GP.curX, GP.curY) || GP.controlMode == ControlMode.CURSOR || !CaveGame.TOUCH) - spriter.draw(Assets.guiCur, + spriter.draw(cursor, GP.curX * 16 - getCamX(), GP.curY * 16 - getCamY()); - spriter.draw(Assets.invBar, getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2, 0); + spriter.draw(hotbar, getWidth() / 2 - (float) hotbar.getRegionWidth() / 2, 0); for (int i = 0; i < 9; i++) { if (GP.player.inventory[i] > 0) { if (GameItems.getItem(GP.player.inventory[i]).isBlock()) { spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(GP.player.inventory[i])).getTex(), - getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 + 3 + i * 20, + getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20, 3); } else { spriter.draw(GameItems.getItem(GP.player.inventory[i]).getTex(), - getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 + 3 + i * 20, + getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20, 3); } } } - spriter.draw(Assets.invBarCur, - getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 - 1 + 20 * GP.player.slot, + spriter.draw(hotbarSelector, + getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 - 1 + 20 * GP.player.slot, -1); } private void drawTouchGui() { - spriter.draw(Assets.touchArrows[0], 26, getHeight() - 52); - spriter.draw(Assets.touchArrows[1], 0, getHeight() - 26); - spriter.draw(Assets.touchArrows[2], 26, getHeight() - 26); - spriter.draw(Assets.touchArrows[3], 52, getHeight() - 26); - spriter.draw(Assets.touchLMB, getWidth() - 52, getHeight() - 26); - spriter.draw(Assets.touchRMB, getWidth() - 26, getHeight() - 26); - spriter.draw(Assets.touchMode, 78, getHeight() - 26); + for (int i = 0; i < guiMap.size; i++) { + Rectangle touchKey = guiMap.getValueAt(i); + spriter.draw(textureRegions.get(guiMap.getKeyAt(i)), + touchKey.x, touchKey.y, touchKey.width, touchKey.height); + } if (GP.controlMode == ControlMode.CURSOR) { - spriter.draw(Assets.shade, 83, getHeight() - 21); + spriter.draw(textureRegions.get("shade"), 83, getHeight() - 21); } } @@ -231,9 +235,9 @@ public class GameRenderer extends Renderer { shaper.end(); //================= } - spriter.begin(); if (GameScreen.SHOW_DEBUG) { + spriter.begin(); drawString("FPS: " + GameScreen.FPS, 0, 0); drawString("X: " + (int) (GP.player.pos.x / 16), 0, 10); drawString("Y: " + (int) (GP.player.pos.y / 16), 0, 20); @@ -244,8 +248,8 @@ public class GameRenderer extends Renderer { drawString("Block: " + GameItems.getBlockKey(GP.world.getForeMap(GP.curX, GP.curY)), 0, 70); drawString("Hand: " + GameItems.getItemKey(GP.player.inventory[GP.player.slot]), 0, 80); drawString("Game mode: " + GP.player.gameMode, 0, 90); + spriter.end(); } - spriter.end(); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Player.java b/core/src/ru/deadsoftware/cavedroid/game/objects/Player.java index 0f12f18..1c12f03 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Player.java @@ -3,8 +3,6 @@ package ru.deadsoftware.cavedroid.game.objects; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; -import ru.deadsoftware.cavedroid.game.GameItems; -import ru.deadsoftware.cavedroid.game.GameWorld; import ru.deadsoftware.cavedroid.game.mobs.Mob; import ru.deadsoftware.cavedroid.misc.Assets; @@ -58,36 +56,36 @@ public class Player extends Mob implements Serializable { @Override public void draw(SpriteBatch spriteBatch, float x, float y) { - if (mov.x != 0 || Assets.plSprite[0][2].getRotation() != 0) { - Assets.plSprite[0][2].rotate(animDelta); - Assets.plSprite[1][2].rotate(-animDelta); - Assets.plSprite[0][3].rotate(-animDelta); - Assets.plSprite[1][3].rotate(animDelta); + if (mov.x != 0 || Assets.playerSprite[0][2].getRotation() != 0) { + Assets.playerSprite[0][2].rotate(animDelta); + Assets.playerSprite[1][2].rotate(-animDelta); + Assets.playerSprite[0][3].rotate(-animDelta); + Assets.playerSprite[1][3].rotate(animDelta); } else { - Assets.plSprite[0][2].setRotation(0); - Assets.plSprite[1][2].setRotation(0); - Assets.plSprite[0][3].setRotation(0); - Assets.plSprite[1][3].setRotation(0); + Assets.playerSprite[0][2].setRotation(0); + Assets.playerSprite[1][2].setRotation(0); + Assets.playerSprite[0][3].setRotation(0); + Assets.playerSprite[1][3].setRotation(0); } - if (Assets.plSprite[0][2].getRotation() >= 60 || Assets.plSprite[0][2].getRotation() <= -60) + if (Assets.playerSprite[0][2].getRotation() >= 60 || Assets.playerSprite[0][2].getRotation() <= -60) animDelta = -animDelta; //back hand - Assets.plSprite[1][2].setPosition(x - 6, y); - Assets.plSprite[1][2].draw(spriteBatch); + Assets.playerSprite[1][2].setPosition(x - 6, y); + Assets.playerSprite[1][2].draw(spriteBatch); //back leg - Assets.plSprite[1][3].setPosition(x - 6, y + 10); - Assets.plSprite[1][3].draw(spriteBatch); + Assets.playerSprite[1][3].setPosition(x - 6, y + 10); + Assets.playerSprite[1][3].draw(spriteBatch); //front leg - Assets.plSprite[0][3].setPosition(x - 6, y + 10); - Assets.plSprite[0][3].draw(spriteBatch); + Assets.playerSprite[0][3].setPosition(x - 6, y + 10); + Assets.playerSprite[0][3].draw(spriteBatch); //head - spriteBatch.draw(Assets.plSprite[getDirection()][0], x - 2, y - 2); + spriteBatch.draw(Assets.playerSprite[getDirection()][0], x - 2, y - 2); //body - spriteBatch.draw(Assets.plSprite[getDirection()][1], x - 2, y + 8); + spriteBatch.draw(Assets.playerSprite[getDirection()][1], x - 2, y + 8); //front hand - Assets.plSprite[0][2].setPosition(x - 6, y); - Assets.plSprite[0][2].draw(spriteBatch); + Assets.playerSprite[0][2].setPosition(x - 6, y); + Assets.playerSprite[0][2].draw(spriteBatch); } @Override diff --git a/core/src/ru/deadsoftware/cavedroid/menu/MenuRenderer.java b/core/src/ru/deadsoftware/cavedroid/menu/MenuRenderer.java index ee62620..2ae84db 100644 --- a/core/src/ru/deadsoftware/cavedroid/menu/MenuRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/menu/MenuRenderer.java @@ -1,23 +1,21 @@ package ru.deadsoftware.cavedroid.menu; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.utils.Array; import org.jetbrains.annotations.NotNull; import ru.deadsoftware.cavedroid.CaveGame; import ru.deadsoftware.cavedroid.GameScreen; -import ru.deadsoftware.cavedroid.game.GameItems; import ru.deadsoftware.cavedroid.game.GameProc; import ru.deadsoftware.cavedroid.game.GameSaver; import ru.deadsoftware.cavedroid.menu.objects.Button; -import ru.deadsoftware.cavedroid.misc.InputHandlerGame; -import ru.deadsoftware.cavedroid.misc.states.AppState; -import ru.deadsoftware.cavedroid.misc.Assets; import ru.deadsoftware.cavedroid.misc.Renderer; +import ru.deadsoftware.cavedroid.misc.states.AppState; import ru.deadsoftware.cavedroid.misc.states.GameState; import ru.deadsoftware.cavedroid.misc.states.MenuState; import static ru.deadsoftware.cavedroid.GameScreen.GP; -import static ru.deadsoftware.cavedroid.GameScreen.RENDERER; +import static ru.deadsoftware.cavedroid.misc.Assets.*; public class MenuRenderer extends Renderer { @@ -25,14 +23,14 @@ public class MenuRenderer extends Renderer { public Array