DEADSOFTWARE

Add item in player's hand
authorfred-boy <fred-boy@protonmail.com>
Fri, 14 Sep 2018 10:20:34 +0000 (17:20 +0700)
committerfred-boy <fred-boy@protonmail.com>
Fri, 14 Sep 2018 10:20:34 +0000 (17:20 +0700)
android/assets/items.png [new file with mode: 0644]
core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java
core/src/ru/deadsoftware/cavecraft/game/GameProc.java
core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java
core/src/ru/deadsoftware/cavecraft/game/Items.java
core/src/ru/deadsoftware/cavecraft/game/objects/Item.java
core/src/ru/deadsoftware/cavecraft/game/objects/Player.java
core/src/ru/deadsoftware/cavecraft/misc/Assets.java
desktop/src/ru/deadsoftware/cavecraft/desktop/DesktopLauncher.java

diff --git a/android/assets/items.png b/android/assets/items.png
new file mode 100644 (file)
index 0000000..cecb5e8
Binary files /dev/null and b/android/assets/items.png differ
index 372bf6e9e6e162a0f711a906ad190408a44d05c3..fdf1206371935e23a848428fca23f3d594c61b33 100644 (file)
@@ -94,7 +94,6 @@ public class GamePhysics {
             }
         } else {
             if (!pl.flyMode && pl.moveY.y<18) pl.moveY.add(gravity);
-            //if (CaveGame.TOUCH && gameProc.swim) gameProc.swim = false;
         }
 
         pl.position.add(pl.moveX);
index 45c62b82beaac999e403bf7fe6ab41ee2407187d..ddeac68a9f9d195f46630c98c1896c829036f5d8 100644 (file)
@@ -374,7 +374,7 @@ public class GameProc implements Serializable{
     }
 
     public void useItem(int x, int y, int id, boolean bg) {
-        if (Items.ITEMS.get(id).getType()==0) {
+        if (id>0 && 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());
         }
index 1eef325463a41614b6d98714ba21110ffb2e5af5..ee9d6d4d9d68c83542c76877831bfc851259e784 100644 (file)
@@ -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; i<gameProc.creativeScroll*8+40; i++) {
             if (i>0 && i<Items.ITEMS.size())
-                if (Items.ITEMS.get(i).getType() == 0)
-                    spriteBatch.draw(Assets.blockTextures[Items.ITEMS.get(i).getTexture()],
-                            x+8+((i-gameProc.creativeScroll*8)%8)*18,
-                            y+18+((i-gameProc.creativeScroll*8)/8)*18);
+                switch (Items.ITEMS.get(i).getType()) {
+                    case 0:
+                        spriteBatch.draw(Assets.blockTextures[Items.ITEMS.get(i).getTexture()],
+                            x + 8 + ((i - gameProc.creativeScroll * 8) % 8) * 18,
+                            y + 18 + ((i - gameProc.creativeScroll * 8) / 8) * 18);
+                        break;
+                    case 1:
+                        spriteBatch.draw(Assets.itemTextures[Items.ITEMS.get(i).getTexture()],
+                                x + 8 + ((i - gameProc.creativeScroll * 8) % 8) * 18,
+                                y + 18 + ((i - gameProc.creativeScroll * 8) / 8) * 18);
+                        break;
+                }
         }
         for (int i=0; i<9; i++) {
             if (gameProc.player.inventory[i]>0)
-                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,
index 2a8c53252424258ddbdb7f8d78cdb15530a09e48..db32ad13b5d25e7bb86f85672121ac6a99365c95 100644 (file)
@@ -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));
 
     }
 
index 5e6fdebe8cf523c225559d57a234612e9b3b5bd9..bc2ed8f48814f8526f45833b14061811d76ad389 100644 (file)
@@ -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;
index 9e2f3dd55238b18d6890794ecf7260b029e28729..2d5ffe8859340f30e92eaae6fb6a7191bdb83272 100644 (file)
@@ -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() {
index 15031d08ca056e807db49c6b61c00c18c85b565b..14575a37ad1b41ef4b752461cace75149eef5fb4 100644 (file)
@@ -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<BLOCK_TEXTURES; i++) {
-            blockTextures[i] = new TextureRegion(terrain,
+            blockTextures[i] = new Sprite(terrain,
                     (i%16)*16, (i/16)*16, 16,16);
             blockTextures[i].flip(false,true);
+            blockTextures[i].setSize(8,8);
+        }
+        terrain = null;
+
+        items = new Texture(Gdx.files.internal("items.png"));
+        for (int i=0; i<ITEM_TEXTURES; i++) {
+            itemTextures[i] = new Sprite(items,
+                    (i%16)*16, (i/16)*16, 16, 16);
+            itemTextures[i].flip(false, true);
+            //itemTextures[i].setSize(8,8);
         }
+        items = null;
     }
 
     public static int getStringWidth(String s){
index 03ddd69ce5358abd94b7da26203e8775c4eff51d..470584b238ee39b21f7cb10faab628e97fb4c6e4 100644 (file)
@@ -14,6 +14,11 @@ public class DesktopLauncher {
                config.title = "CaveCraft";
                config.width = 960;
                config.height = 540;
-               new LwjglApplication(new CaveGame(), config);
+
+                boolean touch = false;
+                for (int i=0; i<arg.length; i++) {
+                    if (arg[i].equals("--touch")) touch = true;
+                }
+               new LwjglApplication(new CaveGame(touch), config);
        }
 }