From 2bcffc45aa9ee27d10e9a54f3aa146a9155899c5 Mon Sep 17 00:00:00 2001
From: Adelline Kania <adellinekaniasetiyawan@gmail.com>
Date: Sat, 15 Apr 2023 13:13:08 +0700
Subject: [PATCH] feat: pet in shopkeeper

---
 Assets/Prefabs/ShopKeeperItem.prefab          |  3 +
 Assets/Scripts/Managers/ShopKeeperManager.cs  |  5 +-
 .../Scripts/ShopKeeperItem/ShopKeeperItem.cs  | 71 ++++++++++++++++++-
 3 files changed, 75 insertions(+), 4 deletions(-)

diff --git a/Assets/Prefabs/ShopKeeperItem.prefab b/Assets/Prefabs/ShopKeeperItem.prefab
index 39d0e43..8d2f91a 100644
--- a/Assets/Prefabs/ShopKeeperItem.prefab
+++ b/Assets/Prefabs/ShopKeeperItem.prefab
@@ -309,6 +309,9 @@ MonoBehaviour:
   succeedBuySound: {fileID: 8300000, guid: c0dece8713f73d94184221e35386a1da, type: 3}
   failedBuySound: {fileID: 8300000, guid: 76916fdf776f76d409d8aa41d2e332a3, type: 3}
   id: 0
+  petBuff: {fileID: 3606018136342516863, guid: 92757ed0bcf3e6e439aa9fd341debbd8, type: 3}
+  petAttack: {fileID: 3680115193894871933, guid: ac3414a5046a0fa4fadc7a6a540faf3a, type: 3}
+  petHeal: {fileID: 5172213328979931919, guid: 2658506674c1dd245b64b2acf4ae411b, type: 3}
 --- !u!82 &6310438699312582860
 AudioSource:
   m_ObjectHideFlags: 0
diff --git a/Assets/Scripts/Managers/ShopKeeperManager.cs b/Assets/Scripts/Managers/ShopKeeperManager.cs
index 794dc7f..082deeb 100644
--- a/Assets/Scripts/Managers/ShopKeeperManager.cs
+++ b/Assets/Scripts/Managers/ShopKeeperManager.cs
@@ -19,7 +19,7 @@ public class ShopKeeperManager : MonoBehaviour
     void Awake()
     {
         anim = GetComponent<Animator>();
-        player = GameObject.Find("Player");
+        player = GameObject.FindGameObjectWithTag("Player");
     }
 
     // Update is called once per frame
@@ -30,7 +30,8 @@ public class ShopKeeperManager : MonoBehaviour
         float distance = Vector3.Distance(shopKeeper.transform.position, player.transform.position);
         bool isPressedTab = Input.GetKeyDown(KeyCode.Tab);
 
-        if (distance < 7.5 && JedaManager.isJeda) {
+        // if (distance < 7.5 && JedaManager.isJeda) {
+        if (distance < 7.5) {
             if (isPressedTab) {
                 ShowShopKeeper();
             } else {
diff --git a/Assets/Scripts/ShopKeeperItem/ShopKeeperItem.cs b/Assets/Scripts/ShopKeeperItem/ShopKeeperItem.cs
index 43bd751..d2201bc 100644
--- a/Assets/Scripts/ShopKeeperItem/ShopKeeperItem.cs
+++ b/Assets/Scripts/ShopKeeperItem/ShopKeeperItem.cs
@@ -14,7 +14,16 @@ public class ShopKeeperItem : MonoBehaviour
 
     public int id;
 
+    GameObject player;
+
+    public GameObject petHeal;
+    public GameObject petAttack;
+    public GameObject petBuff;
+
     IDictionary<string, int> itemPrices = new Dictionary<string, int>(){
+        {"PetHeal", 203},
+        {"PetAttack", 203},
+        {"PetBuff", 203},
         {"weapon", 50},
         {"shotgun", 100},
         {"sword", 100},
@@ -23,13 +32,20 @@ public class ShopKeeperItem : MonoBehaviour
 
     void Awake() {
         audioSource = GetComponent<AudioSource>();
+        player = GameObject.FindGameObjectWithTag("Player");
     }
 
 
     // Update is called once per frame
     void Update()
     {
-        if (id == 3) {
+        if (id == 0) {
+            SetPetAttribute("PetHeal");
+        } else if (id == 1) {
+            SetPetAttribute("PetAttack");
+        } else if (id == 2) {
+            SetPetAttribute("PetBuff");
+        } else if (id == 3) {
             SetWeaponAttribute(true, PlayerShooting.level, "weapon");
         } else if (id == 4) {
             SetWeaponAttribute(WeaponManager.getShotgunInfo(), PlayerShotgun.level, "shotgun");
@@ -38,6 +54,11 @@ public class ShopKeeperItem : MonoBehaviour
         } else if (id == 6) {
             SetWeaponAttribute(WeaponManager.getBowInfo(), Bow.level, "bow");
         } 
+
+        if(Input.GetKeyDown(KeyCode.K)) {
+            Debug.Log(GameObject.FindGameObjectWithTag("Pet"));
+            Instantiate(petBuff, player.transform.position, player.transform.rotation);
+        }
     }
 
     void SucceedBuy() {
@@ -49,7 +70,13 @@ public class ShopKeeperItem : MonoBehaviour
     }
 
     public void ItemOnClick() {
-        if (id == 3) {
+        if (id == 0) {
+            BuyPet("PetHeal", petHeal);
+        } else if (id == 1) {
+            BuyPet("PetAttack", petAttack);
+        } else if (id == 2) {
+             BuyPet("PetBuff", petBuff);
+        } else if (id == 3) {
             if (PlayerShooting.level < 5) {
                 int price = GetWeaponPrice(true, "weapon", PlayerShooting.level);
                 if (GoldManager.gold >= price) {
@@ -157,4 +184,44 @@ public class ShopKeeperItem : MonoBehaviour
         btnItem.GetComponentInChildren<Text>().text = btnText;
         itemName.text = levelName;
     }
+
+    void SetPetAttribute(string petName) {
+
+        GameObject pet = GameObject.FindGameObjectWithTag("Pet");
+        Color color;
+        string btnText;
+
+        if (pet is null) {
+            color = new Color(19f/255f, 72f/255f, 130f/255f);
+            btnText = itemPrices[petName].ToString();
+        } else {
+            if (pet.name == (petName + "(Clone)")) {
+                color = new Color(32f/255f, 40f/255f, 47f/255f);
+                btnText = "Equipped";
+            } else {
+                color = new Color(19f/255f, 72f/255f, 130f/255f);
+                btnText = itemPrices[petName].ToString();
+            }
+        }
+
+        btnItem.GetComponent<Image>().color = color;
+        btnItem.GetComponentInChildren<Text>().text = btnText;
+    }
+
+    void BuyPet(string petName, GameObject petObject) {
+        if (GameObject.FindGameObjectWithTag("Pet") is null ) {
+            if (GoldManager.gold >= itemPrices[petName]) {
+                GoldManager.gold -= itemPrices[petName];
+                Instantiate(petObject, player.transform.position, player.transform.rotation);
+                ShopKeeperManager.shopKeeperNotificationText = "Success buy a new pet!!";
+                SucceedBuy();
+            } else {
+                ShopKeeperManager.shopKeeperNotificationText = "You don't have enough money :(";
+                FailedBuy();
+            }
+        } else {
+            ShopKeeperManager.shopKeeperNotificationText = "You already have a pet!!";
+            FailedBuy();
+        }
+    }
 }
-- 
GitLab