Heroes_of_Hiis/Assets/Project Files/Scripts/JoonasP/CraftingTable.cs

94 lines
2.5 KiB
C#
Raw Normal View History

2022-04-25 09:55:41 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class CraftingTable : MonoBehaviour
{
public Transform item1;
public Transform item2;
public Transform socket1;
public Transform socket2;
public Transform output;
public GameObject startWand;
void Start()
{
item1 = null;
item2 = null;
}
public void Set1()
{
StartCoroutine(DelayCast(socket1,1));
}
public Transform Get1()
{
return item1;
}
public void Set2()
{
StartCoroutine(DelayCast(socket2, 2));
}
public Transform Get2()
{
return item2;
}
public void Craft()
{
//Currently simple if statement check crafting. Could be done better but will see if this system will be expanded
if (item1 == null || item2 == null) Debug.LogError("Missing item!");
else
{
if((item1.name.StartsWith("wand") && item2.name.StartsWith("Log")) || (item2.name.StartsWith("wand") && item1.name.StartsWith("Log")))
{
if (item1.name.StartsWith("wand"))
{
WandData data = item1.GetComponent<WandData>();
2022-04-25 09:55:41 +00:00
data.SetPower(data.power + 0.5f);
item1.transform.position = output.position;
Destroy(item2.gameObject);
item2 = null;
}
else
{
WandData data = item2.GetComponent<WandData>();
2022-04-25 09:55:41 +00:00
data.SetPower(data.power + 0.5f);
item2.transform.position = output.position;
Destroy(item1.gameObject);
item1 = null;
}
}
else if(item1.name.StartsWith("Log") && item2.name.StartsWith("Log"))
{
Instantiate(startWand, output.position, Quaternion.identity);
Destroy(item1.gameObject);
Destroy(item2.gameObject);
item1 = null;
item2 = null;
}
}
Debug.LogError("Invalid Recipe!");
}
IEnumerator DelayCast(Transform from, int i)
{
yield return new WaitForSeconds(0.5f);
RaycastHit hit;
if (Physics.Raycast(from.position + from.up, -from.up, out hit, Mathf.Infinity))
{
if (i == 1) item1 = hit.transform;
else item2 = hit.transform;
}
}
}