DEADSOFTWARE

Fix saves
authorfredboy <fredboy@protonmail.com>
Sat, 20 Apr 2024 19:08:49 +0000 (02:08 +0700)
committerfredboy <fredboy@protonmail.com>
Sat, 20 Apr 2024 19:08:49 +0000 (02:08 +0700)
12 files changed:
core/src/ru/deadsoftware/cavedroid/game/GameComponent.java
core/src/ru/deadsoftware/cavedroid/game/GameInput.java
core/src/ru/deadsoftware/cavedroid/game/GameModule.java
core/src/ru/deadsoftware/cavedroid/game/GameProc.java
core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java
core/src/ru/deadsoftware/cavedroid/game/GameSaver.java
core/src/ru/deadsoftware/cavedroid/game/GameScreen.java
core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java
core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/model/item/Item.kt
core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt
core/src/ru/deadsoftware/cavedroid/game/objects/DropController.java

index a0562ce3dcec25b5cfa17e516ecf20c5c502c783..0bf5c2d4acf38a18bc33dd99af0b105fe1925f6a 100644 (file)
@@ -12,4 +12,6 @@ public interface GameComponent {
     GameProc getGameProc();
 
     GameInputProcessor getGameInputProcessor();
+
+    GameItemsHolder getGameItemsHolder();
 }
index f8c44b310324762c20db96284c995471f3d6144c..fb20d8fb0520ff9fe125d29ce1de5a4645f1161c 100644 (file)
@@ -288,7 +288,7 @@ public class GameInput {
 
     private void holdMB() {
         if (mTouchDownBtn == Input.Buttons.RIGHT) {
-            useItem(mCurX, mCurY, mPlayer.inventory[mPlayer.slot], true);
+            useItem(mCurX, mCurY, mPlayer.inventory[mPlayer.slot].getItem(), true);
             mTouchedDown = false;
         } else {
             if (insideHotbar(mTouchDownX, mTouchDownY)) {
@@ -411,7 +411,7 @@ public class GameInput {
                 }
 
                 System.arraycopy(mPlayer.inventory, 0, mPlayer.inventory, 1, 8);
-                mPlayer.inventory[0] = mGameItemsHolder.getItemFromCreativeInventory(itemPos);
+                mPlayer.inventory[0] = mGameItemsHolder.getItemFromCreativeInventory(itemPos).toInventoryItem();
             } else if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY)) {
                 mMainConfig.setGameUiWindow(GameUiWindow.NONE);
             } else if (screenY < hotbar.getRegionHeight() &&
@@ -420,7 +420,7 @@ public class GameInput {
                 mPlayer.slot = (int) ((screenX - (mMainConfig.getWidth() / 2 - hotbar.getRegionWidth() / 2)) / 20);
             } else if (button == Input.Buttons.RIGHT) {
                 useItem(mCurX, mCurY,
-                        mPlayer.inventory[mPlayer.slot], false);
+                        mPlayer.inventory[mPlayer.slot].getItem(), false);
             } else if (button == Input.Buttons.LEFT) {
                 mBlockDamage = 0;
             }
index 0637f595f819b790b7ac8791ef4db5bcf07042ac..29175da1968fba60e36f64a8ebb8ec746c73b630 100644 (file)
@@ -4,6 +4,7 @@ import dagger.Module;
 import dagger.Provides;
 import ru.deadsoftware.cavedroid.MainConfig;
 import ru.deadsoftware.cavedroid.game.mobs.MobsController;
+import ru.deadsoftware.cavedroid.game.model.block.Block;
 import ru.deadsoftware.cavedroid.game.objects.DropController;
 import ru.deadsoftware.cavedroid.game.world.GameWorld;
 
@@ -15,8 +16,14 @@ public class GameModule {
     @CheckForNull
     private static GameSaver.Data data;
 
-    public static void load(MainConfig mainConfig) {
-        data = GameSaver.load(mainConfig);
+    public static boolean loaded = false;
+
+    private static void load(MainConfig mainConfig, GameItemsHolder gameItemsHolder) {
+        if (loaded) {
+            return;
+        }
+        data = GameSaver.load(mainConfig, gameItemsHolder);
+        loaded = true;
     }
 
     private static void makeDataNullIfEmpty() {
@@ -27,30 +34,35 @@ public class GameModule {
 
     @Provides
     @GameScope
-    public static DropController provideDropController() {
+    public static DropController provideDropController(MainConfig mainConfig, GameItemsHolder gameItemsHolder) {
+        load(mainConfig, gameItemsHolder);
         DropController controller = data != null ? data.retrieveDropController() : new DropController();
         makeDataNullIfEmpty();
+        controller.initDrops(gameItemsHolder);
         return controller;
     }
 
     @Provides
     @GameScope
-    public static MobsController provideMobsController(GameItemsHolder gameItemsHolder) {
+    public static MobsController provideMobsController(MainConfig mainConfig, GameItemsHolder gameItemsHolder) {
+        load(mainConfig, gameItemsHolder);
         MobsController controller = data != null ? data.retrieveMobsController() : new MobsController(gameItemsHolder);
         makeDataNullIfEmpty();
+        controller.getPlayer().initInventory(gameItemsHolder);
         return controller;
     }
 
     @Provides
     @GameScope
-    public static GameWorld provideGameWorld(DropController dropController,
+    public static GameWorld provideGameWorld(MainConfig mainConfig,
+                                             DropController dropController,
                                              MobsController mobsController,
                                              GameItemsHolder gameItemsHolder) {
-        // TODO: 4/20/24 RE-enable saves
-//        int[][] fm = data != null ? data.retrieveForeMap() : null;
-//        int[][] bm = data != null ? data.retrieveBackMap() : null;
+        load(mainConfig, gameItemsHolder);
+        Block[][] fm = data != null ? data.retrieveForeMap() : null;
+        Block[][] bm = data != null ? data.retrieveBackMap() : null;
         makeDataNullIfEmpty();
-        return new GameWorld(dropController, mobsController, gameItemsHolder, null, null);
+        return new GameWorld(dropController, mobsController, gameItemsHolder, fm, bm);
     }
 
 }
index ef33b8ee9c66b5f27e81d7ae1c55eff3464df014..1a0a0448aba0889f6bf85284f6a6b9d88de36458 100644 (file)
@@ -29,8 +29,7 @@ public class GameProc implements Disposable {
                     MobsController mobsController,
                     GameWorldFluidsLogicControllerTask gameWorldFluidsLogicControllerTask,
                     GameWorldBlocksLogicControllerTask gameWorldBlocksLogicControllerTask,
-                    GameItemsHolder gameItemsHolder,
-                    AssetLoader assetLoader
+                    GameItemsHolder gameItemsHolder
     ) {
         mGamePhysics = gamePhysics;
         mGameInput = gameInput;
index 43973bcea0116f9713baff4de5182ec39aa09fe2..a3a9f015fb92b00a2f5fb0019a44af1650e7b3da 100644 (file)
@@ -212,8 +212,8 @@ public class GameRenderer extends Renderer {
             }
         }
         for (int i = 0; i < 9; i++) {
-            if (mMobsController.getPlayer().inventory[i] != null && mMobsController.getPlayer().inventory[i] != mGameItemsHolder.getFallbackItem()) {
-                spriter.draw(mMobsController.getPlayer().inventory[i].getSprite(),
+            if (mMobsController.getPlayer().inventory[i] != null && mMobsController.getPlayer().inventory[i].getItem() != mGameItemsHolder.getFallbackItem()) {
+                spriter.draw(mMobsController.getPlayer().inventory[i].getItem().getSprite(),
                         x + 8 + i * 18, y + creative.getRegionHeight() - 24);
             }
         }
@@ -257,8 +257,8 @@ public class GameRenderer extends Renderer {
         drawHealth(hotbarX, hotbar.getRegionHeight());
 
         for (int i = 0; i < 9; i++) {
-            if (mMobsController.getPlayer().inventory[i] != null && mMobsController.getPlayer().inventory[i] != mGameItemsHolder.getFallbackItem()) {
-                spriter.draw(mMobsController.getPlayer().inventory[i].getSprite(),
+            if (mMobsController.getPlayer().inventory[i] != null && mMobsController.getPlayer().inventory[i].getItem() != mGameItemsHolder.getFallbackItem()) {
+                spriter.draw(mMobsController.getPlayer().inventory[i].getItem().getSprite(),
                         getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20,
                         3);
             }
@@ -391,7 +391,7 @@ public class GameRenderer extends Renderer {
             drawString("Mobs: " + mMobsController.getMobs().size(), 0, 70);
             drawString("Drops: " + mDropController.getSize(), 0, 80);
             drawString("Block: " + mGameWorld.getForeMap(mGameInput.getCurX(), mGameInput.getCurY()).getParams().getKey(), 0, 90);
-            drawString("Hand: " + mMobsController.getPlayer().inventory[mMobsController.getPlayer().slot].getParams().getKey(), 0, 100);
+            drawString("Hand: " + mMobsController.getPlayer().inventory[mMobsController.getPlayer().slot].getItem().getParams().getKey(), 0, 100);
             drawString("Game mode: " + player.gameMode, 0, 110);
             spriter.end();
         }
index 51dbd5bfb935f100b93a9ddd0c4a4f33e80a1de9..53d147228f96a334a31ce9cd2e575c14b154e55e 100644 (file)
@@ -4,12 +4,15 @@ import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.files.FileHandle;
 import ru.deadsoftware.cavedroid.MainConfig;
 import ru.deadsoftware.cavedroid.game.mobs.MobsController;
+import ru.deadsoftware.cavedroid.game.model.block.Block;
 import ru.deadsoftware.cavedroid.game.objects.DropController;
 import ru.deadsoftware.cavedroid.game.world.GameWorld;
 
 import javax.annotation.CheckForNull;
 import java.io.*;
 import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.Map;
 
 public class GameSaver {
 
@@ -21,9 +24,9 @@ public class GameSaver {
         @CheckForNull
         private DropController mDropController;
         @CheckForNull
-        private int[][] mForeMap, mBackMap;
+        private Block[][] mForeMap, mBackMap;
 
-        public Data(MobsController mobsController, DropController dropController, int[][] foreMap, int[][] backMap) {
+        public Data(MobsController mobsController, DropController dropController, Block[][] foreMap, Block[][] backMap) {
             mMobsController = mobsController;
             mDropController = dropController;
             mForeMap = foreMap;
@@ -44,16 +47,16 @@ public class GameSaver {
             return dropController;
         }
 
-        public int[][] retrieveForeMap() {
+        public Block[][] retrieveForeMap() {
             assert mForeMap != null;
-            int[][] foreMap = mForeMap;
+            Block[][] foreMap = mForeMap;
             mForeMap = null;
             return foreMap;
         }
 
-        public int[][] retrieveBackMap() {
+        public Block[][] retrieveBackMap() {
             assert mBackMap != null;
-            int[][] backMap = mBackMap;
+            Block[][] backMap = mBackMap;
             mBackMap = null;
             return backMap;
         }
@@ -69,7 +72,46 @@ public class GameSaver {
         return ByteBuffer.allocate(4).putInt(i).array();
     }
 
-    private static void saveMap(FileHandle file, int[][] map) throws IOException {
+    private static Map<String, Integer> buildBlocksDictionary(Block[][] foreMap, Block[][] backMap) {
+        final HashMap<String, Integer> dict = new HashMap<>();
+
+        int id = 0;
+        for (int i = 0; i < foreMap.length; i++) {
+            for (int j = 0; j < foreMap[i].length; j++) {
+                for (int k = 0; k < 2; k++) {
+                    final Block block = k == 0 ? foreMap[i][j] : backMap[i][j];
+                    final String key = block.getParams().getKey();
+                    if (!dict.containsKey(key)) {
+                        dict.put(key, id++);
+                    }
+                }
+            }
+        }
+
+        return dict;
+    }
+
+    private static void saveDict(FileHandle file, Map<String, Integer> dict) {
+        final String[] arr = new String[dict.size()];
+
+        for (Map.Entry<String, Integer> entry : dict.entrySet()) {
+            arr[entry.getValue()] = entry.getKey();
+        }
+
+        final StringBuilder builder = new StringBuilder();
+        for (String key : arr) {
+            builder.append(key);
+            builder.append('\n');
+        }
+
+        file.writeString(builder.toString(), false);
+    }
+
+    private static String[] loadDict(FileHandle file) {
+        return file.readString().split("\n");
+    }
+
+    private static void saveMap(FileHandle file, Block[][] map, Map<String, Integer> dict) throws IOException {
         int run, block;
         int width = map.length;
         int height = map[0].length;
@@ -81,14 +123,15 @@ public class GameSaver {
         out.write(intToBytes(height));
 
         for (int y = 0; y < height; y++) {
-            block = map[0][y];
+            block = dict.get(map[0][y].getParams().getKey());
             run = 0;
-            for (int[] ints : map) {
-                if (ints[y] != block) {
+            for (Block[] blocks : map) {
+                int newValue = dict.get(blocks[y].getParams().getKey());
+                if (newValue != block) {
                     out.write(intToBytes(run));
                     out.write(intToBytes(block));
                     run = 0;
-                    block = ints[y];
+                    block = dict.get(blocks[y].getParams().getKey());
                 }
                 run++;
             }
@@ -100,8 +143,8 @@ public class GameSaver {
         out.close();
     }
 
-    private static int[][] loadMap(FileHandle file) throws Exception {
-        int[][] map;
+    private static Block[][] loadMap(GameItemsHolder gameItemsHolder, FileHandle file, String[] dict) throws Exception {
+        Block[][] map;
         int version, width, height;
         int run, block;
 
@@ -112,13 +155,13 @@ public class GameSaver {
         if (SAVE_VERSION == version) {
             width = in.readInt();
             height = in.readInt();
-            map = new int[width][height];
+            map = new Block[width][height];
             for (int y = 0; y < height; y++) {
                 for (int x = 0; x < width; x += run) {
                     run = in.readInt();
                     block = in.readInt();
                     for (int i = x; i < x + run; i++) {
-                        map[i][y] = block;
+                        map[i][y] = gameItemsHolder.getBlock(dict[block]);
                     }
                 }
             }
@@ -131,7 +174,7 @@ public class GameSaver {
     }
 
     @CheckForNull
-    public static Data load(MainConfig mainConfig) {
+    public static Data load(MainConfig mainConfig, GameItemsHolder gameItemsHolder) {
         String folder = mainConfig.getGameFolder();
         FileHandle file = Gdx.files.absolute(folder + "/saves/game.sav");
 
@@ -150,8 +193,9 @@ public class GameSaver {
 
             in.close();
 
-            int[][] foreMap = loadMap(Gdx.files.absolute(mainConfig.getGameFolder() + "/saves/foremap.sav"));
-            int[][] backMap = loadMap(Gdx.files.absolute(mainConfig.getGameFolder() + "/saves/backmap.sav"));
+            final String[] dict = loadDict(Gdx.files.absolute(mainConfig.getGameFolder() + "/saves/dict"));
+            Block[][] foreMap = loadMap(gameItemsHolder, Gdx.files.absolute(mainConfig.getGameFolder() + "/saves/foremap.sav"), dict);
+            Block[][] backMap = loadMap(gameItemsHolder, Gdx.files.absolute(mainConfig.getGameFolder() + "/saves/backmap.sav"), dict);
 
             if (dropController == null || mobsController == null) {
                 throw new Exception("couldn't load");
@@ -169,35 +213,36 @@ public class GameSaver {
                             DropController dropController,
                             MobsController mobsController,
                             GameWorld gameWorld) {
-
-        Gdx.app.debug(TAG, "Saves are disabled for this build");
-        return;
-
-
-/*        String folder = mainConfig.getGameFolder();
+        String folder = mainConfig.getGameFolder();
         FileHandle file = Gdx.files.absolute(folder + "/saves/");
         file.mkdirs();
         file = Gdx.files.absolute(folder + "/saves/game.sav");
 
+        final Block[][] foreMap, backMap;
+        foreMap = gameWorld.getFullForeMap();
+        backMap = gameWorld.getFullBackMap();
+
+        final Map<String, Integer> dict = buildBlocksDictionary(foreMap, backMap);
+
         try {
             ObjectOutputStream out = new ObjectOutputStream(file.write(false));
             out.writeInt(SAVE_VERSION);
             out.writeObject(dropController);
             out.writeObject(mobsController);
             out.close();
-            // TODO: 4/20/24 save map
-//            saveMap(Gdx.files.absolute(folder + "/saves/foremap.sav"), gameWorld.getFullForeMap());
-//            saveMap(Gdx.files.absolute(folder + "/saves/backmap.sav"), gameWorld.getFullBackMap());
+
+            saveDict(Gdx.files.absolute(folder + "/saves/dict"), dict);
+            saveMap(Gdx.files.absolute(folder + "/saves/foremap.sav"), gameWorld.getFullForeMap(), dict);
+            saveMap(Gdx.files.absolute(folder + "/saves/backmap.sav"), gameWorld.getFullBackMap(), dict);
         } catch (Exception e) {
             e.printStackTrace();
-        }*/
+        }
     }
 
     public static boolean exists(MainConfig mainConfig) {
-        return false;
-//        String folder = mainConfig.getGameFolder();
-//        return (Gdx.files.absolute(folder + "/saves/game.sav").exists() &&
-//                Gdx.files.absolute(folder + "/saves/foremap.sav").exists() &&
-//                Gdx.files.absolute(folder + "/saves/backmap.sav").exists());
+        String folder = mainConfig.getGameFolder();
+        return (Gdx.files.absolute(folder + "/saves/game.sav").exists() &&
+                Gdx.files.absolute(folder + "/saves/foremap.sav").exists() &&
+                Gdx.files.absolute(folder + "/saves/backmap.sav").exists());
     }
 }
index e2c83bcab66e079ce73cf09b7261fe07297542cc..1d47b9074cb9f65144944486e024ad7462a579cd 100644 (file)
@@ -17,6 +17,8 @@ public class GameScreen implements Screen {
     private GameProc mGameProc;
     @CheckForNull
     private GameInputProcessor mGameInputProcessor;
+    @CheckForNull
+    private GameItemsHolder mGameItemsHolder;
 
     @Inject
     public GameScreen(MainConfig mainConfig) {
@@ -28,6 +30,8 @@ public class GameScreen implements Screen {
             mGameProc.dispose();
         }
 
+        GameModule.loaded = true;
+
         GameComponent gameComponent = DaggerGameComponent.builder()
                 .mainComponent(mMainConfig.getMainComponent()).build();
 
@@ -44,7 +48,7 @@ public class GameScreen implements Screen {
             mGameProc.dispose();
         }
 
-        GameModule.load(mMainConfig);
+        GameModule.loaded = false;
 
         GameComponent gameComponent = DaggerGameComponent.builder()
                 .mainComponent(mMainConfig.getMainComponent()).build();
index 3c33ee574efefe17a1010f8a96694a42b67e1744..bce9a2c435a0da4e2d81c794af791f95f55d9a49 100644 (file)
@@ -5,6 +5,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.math.MathUtils;
 import com.badlogic.gdx.math.Vector2;
 import ru.deadsoftware.cavedroid.game.GameItemsHolder;
+import ru.deadsoftware.cavedroid.game.model.item.InventoryItem;
 import ru.deadsoftware.cavedroid.game.model.item.Item;
 import ru.deadsoftware.cavedroid.game.objects.Drop;
 import ru.deadsoftware.cavedroid.game.world.GameWorld;
@@ -12,6 +13,8 @@ import ru.deadsoftware.cavedroid.misc.Assets;
 import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin;
 import ru.deadsoftware.cavedroid.misc.utils.SpriteUtilsKt;
 
+import javax.annotation.CheckForNull;
+
 public class Player extends Mob {
 
     private static final float SPEED = 69.072f;
@@ -22,7 +25,7 @@ public class Player extends Mob {
     private float hitAnim = 0f;
     private float hitAnimDelta = ANIMATION_SPEED;
 
-    public final Item[] inventory;
+    public final InventoryItem[] inventory;
     public int slot;
     public int gameMode;
     public boolean swim;
@@ -30,13 +33,24 @@ public class Player extends Mob {
 
     public Player(GameItemsHolder gameItemsHolder) {
         super(0, 0, 4, 30, randomDir(), Type.MOB, MAX_HEALTH);
-        inventory = new Item[9];
+        inventory = new InventoryItem[9];
         for (int i = 0; i < 9; i++) {
-            inventory[i] = gameItemsHolder.getFallbackItem();
+            inventory[i] = gameItemsHolder.getFallbackItem().toInventoryItem();
         }
         swim = false;
     }
 
+    public void initInventory(GameItemsHolder gameItemsHolder) {
+        for (InventoryItem invItem : inventory) {
+            invItem.init(gameItemsHolder);
+        }
+    }
+
+    @CheckForNull
+    public Item inventory(int i) {
+        return inventory[i].getItem();
+    }
+    
     public void respawn(GameWorld gameWorld, GameItemsHolder itemsHolder) {
         Vector2 pos = getSpawnPoint(gameWorld, itemsHolder);
         this.x = pos.x;
@@ -48,8 +62,8 @@ public class Player extends Mob {
 
     public void pickUpDrop(Drop drop) {
         for (int i = 0; i < inventory.length; i++) {
-            if (inventory[i] == null || inventory[i].getParams().getKey().equals(GameItemsHolder.FALLBACK_ITEM_KEY) || inventory[i] == drop.getItem()) {
-                inventory[i] = drop.getItem();
+            if (inventory(i) == null || inventory(i).getParams().getKey().equals(GameItemsHolder.FALLBACK_ITEM_KEY) || inventory(i) == drop.getItem()) {
+                inventory[i] = drop.getItem().toInventoryItem();
                 drop.setPickedUp(true);
                 break;
             }
@@ -78,7 +92,7 @@ public class Player extends Mob {
     }
 
     public void setCurrentInventorySlotItem(Item item) {
-        inventory[slot] = item;
+        inventory[slot] = item.toInventoryItem();
     }
 
     @Override
@@ -116,7 +130,7 @@ public class Player extends Mob {
     }
 
     private void drawItem(SpriteBatch spriteBatch, float x, float y, float anim) {
-        final Item item = inventory[slot];
+        final Item item = inventory(slot);
 
         if (item == null || item.getParams().getKey().equals(GameItemsHolder.FALLBACK_ITEM_KEY)) {
             return;
diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt b/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt
new file mode 100644 (file)
index 0000000..1e1f43e
--- /dev/null
@@ -0,0 +1,27 @@
+package ru.deadsoftware.cavedroid.game.model.item
+
+import ru.deadsoftware.cavedroid.game.GameItemsHolder
+import java.io.Serializable
+
+class InventoryItem @JvmOverloads constructor(
+    val itemKey: String,
+    val amount: Int = 1,
+) : Serializable {
+
+    @Transient
+    lateinit var item: Item
+        private set
+
+    @JvmOverloads
+    constructor(_item: Item, amount: Int = 1) : this(_item.params.key, amount) {
+        item = _item
+    }
+
+    fun init(gameItemsHolder: GameItemsHolder) {
+        if (this::item.isInitialized) {
+            return
+        }
+        item = gameItemsHolder.getItem(itemKey)
+    }
+
+}
index a479a4f7280c2ad8d5fd936dd78546a01f13bf74..fe2bc46bad46030cd43eb0a74f284fa4bd8f4629 100644 (file)
@@ -33,6 +33,11 @@ sealed class Item {
         contract { returns(true) implies (this@Item is Placeable) }
         return this is Placeable
     }
+
+    @JvmOverloads
+    fun toInventoryItem(amount: Int = 1): InventoryItem {
+        return InventoryItem(this, amount)
+    }
     
     sealed class Tool : Item() {
         abstract val mobDamageMultiplier: Float
index b9a141c33cdb0fce3734ed5a507811699f75acfa..151e48609fbc98a1c4bd95671b4530790f9abc9a 100644 (file)
@@ -3,17 +3,35 @@ package ru.deadsoftware.cavedroid.game.objects
 import com.badlogic.gdx.math.Intersector
 import com.badlogic.gdx.math.Rectangle
 import com.badlogic.gdx.math.Vector2
+import ru.deadsoftware.cavedroid.game.GameItemsHolder
 import ru.deadsoftware.cavedroid.game.model.item.Item
 
 class Drop(
     x: Float,
     y: Float,
-    val item: Item,
+    _item: Item,
 ) : Rectangle(x, y, DROP_SIZE, DROP_SIZE) {
 
+    val itemKey = _item.params.key
     val velocity = getInitialVelocity()
     var pickedUp = false
 
+    @Transient
+    lateinit var item: Item
+        private set
+
+    init {
+        item = _item
+    }
+
+    fun initItem(gameItemsHolder: GameItemsHolder) {
+        if (this::item.isInitialized) {
+            return
+        }
+
+        item = gameItemsHolder.getItem(itemKey)
+    }
+
     fun canMagnetTo(rectangle: Rectangle): Boolean {
         val magnetArea = getMagnetArea()
         return Intersector.overlaps(magnetArea, rectangle)
index 084180d10b9cb4ae4a4c5b895b95c2a44fdb00f8..826fd6878fc950ed5911d95b9bdc5358a84d6ddc 100644 (file)
@@ -1,5 +1,6 @@
 package ru.deadsoftware.cavedroid.game.objects;
 
+import ru.deadsoftware.cavedroid.game.GameItemsHolder;
 import ru.deadsoftware.cavedroid.game.GameScope;
 import ru.deadsoftware.cavedroid.game.model.item.Item;
 
@@ -21,6 +22,10 @@ public class DropController implements Serializable {
     public DropController() {
     }
 
+    public void initDrops(GameItemsHolder gameItemsHolder) {
+        mDrops.forEach((drop) -> drop.initItem(gameItemsHolder));
+    }
+
     public void addDrop(float x, float y, Item item) {
         mDrops.add(new Drop(x, y, item));
     }