-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFallingLaunch.js
338 lines (270 loc) · 10.8 KB
/
FallingLaunch.js
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
#pragma strict
static var Analytics : AnalyticsUtil;
static var flipMultiplier : float = 1.0;
static var levelEndSlowdown : float = 0.0;
static var alreadyLaunched : boolean = false;
static var NewGamePlus : boolean = false;
var targetFPS : int = 30;
static var isTablet : boolean = false;
static var tiltable : boolean = false;
static var initialInputDeviceOrientation : DeviceOrientation;
static var cachedScreenOrientation : ScreenOrientation;
static var hasSetAccel : boolean = false;
static var restPosition : Vector3;
static var neutralPosFlat : Vector3 = Vector3(0,0,-1.0);
static var neutralPosTiltedRegular : Vector3 = Vector3(.6,0,-.9);
static var neutralPosTiltedFlipped : Vector3 = Vector3(-.6,0,-.9);
static var neutralPosVerticalRegular = Vector3(1.0,0,0.0);
static var neutralPosVerticalFlipped = Vector3(-1.0,0,0.0);
static var neutralPosVertical : Vector3 = neutralPosVerticalRegular;
static var neutralPosTilted : Vector3 = neutralPosTiltedRegular;
static var accelerator : Vector3;
static var calibrationRotation : Quaternion;
static var acceleratorSnapshot : Vector3;
static var invertHorizAxisVal : int;
static var invertVertAxisVal : int;
static var LoadedLatestLevel : boolean = false;
static var levelAchieved : int;
static var debugMode : boolean = false; // true;
var testFlightToken : String;
static var isVRMode : boolean = false;
static var vrModeAnalyticsString : String = "nonVRMode:";
static var shouldShowVRIntroUI : boolean = false;
static var showingVREndGameUI : boolean = false;
//GameAnalytics variables
static var secondsAlive : float = 0;
static var secondsInLevel : float = 0;
static var thisLevel : String = "unknownLevel";
static var thisLevelArea : String = "start";
//var myTimer : GAUserTimer;
enum iPads {
iPadUnknown,
iPad1Gen,
iPad2Gen,
iPad3Gen,
iPad4Gen,
iPadMini1Gen
};
function Awake () {
isVRMode = false; // User can override this via start menu UI
}
function Start () {
// PlayerPrefs.DeleteAll();
if (!alreadyLaunched) {
Analytics = GetComponent.<AnalyticsUtil>();
// TestFlightUnity.TestFlight.TakeOff( testFlightToken );
if (Debug.isDebugBuild) {
Debug.Log("Your device orientation is " + Input.deviceOrientation + "!");
}
initialInputDeviceOrientation = Input.deviceOrientation;
cachedScreenOrientation = Screen.orientation;
// this is necessary to override Unity 4's auto-orientation code
Input.compensateSensors = false;
// Debug.Log("Device orientation after Input.compensateSensors = false is " + Input.deviceOrientation);
var iOSGen = iOS.Device.generation;
// Debug.Log("this is an " + iOSGen + " device!");
// Reduce target framerate for very old iOS devices:
if (iOSGen == UnityEngine.iOS.DeviceGeneration.iPad1Gen || iOSGen == UnityEngine.iOS.DeviceGeneration.iPad2Gen ||
iOSGen == UnityEngine.iOS.DeviceGeneration.iPhone4 || iOSGen == UnityEngine.iOS.DeviceGeneration.iPodTouch4Gen ||
iOSGen.ToString().Contains("iPhone3G")) {
QualitySettings.DecreaseLevel(false);
targetFPS = 30;
}
else {
targetFPS = 60;
}
var screenDPI : float = Screen.dpi;
var screenWidthInInches: float = (Screen.width / screenDPI);
var screenHeightInInches: float = (Screen.height / screenDPI);
// Debug.Log("Screen DPI: " + screenDPI);
// Debug.Log("Screen width in inches: " + screenWidthInInches);
// Debug.Log("Screen height in inches: " + screenHeightInInches);
// DPI/screen size no longer a good heuristic for tablet vs. phone, so just use the iPad string regex:
// var hasLargeScreen: boolean = screenDPI > 0 && (screenWidthInInches > 8 && screenHeightInInches > 5);
if (iOSGen.ToString().Contains("iPad")) {
// Debug.Log("Looks like a tablet!");
isTablet = true;
} else {
// Debug.Log("Based on device generation name string, not a tablet...");
isTablet = false;
}
if (!Input.gyro.enabled) {
Debug.Log("Your device doesn't have a gyroscope...");
}
// if ((iOSGen &&
// (iPads.iPad1Gen | iPads.iPad2Gen | iPads.iPad3Gen | iPads.iPad4Gen | iPads.iPadMini1Gen | iPads.iPadUnknown)) != 0) {
flipMultiplier = isTablet ? 2 * flipMultiplier : 1.5 * flipMultiplier;
DontDestroyOnLoad (this);
alreadyLaunched = true;
Application.targetFrameRate = targetFPS;
Application.LoadLevel("Falling-scene-menu");
}
else {
Destroy(this.gameObject);
}
// myTimer = new GAUserTimer("Timer", "Session Length");
// myTimer.Start();
//Calibrate();
//Calibrating in Start here was unwise, in case the user is swinging
//the device around and the accelerometer readings haven't settled yet.
}
function EnableVRMode () {
isVRMode = true;
vrModeAnalyticsString = "isVRMode:";
}
function DisableVRMode () {
isVRMode = false;
vrModeAnalyticsString = "nonVRMode:";
}
function SetAxesInversion () {
if (PlayerPrefs.GetInt("invertHorizAxis", 0) == 1) {invertHorizAxisVal = -1;}
else {invertHorizAxisVal = 1;}
if (PlayerPrefs.GetInt("invertVertAxis", 0) == 1) {invertVertAxisVal = -1;}
else {invertVertAxisVal = 1;}
}
function Calibrate () {
tiltable = false;
if (PlayerPrefs.GetInt("TiltNeutral", 0) == 1) {restPosition = neutralPosTilted;}
else if (PlayerPrefs.GetInt("TiltNeutral", 0) == 2) {restPosition = neutralPosVertical;}
else {restPosition = neutralPosFlat;}
SetAxesInversion();
//acceleratorSnapshot = Input.acceleration;
acceleratorSnapshot = Vector3(0.0,0.0,-1.0);
calibrationRotation = Quaternion.FromToRotation(acceleratorSnapshot, restPosition);
tiltable = true;
}
function ChangeTilt (toFlat : int) {
if (toFlat == 2) {
PlayerPrefs.SetInt("TiltNeutral", 2);
Debug.Log("tilt set to vertical.");
}
else if (toFlat == 0) {
PlayerPrefs.SetInt("TiltNeutral", 0);
Debug.Log("tilt set to flat.");
}
else {
PlayerPrefs.SetInt("TiltNeutral", 1);
Debug.Log("tilt set to angled.");
}
Calibrate();
}
// Only relevant in non-VR mode.
// In VR mode, we simply set landscapeLeft in all cases to meet the Google Cardboard SDK's expectations.
function LockDeviceOrientation (waitTime: float) {
cachedScreenOrientation = Screen.orientation;
// Our outer function shouldn't actually get called if in VR mode, but for
// safety, defensively force landscapeLeft and early return in that case...
if (isVRMode) {
LockLandscapeLeftOrientation(isVRMode);
return;
}
// Let the device auto-rotate if necessary:
Screen.autorotateToLandscapeLeft = true;
Screen.autorotateToLandscapeRight = true;
Screen.orientation = ScreenOrientation.AutoRotation;
// iOS/Unity can give strange/wrong orientation values while screen is mid-rotation
// or close to flat, so we manually add a wait yield (~1s).
yield WaitForSeconds(waitTime);
switch (Input.deviceOrientation) {
case DeviceOrientation.LandscapeLeft:
LockLandscapeLeftOrientation(isVRMode);
FallingLaunch.Analytics.Event("DeviceOrientationSet:LandscapeLeft", 0.0);
break;
case DeviceOrientation.LandscapeRight:
LockLandscapeRightOrientation();
FallingLaunch.Analytics.Event("DeviceOrientationSet:LandscapeRight", 0.0);
break;
default:
HandleDeviceOrientationMismatch();
FallingLaunch.Analytics.Event("DeviceOrientationCheck:NonLandscape:" + Input.deviceOrientation, 0.0);
break;
}
// These are handled within each individual LockLandscapeLeft/Right function:
// Screen.autorotateToLandscapeLeft = false;
// Screen.autorotateToLandscapeRight = false;
Calibrate();
if (Debug.isDebugBuild) {
Debug.Log("Final screen orientation is: " + Screen.orientation);
}
return;
}
function HandleDeviceOrientationMismatch() {
if (initialInputDeviceOrientation != Input.deviceOrientation) {
FallingLaunch.Analytics.Event(
"DeviceOrientationCheck:CachedAndCurrentMismatch:" + Input.deviceOrientation + ":" + initialInputDeviceOrientation,
0.0
);
if (initialInputDeviceOrientation == DeviceOrientation.LandscapeLeft) {
if (Debug.isDebugBuild) {
Debug.Log("There's been a cached/current deviceOrientation mismatch. Setting to landscape left...");
}
initialInputDeviceOrientation = Input.deviceOrientation;
LockLandscapeLeftOrientation(isVRMode);
} else if (initialInputDeviceOrientation == DeviceOrientation.LandscapeRight) {
if (Debug.isDebugBuild) {
Debug.Log("There's been a cached/current deviceOrientation mismatch. Setting to landscape right...");
}
initialInputDeviceOrientation = Input.deviceOrientation;
LockLandscapeRightOrientation();
} else {
DefaultToLandscapeLeftOrientation();
}
} else {
if (Debug.isDebugBuild) {
Debug.Log("InitialInputDeviceOrientation and Input.deviceOrientation do match, as " + Input.deviceOrientation);
}
FallingLaunch.Analytics.Event("DeviceOrientationSet:CachedAndCurrentMatch:" + Input.deviceOrientation, 0.0);
// But we must choose left or right, ultimately...
DefaultToLandscapeLeftOrientation();
}
return;
}
function DefaultToLandscapeLeftOrientation() {
if (Screen.orientation == ScreenOrientation.LandscapeRight || cachedScreenOrientation == ScreenOrientation.LandscapeRight ||
Input.deviceOrientation == DeviceOrientation.LandscapeRight) {
if (Debug.isDebugBuild) {
Debug.Log("Picking LandscapeRight, to match Screen.orientation or Input.deviceOrientation");
}
LockLandscapeRightOrientation();
} else {
if (Debug.isDebugBuild) {
Debug.Log("Defaulting to LandscapeLeft, since Screen.orientation / Input.deviceOrientation were not LandscapeRight");
}
LockLandscapeLeftOrientation(isVRMode);
}
}
function LockLandscapeLeftOrientation (isVR : boolean) {
if (Debug.isDebugBuild) {Debug.Log("Locking LandscapeLeft orientation with isVR " + isVR);}
// if the device is held in landscapeRight already,
// the autorotateToLandscapeRight = false below is not enough
// to force the left-hand orientation needed for VR mode.
if (isVR) {
// We disable all autorotation before forcing the new orientation, to prevent
// UnityViewControllerBaseiOS.mm `UnityShouldAutorotate` assert crashes:
Screen.autorotateToLandscapeLeft = false;
Screen.autorotateToLandscapeRight = false;
Screen.autorotateToPortrait = false;
Screen.autorotateToPortraitUpsideDown = false;
Screen.orientation = ScreenOrientation.LandscapeLeft;
}
cachedScreenOrientation = Screen.orientation;
// Further interaction with Screen.autorotate... values will crash
// the app if we've already forced a given Screen.orientation
// (see UnityViewControllerBaseiOS.mm assert note above), so the below
// is for non-VR mode only:
if (!isVR) {
Screen.autorotateToLandscapeRight = false;
}
neutralPosTilted = neutralPosTiltedRegular;
neutralPosVertical = neutralPosVerticalRegular;
flipMultiplier = 1.0;
}
function LockLandscapeRightOrientation () {
if (Debug.isDebugBuild) {Debug.Log("Locking LandscapeRight orientation");}
// Screen.orientation = ScreenOrientation.LandscapeRight;
cachedScreenOrientation = Screen.orientation;
Screen.autorotateToLandscapeLeft = false;
neutralPosTilted = neutralPosTiltedFlipped;
neutralPosVertical = neutralPosVerticalFlipped;
flipMultiplier = -1.0;
}