| | 1 | | using DCL.Configuration; |
| | 2 | | using System; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | public class DCLCharacterPosition |
| | 8 | | { |
| | 9 | | private Vector3 worldPositionValue; |
| | 10 | | private Vector3 unityPositionValue; |
| | 11 | | private Vector3 offset; |
| | 12 | |
|
| | 13 | | private int lastRepositionFrame = 0; |
| | 14 | |
|
| | 15 | | public Vector3 worldPosition |
| | 16 | | { |
| 0 | 17 | | get { return worldPositionValue; } |
| | 18 | | set |
| | 19 | | { |
| 5588 | 20 | | worldPositionValue = value; |
| 5588 | 21 | | unityPositionValue = PositionUtils.WorldToUnityPosition(worldPositionValue); |
| 5588 | 22 | | CheckAndRepositionWorld(); |
| 5588 | 23 | | } |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public Vector3 unityPosition |
| | 27 | | { |
| 0 | 28 | | get { return unityPositionValue; } |
| | 29 | | set |
| | 30 | | { |
| 0 | 31 | | unityPositionValue = value; |
| 0 | 32 | | worldPositionValue = PositionUtils.UnityToWorldPosition(unityPositionValue); |
| 0 | 33 | | CheckAndRepositionWorld(); |
| 0 | 34 | | } |
| | 35 | | } |
| | 36 | |
|
| 574 | 37 | | public DCLCharacterPosition() |
| | 38 | | { |
| 574 | 39 | | CommonScriptableObjects.worldOffset.Set(Vector3.zero); |
| 574 | 40 | | CommonScriptableObjects.playerWorldPosition.Set(Vector3.zero); |
| 574 | 41 | | } |
| | 42 | |
|
| | 43 | | private void CheckAndRepositionWorld() |
| | 44 | | { |
| 5588 | 45 | | bool dirty = false; |
| 5588 | 46 | | float minDistanceForReposition = PlayerSettings.WORLD_REPOSITION_MINIMUM_DISTANCE; |
| | 47 | |
|
| 5588 | 48 | | if (Mathf.Abs(unityPositionValue.x) > minDistanceForReposition) |
| | 49 | | { |
| 2 | 50 | | float dist = (int) (unityPositionValue.x / minDistanceForReposition) * minDistanceForReposition; |
| 2 | 51 | | unityPositionValue.x -= dist; |
| 2 | 52 | | offset.x += dist; |
| 2 | 53 | | dirty = true; |
| | 54 | | } |
| | 55 | |
|
| 5588 | 56 | | if (Mathf.Abs(unityPositionValue.z) > minDistanceForReposition) |
| | 57 | | { |
| 2 | 58 | | float dist = (int) (unityPositionValue.z / minDistanceForReposition) * minDistanceForReposition; |
| 2 | 59 | | unityPositionValue.z -= dist; |
| 2 | 60 | | offset.z += dist; |
| 2 | 61 | | dirty = true; |
| | 62 | | } |
| | 63 | |
|
| 5588 | 64 | | if (dirty) |
| | 65 | | { |
| 2 | 66 | | worldPositionValue = unityPositionValue + offset; |
| | 67 | |
|
| 2 | 68 | | lastRepositionFrame = Time.frameCount; |
| | 69 | |
|
| 2 | 70 | | CommonScriptableObjects.playerWorldPosition.Set(worldPositionValue); |
| 2 | 71 | | CommonScriptableObjects.worldOffset.Set(offset); |
| 2 | 72 | | DCL.Environment.i.platform.physicsSyncController.MarkDirty(); |
| | 73 | | } |
| 5588 | 74 | | } |
| | 75 | |
|
| 59 | 76 | | public bool RepositionedWorldLastFrame() { return lastRepositionFrame == Time.frameCount - 1; } |
| | 77 | |
|
| 0 | 78 | | public override string ToString() { return $"worldPos: {worldPositionValue} - unityPos: {unityPositionValue} - offse |
| | 79 | | } |