Newer
Older
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using static CommunicationEvents;
public class FactManager : MonoBehaviour
{
public GameObject SmartMenu;
private List<int> NextEmpties = new List<int>();
Richard Marcus
committed
// Start is called before the first frame update
void Start()
{
// CommunicationEvents.SnapEvent.AddListener(Rocket);
//We dont want to have this here anymore...
//CommunicationEvents.RemoveFactEvent.AddListener(DeleteFact);
NextEmpties.Add(0);
}
Richard Marcus
committed
// Update is called once per frame
void Update()
{
}
//TODO: change the return find....
public PointFact AddPointFact(RaycastHit hit, int id)
{
Facts.Insert(id, new PointFact(id, hit.point, hit.normal));
return Facts.Find(x => x.Id == id) as PointFact;
}
public LineFact AddLineFact(int pid1, int pid2, int id)
{
Facts.Insert(id, new LineFact(id, pid1, pid2));
return Facts.Find(x => x.Id == id) as LineFact;
}
public List<Fact> AddRayFact(int pid1, int pid2, int id)
{
Facts.Insert(id, new RayFact(id, pid1, pid2));
var oLid1 = GetFirstEmptyID();
Facts.Insert(oLid1, new OnLineFact(oLid1, pid1, id));
var oLid2 = GetFirstEmptyID();
Facts.Insert(oLid2, new OnLineFact(oLid2, pid2, id));
var p1 = Facts.Find(x => x.Id == pid1);
var p2 = Facts.Find(x => x.Id == pid2);
Vector3 dir = p2.Representation.transform.position - p1.Representation.transform.position;
// Bit shift the index of the layer Point to get a bit mask
int layerMask = 1 << LayerMask.NameToLayer("Point");
// This casts rays only against colliders in layer 8
RaycastHit[] hits;
hits = Physics.RaycastAll(p1.Representation.transform.position - dir * 1000, dir, Mathf.Infinity, layerMask);
Debug.Log(hits.Length + " hits");
for (int i = 0; i < hits.Length; i++)
{
RaycastHit hit = hits[i];
bool exists = false;
foreach (Fact fact in Facts)
{
if (typeof(OnLineFact).IsInstanceOfType(fact))
{
OnLineFact oLFact = (OnLineFact)fact;
if ((oLFact.Rid == id && oLFact.Pid == hit.transform.gameObject.GetComponent<FactObject>().Id))
{
exists = true;
break;
}
}
}
if (!exists)
{
var anotherOLid = GetFirstEmptyID();
var olF = new OnLineFact(anotherOLid, hit.transform.gameObject.GetComponent<FactObject>().Id, id);
Facts.Insert(anotherOLid, olF);
List<Fact> returnedFacts = new List<Fact>();
returnedFacts.Add(Facts.Find(x => x.Id == id) as RayFact);
returnedFacts.Add(Facts.Find(x => x.Id == oLid1) as OnLineFact);
returnedFacts.Add(Facts.Find(x => x.Id == oLid2) as OnLineFact);
return returnedFacts;
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
}
public AngleFact AddAngleFact(int pid1, int pid2, int pid3, int id)
{
Facts.Insert(id, new AngleFact(id, pid1, pid2, pid3));
return Facts.Find(x => x.Id == id) as AngleFact;
}
public void DeleteFact(Fact fact)
{
if (Facts.Contains(fact)) {
NextEmpties.Add(fact.Id);
//Facts.RemoveAt(fact.Id);
Facts.Remove(Facts.Find(x => x.Id == fact.Id));
CommunicationEvents.RemoveFactEvent.Invoke(fact);
}
}
public int GetFirstEmptyID()
{
/* for (int i = 0; i < Facts.Length; ++i)
{
if (Facts[i] == "")
return i;
}
return Facts.Length - 1;*/
NextEmpties.Sort();
int id = NextEmpties[0];
NextEmpties.RemoveAt(0);
if (NextEmpties.Count == 0)
NextEmpties.Add(id + 1);
Debug.Log("place fact at " + id);
return id;
}
public Boolean factAlreadyExists(int[] ids)
{
if (GadgetManager.activeGadget.GetType() == typeof(Tape)) {
foreach (Fact fact in Facts)
{
if (typeof(LineFact).IsInstanceOfType(fact))
LineFact line = (LineFact)fact;
if (line.Pid1 == ids[0] && line.Pid2 == ids[1])
}
return false;
}
else if (GadgetManager.activeGadget.GetType() == typeof(AngleTool))
{
foreach (Fact fact in Facts)
{
if (typeof(AngleFact).IsInstanceOfType(fact))
AngleFact angle = (AngleFact)fact;
if (angle.Pid1 == ids[0] && angle.Pid2 == ids[1] && angle.Pid3 == ids[2])
}
return false;
else
return false;
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
}
/*
//automatic 90 degree angle construction
public void Rocket(RaycastHit hit)
{
int idA, idB, idC;
//usual point
idA = this.GetFirstEmptyID();
CommunicationEvents.AddFactEvent.Invoke(this.AddPointFact(hit, idA));
//second point
idB = this.GetFirstEmptyID();
var shiftedHit = hit;
var playerPos = Camera.main.transform.position;
playerPos.y = hit.point.y;
shiftedHit.point = playerPos;
CommunicationEvents.AddFactEvent.Invoke(this.AddPointFact(shiftedHit, idB));
//third point with unknown height
idC = this.GetFirstEmptyID();
var skyHit = hit;
skyHit.point += Vector3.up * 20;
CommunicationEvents.AddFactEvent.Invoke(this.AddPointFact(skyHit, idC));
//lines
CommunicationEvents.AddFactEvent.Invoke(this.AddLineFact(idA, idB, this.GetFirstEmptyID()));
//lines
CommunicationEvents.AddFactEvent.Invoke(this.AddLineFact(idA, idC, this.GetFirstEmptyID()));
//90degree angle
CommunicationEvents.AddFactEvent.Invoke(this.AddAngleFact(idB, idA, idC, GetFirstEmptyID()));
}