From 59e34ef728e0156e1a335d6bdc5881dc500a9f8a Mon Sep 17 00:00:00 2001 From: Nicola Sovic Date: Thu, 14 Jul 2022 23:48:37 +0200 Subject: [PATCH] Added NPCs to game, fixed explore and collect quest, added quest remove, fixed quest update, v1.4.0 --- Assets/Scenes/GameScene.unity | 1 + Assets/Scripts/ContentGenerator.cs | 42 ++++++++---- Assets/Scripts/Controls.cs | 1 + Assets/Scripts/InventorySlot.cs | 5 ++ Assets/Scripts/NPC.cs | 40 ++++++++---- Assets/Scripts/Player.cs | 5 ++ Assets/Scripts/QuestLog.cs | 51 ++++++++++++--- Assets/Scripts/Quests/CollectQuest.cs | 94 +++++++++++++++++++-------- Assets/Scripts/Quests/ExploreQuest.cs | 8 +-- Assets/Scripts/Quests/Quest.cs | 14 ++++ 10 files changed, 199 insertions(+), 62 deletions(-) diff --git a/Assets/Scenes/GameScene.unity b/Assets/Scenes/GameScene.unity index fec91f9..256a600 100644 --- a/Assets/Scenes/GameScene.unity +++ b/Assets/Scenes/GameScene.unity @@ -9778,6 +9778,7 @@ MonoBehaviour: - {fileID: 4813231546532491625, guid: ee075607d6b41e94d959a6a91288e2d3, type: 3} grass: {fileID: 5970796181653315724, guid: 6622329b9828ab547b3ba34a33107c2d, type: 3} boss: {fileID: 5250108364493051291, guid: a9001384d18dd1c4f94df7c4711588e4, type: 3} + npc: {fileID: 6513753043401943383, guid: fba991e0db8729b4c934e5c9620457f7, type: 3} --- !u!4 &667353831 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/ContentGenerator.cs b/Assets/Scripts/ContentGenerator.cs index 4deb9b5..0354cb6 100644 --- a/Assets/Scripts/ContentGenerator.cs +++ b/Assets/Scripts/ContentGenerator.cs @@ -10,7 +10,7 @@ public class ContentGenerator : MonoBehaviour public GameObject[] stones; public GameObject grass; public GameObject boss; - //public GameObject npc; + public GameObject npc; static System.Random rand = new System.Random(); public GameObject generateEnemy() @@ -55,14 +55,18 @@ public class ContentGenerator : MonoBehaviour return stones[rand.Next(0, stones.Length)]; } } - else if (chance >= 90 && chance < 99) + else if (chance >= 90 && chance < 95) { return generateEnemy(); } - else + else if(chance >= 95 && chance < 99) { return boss; } + else + { + return npc; + } } public GameObject generateStoneTileContent() @@ -76,14 +80,18 @@ public class ContentGenerator : MonoBehaviour { return trees[rand.Next(0, trees.Length)]; } - else if (chance >= 90 && chance < 99) + else if (chance >= 90 && chance < 95) { return generateEnemy(); } - else + else if (chance >= 95 && chance < 99) { return boss; } + else + { + return npc; + } } public GameObject generateTreeTileContent() @@ -97,14 +105,18 @@ public class ContentGenerator : MonoBehaviour { return stones[rand.Next(0, stones.Length)]; } - else if (chance >= 90 && chance < 99) + else if (chance >= 90 && chance < 95) { return generateEnemy(); } - else + else if (chance >= 95 && chance < 99) { return boss; } + else + { + return npc; + } } public GameObject generateRiverTileContent() @@ -125,14 +137,18 @@ public class ContentGenerator : MonoBehaviour return stones[rand.Next(0, stones.Length)]; } } - else if (chance >= 90 && chance < 99) + else if (chance >= 90 && chance < 95) { return generateEnemy(); } - else + else if (chance >= 95 && chance < 99) { return boss; } + else + { + return npc; + } } public GameObject generateLakeTileContent() @@ -146,14 +162,18 @@ public class ContentGenerator : MonoBehaviour { return stones[rand.Next(0, stones.Length)]; } - else if (chance >= 90 && chance < 99) + else if (chance >= 90 && chance < 95) { return generateEnemy(); } - else + else if (chance >= 95 && chance < 99) { return boss; } + else + { + return npc; + } } public GameObject loadObject(JToken json) diff --git a/Assets/Scripts/Controls.cs b/Assets/Scripts/Controls.cs index 1de3685..603936e 100644 --- a/Assets/Scripts/Controls.cs +++ b/Assets/Scripts/Controls.cs @@ -62,6 +62,7 @@ public class Controls : MonoBehaviour Destroy(target); break; case "NPC": + target.GetComponent().interact(); break; case "House": break; diff --git a/Assets/Scripts/InventorySlot.cs b/Assets/Scripts/InventorySlot.cs index da1e599..a46c6d1 100644 --- a/Assets/Scripts/InventorySlot.cs +++ b/Assets/Scripts/InventorySlot.cs @@ -48,6 +48,11 @@ namespace Assets.Scripts items[currentBag] = null; } + public void removeItem(int bag) + { + items[bag] = null; + } + public void showTooltip() { Item item = getItemForTooltip(); diff --git a/Assets/Scripts/NPC.cs b/Assets/Scripts/NPC.cs index f0b5fad..e141dd2 100644 --- a/Assets/Scripts/NPC.cs +++ b/Assets/Scripts/NPC.cs @@ -2,22 +2,36 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -public class NPC : MonoBehaviour +namespace Assets.Scripts { - // Start is called before the first frame update - void Start() + public class NPC : MonoBehaviour { - - } + bool hasQuest = true; + // Start is called before the first frame update + void Start() + { - // Update is called once per frame - void Update() - { - - } + } - public void interact() - { + // Update is called once per frame + void Update() + { + } + + public void interact() + { + if (hasQuest) + { + GameObject.Find("UIHandler").GetComponent().showMessage("SUCCESS;You got a quest from this NPC!"); + GameObject.Find("QuestLog").GetComponent().addQuest(); + hasQuest = false; + } + else + { + GameObject.Find("UIHandler").GetComponent().showMessage("ERROR;You already got the quest from this NPC!"); + } + GameObject.Find("QuestLog").GetComponent().removeQuests(); + } } -} +} \ No newline at end of file diff --git a/Assets/Scripts/Player.cs b/Assets/Scripts/Player.cs index 36efb58..e678963 100644 --- a/Assets/Scripts/Player.cs +++ b/Assets/Scripts/Player.cs @@ -693,5 +693,10 @@ namespace Assets.Scripts break; } } + + public int getLuck() + { + return luck; + } } } diff --git a/Assets/Scripts/QuestLog.cs b/Assets/Scripts/QuestLog.cs index 1c576a4..f896dec 100644 --- a/Assets/Scripts/QuestLog.cs +++ b/Assets/Scripts/QuestLog.cs @@ -19,16 +19,12 @@ namespace Assets.Scripts void Start() { quests = new Dictionary>(); - quests.Add("main", new List()); + quests.Add("find", new List()); GameObject newQuest = Instantiate(quest); newQuest.transform.SetParent(content.transform, false); FindQuest main = new FindQuest(newQuest); main.generateCityQuest(); - quests["main"].Add(main); - for (int i = 0; i < 20; i++) - { - addQuest(); - } + quests["find"].Add(main); } // Update is called once per frame @@ -39,9 +35,12 @@ namespace Assets.Scripts public void updateQuests(string key, object obj, int amount) { - foreach (Quest quest in quests[key]) + if (quests.ContainsKey(key)) { - quest.update(obj, amount); + foreach (Quest quest in quests[key]) + { + quest.update(obj, amount); + } } } @@ -100,6 +99,42 @@ namespace Assets.Scripts scrollBar.value = 0; content.transform.localPosition = new Vector3(0,0,0); } + + public void removeQuests() + { + Inventory inventory = GameObject.Find("Inventory").GetComponent(); + int luck = GameObject.Find("Player").GetComponent().getLuck(); + luck = luck + inventory.getEquipmentBonus()["LCK"]; + Dictionary> toDelete = new Dictionary>(); + foreach (string key in quests.Keys) + { + foreach (Quest quest in quests[key]) + { + if (quest.isFinishedQuest()) + { + if (!toDelete.ContainsKey(key)) + { + toDelete.Add(key, new List()); + } + toDelete[key].Add(quest); + } + } + } + + foreach (string key in toDelete.Keys) + { + foreach (Quest quest in toDelete[key]) + { + if (quest is CollectQuest) + { + ((CollectQuest)quest).removeItems(inventory); + } + quest.delete(); + quests[key].Remove(quest); + inventory.addItem(new Item(luck)); + } + } + } } } diff --git a/Assets/Scripts/Quests/CollectQuest.cs b/Assets/Scripts/Quests/CollectQuest.cs index 0874ec9..7e22218 100644 --- a/Assets/Scripts/Quests/CollectQuest.cs +++ b/Assets/Scripts/Quests/CollectQuest.cs @@ -49,31 +49,7 @@ namespace Assets.Scripts public void update(object obj, int amount) { Item item = (Item)obj; - if (keyword == "Slimeball" && item.getName().ToLower().Contains("slimeball")) - { - current = current + amount; - } - if (keyword == "Stone" && item.getName().ToLower().Contains("stone")) - { - current = current + amount; - } - if (keyword == "Wood" && item.getName().ToLower().Contains("wood")) - { - current = current + amount; - } - if (keyword == "Common" && item.getName().ToLower().Contains("common")) - { - current = current + amount; - } - if (keyword == "Rare" && item.getName().ToLower().Contains("rare")) - { - current = current + amount; - } - if (keyword == "Epic" && item.getName().ToLower().Contains("epic")) - { - current = current + amount; - } - if (keyword == "Legendary" && item.getName().ToLower().Contains("legendary")) + if (checkItem(item)) { current = current + amount; } @@ -81,7 +57,73 @@ namespace Assets.Scripts { isFinished = true; } + else + { + isFinished = false; + } + } + + public void removeItems(Inventory inventory) + { + Item item; + int counter = 0; + for (int i = 0; i < 3; i++) + { + foreach (GameObject slot in inventory.slots) + { + item = slot.GetComponent().getItem(i); + if (item != null) + { + if (checkItem(item)) + { + slot.GetComponent().removeItem(i); + counter++; + } + } + if (counter == goal) + { + break; + } + } + if (counter == goal) + { + break; + } + } + } + + private bool checkItem(Item item) + { + bool result = false; + if (keyword == "Slimeball" && item.getName().ToLower().Contains("slimeball")) + { + return true; + } + if (keyword == "Stone" && item.getName().ToLower().Contains("stone")) + { + return true; + } + if (keyword == "Wood" && item.getName().ToLower().Contains("wood")) + { + return true; + } + if (keyword == "Common" && item.getName().ToLower().Contains("common")) + { + return true; + } + if (keyword == "Rare" && item.getName().ToLower().Contains("rare")) + { + return true; + } + if (keyword == "Epic" && item.getName().ToLower().Contains("epic")) + { + return true; + } + if (keyword == "Legendary" && item.getName().ToLower().Contains("legendary")) + { + return true; + } + return result; } } - } diff --git a/Assets/Scripts/Quests/ExploreQuest.cs b/Assets/Scripts/Quests/ExploreQuest.cs index aadaf26..9d6caf8 100644 --- a/Assets/Scripts/Quests/ExploreQuest.cs +++ b/Assets/Scripts/Quests/ExploreQuest.cs @@ -9,17 +9,17 @@ namespace Assets.Scripts public ExploreQuest(GameObject display) : base(display) { Vector3 playerPos = GameObject.Find("Player").GetComponent().transform.position; - float coordX = getRandomNumber(20) - 10; - float coordZ = getRandomNumber(20) - 10; + float coordX = getRandomNumber(1000) - 500; + float coordZ = getRandomNumber(1000) - 500; coordinates = new Vector3(playerPos.x + coordX, 0, playerPos.z + coordZ); - questname = "Travel to " + coordinates.x + "/" + coordinates.z + "(X/Z)"; + questname = "Travel to " + Mathf.Floor(coordinates.x) + "/" + Mathf.Floor(coordinates.z) + "(X/Z)"; } override public void update(object obj, int amount) { Vector3 player = ((GameObject)obj).transform.position; - if (player.x - 5 >= coordinates.x && player.x + 5 <= coordinates.x && player.z - 5 >= coordinates.z && player.z + 5 <= coordinates.z) + if (player.x >= coordinates.x - 1 && player.x <= coordinates.x + 1 && player.z >= coordinates.z - 1 && player.z <= coordinates.z + 1) { isFinished = true; } diff --git a/Assets/Scripts/Quests/Quest.cs b/Assets/Scripts/Quests/Quest.cs index 7421c4e..c6c3f58 100644 --- a/Assets/Scripts/Quests/Quest.cs +++ b/Assets/Scripts/Quests/Quest.cs @@ -33,6 +33,10 @@ namespace Assets.Scripts { display.transform.Find("imgDone").GetComponent().color = new Color(255,255,255,255); } + else + { + display.transform.Find("imgDone").GetComponent().color = new Color(255, 255, 255, 0); + } float x = display.transform.localPosition.x; float z = display.transform.localPosition.z; display.transform.localPosition = new Vector3(x, positionY, z); @@ -47,6 +51,16 @@ namespace Assets.Scripts } return result; } + + public bool isFinishedQuest() + { + return isFinished; + } + + public void delete() + { + GameObject.Destroy(display); + } } } \ No newline at end of file