From 1cc111dfe31e7f7098e01afeaa025e19f91aa624 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Fri, 14 Sep 2018 17:20:34 +0700 Subject: [PATCH] Add item in player's hand --- android/assets/items.png | Bin 0 -> 1349 bytes .../cavecraft/game/GamePhysics.java | 1 - .../deadsoftware/cavecraft/game/GameProc.java | 2 +- .../cavecraft/game/GameRenderer.java | 64 ++++++++-- .../ru/deadsoftware/cavecraft/game/Items.java | 116 ++++++++++-------- .../cavecraft/game/objects/Item.java | 7 +- .../cavecraft/game/objects/Player.java | 9 -- .../deadsoftware/cavecraft/misc/Assets.java | 19 ++- .../cavecraft/desktop/DesktopLauncher.java | 7 +- 9 files changed, 148 insertions(+), 77 deletions(-) create mode 100644 android/assets/items.png diff --git a/android/assets/items.png b/android/assets/items.png new file mode 100644 index 0000000000000000000000000000000000000000..cecb5e8cc63369f91734298023b6aa0a34898973 GIT binary patch literal 1349 zcmd^8`7_%I6#r_C)LlD0Y+Wn6R3)0F2204+d`VGhC!$oTHrneNsVRLo8_h@(<$v1GF(nL*IF`bCnB@n`IN^XBvUyf^daeSUa(H*r`)y$gB( z02umR1MvW$rJ-8DDIE=jC%zTuH8-o0Hda((h6_3HPtj> z$$o)8uzz)pP9bdHLr5$DXbb(otAvy}`K&0mjA{T`{o46IBcn_^f0m9_ z;9O9&AMvkxUh8v;7lPGUKZGPWp-A;zPvCdu-Zrt>b|&B#UG1TQX5BmOEqU*|^iyYM z-4e!ApAN`(dfZ(%lB)A%O3w+|)){!tn+P-fF4XJ6J9}Zw*vROz6pO{0jT;*nAf=pe zu8_h|s;UPMHzJuLN`Du9p2&E#cGvo!FFR~+K*nWrL{Y2*mqDRWzP0emaNFPCPn5uP zbc7NCdue&NgsWKH+pCw$Ff0cvW;sGM-|G<3-G0$axfV<}Ze9h3Mlh~wx3J2Zi3k;! z%Y7IfJshDL9v=S4O^RAqp#DCvsqCaKUsiKIu!sQ%`su#LXrLaT)$ zd;a4z0mjLx7+2+1B;mzdrpLqY!0L?XPQ^jPhoyQ^uL>0a_wif3@4*3L5~iDtLZ z4UsA`y+BLzcCc~yiY=&Tdb(;q-z*z zht_~6hu|5vN^AlK#^M5tEKUf{GuZEfAU>b3-+;eFrPHyX zbGRxmFV7pS7mLMqY=I%0DPAX7!}0Tzzm|WO*(O%7SPPdx&|R@8&CN{~3*#kGR8K8J zO#NZ-mSHKc(sv|s=lh4XhtqbLX4?79c(tH$4p(cbS#RM%Lk7?5PSt752ED*F#CT+- zImc~Wm)B&JHp#VDdQ^*T+k^1uT|pDQ|Zv-w};A?;?u|n?pFHrK=0jYx)eRIs9|1LRPMp zrny*7F_lTE!J<7E{?t@m_wID|BlP34-jk+l(Sey9a>13ly$8ho`x?7%O9t}7;r zO^l}k3nsUt!_$=NbBm?}E9KDEG9pTahDItdH~JH$6IA8dE4<#UX;L8gwzWoDvl0-$ y*as1yMD8I0s`wzQwrB2+X3|e+-u{0So4f0 && 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()); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java index 1eef325..ee9d6d4 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavecraft.CaveGame; import ru.deadsoftware.cavecraft.GameScreen; @@ -112,6 +113,26 @@ public class GameRenderer extends Renderer { //body spriteBatch.draw(Assets.playerSprite[pl.dir][1], pl.position.x - camera.position.x - 2, pl.position.y - camera.position.y + 8); + //item in hand + if (pl.inventory[gameProc.invSlot]>0) + switch (Items.ITEMS.get(pl.inventory[gameProc.invSlot]).getType()) { + case 0: + Assets.blockTextures[Items.ITEMS.get(pl.inventory[gameProc.invSlot]).getTexture()].setPosition( + pl.position.x - camera.position.x - 8*MathUtils.sin(MathUtils.degRad*Assets.playerSprite[0][2].getRotation()), + pl.position.y - camera.position.y + 6 + 8*MathUtils.cos(MathUtils.degRad*Assets.playerSprite[0][2].getRotation())); + Assets.blockTextures[Items.ITEMS.get(pl.inventory[gameProc.invSlot]).getTexture()].draw(spriteBatch); + break; + default: + Assets.itemTextures[Items.ITEMS.get(pl.inventory[gameProc.invSlot]).getTexture()].flip((pl.dir==0), false); + Assets.itemTextures[Items.ITEMS.get(pl.inventory[gameProc.invSlot]).getTexture()].setRotation( + -45+pl.dir*90+Assets.playerSprite[0][2].getRotation()); + Assets.itemTextures[Items.ITEMS.get(pl.inventory[gameProc.invSlot]).getTexture()].setPosition( + pl.position.x - camera.position.x -10+(12*pl.dir) - 8*MathUtils.sin(MathUtils.degRad*Assets.playerSprite[0][2].getRotation()), + pl.position.y - camera.position.y + 2 + 8*MathUtils.cos(MathUtils.degRad*Assets.playerSprite[0][2].getRotation())); + Assets.itemTextures[Items.ITEMS.get(pl.inventory[gameProc.invSlot]).getTexture()].draw(spriteBatch); + Assets.itemTextures[Items.ITEMS.get(pl.inventory[gameProc.invSlot]).getTexture()].flip((pl.dir==0), false); + break; + } //front hand Assets.playerSprite[0][2].setPosition( pl.position.x - camera.position.x - 6, @@ -127,16 +148,31 @@ public class GameRenderer extends Renderer { y+18+(gameProc.creativeScroll*(72/gameProc.maxCreativeScroll))); for (int i=gameProc.creativeScroll*8; i0 && i0) - 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); + switch (Items.ITEMS.get(gameProc.player.inventory[i]).getType()) { + case 0: + spriteBatch.draw(Assets.blockTextures[Items.ITEMS.get(gameProc.player.inventory[i]).getTexture()], + x + 8 + i * 18, y + Assets.creativeInv.getRegionHeight() - 24); + break; + case 1: + spriteBatch.draw(Assets.itemTextures[Items.ITEMS.get(gameProc.player.inventory[i]).getTexture()], + x + 8 + i * 18, y + Assets.creativeInv.getRegionHeight() - 24); + break; + } } } @@ -151,10 +187,18 @@ 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) { - 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, + switch (Items.ITEMS.get(gameProc.player.inventory[i]).getType()) { + case 0: + spriteBatch.draw(Assets.blockTextures[Items.ITEMS.get(gameProc.player.inventory[i]).getTexture()], + camera.viewportWidth / 2 - Assets.invBar.getRegionWidth() / 2 + 3 + i * 20, 3); + break; + case 1: + spriteBatch.draw(Assets.itemTextures[Items.ITEMS.get(gameProc.player.inventory[i]).getTexture()], + camera.viewportWidth / 2 - Assets.invBar.getRegionWidth() / 2 + 3 + i * 20, + 3); + break; + } } } 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 2a8c532..db32ad1 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/Items.java +++ b/core/src/ru/deadsoftware/cavecraft/game/Items.java @@ -29,111 +29,123 @@ public class Items { public static void loadItems() { //0 - ITEMS.add(new Item("Stone", 0, 0, 1)); + ITEMS.add(null); //1 - ITEMS.add(new Item("Grass", 1, 0, 2)); + ITEMS.add(new Item("Stone", 0, 0, 1)); //2 - ITEMS.add(new Item("Dirt", 2, 0, 3)); + ITEMS.add(new Item("Grass", 1, 0, 2)); //3 - ITEMS.add(new Item("Cobblestone", 3, 0, 4)); + ITEMS.add(new Item("Dirt", 2, 0, 3)); //4 - ITEMS.add(new Item("Planks", 4, 0, 5)); + ITEMS.add(new Item("Cobblestone", 3, 0, 4)); //5 - ITEMS.add(new Item("Sapling", 5, 0, 6)); + ITEMS.add(new Item("Planks", 4, 0, 5)); //6 - ITEMS.add(new Item("Bedrock", 6, 0, 7)); + ITEMS.add(new Item("Sapling", 5, 0, 6)); //7 - ITEMS.add(new Item("Sand", 9, 0, 10)); + ITEMS.add(new Item("Bedrock", 6, 0, 7)); //8 - ITEMS.add(new Item("Gravel", 10, 0, 11)); + ITEMS.add(new Item("Sand", 9, 0, 10)); //9 - ITEMS.add(new Item("Golden Ore", 11, 0, 12)); + ITEMS.add(new Item("Gravel", 10, 0, 11)); //10 - ITEMS.add(new Item("Iron Ore", 12, 0, 13)); + ITEMS.add(new Item("Golden Ore", 11, 0, 12)); //11 - ITEMS.add(new Item("Coal Ore", 13, 0, 14)); + ITEMS.add(new Item("Iron Ore", 12, 0, 13)); //12 - ITEMS.add(new Item("Wood", 14, 0, 15)); + ITEMS.add(new Item("Coal Ore", 13, 0, 14)); //13 - ITEMS.add(new Item("Leaves", 15, 0, 16)); + ITEMS.add(new Item("Wood", 14, 0, 15)); //14 - ITEMS.add(new Item("Glass", 17, 0, 18)); + ITEMS.add(new Item("Leaves", 15, 0, 16)); //15 - ITEMS.add(new Item("Lapis Ore", 18, 0, 19)); + ITEMS.add(new Item("Glass", 17, 0, 18)); //16 - ITEMS.add(new Item("Lapis Block", 19, 0, 20)); + ITEMS.add(new Item("Lapis Ore", 18, 0, 19)); //17 - ITEMS.add(new Item("Sandstone", 20, 0, 21)); + ITEMS.add(new Item("Lapis Block", 19, 0, 20)); //18 - ITEMS.add(new Item("Cobweb", 24, 0, 25)); + ITEMS.add(new Item("Sandstone", 20, 0, 21)); //19 - ITEMS.add(new Item("Tall Grass", 25, 0, 26)); + ITEMS.add(new Item("Cobweb", 24, 0, 25)); //20 - ITEMS.add(new Item("Dead Bush", 26, 0, 27)); + ITEMS.add(new Item("Tall Grass", 25, 0, 26)); //21 - ITEMS.add(new Item("Bricks", 27, 0, 28)); + ITEMS.add(new Item("Dead Bush", 26, 0, 27)); //22 - ITEMS.add(new Item("Dandelion", 28, 0, 29)); + ITEMS.add(new Item("Bricks", 27, 0, 28)); //23 - ITEMS.add(new Item("Rose", 29, 0, 30)); + ITEMS.add(new Item("Dandelion", 28, 0, 29)); //24 - ITEMS.add(new Item("Mushroom", 30, 0, 31)); + ITEMS.add(new Item("Rose", 29, 0, 30)); //25 - ITEMS.add(new Item("Mushroom", 31, 0, 32)); + ITEMS.add(new Item("Mushroom", 30, 0, 31)); //26 - ITEMS.add(new Item("White Wool", 32, 0, 33)); + ITEMS.add(new Item("Mushroom", 31, 0, 32)); //27 - ITEMS.add(new Item("Orange Wool", 33, 0, 34)); + ITEMS.add(new Item("White Wool", 32, 0, 33)); //28 - ITEMS.add(new Item("Magenta Wool", 34, 0, 35)); + ITEMS.add(new Item("Orange Wool", 33, 0, 34)); //29 - ITEMS.add(new Item("Light Blue Wool", 35, 0, 36)); + ITEMS.add(new Item("Magenta Wool", 34, 0, 35)); //30 - ITEMS.add(new Item("Yellow Wool", 36, 0, 37)); + ITEMS.add(new Item("Light Blue Wool", 35, 0, 36)); //31 - ITEMS.add(new Item("Lime Wool", 37, 0, 38)); + ITEMS.add(new Item("Yellow Wool", 36, 0, 37)); //32 - ITEMS.add(new Item("Pink Wool", 38, 0, 39)); + ITEMS.add(new Item("Lime Wool", 37, 0, 38)); //33 - ITEMS.add(new Item("Gray Wool", 39, 0, 40)); + ITEMS.add(new Item("Pink Wool", 38, 0, 39)); //34 - ITEMS.add(new Item("Light Gray Wool", 40, 0, 41)); + ITEMS.add(new Item("Gray Wool", 39, 0, 40)); //35 - ITEMS.add(new Item("Cyan Wool", 41, 0, 42)); + ITEMS.add(new Item("Light Gray Wool", 40, 0, 41)); //36 - ITEMS.add(new Item("Purple Wool", 42, 0, 43)); + ITEMS.add(new Item("Cyan Wool", 41, 0, 42)); //37 - ITEMS.add(new Item("Blue Wool", 43, 0, 44)); + ITEMS.add(new Item("Purple Wool", 42, 0, 43)); //38 - ITEMS.add(new Item("Brown Wool", 44, 0, 45)); + ITEMS.add(new Item("Blue Wool", 43, 0, 44)); //39 - ITEMS.add(new Item("Green Wool", 45, 0, 46)); + ITEMS.add(new Item("Brown Wool", 44, 0, 45)); //40 - ITEMS.add(new Item("Red Wool", 46, 0, 47)); + ITEMS.add(new Item("Green Wool", 45, 0, 46)); //41 - ITEMS.add(new Item("Black Wool", 47, 0, 48)); + ITEMS.add(new Item("Red Wool", 46, 0, 47)); //42 - ITEMS.add(new Item("Golden Block", 48, 0, 49)); + ITEMS.add(new Item("Black Wool", 47, 0, 48)); //43 - ITEMS.add(new Item("Iron Block", 49, 0, 50)); + ITEMS.add(new Item("Golden Block", 48, 0, 49)); //44 - ITEMS.add(new Item("Stone Slab", 50, 0, 51)); + ITEMS.add(new Item("Iron Block", 49, 0, 50)); //45 - ITEMS.add(new Item("Sandstone Slab", 52, 0, 53)); + ITEMS.add(new Item("Stone Slab", 50, 0, 51)); //46 - ITEMS.add(new Item("Wooden Slab", 53, 0, 54)); + ITEMS.add(new Item("Sandstone Slab", 52, 0, 53)); //47 - ITEMS.add(new Item("Cobblestone Slab", 54, 0, 55)); + ITEMS.add(new Item("Wooden Slab", 53, 0, 54)); //48 - ITEMS.add(new Item("Brick Slab", 55, 0, 56)); + ITEMS.add(new Item("Cobblestone Slab", 54, 0, 55)); //49 - ITEMS.add(new Item("Stone Brick", 64, 0, 57)); + ITEMS.add(new Item("Brick Slab", 55, 0, 56)); //50 - ITEMS.add(new Item("Stone Brick Slab", 56, 0, 58)); + ITEMS.add(new Item("Stone Brick", 64, 0, 57)); //51 - ITEMS.add(new Item("Cactus", 57, 0, 59)); + ITEMS.add(new Item("Stone Brick Slab", 56, 0, 58)); //52 + ITEMS.add(new Item("Cactus", 57, 0, 59)); + //53 ITEMS.add(new Item("Obsidian", 65, 0, 68)); + //54 + ITEMS.add(new Item("Wooden Sword", 0, 1)); + //55 + ITEMS.add(new Item("Stone Sword", 1, 1)); + //56 + ITEMS.add(new Item("Iron Sword", 2, 1)); + //57 + ITEMS.add(new Item("Diamond Sword", 3, 1)); + //58 + ITEMS.add(new Item("Golden Sword", 4, 1)); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Item.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Item.java index 5e6fdeb..bc2ed8f 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/objects/Item.java +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Item.java @@ -2,10 +2,15 @@ package ru.deadsoftware.cavecraft.game.objects; public class Item { - private int texture, type; + private int texture; + private int type; // 0 - block, 1 - tool, 2 - other private int block; private String name; + public Item(String name, int texture, int type) { + this(name, texture, type, -1); + } + public Item(String name, int texture, int type, int block) { this.name = name; this.texture = texture; diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java index 9e2f3dd..2d5ffe8 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java +++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Player.java @@ -24,15 +24,6 @@ public class Player implements Serializable { height = 30; texWidth = 8; inventory = new int[9]; - inventory[0] = 1; - inventory[1] = 2; - inventory[2] = 3; - inventory[3] = 4; - inventory[4] = 5; - inventory[5] = 6; - inventory[6] = 7; - inventory[7] = 8; - inventory[8] = 9; } public Rectangle getRect() { diff --git a/core/src/ru/deadsoftware/cavecraft/misc/Assets.java b/core/src/ru/deadsoftware/cavecraft/misc/Assets.java index 15031d0..14575a3 100644 --- a/core/src/ru/deadsoftware/cavecraft/misc/Assets.java +++ b/core/src/ru/deadsoftware/cavecraft/misc/Assets.java @@ -11,6 +11,7 @@ import ru.deadsoftware.cavecraft.CaveGame; public class Assets { public static final int BLOCK_TEXTURES = 66; + public static final int ITEM_TEXTURES = 5; private static GlyphLayout layout; @@ -30,7 +31,10 @@ public class Assets { public static Sprite shade; public static Texture terrain; - public static TextureRegion[] blockTextures = new TextureRegion[BLOCK_TEXTURES]; + public static Sprite[] blockTextures = new Sprite[BLOCK_TEXTURES]; + + public static Texture items; + public static Sprite[] itemTextures = new Sprite[ITEM_TEXTURES]; public static Texture gui; public static TextureRegion invBar; @@ -138,10 +142,21 @@ public class Assets { terrain = new Texture(Gdx.files.internal("terrain.png")); for (int i=0; i