From 7370b011158c7f9a1216dbf76de5acd9d277b7e9 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Tue, 11 Sep 2018 00:02:01 +0700 Subject: [PATCH] Add items --- .../cavecraft/game/GameInput.java | 6 +- .../deadsoftware/cavecraft/game/GameProc.java | 12 +- .../cavecraft/game/GameRenderer.java | 21 ++-- .../ru/deadsoftware/cavecraft/game/Items.java | 115 ++++++++++++++++++ .../cavecraft/game/objects/Item.java | 29 +++++ 5 files changed, 168 insertions(+), 15 deletions(-) create mode 100644 core/src/ru/deadsoftware/cavecraft/game/objects/Item.java diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInput.java b/core/src/ru/deadsoftware/cavecraft/game/GameInput.java index 7a66d99..672c4c9 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInput.java @@ -144,7 +144,7 @@ public class GameInput { int iy = (int) (screenY - (gameProc.renderer.camera.viewportHeight / 2 - Assets.creativeInv.getRegionHeight() / 2 + 18)) / 18; int item = gameProc.creativeScroll*8+(ix + iy * 8); if (ix>=8 || ix<0 || iy<0 || iy>=5) item=-1; - if (item >= 0 && item < Items.BLOCKS.size) { + if (item >= 0 && item < Items.ITEMS.size()) { for (int i = 8; i > 0; i--) { gameProc.player.inventory[i] = gameProc.player.inventory[i - 1]; } @@ -157,8 +157,8 @@ public class GameInput { screenX 0) { gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 0); diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index 49fe705..45c62b8 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -59,7 +59,7 @@ public class GameProc implements Serializable{ renderer = new GameRenderer(this,480, 480*((float)GameScreen.getHeight()/GameScreen.getWidth())); } - maxCreativeScroll = Items.BLOCKS.size/8; + maxCreativeScroll = Items.ITEMS.size()/8; GameSaver.save(this); } @@ -373,6 +373,13 @@ public class GameProc implements Serializable{ } } + public void useItem(int x, int y, int id, boolean bg) { + if (Items.ITEMS.get(id).getType()==0) { + if (!bg) world.placeToForeground(x, y, Items.ITEMS.get(id).getBlock()); + else world.placeToBackground(x, y, Items.ITEMS.get(id).getBlock()); + } + } + public void update(float delta) { RUN_TIME += delta; @@ -406,8 +413,7 @@ public class GameProc implements Serializable{ if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) { if (touchDownButton== Input.Buttons.RIGHT) { - world.placeToBackground(cursorX, cursorY, - player.inventory[invSlot]); + useItem(cursorX, cursorY, player.inventory[invSlot], true); } else if (touchDownY< Assets.invBar.getRegionHeight() && touchDownX>renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 && touchDownX0 && i0 && i0) - spriteBatch.draw(Assets.blockTextures[Items.BLOCKS.getValueAt(gameProc.player.inventory[i]).getTexture()], - x+8+i*18, y+Assets.creativeInv.getRegionHeight()-24); + if (Items.ITEMS.get(i).getType() == 0) + spriteBatch.draw(Assets.blockTextures[Items.ITEMS.get(i).getTexture()], + x+8+i*18, y+Assets.creativeInv.getRegionHeight()-24); } } @@ -149,9 +151,10 @@ public class GameRenderer extends Renderer { 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) { - spriteBatch.draw(Assets.blockTextures[Items.BLOCKS.getValueAt(gameProc.player.inventory[i]).getTexture()], - camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2+3+i*20, - 3); + if (Items.ITEMS.get(gameProc.player.inventory[i]).getType()==0) + spriteBatch.draw(Assets.blockTextures[Items.ITEMS.get(gameProc.player.inventory[i]).getTexture()], + camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2+3+i*20, + 3); } } spriteBatch.draw(Assets.invBarCur, diff --git a/core/src/ru/deadsoftware/cavecraft/game/Items.java b/core/src/ru/deadsoftware/cavecraft/game/Items.java index cb2f736..2a8c532 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/Items.java +++ b/core/src/ru/deadsoftware/cavecraft/game/Items.java @@ -2,10 +2,14 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.utils.ArrayMap; import ru.deadsoftware.cavecraft.game.objects.Block; +import ru.deadsoftware.cavecraft.game.objects.Item; + +import java.util.ArrayList; public class Items { public static ArrayMap BLOCKS = new ArrayMap(); + public static ArrayList ITEMS = new ArrayList(); public static boolean isFluid(int bl) { return (bl == 8 || bl == 9 || bl == 60 || bl == 61 || bl == 62 || bl == 63 || bl == 64 || bl == 65 || bl == 66 || bl == 67); @@ -23,6 +27,116 @@ public class Items { return (bl == 51 || bl == 53 || bl == 54 || bl == 55 || bl == 56 || bl == 58); } + public static void loadItems() { + //0 + ITEMS.add(new Item("Stone", 0, 0, 1)); + //1 + ITEMS.add(new Item("Grass", 1, 0, 2)); + //2 + ITEMS.add(new Item("Dirt", 2, 0, 3)); + //3 + ITEMS.add(new Item("Cobblestone", 3, 0, 4)); + //4 + ITEMS.add(new Item("Planks", 4, 0, 5)); + //5 + ITEMS.add(new Item("Sapling", 5, 0, 6)); + //6 + ITEMS.add(new Item("Bedrock", 6, 0, 7)); + //7 + ITEMS.add(new Item("Sand", 9, 0, 10)); + //8 + ITEMS.add(new Item("Gravel", 10, 0, 11)); + //9 + ITEMS.add(new Item("Golden Ore", 11, 0, 12)); + //10 + ITEMS.add(new Item("Iron Ore", 12, 0, 13)); + //11 + ITEMS.add(new Item("Coal Ore", 13, 0, 14)); + //12 + ITEMS.add(new Item("Wood", 14, 0, 15)); + //13 + ITEMS.add(new Item("Leaves", 15, 0, 16)); + //14 + ITEMS.add(new Item("Glass", 17, 0, 18)); + //15 + ITEMS.add(new Item("Lapis Ore", 18, 0, 19)); + //16 + ITEMS.add(new Item("Lapis Block", 19, 0, 20)); + //17 + ITEMS.add(new Item("Sandstone", 20, 0, 21)); + //18 + ITEMS.add(new Item("Cobweb", 24, 0, 25)); + //19 + ITEMS.add(new Item("Tall Grass", 25, 0, 26)); + //20 + ITEMS.add(new Item("Dead Bush", 26, 0, 27)); + //21 + ITEMS.add(new Item("Bricks", 27, 0, 28)); + //22 + ITEMS.add(new Item("Dandelion", 28, 0, 29)); + //23 + ITEMS.add(new Item("Rose", 29, 0, 30)); + //24 + ITEMS.add(new Item("Mushroom", 30, 0, 31)); + //25 + ITEMS.add(new Item("Mushroom", 31, 0, 32)); + //26 + ITEMS.add(new Item("White Wool", 32, 0, 33)); + //27 + ITEMS.add(new Item("Orange Wool", 33, 0, 34)); + //28 + ITEMS.add(new Item("Magenta Wool", 34, 0, 35)); + //29 + ITEMS.add(new Item("Light Blue Wool", 35, 0, 36)); + //30 + ITEMS.add(new Item("Yellow Wool", 36, 0, 37)); + //31 + ITEMS.add(new Item("Lime Wool", 37, 0, 38)); + //32 + ITEMS.add(new Item("Pink Wool", 38, 0, 39)); + //33 + ITEMS.add(new Item("Gray Wool", 39, 0, 40)); + //34 + ITEMS.add(new Item("Light Gray Wool", 40, 0, 41)); + //35 + ITEMS.add(new Item("Cyan Wool", 41, 0, 42)); + //36 + ITEMS.add(new Item("Purple Wool", 42, 0, 43)); + //37 + ITEMS.add(new Item("Blue Wool", 43, 0, 44)); + //38 + ITEMS.add(new Item("Brown Wool", 44, 0, 45)); + //39 + ITEMS.add(new Item("Green Wool", 45, 0, 46)); + //40 + ITEMS.add(new Item("Red Wool", 46, 0, 47)); + //41 + ITEMS.add(new Item("Black Wool", 47, 0, 48)); + //42 + ITEMS.add(new Item("Golden Block", 48, 0, 49)); + //43 + ITEMS.add(new Item("Iron Block", 49, 0, 50)); + //44 + ITEMS.add(new Item("Stone Slab", 50, 0, 51)); + //45 + ITEMS.add(new Item("Sandstone Slab", 52, 0, 53)); + //46 + ITEMS.add(new Item("Wooden Slab", 53, 0, 54)); + //47 + ITEMS.add(new Item("Cobblestone Slab", 54, 0, 55)); + //48 + ITEMS.add(new Item("Brick Slab", 55, 0, 56)); + //49 + ITEMS.add(new Item("Stone Brick", 64, 0, 57)); + //50 + ITEMS.add(new Item("Stone Brick Slab", 56, 0, 58)); + //51 + ITEMS.add(new Item("Cactus", 57, 0, 59)); + //52 + ITEMS.add(new Item("Obsidian", 65, 0, 68)); + + } + public static void loadBlocks() { //0 BLOCKS.put("none", null); @@ -166,6 +280,7 @@ public class Items { public static void load() { loadBlocks(); + loadItems(); } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Item.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Item.java new file mode 100644 index 0000000..5e6fdeb --- /dev/null +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Item.java @@ -0,0 +1,29 @@ +package ru.deadsoftware.cavecraft.game.objects; + +public class Item { + + private int texture, type; + private int block; + private String name; + + public Item(String name, int texture, int type, int block) { + this.name = name; + this.texture = texture; + this.type = type; + this.block = block; + } + + public int getTexture() { + return texture; + } + public int getType() { + return type; + } + public int getBlock() { + return block; + } + public String getName() { + return name; + } + +} -- 2.29.2