-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForceFieldCreator.cs
168 lines (127 loc) · 4.59 KB
/
ForceFieldCreator.cs
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
using HoloToolkit.Unity.InputModule;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using HoloToolkit.Unity;
using System.Linq;
public class ForceFieldCreator : Singleton<ForceFieldCreator>, IInputHandler, ISourceStateHandler
{
private int totalNumForceFields;
private IInputSource currentInputSource;
internal void DecrementForceFieldCount()
{
this.totalNumForceFields--;
}
private uint currentInputSourceId;
public GameObject cylinderPrefab;
public int maxNumForceFields = 100;
public int forceFieldLifeSpan = 30;
public float totalForceFieldArea = 10f;
private const float startArea = 0.25f * (float)Math.PI;
//TODO: re-org into start paint and end paint functions?
private List<GameObject> subMeshes; //TODO: How do I clear this? Will have to add this in the future
private Material material;
public float CurrentTotalForceFieldArea
{
get
{
float totalArea = 0f;
foreach (GameObject forceField in subMeshes)
{
totalArea += this.getForceFieldArea(forceField);
}
return totalArea;
}
}
public float StartArea
{
get { return startArea; }
}
public void OnInputDown(InputEventData eventData)
{
if (!eventData.InputSource.SupportsInputInfo(eventData.SourceId, SupportedInputInfo.Position))
{
// The input source must provide positional data for this script to be usable
return;
}
Debug.Log("Start hold");
currentInputSource = eventData.InputSource;
currentInputSourceId = eventData.SourceId;
//TODO: modularize into a util function?
Vector3 handPosition;
currentInputSource.TryGetPosition(currentInputSourceId, out handPosition);
handPosition += Camera.main.transform.forward * 0.5f;
//get total force field area
float totalArea = CurrentTotalForceFieldArea;
//now spawn a point in that area:
if (this.totalNumForceFields < this.maxNumForceFields && totalArea + startArea <= totalForceFieldArea) //make sure we can add another force field and it doesn't go over the limit
{
GameObject meshGameObject = (GameObject)Instantiate(cylinderPrefab);
HandDraggableAndScalable handDraggable = meshGameObject.AddComponent<HandDraggableAndScalable>(); //make it draggable
handDraggable.IsDraggingAndScalingEnabled = true;
this.totalNumForceFields++;
Destroy(meshGameObject, forceFieldLifeSpan);
/*
MeshFilter subMeshFilter = meshGameObject.AddComponent<MeshFilter>();
subMeshFilter.transform.position = handPosition;
subMeshFilter.mesh = mesh;
MeshRenderer subMeshRenderer = meshGameObject.AddComponent<MeshRenderer>();
subMeshRenderer.materials[0] = material;
*/
meshGameObject.transform.localScale = new Vector3(0.5f, 0.01f, 0.5f);
meshGameObject.transform.localPosition = handPosition;
subMeshes.Add(meshGameObject);
}
}
public void OnInputUp(InputEventData eventData)
{
currentInputSource = null;
currentInputSourceId = 0;
}
public void OnSourceDetected(SourceStateEventData eventData)
{
//throw new NotImplementedException();
}
public void OnSourceLost(SourceStateEventData eventData)
{
//throw new NotImplementedException();
}
// Use this for initialization
void Start()
{
subMeshes = new List<GameObject>();
material = Resources.Load("Materials/DefaultMaterial", typeof(Material)) as Material;
this.Enable(true);
}
// Update is called once per frame
void Update()
{
//cleanup all destroyed objects
subMeshes = subMeshes.Where(x => x != null).ToList();
//TODO: get all areas?
}
public void Enable(bool enabled)
{
if (enabled)
{
InputManager.Instance.PushModalInputHandler(gameObject);
this.enabled = true;
}
else if (this.enabled)
{
InputManager.Instance.PopModalInputHandler();
this.enabled = false;
}
}
public ForceFieldCreator()
{
currentInputSource = null;
currentInputSourceId = 0;
this.totalNumForceFields = 0;
}
private float getForceFieldArea(GameObject gameObject)
{
return (float)(Math.Pow(gameObject.transform.localScale.x, 2) * Math.PI);
}
}