2021-07-18 04:03:43 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using SanAndreasUnity.Behaviours.World;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
using UnityEngine;
|
2021-07-18 04:03:43 +00:00
|
|
|
|
using Object = UnityEngine.Object;
|
|
|
|
|
using Profiler = UnityEngine.Profiling.Profiler;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Behaviours
|
|
|
|
|
{
|
2021-07-18 04:03:43 +00:00
|
|
|
|
public abstract class MapObject : MonoBehaviour
|
2020-05-31 17:07:22 +00:00
|
|
|
|
{
|
|
|
|
|
private static Texture2D _sNoiseTex;
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
private static bool ShouldGenerateNoiseTex => _sNoiseTex == null || _sNoiseTex.width != Screen.width || _sNoiseTex.height != Screen.height;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
private static int _sBreakableLayer = -1;
|
2021-07-18 04:03:43 +00:00
|
|
|
|
public static int BreakableLayer => _sBreakableLayer == -1 ? _sBreakableLayer = LayerMask.NameToLayer("Breakable") : _sBreakableLayer;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
private static int _sNoiseTexPropertyId = -1;
|
|
|
|
|
protected static int NoiseTexPropertyId => _sNoiseTexPropertyId == -1 ? _sNoiseTexPropertyId = Shader.PropertyToID("_NoiseTex") : _sNoiseTexPropertyId;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
private static int _sFadePropertyId = -1;
|
|
|
|
|
protected static int FadePropertyId => _sFadePropertyId == -1 ? _sFadePropertyId = Shader.PropertyToID("_Fade") : _sFadePropertyId;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
private static void GenerateNoiseTex()
|
|
|
|
|
{
|
|
|
|
|
var width = Screen.width;
|
|
|
|
|
var height = Screen.height;
|
|
|
|
|
|
|
|
|
|
var timer = new Stopwatch();
|
|
|
|
|
timer.Start();
|
|
|
|
|
|
|
|
|
|
if (_sNoiseTex == null)
|
|
|
|
|
{
|
|
|
|
|
_sNoiseTex = new Texture2D(width, height, TextureFormat.Alpha8, false);
|
|
|
|
|
_sNoiseTex.filterMode = FilterMode.Bilinear;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_sNoiseTex.Resize(width, height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var rand = new System.Random(0x54e03b19);
|
|
|
|
|
var buffer = new byte[width * height];
|
|
|
|
|
rand.NextBytes(buffer);
|
|
|
|
|
|
|
|
|
|
_sNoiseTex.LoadRawTextureData(buffer);
|
|
|
|
|
_sNoiseTex.Apply(false, false);
|
|
|
|
|
|
|
|
|
|
UnityEngine.Debug.LogFormat("Noise gen time: {0:F2} ms", timer.Elapsed.TotalMilliseconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static Texture2D NoiseTex
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (ShouldGenerateNoiseTex) GenerateNoiseTex();
|
|
|
|
|
return _sNoiseTex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
public bool IsVisibleInMapSystem { get; private set; } = false;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
public float LoadPriority { get; private set; } = float.PositiveInfinity;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
private bool _loaded;
|
|
|
|
|
public bool HasLoaded => _loaded;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
public Vector3 CachedPosition { get; private set; }
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
protected static T Create<T>(GameObject prefab)
|
|
|
|
|
where T : MapObject
|
|
|
|
|
{
|
|
|
|
|
GameObject go = Object.Instantiate(prefab, Cell.Instance.transform);
|
|
|
|
|
return go.GetComponent<T>();
|
|
|
|
|
}
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
protected void Initialize(Vector3 pos, Quaternion rot)
|
|
|
|
|
{
|
2021-07-18 04:03:43 +00:00
|
|
|
|
this.transform.position = pos;
|
|
|
|
|
this.transform.localRotation = rot;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
this.CachedPosition = pos;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
_loaded = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
public void SetDrawDistance(float f)
|
2020-05-31 17:07:22 +00:00
|
|
|
|
{
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
}
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
public void Show(float loadPriority)
|
2020-05-31 17:07:22 +00:00
|
|
|
|
{
|
2021-07-18 04:03:43 +00:00
|
|
|
|
if (this.IsVisibleInMapSystem)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.IsVisibleInMapSystem = true;
|
|
|
|
|
|
|
|
|
|
this.LoadPriority = loadPriority;
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
if (!_loaded)
|
|
|
|
|
{
|
|
|
|
|
_loaded = true;
|
|
|
|
|
|
|
|
|
|
Profiler.BeginSample ("OnLoad", this);
|
2021-07-18 04:03:43 +00:00
|
|
|
|
this.OnLoad();
|
2020-05-31 17:07:22 +00:00
|
|
|
|
Profiler.EndSample ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Profiler.BeginSample ("OnShow", this);
|
2021-07-18 04:03:43 +00:00
|
|
|
|
this.OnShow();
|
2020-05-31 17:07:22 +00:00
|
|
|
|
Profiler.EndSample ();
|
2021-07-18 04:03:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnShow()
|
|
|
|
|
{
|
|
|
|
|
if (!this.IsVisibleInMapSystem)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.IsVisibleInMapSystem = false;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
this.OnUnShow();
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void OnLoad()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void OnShow()
|
|
|
|
|
{
|
2021-07-18 04:03:43 +00:00
|
|
|
|
this.gameObject.SetActive(true);
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
protected virtual void OnUnShow()
|
2020-05-31 17:07:22 +00:00
|
|
|
|
{
|
2021-07-18 04:03:43 +00:00
|
|
|
|
this.gameObject.SetActive(false);
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|