diff --git a/Assets/DemoGame/GameScript/Hotfix/FloorBase.meta b/Assets/DemoGame/GameScript/Hotfix/FloorBase.meta new file mode 100644 index 0000000..5ca1e88 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/FloorBase.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ffc6016e330b134429d2d8d6286a541d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/DemoGame/GameScript/Hotfix/FloorBase/GameObjectBase.cs b/Assets/DemoGame/GameScript/Hotfix/FloorBase/GameObjectBase.cs new file mode 100644 index 0000000..7b5745b --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/FloorBase/GameObjectBase.cs @@ -0,0 +1,64 @@ +using UnityEngine; + +namespace ZC +{ + public class GameObjectBase : IBehaviour, ICreateBindingGo + { + private GameObject _go; + private long _id; + private bool _isDisposed; + private bool _isPause; + public GameObject go => this._go; + + public long Id => this._id; + + public bool isDisposed => this._isDisposed; + + public bool isPause => this._isPause; + + protected GameObjectBase(GameObject go = null) + { + this._id = IDGenerator.Generate(); + Debug.Log($"name is {typeof(T)}, id is {this._id}"); + if (go == null) + { + var name = typeof(T).FullName; + this._go = new GameObject(name); + _go.transform.SetParent(ZCGame.ObjectPool); + this._go.name = name; + } + else + this._go = go; + } + + public void SetGo(GameObject go) + { + this._go = go; + } + + public virtual void OnInit() + { + this._isDisposed = false; + this._isPause = false; + } + + public virtual void OnUpdate(float dateTime) + { + } + + public virtual void OnPause() + { + this._isPause = true; + } + + public virtual void OnResume() + { + this._isPause = false; + } + + public virtual void OnDispose() + { + this._isDisposed = true; + } + } +} \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/FloorBase/GameObjectBase.cs.meta b/Assets/DemoGame/GameScript/Hotfix/FloorBase/GameObjectBase.cs.meta new file mode 100644 index 0000000..1fe6d8d --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/FloorBase/GameObjectBase.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 33ec6777b2ce4e50994218a7cd9b7d35 +timeCreated: 1721114843 \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/FloorBase/GameObjectFactory.cs b/Assets/DemoGame/GameScript/Hotfix/FloorBase/GameObjectFactory.cs new file mode 100644 index 0000000..2b07643 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/FloorBase/GameObjectFactory.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace ZC +{ + public static class GameObjectFactory + { +// public static T Create(GameObject go = null) where T : GameObjectBase +// { +// var t = new T(); +// var id = t.GetHashCode(); +// +// t.OnInit(); +// ZCGame.ObjectManager.Add(t); +// return t; +// } + } +} \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/FloorBase/GameObjectFactory.cs.meta b/Assets/DemoGame/GameScript/Hotfix/FloorBase/GameObjectFactory.cs.meta new file mode 100644 index 0000000..5568ee5 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/FloorBase/GameObjectFactory.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 65a3597d68994c0fb9a1e37a1b60515e +timeCreated: 1721117317 \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/FloorBase/IDGenerator.cs b/Assets/DemoGame/GameScript/Hotfix/FloorBase/IDGenerator.cs new file mode 100644 index 0000000..ce7e130 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/FloorBase/IDGenerator.cs @@ -0,0 +1,54 @@ +using System; + +namespace ZC +{ + /// + /// id生成器,当前使用的是时间戳作为id生成器的唯一id + /// + public static class IDGenerator + { + // + private static long lastTimestamp = -1L; + private static long sequence = 0L; + private static readonly long twepoch = 1288834974657L; // Twitter Snowflake算法中的自定义起始时间 + private static readonly long machineId = 1L; // 机器码,可以自定义 + + public static long Generate() + { + long timestamp = TimeGen(); + + if (lastTimestamp == timestamp) + { + sequence = (sequence + 1) & 4095L; + if (sequence == 0) + { + timestamp = TilNextMillis(lastTimestamp); + } + } + else + { + sequence = 0L; + } + + lastTimestamp = timestamp; + + return (timestamp - twepoch) << 22 | machineId << 12 | sequence; + } + + private static long TilNextMillis(long lastTimestamp) + { + long timestamp = TimeGen(); + while (timestamp <= lastTimestamp) + { + timestamp = TimeGen(); + } + + return timestamp; + } + + private static long TimeGen() + { + return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; + } + } +} \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/FloorBase/IDGenerator.cs.meta b/Assets/DemoGame/GameScript/Hotfix/FloorBase/IDGenerator.cs.meta new file mode 100644 index 0000000..2d6da66 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/FloorBase/IDGenerator.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 468958ca434443d79f1bf408bca3bf58 +timeCreated: 1721350450 \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/FloorBase/Interfaces.cs b/Assets/DemoGame/GameScript/Hotfix/FloorBase/Interfaces.cs new file mode 100644 index 0000000..732f429 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/FloorBase/Interfaces.cs @@ -0,0 +1,26 @@ +using UnityEngine; + +namespace ZC +{ + public interface ICreateBindingGo + { + GameObject go { get; } + void SetGo(GameObject go); + } + + public interface IBehaviour + { + long Id { get; } + bool isDisposed { get; } + bool isPause { get; } + void OnInit(); + + void OnUpdate(float dateTime); + + void OnPause(); + + void OnResume(); + + void OnDispose(); + } +} \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/FloorBase/Interfaces.cs.meta b/Assets/DemoGame/GameScript/Hotfix/FloorBase/Interfaces.cs.meta new file mode 100644 index 0000000..43f016b --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/FloorBase/Interfaces.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a146ca6ae4bc4923887b45b0488f8beb +timeCreated: 1721114797 \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/FloorBase/ManagerBase.cs b/Assets/DemoGame/GameScript/Hotfix/FloorBase/ManagerBase.cs new file mode 100644 index 0000000..35a040a --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/FloorBase/ManagerBase.cs @@ -0,0 +1,13 @@ +using UnityEngine; + +namespace ZC +{ + public interface IManager + { + } + + internal abstract class ManagerBase : GameObjectBase, IManager + { + + } +} \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/FloorBase/ManagerBase.cs.meta b/Assets/DemoGame/GameScript/Hotfix/FloorBase/ManagerBase.cs.meta new file mode 100644 index 0000000..4e17468 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/FloorBase/ManagerBase.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b4980a297cb34d8e85b4f80115b3e206 +timeCreated: 1721114891 \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/FloorBase/ObjectManager.cs b/Assets/DemoGame/GameScript/Hotfix/FloorBase/ObjectManager.cs new file mode 100644 index 0000000..3aa5a4c --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/FloorBase/ObjectManager.cs @@ -0,0 +1,76 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace ZC +{ + public interface IObjectManager + { + void Add(IBehaviour behaviour); + void Remove(long id); + void Clear(); + } + + internal class ObjectManager : ManagerBase, IObjectManager + { + private Dictionary pool; + + public override void OnInit() + { + base.OnInit(); + this.pool = new Dictionary(); + } + + public override void OnUpdate(float time) + { + base.OnUpdate(time); + foreach (var behaviour in this.pool.Values) + { + if (behaviour.isDisposed) + { + this.pool.Remove(behaviour.Id); + continue; + } + + behaviour.OnUpdate(time); + } + + Debug.Log(this.pool.Count); + } + + public override void OnDispose() + { + base.OnDispose(); + this.Clear(); + } + +#region Dic + + public void Add(IBehaviour behaviour) + { + behaviour.OnInit(); + this.pool.Add(behaviour.Id, behaviour); + } + + public void Remove(long id) + { + if (this.pool.TryGetValue(id, out var behaviour)) + { + behaviour?.OnDispose(); + } + + this.pool.Remove(id); + } + + public void Clear() + { + foreach (var value in this.pool.Values) + { + value.OnDispose(); + } + + this.pool.Clear(); + } + +#endregion + } +} \ No newline at end of file diff --git a/Assets/DemoGame/GameScript/Hotfix/FloorBase/ObjectManager.cs.meta b/Assets/DemoGame/GameScript/Hotfix/FloorBase/ObjectManager.cs.meta new file mode 100644 index 0000000..99b9ca9 --- /dev/null +++ b/Assets/DemoGame/GameScript/Hotfix/FloorBase/ObjectManager.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 13fd1962ffff41de9804bc3dc697d1bf +timeCreated: 1721116305 \ No newline at end of file