2023-05-10 16:50:27 +02:00

67 lines
2.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Newtonsoft.Json.Linq;
namespace Assets.Scripts
{
public class Door : MonoBehaviour
{
public bool hasInteracted = false;
bool isOpen = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void interact()
{
if (hasInteracted)
{
GameObject.Find("UIHandler").GetComponent<UIHandler>().showMessage("ERROR;You already tried this house");
}
else
{
int openChance = new System.Random().Next(4);
if(openChance == 0){
gameObject.GetComponent<Animator>().Play("DoorOpen");
Destroy(gameObject.GetComponent<BoxCollider>());
isOpen = true;
}
else{
GameObject.Find("UIHandler").GetComponent<UIHandler>().showMessage("ERROR;This house is locked.");
}
}
hasInteracted = true;
}
public string saveHouse(){
string result = "";
result = result + FileHandler.generateJSON("objectname", "\"" + transform.parent.name + "\",\r\n");
result = result + FileHandler.generateJSON("hasInteracted", "\"" + hasInteracted + "\",\r\n");
result = result + FileHandler.generateJSON("isOpen", "\"" + isOpen + "\",\r\n");
result = result + FileHandler.generateJSON("gotItem", "\"" + transform.parent.Find("chest").Find("Body").GetComponent<Chest>().saveChest() + "\"");
return result;
}
public void loadHouse(JToken json){
hasInteracted = bool.Parse(json["hasInteracted"].ToString());
isOpen = bool.Parse(json["isOpen"].ToString());
transform.parent.Find("chest").Find("Body").GetComponent<Chest>().loadChest(bool.Parse(json["gotItem"].ToString()));
if(isOpen){
gameObject.GetComponent<Animator>().Play("DoorOpen");
Destroy(gameObject.GetComponent<BoxCollider>());
}
}
}
}