238 lines
8.0 KiB
C#
238 lines
8.0 KiB
C#
using Assets.Scripts.Player;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Assets.Scripts
|
|
{
|
|
public class QuestLog : MonoBehaviour
|
|
{
|
|
public Dictionary<string, List<Quest>> quests;
|
|
public GameObject content;
|
|
public Scrollbar scrollBar;
|
|
System.Random rand = new System.Random();
|
|
public GameObject quest;
|
|
public Texture finished;
|
|
public GameObject uiHandler;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
if (quests == null && PlayerPrefs.GetInt("isLoad") != 1)
|
|
{
|
|
quests = new Dictionary<string, List<Quest>>();
|
|
quests.Add("find", new List<Quest>());
|
|
FindQuest main = new FindQuest(createQuestDisplay());
|
|
main.generateCityQuest();
|
|
quests["find"].Add(main);
|
|
}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
public void updateQuests(string key, object obj, int amount)
|
|
{
|
|
if (quests.ContainsKey(key))
|
|
{
|
|
foreach (Quest quest in quests[key])
|
|
{
|
|
quest.update(obj, amount);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void addQuest()
|
|
{
|
|
|
|
int index = rand.Next(4);
|
|
string type = "";
|
|
Quest questItem;
|
|
switch (index)
|
|
{
|
|
case 0:
|
|
type = "collect";
|
|
questItem = new CollectQuest(createQuestDisplay());
|
|
break;
|
|
case 1:
|
|
type = "kill";
|
|
questItem = new KillQuest(createQuestDisplay());
|
|
break;
|
|
case 2:
|
|
type = "find";
|
|
questItem = new FindQuest(createQuestDisplay());
|
|
break;
|
|
/*case 3:
|
|
type = "craft";
|
|
break;*/
|
|
case 3:
|
|
type = "explore";
|
|
questItem = new ExploreQuest(createQuestDisplay());
|
|
break;
|
|
default:
|
|
questItem = new Quest(createQuestDisplay());
|
|
break;
|
|
}
|
|
if (!quests.ContainsKey(type))
|
|
{
|
|
quests.Add(type, new List<Quest>());
|
|
}
|
|
quests[type].Add(questItem);
|
|
}
|
|
|
|
private GameObject createQuestDisplay()
|
|
{
|
|
GameObject newQuest = Instantiate(quest);
|
|
newQuest.transform.SetParent(content.transform, false);
|
|
return newQuest;
|
|
}
|
|
|
|
public void showQuests()
|
|
{
|
|
content.GetComponent<RectTransform>().sizeDelta = new Vector2(0, 10);
|
|
float y = -37.5f;
|
|
foreach (string key in quests.Keys)
|
|
{
|
|
foreach (Quest quest in quests[key])
|
|
{
|
|
quest.show(y);
|
|
y = y - 75;
|
|
content.GetComponent<RectTransform>().sizeDelta = new Vector2(0, content.GetComponent<RectTransform>().sizeDelta.y + 75);
|
|
}
|
|
}
|
|
scrollBar.value = 0;
|
|
content.transform.localPosition = new Vector3(0,0,0);
|
|
}
|
|
|
|
public void removeQuests()
|
|
{
|
|
Inventory inventory = GameObject.Find("Inventory").GetComponent<Inventory>();
|
|
int luck = GameObject.Find("Player").GetComponent<PlayerGameObject>().getPlayerStat("Luck").getAmount();
|
|
luck = luck + inventory.getEquipmentBonus()["LCK"];
|
|
Dictionary<string, List<Quest>> toDelete = new Dictionary<string, List<Quest>>();
|
|
foreach (string key in quests.Keys)
|
|
{
|
|
foreach (Quest quest in quests[key])
|
|
{
|
|
if (quest.isFinishedQuest())
|
|
{
|
|
if (!toDelete.ContainsKey(key))
|
|
{
|
|
toDelete.Add(key, new List<Quest>());
|
|
}
|
|
toDelete[key].Add(quest);
|
|
}
|
|
}
|
|
}
|
|
int rand = new System.Random().Next(100) + 1;
|
|
foreach (string key in toDelete.Keys)
|
|
{
|
|
foreach (Quest quest in toDelete[key])
|
|
{
|
|
rand = new System.Random().Next(4);
|
|
if (quest is CollectQuest)
|
|
{
|
|
((CollectQuest)quest).removeItems(inventory);
|
|
}
|
|
quest.delete();
|
|
quests[key].Remove(quest);
|
|
if(rand == 1){
|
|
inventory.addItem(new Equipment(luck));
|
|
}
|
|
else{
|
|
inventory.addItem(new Item(luck, false));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public string saveGame()
|
|
{
|
|
string result = "";
|
|
int counter = 0;
|
|
int count = 0;
|
|
foreach (string key in quests.Keys)
|
|
{
|
|
counter = 0;
|
|
result = result + "\""+key+"\": {\r\n";
|
|
foreach (Quest quest in quests[key])
|
|
{
|
|
result = result + "\"quest" + counter + "\": {\r\n";
|
|
result = result + quest.saveQuest();
|
|
result = result + "\r\n}";
|
|
if (counter < quests[key].Count - 1)
|
|
{
|
|
result = result + ",\r\n";
|
|
}
|
|
counter++;
|
|
}
|
|
result = result + "\r\n}";
|
|
if (count < quests.Keys.Count - 1)
|
|
{
|
|
result = result + ",\r\n";
|
|
}
|
|
count++;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private void clearQuestlog(){
|
|
int children = content.transform.childCount;
|
|
for(int i = 0; i < children; i++){
|
|
Destroy(content.transform.GetChild(i).gameObject);
|
|
}
|
|
}
|
|
|
|
public void loadQuests(JToken json)
|
|
{
|
|
var jsonData = JObject.Parse(json.ToString()).Children();
|
|
List<JToken> keywords = jsonData.Children().ToList();
|
|
List<JToken> quests;
|
|
string key = "";
|
|
Quest questItem;
|
|
this.quests = new Dictionary<string, List<Quest>>();
|
|
this.quests.Clear();
|
|
clearQuestlog();
|
|
foreach (JToken keyword in keywords)
|
|
{
|
|
jsonData = JObject.Parse(keyword.ToString()).Children();
|
|
quests = jsonData.Children().ToList();
|
|
foreach (JToken quest in quests)
|
|
{
|
|
key = quest["questname"].ToString().Split(' ')[0].ToLower();
|
|
if (!this.quests.ContainsKey(key))
|
|
{
|
|
this.quests.Add(key, new List<Quest>());
|
|
}
|
|
switch (key)
|
|
{
|
|
case "collect":
|
|
questItem = new CollectQuest(quest, createQuestDisplay());
|
|
break;
|
|
case "kill":
|
|
questItem = new KillQuest(quest, createQuestDisplay());
|
|
break;
|
|
case "find":
|
|
questItem = new FindQuest(quest, createQuestDisplay());
|
|
break;
|
|
case "explore":
|
|
questItem = new ExploreQuest(quest, createQuestDisplay());
|
|
break;
|
|
default:
|
|
questItem = new Quest(quest, createQuestDisplay());
|
|
break;
|
|
}
|
|
this.quests[key].Add(questItem);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|