Tilt Five™ Unity API  1.3.0
 
Loading...
Searching...
No Matches
GameBoard.cs
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020-2022 Tilt Five, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17using System;
18using System.Collections.Generic;
19using UnityEngine;
21
22namespace TiltFive
23{
27 [ExecuteInEditMode]
29 {
30 #region Public Fields
31
35 [Tooltip("Show/Hide the Board Gizmo in the Editor.")]
36 public bool ShowGizmo;
37
38 [Tooltip("Show/Hide the Unit Grid on the Board Gizmo in the Editor.")]
39 public bool ShowGrid;
40
41 public float GridHeightOffset = 0f;
42 public bool StickyHeightOffset = true;
43
44
48 [Tooltip("Sets the Alpha transparency of the Board Gizmo in the Editor.")]
49 [Range(0f, 1f)]
50 public float GizmoOpacity = 0.75f;
51
55 [HideInInspector]
56 [Obsolete("Please use Gameboard.TryGetGameboardType() instead.")]
58
59 #endregion Public Fields
60
61
62 #region Private Fields
63
64#if UNITY_EDITOR
65
69 private TableTopGizmo boardGizmo = new TableTopGizmo();
70
74 private float gridOffsetY => StickyHeightOffset ? Mathf.RoundToInt(GridHeightOffset) : GridHeightOffset;
75
79 private LengthUnit currentContentScaleUnit;
80
84 private float currentContentScaleRatio;
85
89 private Vector3 currentScale;
90
91 private const float MIN_SCALE = 0.00001f;
92
93#endif // UNITY_EDITOR
94
95 private static Dictionary<PlayerIndex, GameboardType> playerGameboards = new Dictionary<PlayerIndex, GameboardType>();
96
97 #endregion Private Fields
98
99
100 #region Public Structs
101
103 {
109
111 {
112 playableSpaceX = new Length(gameboardSize.PlayableSpaceX, LengthUnit.Meters);
113 playableSpaceY = new Length(gameboardSize.PlayableSpaceY, LengthUnit.Meters);
114 borderWidth = new Length(gameboardSize.BorderWidth, LengthUnit.Meters);
115 }
116 }
117
118 #endregion Private Structs
119
120
121 #region Public Functions
122
130 public static bool TryGetGameboardType(out GameboardType gameboardType)
131 {
132 return TryGetGameboardType(PlayerIndex.One, out gameboardType);
133 }
134
135 public static bool TryGetGameboardType(PlayerIndex playerIndex, out GameboardType gameboardType)
136 {
137 // Return false and a default gameboard if the player is nonexistent or disconnected.
138 if(!Player.TryGetGlassesHandle(playerIndex, out var glassesHandle)
139 || !playerGameboards.TryGetValue(playerIndex, out var currentGameboardType))
140 {
141 gameboardType = GameboardType.GameboardType_None;
142 return false;
143 }
144
145 gameboardType = currentGameboardType;
146 return true;
147 }
148
155 public static bool TryGetGameboardDimensions(GameboardType gameboardType, out GameboardDimensions gameboardDimensions)
156 {
157 if(gameboardType == GameboardType.GameboardType_None)
158 {
159 gameboardDimensions = new GameboardDimensions();
160 return false;
161 }
162
163 // Default to the LE gameboard dimensions in meters.
164 T5_GameboardSize gameboardSize = new T5_GameboardSize(0.7f, 0.7f, 0.05f);
165 int result = 1;
166
167 try
168 {
169 result = NativePlugin.GetGameboardDimensions(gameboardType, ref gameboardSize);
170 }
171 catch (Exception e)
172 {
173 Log.Error(e.Message);
174 }
175
176 gameboardDimensions = new GameboardDimensions(gameboardSize);
177
178 return result == 0;
179 }
180
181#if UNITY_EDITOR
182
183 new public void Awake()
184 {
185 base.Awake();
186 currentScale = transform.localScale;
187 }
188
192 public void DrawGizmo(ScaleSettings scaleSettings, GameBoardSettings gameBoardSettings)
193 {
194 UnifyScale();
195
196 if (ShowGizmo)
197 {
198 boardGizmo.Draw(scaleSettings, gameBoardSettings, GizmoOpacity, ShowGrid, gridOffsetY);
199 }
200
201 var sceneViewRepaintNecessary = ScaleCompensate(scaleSettings);
202 sceneViewRepaintNecessary |= ContentScaleCompensate(scaleSettings);
203
204 if(sceneViewRepaintNecessary)
205 {
206 boardGizmo.ResetGrid(scaleSettings, gameBoardSettings); // This may need to change once separate game board configs are in.
207 UnityEditor.SceneView.lastActiveSceneView.Repaint();
208 }
209 }
210
216 public GameboardType GetDisplayedGameboardType(GameBoardSettings gameBoardSettings)
217 {
218 return GetDisplayedGameboardType(PlayerIndex.One, gameBoardSettings);
219 }
220
221 public static GameboardType GetDisplayedGameboardType(PlayerIndex playerIndex, GameBoardSettings gameBoardSettings)
222 {
223 if(playerIndex == PlayerIndex.None)
224 {
225 return GameboardType.GameboardType_None;
226 }
227
228 var displayedGameboardType = playerGameboards.TryGetValue(playerIndex, out var gameboardType)
229 ? gameboardType
230 : GameboardType.GameboardType_LE;
231
232 displayedGameboardType = gameBoardSettings.gameboardTypeOverride != GameboardType.GameboardType_None
233 ? gameBoardSettings.gameboardTypeOverride
234 : displayedGameboardType;
235
236 return displayedGameboardType;
237 }
238
239#endif // UNITY_EDITOR
240
241 #endregion Public Functions
242
243
244 #region Internal Functions
245
246 internal static void SetGameboardType(PlayerIndex playerIndex, GameboardType gameboardType)
247 {
248 if(playerIndex == PlayerIndex.None)
249 {
250 return;
251 }
252
253 playerGameboards[playerIndex] = gameboardType;
254 }
255
256 internal static void SetGameboardType(GlassesHandle glassesHandle, GameboardType gameboardType)
257 {
258 if (Player.TryGetPlayerIndex(glassesHandle, out var playerIndex))
259 {
260 SetGameboardType(playerIndex, gameboardType);
261 }
262 }
263
264 #endregion
265
266
267 #region Private Functions
268
269#if UNITY_EDITOR
270
277 private bool ScaleCompensate(ScaleSettings scaleSettings)
278 {
279 if(currentScale == transform.localScale) { return false; }
280
281 // Prevent negative scale values for the game board.
282 if( transform.localScale.x < MIN_SCALE)
283 {
284 transform.localScale = Vector3.one * MIN_SCALE;
285 }
286
287 //var sceneView = UnityEditor.SceneView.lastActiveSceneView;
288
289 //sceneView.Frame(new Bounds(transform.position, (1/5f) * Vector3.one * scaleSettings.worldSpaceUnitsPerPhysicalMeter / localScale ), true);
290
291 currentScale = transform.localScale;
292 return true;
293 }
294
298 private bool ContentScaleCompensate(ScaleSettings scaleSettings)
299 {
300 if(currentContentScaleRatio == scaleSettings.contentScaleRatio
301 && currentContentScaleUnit == scaleSettings.contentScaleUnit) { return false; }
302
303 //var sceneView = UnityEditor.SceneView.lastActiveSceneView;
304
305 currentContentScaleUnit = scaleSettings.contentScaleUnit;
306 currentContentScaleRatio = scaleSettings.contentScaleRatio;
307
308 //sceneView.Frame(new Bounds(transform.position, (1/5f) * Vector3.one * scaleSettings.worldSpaceUnitsPerPhysicalMeter / localScale ), true);
309
310 return true;
311 }
312
313
314#endif // UNITY_EDITOR
315
316 #endregion Private Functions
317 }
318}
Represents the game board.
Definition: GameBoard.cs:29
static bool TryGetGameboardDimensions(GameboardType gameboardType, out GameboardDimensions gameboardDimensions)
Attempts to obtain the physical dimensions for a particular gameboard type.
Definition: GameBoard.cs:155
bool ShowGizmo
Shows the game board gizmo in the editor.
Definition: GameBoard.cs:36
static Dictionary< PlayerIndex, GameboardType > playerGameboards
Definition: GameBoard.cs:95
static bool TryGetGameboardType(out GameboardType gameboardType)
Attempts to check the latest glasses pose for the current gameboard type, such as LE,...
Definition: GameBoard.cs:130
static bool TryGetGameboardType(PlayerIndex playerIndex, out GameboardType gameboardType)
Definition: GameBoard.cs:135
bool StickyHeightOffset
Definition: GameBoard.cs:42
float GizmoOpacity
Sets the opacity of the game board gizmo in the editor.
Definition: GameBoard.cs:50
float GridHeightOffset
Definition: GameBoard.cs:41
GameboardType GameboardType
The gameboard configuration, such as LE, XE, or folded XE.
Definition: GameBoard.cs:57
The Logger.
Definition: Log.cs:42
static void Error(string m, params object[] list)
ERROR logging function call.
Definition: Log.cs:127
static int GetGameboardDimensions([MarshalAs(UnmanagedType.I4)] GameboardType gameboardType, ref T5_GameboardSize playableSpaceInMeters)
Provides access to player settings and functionality.
Definition: Player.cs:16
ScaleSettings contains the scale data used to translate between Unity units and the user's physical s...
LengthUnit contentScaleUnit
The real-world unit to be compared against when using .
float contentScaleRatio
The scaling ratio relates physical distances to world-space units.
Enforces uniform scaling by setting the x, y, and z scale components of the associated transform to b...
void UnifyScale()
Synchronizes the component values of the game object's local scale vector (e.g. [1,...
Definition: Log.cs:21
LengthUnit
Definition: Length.cs:22
GameboardType
The type of Gameboard being tracked by the glasses.
PlayerIndex
The Player index (e.g. Player One, Player Two, etc)
GameboardDimensions(T5_GameboardSize gameboardSize)
Definition: GameBoard.cs:110
Physical dimensions of a gameboard, in meters.