Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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>();
//Variables for AngleMode distinction
public bool angleModeIsFirstPointSelected = false;
public Fact angleModeFirstPointSelected = null;
public bool angleModeIsSecondPointSelected = false;
public Fact angleModeSecondPointSelected = null;
//Solving game parameters
public GameObject snapZoneTop;
public GameObject snapZoneBottom;
public static Vector3 solutionVector;
public static bool solved = false;
// Start is called before the first frame update
void Start()
{
CommunicationEvents.ToolModeChangedEvent.AddListener(OnToolModeChanged);
// CommunicationEvents.SnapEvent.AddListener(Rocket);
//We dont want to have this here anymore...
//CommunicationEvents.RemoveFactEvent.AddListener(DeleteFact);
NextEmpties.Add(0);
//Calculate Solution-Vector
solutionVector = snapZoneTop.transform.position - snapZoneBottom.transform.position;
}
// 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 RayFact AddRayFact(int pid1, int pid2, int id)
{
Facts.Insert(id, new RayFact(id, pid1, pid2));
var oLid = GetFirstEmptyID();
Facts.Insert(oLid, new OnLineFact(oLid, pid1, id));
oLid = GetFirstEmptyID();
Facts.Insert(oLid, new OnLineFact(oLid, 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.Lid == id && oLFact.Pid == hit.transform.gameObject.GetComponent<FactObject>().Id))
{
exists = true;
break;
}
}
}
if (!exists)
{
oLid = GetFirstEmptyID();
var olF = new OnLineFact(oLid, hit.transform.gameObject.GetComponent<FactObject>().Id, id);
Facts.Insert(oLid, olF);
}
}
return Facts.Find(x => x.Id == id) as RayFact;
}
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)
{
switch (ActiveToolMode)
{
case ToolMode.CreateLineMode:
foreach (Fact fact in Facts)
{
if (typeof(LineFact).IsInstanceOfType(fact))
{
LineFact line = (LineFact)fact;
if (line.Pid1 == ids[0] && line.Pid2 == ids[1])
{
return true;
}
}
}
return false;
case ToolMode.CreateAngleMode:
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 true;
}
}
}
return false;
default:
return false;
}
}
public static Boolean gameSolved() {
Vector3 tempDir1 = new Vector3(0, 0, 0);
Vector3 tempDir2 = new Vector3(0, 0, 0);
if (solved == true)
return true;
else {
//Look for solutionFact in global factList
foreach (Fact fact in Facts)
{
if (typeof(LineFact).IsInstanceOfType(fact))
{
tempDir1 = ((PointFact)Facts.Find(x => x.Id == ((LineFact)fact).Pid1)).Point - ((PointFact)Facts.Find(x => x.Id == ((LineFact)fact).Pid2)).Point;
tempDir2 = ((PointFact)Facts.Find(x => x.Id == ((LineFact)fact).Pid2)).Point - ((PointFact)Facts.Find(x => x.Id == ((LineFact)fact).Pid1)).Point;
if (solutionVector == tempDir1 || solutionVector == tempDir2)
{
solved = true;
return true;
}
}
}
return false;
}
}
/*
//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()));
}
//Creating 90-degree Angles
public void SmallRocket(RaycastHit hit, int idA)
{
//enable collider to measure angle to the treetop
int idB = this.GetFirstEmptyID();
CommunicationEvents.AddFactEvent.Invoke(this.AddPointFact(hit, idB));
Facts[idB].Representation.GetComponentInChildren<Collider>().enabled = true;
//third point with unknown height
int idC = this.GetFirstEmptyID();
var skyHit = hit;
skyHit.point = (Facts[idA] as PointFact).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()));
}*/
public void OnToolModeChanged(ToolMode ActiveToolMode)
{
//TODO: instead of enabling/disabling colliders we want to change the raycast mask
switch (ActiveToolMode)
{
case ToolMode.MarkPointMode:
//If MarkPointMode is activated we want to have the ability to mark the point
//everywhere, independent of already existing facts
foreach (Fact fact in Facts)
{
GameObject gO = fact.Representation;
if (gO == null) continue;
if ((gO.layer == LayerMask.NameToLayer("Ray")))
gO.GetComponentInChildren<Collider>().enabled = true;
}
break;
case ToolMode.CreateRayMode:
//same as for line mode atm
case ToolMode.CreateLineMode:
//If CreateLineMode is activated we want to have the ability to select points for the Line
//but we don't want to have the ability to select Lines or Angles
foreach (Fact fact in Facts)
{
GameObject gO = fact.Representation;
if (gO == null) continue;
if (gO.layer == LayerMask.NameToLayer("Line") || gO.layer == LayerMask.NameToLayer("Angle")|| gO.layer == LayerMask.NameToLayer("Ray"))
{
gO.GetComponentInChildren<Collider>().enabled = false;
}
else if (gO.layer == LayerMask.NameToLayer("Point"))
{
gO.GetComponentInChildren<Collider>().enabled = true;
}
}
break;
case ToolMode.CreateAngleMode:
//If CreateAngleMode is activated we want to have the ability to select Points for the Angle
//but we don't want to have the ability to select Lines or Angles
foreach (Fact fact in Facts)
{
GameObject gO = fact.Representation;
if (gO == null) continue;
if (gO.layer == LayerMask.NameToLayer("Line") || gO.layer == LayerMask.NameToLayer("Angle"))
{
gO.GetComponentInChildren<Collider>().enabled = false;
}
else if (gO.layer == LayerMask.NameToLayer("Point"))
{
gO.GetComponentInChildren<Collider>().enabled = true;
}
}
break;
/*
case ToolMode.DeleteMode:
//If DeleteMode is activated we want to have the ability to delete every Fact
//independent of the concrete type of fact
foreach (Fact fact in Facts)
{
GameObject gO = fact.Representation;
gO.GetComponentInChildren<Collider>().enabled = true;
}
break;*/
case ToolMode.ExtraMode:
/*foreach (Fact fact in Facts)
{
}
*/
break;
}
//Stop PreviewEvents in ShineThings on ToolModeChange
CommunicationEvents.StopPreviewsEvent.Invoke(null);
}
}