-
Notifications
You must be signed in to change notification settings - Fork 2
/
toucher.cs
377 lines (335 loc) · 11.5 KB
/
toucher.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class toucher : MonoBehaviour {
//Publics
public float camera_speed=3f;
public int ammount_of_cubes=15;
public Material active_cube_material;
public Material normal_cube_material;
//Privates for inspector
[System.NonSerialized] public bool cube_active=false;
[System.NonSerialized] public GameObject active_cube=null;
[System.NonSerialized] public bool cube_selection_is_open = false;
[System.NonSerialized] public Transform new_cube_typ;
[System.NonSerialized] public bool new_cube_get_placed;
[System.NonSerialized] public bool popup_opened = false;
[System.NonSerialized] public int c_ammount_of_cubes;
//Privates
private int c_ammount_typ1; //current ammount of cubes in the scene
private int c_ammount_typ2;
private int c_ammount_typ3;
private int c_ammount_typ4;
private int c_ammount_typ5;
private int c_ammount_typ6;
private int c_ammount_typ7;
private int c_ammount_typ8;
private int c_ammount_typ9;
private int c_ammount_typ10;
private int c_ammount_typ11;
private int c_ammount_typ12;
private int c_ammount_typ13;
private int ammount_typ1=46;
private int ammount_typ2=4;
private int ammount_typ3=7;
private int ammount_typ4=2;
private int ammount_typ5=4;
private int ammount_typ6=4;
private int ammount_typ7=6;
private int ammount_typ8=6;
private int ammount_typ9=4;
private int ammount_typ10=4;
private int ammount_typ11=12;
private int ammount_typ12=5;
private int ammount_typ13=2;
private float new_cube_x;
private float new_cube_y;
private float new_cube_z;
private bool dragging_camera = false;
private Transform cubes;
private float zoomSpeed;
private float dist;
private Vector3 center=Vector3.zero;
void OnEnable(){
Gesture.onDraggingE += OnDrag;
Gesture.onTouchUpE += OnTouchUp;
Gesture.onMouse1UpE += OnTouchUp;
Gesture.onTouchDownE +=OnTouchDown;
Gesture.onMouse1DownE +=OnTouchDown;
Gesture.onPinchE +=onPinch;
}
void OnDisable(){
Gesture.onDraggingE -= OnDrag;
Gesture.onTouchUpE -= OnTouchUp;
Gesture.onMouse1UpE -= OnTouchUp;
Gesture.onTouchDownE -=OnTouchDown;
Gesture.onMouse1DownE -=OnTouchDown;
}
void OnTouchUp(Vector2 touch){
dragging_camera=false;
zoomSpeed=0.0f;
}
void Update () {
//calculate the distance from center based on zoomSpeed
dist+=Time.deltaTime*zoomSpeed*0.01f;
//clamp the distance so it's within permmited range
dist=Mathf.Clamp(dist, -15, -3);
//set the camera rotation back to center, so we can calculate things from this point
transform.position=center;
//apply the distance
transform.position=transform.TransformPoint(new Vector3(0, 0, dist));
//use mouse scroll wheel to simulate pinch, sorry I sort of cheated here
zoomSpeed+=Input.GetAxis("Mouse ScrollWheel")*500*2;
}
void OnTouchDown(Vector2 touch){
//This function get called whenever a touch recives the screeen
//Ignore touch if some popup is open
if (popup_opened){
return;
}
//ignore touches on GUI elements
if (cube_selection_is_open==false){
if (touch.x<Screen.width-20 & touch.x>Screen.width-170 & touch.y>Screen.height-70 & touch.y<Screen.height-20){
if (active_cube!=null){
active_cube.renderer.material=normal_cube_material;
}
active_cube=null;
cube_active=false;
return;
}
}else{
if (touch.x<100 | touch.x>Screen.width-100){
if (active_cube!=null){
active_cube.renderer.material=normal_cube_material;
}
active_cube=null;
cube_active=false;
return;
}
}
//ignore touch on cube rotation gui element
if (cube_active & (active_cube!=null)){
float cx = camera.WorldToScreenPoint(active_cube.transform.position).x;
float cy = camera.WorldToScreenPoint(active_cube.transform.position).y;
//Right left
if (touch.x>cx-50 & touch.x<cx+50){
if (touch.y>cy-30 & touch.y<cy){
return;
}
}
//delete
if (touch.x>cx-15 & touch.x<cx+15){
if (touch.y>cy+30 & touch.y<cy+60){
return;
}
}
}
//detect if touch is on camera or cube
detect_camera_or_cube(touch);
}
void detect_camera_or_cube(Vector2 touch){
//Create raycasts to detect if touch is over cube
Ray ray = Camera.main.ScreenPointToRay(touch);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, Mathf.Infinity)){
if (hit.collider.gameObject.tag=="cuboro-cube"){
//check for cube
Vector3 position = new Vector3(hit.collider.gameObject.transform.position.x, hit.collider.gameObject.transform.position.y+1.0f, hit.collider.gameObject.transform.position.z);
GameObject[] objs = GameObject.FindGameObjectsWithTag("cuboro-cube");
bool found = false;
foreach (GameObject go in objs) {
if (go!=hit.collider.gameObject){
if (go.transform.position == position) {
found=true;
}
}
}
if (found==false){
if (active_cube!=hit.collider.gameObject){
//we touched another cube than before
if (cube_active){
deselct_active_cube();
}
hit.collider.gameObject.renderer.material=active_cube_material;
}
cube_active=true;
active_cube=hit.collider.gameObject;
}
}else{
if (hit.collider.gameObject.tag=="ground"){
dragging_camera=true;
cube_active=false;
deselct_active_cube();
active_cube=null;
}
}
}else{
//Move camera (dind't hit any objects)
dragging_camera=true;
cube_active=false;
deselct_active_cube();
active_cube=null;
}
}
public void deselct_active_cube(){
if (active_cube!=null){
active_cube.renderer.material=normal_cube_material;
}
}
void onPinch(PinchInfo pinfo){
//This function get called when a pinch is done on the screen -> zoom
zoomSpeed-=pinfo.magnitude*3;
}
void OnDrag(DragInfo draginfo){
//This function get called whenever a touch get draged on the screen.
//Check if popup is open
if (popup_opened){
new_cube_get_placed=false;
new_cube_typ=null;
return;
}
//Check if a new cube gets placed in the scene
if (new_cube_get_placed){
if (draginfo.pos.x>100 & draginfo.pos.x<Screen.width-100){
Ray ray = Camera.main.ScreenPointToRay(draginfo.pos);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, Mathf.Infinity)){
//if the max of cubes is reached, dont create cube
if (c_ammount_of_cubes>=ammount_of_cubes | is_max_of_ammount_reached(new_cube_typ)){
new_cube_get_placed=false;
return;
}
//create new cube in scene
MoveCube(hit.point, new_cube_typ);
new_cube_get_placed=false;
//detect_camera_or_cube(draginfo.pos);
c_ammount_of_cubes++;
update_ammount(new_cube_typ, true);
}
}
}
//Check if cube gets draged
if (cube_active & dragging_camera==false){
Ray ray = Camera.main.ScreenPointToRay(draginfo.pos);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, Mathf.Infinity)){
MoveCube(hit.point, null);
}
}
//Check if camera get rotated
if (dragging_camera){
Camera.mainCamera.transform.RotateAround(Vector3.zero, Vector3.up, draginfo.delta.x/10);
}
}
void MoveCube(Vector3 point, Transform cube_typ){
//This function moves a selected cube arround in the scene.
//round coordinates -> Cube gets into grid
new_cube_x=Mathf.Round(point.x);
new_cube_y=0.5f;
new_cube_z=Mathf.Round(point.z);
//Check if a cube is already at the position we wana locate the cube
//When we found another cube on that position => Place selected cube obove cube which is targeted by the finger
bool found=true;
int exit=0;
while (found==true){
Vector3 position = new Vector3(new_cube_x,new_cube_y,new_cube_z);
GameObject[] objs = GameObject.FindGameObjectsWithTag("cuboro-cube");
bool found_round=false;
foreach (GameObject go in objs) {
if (go!=active_cube){
if (go.transform.position == position) {
found_round=true;
}
}
}
if (found_round){
found=true;
new_cube_y=new_cube_y+1.0f;
}else{
found=false;
}
exit=exit+1;
}
if (new_cube_y>4.5f | new_cube_x>4 | new_cube_z>2 | new_cube_x<-3 | new_cube_z<-3){
return;
}
//move cube
if (cube_typ==null){
if (active_cube!=null){
active_cube.transform.position=new Vector3(new_cube_x, new_cube_y, new_cube_z);
}
}else{
Instantiate(cube_typ, new Vector3(new_cube_x, new_cube_y, new_cube_z), Quaternion.identity);
//find created cube
GameObject[] objs = GameObject.FindGameObjectsWithTag("cuboro-cube");
foreach (GameObject go in objs) {
CubeBehavior CB = (CubeBehavior) go.GetComponent(typeof(CubeBehavior));
if (CB.number==-1){
//we found the instantated cube
active_cube=go;
cube_active=true;
go.renderer.material=active_cube_material;
//reset the number in cube
CB.number=1;
}
}
}
}
//removes the selected cube
public void remove_cube(){
update_ammount(active_cube.gameObject.transform, false);
Destroy(active_cube.gameObject);
cube_active=false;
active_cube=null;
c_ammount_of_cubes--;
}
//remove all cubes from stage
public void clear_stage(){
int test = 0;
GameObject[] objs = GameObject.FindGameObjectsWithTag("cuboro-cube");
foreach (GameObject go in objs) {
update_ammount(go.transform, false);
Destroy(go);
c_ammount_of_cubes--;
}
}
void update_ammount(Transform go, bool up){
//Get cube typ
CubeBehavior CB = (CubeBehavior) go.GetComponent(typeof(CubeBehavior));
int typ = CB.typ;
//Update correct typ
if (typ==1){if (up){c_ammount_typ1++;}else{c_ammount_typ1--;};}
if (typ==2){if (up){c_ammount_typ2++;}else{c_ammount_typ2--;};}
if (typ==3){if (up){c_ammount_typ3++;}else{c_ammount_typ3--;};}
if (typ==4){if (up){c_ammount_typ4++;}else{c_ammount_typ4--;};}
if (typ==5){if (up){c_ammount_typ5++;}else{c_ammount_typ5--;};}
if (typ==6){if (up){c_ammount_typ6++;}else{c_ammount_typ6--;};}
if (typ==7){if (up){c_ammount_typ7++;}else{c_ammount_typ7--;};}
if (typ==8){if (up){c_ammount_typ8++;}else{c_ammount_typ8--;};}
if (typ==9){if (up){c_ammount_typ9++;}else{c_ammount_typ9--;};}
if (typ==10){if (up){c_ammount_typ10++;}else{c_ammount_typ10--;};}
if (typ==11){if (up){c_ammount_typ11++;}else{c_ammount_typ11--;};}
if (typ==12){if (up){c_ammount_typ12++;}else{c_ammount_typ12--;};}
if (typ==13){if (up){c_ammount_typ13++;}else{c_ammount_typ13--;};}
}
bool is_max_of_ammount_reached(Transform go){
//Get cube typ
CubeBehavior CB = (CubeBehavior) go.GetComponent(typeof(CubeBehavior));
int typ = CB.typ;
//Check ammount
if (typ==1){if (c_ammount_typ1<ammount_typ1){return false;}else{return true;}}
if (typ==2){if (c_ammount_typ2<ammount_typ2){return false;}else{return true;}}
if (typ==3){if (c_ammount_typ3<ammount_typ3){return false;}else{return true;}}
if (typ==4){if (c_ammount_typ4<ammount_typ4){return false;}else{return true;}}
if (typ==5){if (c_ammount_typ5<ammount_typ5){return false;}else{return true;}}
if (typ==6){if (c_ammount_typ6<ammount_typ6){return false;}else{return true;}}
if (typ==7){if (c_ammount_typ7<ammount_typ7){return false;}else{return true;}}
if (typ==8){if (c_ammount_typ8<ammount_typ8){return false;}else{return true;}}
if (typ==9){if (c_ammount_typ9<ammount_typ9){return false;}else{return true;}}
if (typ==10){if (c_ammount_typ10<ammount_typ10){return false;}else{return true;}}
if (typ==11){if (c_ammount_typ11<ammount_typ11){return false;}else{return true;}}
if (typ==12){if (c_ammount_typ12<ammount_typ12){return false;}else{return true;}}
if (typ==13){if (c_ammount_typ13<ammount_typ13){return false;}else{return true;}}
return false;
}
}