| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | public class BuilderInWorldFirstPersonMode : BuilderInWorldMode |
| | 9 | | { |
| | 10 | | [Header("Design variables")] |
| 198 | 11 | | public float scaleSpeed = 0.25f; |
| 198 | 12 | | public float rotationSpeed = 0.5f; |
| 198 | 13 | | public float distanceFromCameraForNewEntitties = 5; |
| | 14 | |
|
| | 15 | | [Header("Prefab references")] |
| | 16 | | public BuilderInWorldInputWrapper builderInputWrapper; |
| | 17 | |
|
| | 18 | | [Header("InputActions")] |
| | 19 | | [SerializeField] internal InputAction_Hold rotationHold; |
| | 20 | |
|
| | 21 | | private Quaternion initialRotation; |
| | 22 | |
|
| | 23 | | private float currentScaleAdded, currentYRotationAdded; |
| | 24 | |
|
| | 25 | | private bool snapObjectAlreadyMoved = false, shouldRotate = false; |
| | 26 | | private Transform originalParentGOEdit; |
| | 27 | |
|
| | 28 | | private InputAction_Hold.Started rotationHoldStartDelegate; |
| | 29 | | private InputAction_Hold.Finished rotationHoldFinishedDelegate; |
| | 30 | |
|
| | 31 | | private void Start() |
| | 32 | | { |
| 0 | 33 | | rotationHoldStartDelegate = (action) => { shouldRotate = true; }; |
| 0 | 34 | | rotationHoldFinishedDelegate = (action) => { shouldRotate = false; }; |
| | 35 | |
|
| 0 | 36 | | rotationHold.OnStarted += rotationHoldStartDelegate; |
| 0 | 37 | | rotationHold.OnFinished += rotationHoldFinishedDelegate; |
| | 38 | |
|
| 0 | 39 | | BuilderInWorldInputWrapper.OnMouseClick += OnMouseClick; |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | private void OnDestroy() |
| | 43 | | { |
| 0 | 44 | | rotationHold.OnStarted -= rotationHoldStartDelegate; |
| 0 | 45 | | rotationHold.OnFinished -= rotationHoldFinishedDelegate; |
| | 46 | |
|
| 0 | 47 | | BuilderInWorldInputWrapper.OnMouseClick -= OnMouseClick; |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | void LateUpdate() |
| | 51 | | { |
| 0 | 52 | | if (selectedEntities.Count == 0 || isMultiSelectionActive) |
| 0 | 53 | | return; |
| | 54 | |
|
| 0 | 55 | | if (isSnapActive) |
| | 56 | | { |
| 0 | 57 | | if (snapObjectAlreadyMoved) |
| | 58 | | { |
| 0 | 59 | | Vector3 objectPosition = snapGO.transform.position; |
| 0 | 60 | | Vector3 eulerRotation = snapGO.transform.rotation.eulerAngles; |
| | 61 | |
|
| 0 | 62 | | float currentSnapFactor = snapFactor; |
| | 63 | |
|
| 0 | 64 | | objectPosition.x = Mathf.RoundToInt(objectPosition.x / currentSnapFactor) * currentSnapFactor; |
| 0 | 65 | | objectPosition.y = Mathf.RoundToInt(objectPosition.y / currentSnapFactor) * currentSnapFactor; |
| 0 | 66 | | objectPosition.z = Mathf.RoundToInt(objectPosition.z / currentSnapFactor) * currentSnapFactor; |
| 0 | 67 | | eulerRotation.y = snapRotationDegresFactor * Mathf.FloorToInt((eulerRotation.y % snapRotationDegresFacto |
| | 68 | |
|
| 0 | 69 | | Quaternion destinationRotation = Quaternion.AngleAxis(currentYRotationAdded, Vector3.up); |
| 0 | 70 | | editionGO.transform.rotation = initialRotation * destinationRotation; |
| 0 | 71 | | editionGO.transform.position = objectPosition; |
| 0 | 72 | | } |
| 0 | 73 | | else if (Vector3.Distance(snapGO.transform.position, editionGO.transform.position) >= snapDistanceToActivate |
| | 74 | | { |
| 0 | 75 | | BuilderInWorldUtils.CopyGameObjectStatus(editionGO, snapGO, false); |
| 0 | 76 | | snapObjectAlreadyMoved = true; |
| 0 | 77 | | SetEditObjectParent(); |
| | 78 | | } |
| 0 | 79 | | } |
| | 80 | | else |
| | 81 | | { |
| 0 | 82 | | Vector3 pointToLookAt = Camera.main.transform.position; |
| 0 | 83 | | pointToLookAt.y = editionGO.transform.position.y; |
| 0 | 84 | | Quaternion lookOnLook = Quaternion.LookRotation(editionGO.transform.position - pointToLookAt); |
| 0 | 85 | | freeMovementGO.transform.rotation = lookOnLook; |
| | 86 | | } |
| | 87 | |
|
| 0 | 88 | | } |
| | 89 | |
|
| 0 | 90 | | public override Vector3 GetPointerPosition() { return new Vector3(Screen.width / 2, Screen.height / 2, 0); } |
| | 91 | |
|
| | 92 | | public void OnMouseClick(int buttonId, Vector3 mouseposition) |
| | 93 | | { |
| 0 | 94 | | if (!isModeActive) |
| 0 | 95 | | return; |
| 0 | 96 | | if (buttonId != 1) |
| 0 | 97 | | return; |
| 0 | 98 | | if (selectedEntities.Count <= 0) |
| 0 | 99 | | return; |
| | 100 | |
|
| 0 | 101 | | UndoSelection(); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | public override bool ShouldCancelUndoAction() |
| | 105 | | { |
| 0 | 106 | | if (builderInWorldEntityHandler.GetSelectedEntityList().Count >= 1) |
| | 107 | | { |
| 0 | 108 | | UndoSelection(); |
| 0 | 109 | | return true; |
| | 110 | | } |
| 0 | 111 | | return false; |
| | 112 | | } |
| | 113 | |
|
| | 114 | | public void UndoSelection() |
| | 115 | | { |
| 0 | 116 | | BuilderInWorldUtils.CopyGameObjectStatus(undoGO, editionGO, false, false); |
| 0 | 117 | | builderInWorldEntityHandler.DeselectEntities(); |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | public override void SetDuplicationOffset(float offset) |
| | 121 | | { |
| 0 | 122 | | base.SetDuplicationOffset(offset); |
| 0 | 123 | | if (isSnapActive) |
| 0 | 124 | | editionGO.transform.position += Vector3.right * offset; |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | public override void ResetScaleAndRotation() |
| | 128 | | { |
| 0 | 129 | | base.ResetScaleAndRotation(); |
| 0 | 130 | | currentScaleAdded = 0; |
| 0 | 131 | | currentYRotationAdded = 0; |
| | 132 | |
|
| 0 | 133 | | Quaternion zeroAnglesQuaternion = Quaternion.Euler(Vector3.zero); |
| 0 | 134 | | initialRotation = zeroAnglesQuaternion; |
| 0 | 135 | | } |
| | 136 | |
|
| | 137 | | public override void Activate(ParcelScene scene) |
| | 138 | | { |
| 1 | 139 | | base.Activate(scene); |
| 1 | 140 | | SetEditObjectParent(); |
| 1 | 141 | | freeMovementGO.transform.SetParent(Camera.main.transform); |
| 1 | 142 | | if (HUDController.i.builderInWorldMainHud != null) |
| | 143 | | { |
| 0 | 144 | | HUDController.i.builderInWorldMainHud.ActivateFirstPersonModeUI(); |
| 0 | 145 | | HUDController.i.builderInWorldMainHud.SetVisibilityOfCatalog(false); |
| | 146 | | } |
| 1 | 147 | | } |
| | 148 | |
|
| | 149 | | public override void StartMultiSelection() |
| | 150 | | { |
| 0 | 151 | | base.StartMultiSelection(); |
| 0 | 152 | | originalParentGOEdit = editionGO.transform.parent; |
| | 153 | |
|
| 0 | 154 | | SetEditObjectParent(); |
| 0 | 155 | | snapGO.transform.SetParent(null); |
| 0 | 156 | | freeMovementGO.transform.SetParent(null); |
| 0 | 157 | | } |
| | 158 | |
|
| | 159 | | public override void EndMultiSelection() |
| | 160 | | { |
| 0 | 161 | | base.EndMultiSelection(); |
| 0 | 162 | | SetEditObjectParent(); |
| | 163 | |
|
| 0 | 164 | | snapGO.transform.SetParent(Camera.main.transform); |
| 0 | 165 | | freeMovementGO.transform.SetParent(Camera.main.transform); |
| | 166 | |
|
| 0 | 167 | | SetObjectIfSnapOrNot(); |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | public override void SelectedEntity(DCLBuilderInWorldEntity selectedEntity) |
| | 171 | | { |
| 0 | 172 | | base.SelectedEntity(selectedEntity); |
| | 173 | |
|
| 0 | 174 | | initialRotation = editionGO.transform.rotation; |
| | 175 | |
|
| 0 | 176 | | SetObjectIfSnapOrNot(); |
| | 177 | |
|
| 0 | 178 | | currentYRotationAdded = 0; |
| 0 | 179 | | BuilderInWorldUtils.CopyGameObjectStatus(editionGO, snapGO, false); |
| 0 | 180 | | } |
| | 181 | |
|
| | 182 | | public override void CreatedEntity(DCLBuilderInWorldEntity createdEntity) |
| | 183 | | { |
| 0 | 184 | | base.CreatedEntity(createdEntity); |
| 0 | 185 | | Utils.LockCursor(); |
| 0 | 186 | | } |
| | 187 | |
|
| | 188 | | public override void SetSnapActive(bool isActive) |
| | 189 | | { |
| 1 | 190 | | base.SetSnapActive(isActive); |
| 1 | 191 | | if (isSnapActive) |
| | 192 | | { |
| 1 | 193 | | snapObjectAlreadyMoved = false; |
| 1 | 194 | | snapGO.transform.SetParent(Camera.main.transform); |
| | 195 | | } |
| 1 | 196 | | SetObjectIfSnapOrNot(); |
| 1 | 197 | | } |
| | 198 | |
|
| | 199 | | public override void CheckInputSelectedEntities() |
| | 200 | | { |
| 0 | 201 | | base.CheckInputSelectedEntities(); |
| 0 | 202 | | if (selectedEntities.Count == 0) |
| 0 | 203 | | return; |
| | 204 | |
|
| 0 | 205 | | if (isModeActive && shouldRotate) |
| | 206 | | { |
| 0 | 207 | | if (isSnapActive) |
| | 208 | | { |
| 0 | 209 | | RotateSelection(snapRotationDegresFactor); |
| 0 | 210 | | InputDone(); |
| 0 | 211 | | } |
| | 212 | | else |
| | 213 | | { |
| 0 | 214 | | RotateSelection(rotationSpeed); |
| | 215 | | } |
| | 216 | | } |
| | 217 | |
|
| 0 | 218 | | if (Input.mouseScrollDelta.y > 0.5f) |
| | 219 | | { |
| 0 | 220 | | if (isSnapActive) |
| | 221 | | { |
| 0 | 222 | | ScaleSelection(snapScaleFactor); |
| 0 | 223 | | InputDone(); |
| 0 | 224 | | } |
| | 225 | | else |
| | 226 | | { |
| 0 | 227 | | ScaleSelection(scaleSpeed); |
| | 228 | | } |
| 0 | 229 | | } |
| 0 | 230 | | else if (Input.mouseScrollDelta.y < -0.5f) |
| | 231 | | { |
| 0 | 232 | | if (isSnapActive) |
| | 233 | | { |
| 0 | 234 | | ScaleSelection(-snapScaleFactor); |
| 0 | 235 | | InputDone(); |
| 0 | 236 | | } |
| | 237 | | else |
| | 238 | | { |
| 0 | 239 | | ScaleSelection(-scaleSpeed); |
| | 240 | | } |
| | 241 | | } |
| | 242 | |
|
| 0 | 243 | | } |
| | 244 | |
|
| 0 | 245 | | public override Vector3 GetCreatedEntityPoint() { return Camera.main.transform.position + Camera.main.transform.forw |
| | 246 | |
|
| | 247 | | void SetObjectIfSnapOrNot() |
| | 248 | | { |
| 1 | 249 | | if (isMultiSelectionActive) |
| 0 | 250 | | return; |
| | 251 | |
|
| 1 | 252 | | if (!isSnapActive) |
| | 253 | | { |
| 0 | 254 | | editionGO.transform.SetParent(null); |
| 0 | 255 | | freeMovementGO.transform.position = editionGO.transform.position; |
| 0 | 256 | | freeMovementGO.transform.rotation = editionGO.transform.rotation; |
| 0 | 257 | | freeMovementGO.transform.localScale = editionGO.transform.localScale; |
| | 258 | |
|
| 0 | 259 | | editionGO.transform.SetParent(null); |
| | 260 | |
|
| 0 | 261 | | Vector3 pointToLookAt = Camera.main.transform.position; |
| 0 | 262 | | pointToLookAt.y = editionGO.transform.position.y; |
| 0 | 263 | | Quaternion lookOnLook = Quaternion.LookRotation(editionGO.transform.position - pointToLookAt); |
| | 264 | |
|
| 0 | 265 | | freeMovementGO.transform.rotation = lookOnLook; |
| 0 | 266 | | editionGO.transform.SetParent(freeMovementGO.transform, true); |
| 0 | 267 | | } |
| | 268 | | else |
| | 269 | | { |
| 1 | 270 | | editionGO.transform.SetParent(null); |
| | 271 | | } |
| | 272 | |
|
| 1 | 273 | | } |
| | 274 | |
|
| | 275 | | private void SetEditObjectParent() |
| | 276 | | { |
| 1 | 277 | | Transform parentToAsign = null; |
| 1 | 278 | | bool worldPositionStays = false; |
| 1 | 279 | | if (!isMultiSelectionActive) |
| | 280 | | { |
| 1 | 281 | | if (isSnapActive) |
| | 282 | | { |
| 0 | 283 | | if (snapObjectAlreadyMoved) |
| 0 | 284 | | parentToAsign = Camera.main.transform; |
| 0 | 285 | | } |
| | 286 | | else |
| | 287 | | { |
| 1 | 288 | | worldPositionStays = true; |
| 1 | 289 | | if (freeMovementGO != null) |
| 1 | 290 | | parentToAsign = freeMovementGO.transform; |
| | 291 | | } |
| | 292 | |
|
| 1 | 293 | | } |
| | 294 | | else |
| | 295 | | { |
| 0 | 296 | | if (!isSnapActive) |
| | 297 | | { |
| 0 | 298 | | parentToAsign = originalParentGOEdit; |
| | 299 | | } |
| 0 | 300 | | worldPositionStays = true; |
| | 301 | | } |
| | 302 | |
|
| 1 | 303 | | if (editionGO != null) |
| 1 | 304 | | editionGO.transform.SetParent(parentToAsign, worldPositionStays); |
| 1 | 305 | | } |
| | 306 | |
|
| | 307 | | void RotateSelection(float angleToRotate) |
| | 308 | | { |
| 0 | 309 | | currentYRotationAdded += angleToRotate; |
| 0 | 310 | | editionGO.transform.Rotate(Vector3.up, angleToRotate); |
| 0 | 311 | | snapGO.transform.Rotate(Vector3.up, angleToRotate); |
| 0 | 312 | | snapGO.transform.rotation = Quaternion.Euler(BuilderInWorldUtils.SnapFilterEulerAngles(snapGO.transform.rotation |
| 0 | 313 | | } |
| | 314 | |
|
| | 315 | | void ScaleSelection(float scaleFactor) |
| | 316 | | { |
| 0 | 317 | | currentScaleAdded += scaleFactor; |
| 0 | 318 | | editionGO.transform.localScale += Vector3.one * scaleFactor; |
| 0 | 319 | | snapGO.transform.localScale += Vector3.one * scaleFactor; |
| 0 | 320 | | } |
| | 321 | | } |