From 3fb849898964a43a51cc6483ac72f5e3abbd01f0 Mon Sep 17 00:00:00 2001
From: fred-boy <fred-boy@protonmail.com>
Date: Fri, 28 Sep 2018 19:10:56 +0700
Subject: [PATCH] Add drop picking

---
 .../cavecraft/game/GamePhysics.java           | 19 ++++++++++++-------
 .../cavecraft/game/objects/Drop.java          | 18 ++++++++++--------
 2 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java
index c387bab..e38e3b0 100644
--- a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java
+++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java
@@ -1,6 +1,5 @@
 package ru.deadsoftware.cavecraft.game;
 
-import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.math.Intersector;
 import com.badlogic.gdx.math.MathUtils;
 import com.badlogic.gdx.math.Rectangle;
@@ -181,16 +180,22 @@ public class GamePhysics {
 }
 
     public void update(float delta) {
-        for (Drop drop : gameProc.drops) dropPhy(drop);
-        for (Mob mob : gameProc.mobs) {
-            mob.ai();
-            mobPhy(mob);
+        for (Iterator<Drop> it = gameProc.drops.iterator(); it.hasNext(); ) {
+            Drop drop = it.next();
+            dropPhy(drop);
+            if (Intersector.overlaps(drop.getRect(), gameProc.player.getRect()))
+                drop.pickUpDrop(gameProc.player);
+            if (drop.pickedUp) it.remove();
         }
+
         for (Iterator<Mob> it = gameProc.mobs.iterator(); it.hasNext();) {
-            Mob m = it.next();
-            if (m.dead)
+            Mob mob = it.next();
+            mob.ai();
+            mobPhy(mob);
+            if (mob.dead)
                 it.remove();
         }
+
         playerPhy(gameProc.player);
 
         gameProc.renderer.camera.position.set(
diff --git a/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java b/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java
index 5ff36e4..e967f88 100644
--- a/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java
+++ b/core/src/ru/deadsoftware/cavecraft/game/objects/Drop.java
@@ -7,23 +7,25 @@ import java.io.Serializable;
 
 public class Drop implements Serializable {
     private int id;
+    public boolean pickedUp = false;
     public Vector2 move, position;
 
-    public static void pickUpDrop(Player pl, int id) {
+    public Drop(float x, float y, int id) {
+        this.id = id;
+        position = new Vector2(x, y);
+        move = new Vector2(0, -1);
+    }
+
+    public void pickUpDrop(Player pl) {
         for (int i = 0; i < pl.inventory.length; i++) {
-            if (pl.inventory[i] == 0) {
+            if (pl.inventory[i] == 0 || pl.inventory[i] == id) {
                 pl.inventory[i] = id;
+                pickedUp = true;
                 break;
             }
         }
     }
 
-    public Drop(float x, float y, int id) {
-        this.id = id;
-        position = new Vector2(x, y);
-        move = new Vector2(0, -1);
-    }
-
     public int getId() {
         return id;
     }
-- 
2.29.2