提交 0cce11e2 编写于 作者: T tanghai

分离Unit的逻辑跟显示层,Unit的GameObject放到GameObjectComponent组件上

上级 a5f7fc25
......@@ -52,10 +52,10 @@ message G2C_EnterMap // IResponse
message UnitInfo
{
int64 UnitId = 1;
float X = 2;
float Y = 3;
float Z = 4;
int32 ConfigId = 2;
float X = 3;
float Y = 4;
float Z = 5;
}
message M2C_CreateUnits // IActorMessage
......
......@@ -8,7 +8,7 @@ namespace ET
{
protected override async ETTask Run(Scene scene, G2M_CreateUnit request, M2G_CreateUnit response, Action reply)
{
Unit unit = EntityFactory.CreateWithId<Unit>(scene, IdGenerater.Instance.GenerateId());
Unit unit = EntityFactory.CreateWithId<Unit, int>(scene, IdGenerater.Instance.GenerateId(), 1001);
unit.AddComponent<MoveComponent>();
unit.AddComponent<UnitPathComponent>();
unit.Position = new Vector3(-10, 0, -10);
......@@ -30,6 +30,7 @@ namespace ET
unitInfo.Y = u.Position.y;
unitInfo.Z = u.Position.z;
unitInfo.UnitId = u.Id;
unitInfo.ConfigId = u.ConfigId;
createUnits.Units.Add(unitInfo);
}
MessageHelper.Broadcast(unit, createUnits);
......
using System.Linq;
namespace ET
{
public class UnitComponentAwakeSystem: AwakeSystem<UnitComponent>
{
public override void Awake(UnitComponent self)
{
}
}
public class UnitComponentDestroySystem: DestroySystem<UnitComponent>
{
public override void Destroy(UnitComponent self)
{
self.idUnits.Clear();
}
}
public static class UnitComponentSystem
{
public static void Add(this UnitComponent self, Unit unit)
{
unit.Parent = self;
self.idUnits.Add(unit.Id, unit);
}
public static Unit Get(this UnitComponent self, long id)
{
self.idUnits.TryGetValue(id, out Unit unit);
return unit;
}
public static void Remove(this UnitComponent self, long id)
{
Unit unit;
self.idUnits.TryGetValue(id, out unit);
self.idUnits.Remove(id);
unit?.Dispose();
}
public static void RemoveNoDispose(this UnitComponent self, long id)
{
self.idUnits.Remove(id);
}
public static Unit[] GetAll(this UnitComponent self)
{
return self.idUnits.Values.ToArray();
}
}
}
\ No newline at end of file
namespace ET
{
public class UnitSystem: AwakeSystem<Unit, int>
{
public override void Awake(Unit self, int configId)
{
self.ConfigId = configId;
}
}
}
\ No newline at end of file
using UnityEngine;
namespace ET
{
public enum UnitType
{
Hero,
Npc
}
public class UnitAwakeSystem : AwakeSystem<Unit, UnitType>
{
public override void Awake(Unit self, UnitType a)
{
self.Awake(a);
}
}
public sealed class Unit: Entity
{
public UnitType UnitType { get; private set; }
public Vector3 Position { get; set; }
public void Awake(UnitType unitType)
{
this.UnitType = unitType;
}
public override void Dispose()
{
if (this.IsDisposed)
{
return;
}
base.Dispose();
}
}
}
\ No newline at end of file
using System;
using MongoDB.Bson.Serialization.Attributes;
using UnityEngine;
namespace ET
{
[BsonIgnoreExtraElements]
public sealed class Unit: Entity
{
public int ConfigId; //配置表id
[BsonIgnore]
public UnitConfig Config => UnitConfigCategory.Instance.Get(this.ConfigId);
private Vector3 position; //坐标
public Vector3 Position
{
get => this.position;
set
{
this.position = value;
Game.EventSystem.Publish(new EventType.ChangePosition() { Unit = this }).Coroutine();
}
}
[BsonIgnore]
public Vector3 Forward
{
get => this.Rotation * Vector3.forward;
set => this.Rotation = Quaternion.LookRotation(value, Vector3.up);
}
private Quaternion rotation;
public Quaternion Rotation
{
get => this.rotation;
set
{
this.rotation = value;
Game.EventSystem.Publish(new EventType.ChangeRotation() {Unit = this}).Coroutine();
}
}
}
}
\ No newline at end of file
......@@ -9,48 +9,8 @@ namespace ET
{
[BsonElement]
[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfArrays)]
private readonly Dictionary<long, Unit> idUnits = new Dictionary<long, Unit>();
public override void Dispose()
{
if (this.IsDisposed)
{
return;
}
base.Dispose();
foreach (Unit unit in this.idUnits.Values)
{
unit.Dispose();
}
this.idUnits.Clear();
}
public void Add(Unit unit)
{
this.idUnits.Add(unit.Id, unit);
unit.Parent = this;
}
public Unit Get(long id)
{
this.idUnits.TryGetValue(id, out Unit unit);
return unit;
}
public void Remove(long id)
{
Unit unit;
this.idUnits.TryGetValue(id, out unit);
this.idUnits.Remove(id);
unit?.Dispose();
}
public void RemoveNoDispose(long id)
{
this.idUnits.Remove(id);
}
public readonly Dictionary<long, Unit> idUnits = new Dictionary<long, Unit>();
public int Count
{
get
......@@ -58,10 +18,5 @@ namespace ET
return this.idUnits.Count;
}
}
public Unit[] GetAll()
{
return this.idUnits.Values.ToArray();
}
}
}
\ No newline at end of file
......@@ -5,5 +5,15 @@
public struct AppStart
{
}
public struct ChangePosition
{
public Unit Unit;
}
public struct ChangeRotation
{
public Unit Unit;
}
}
}
\ No newline at end of file
......@@ -111,12 +111,15 @@ namespace ET
public long UnitId { get; set; }
[ProtoMember(2)]
public float X { get; set; }
public int ConfigId { get; set; }
[ProtoMember(3)]
public float Y { get; set; }
public float X { get; set; }
[ProtoMember(4)]
public float Y { get; set; }
[ProtoMember(5)]
public float Z { get; set; }
}
......
......@@ -16,8 +16,7 @@ namespace ET
{
continue;
}
Unit unit = UnitFactory.Create(session.Domain, unitInfo.UnitId);
unit.Position = new Vector3(unitInfo.X, unitInfo.Y, unitInfo.Z);
Unit unit = UnitFactory.Create(session.Domain, unitInfo);
}
await ETTask.CompletedTask;
......
......@@ -37,7 +37,7 @@ namespace ET
/// </summary>
public static void Turn2D(this TurnComponent self, Vector3 dir, float turnTime = 0.1f)
{
Vector3 nexpos = self.GetParent<Unit>().GameObject.transform.position + dir;
Vector3 nexpos = self.GetParent<Unit>().Position + dir;
self.Turn(nexpos, turnTime);
}
......
......@@ -4,18 +4,19 @@ namespace ET
{
public static class UnitFactory
{
public static Unit Create(Entity domain, long id)
public static Unit Create(Entity domain, UnitInfo unitInfo)
{
Unit unit = EntityFactory.CreateWithId<Unit>(domain, id);
Unit unit = EntityFactory.CreateWithId<Unit, int>(domain, unitInfo.UnitId, unitInfo.ConfigId);
unit.Position = new Vector3(unitInfo.X, unitInfo.Y, unitInfo.Z);
unit.AddComponent<MoveComponent>();
unit.AddComponent<TurnComponent>();
unit.AddComponent<UnitPathComponent>();
Game.EventSystem.Publish(new EventType.AfterUnitCreate() {Unit = unit});
UnitComponent unitComponent = domain.GetComponent<UnitComponent>();
unitComponent.Add(unit);
Game.EventSystem.Publish(new EventType.AfterUnitCreate() {Unit = unit});
return unit;
}
}
......
namespace ET
{
public class UnitSystem: AwakeSystem<Unit, int>
{
public override void Awake(Unit self, int configId)
{
self.ConfigId = configId;
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 0daabdf06b115458495f514cc74d9e34
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -21,6 +21,8 @@ namespace ET
Game.Scene.AddComponent<NetThreadComponent>();
Game.Scene.AddComponent<ZoneSceneManagerComponent>();
Game.Scene.AddComponent<GlobalComponent>();
ResourcesComponent.Instance.LoadBundle("unit.unity3d");
......
fileFormatVersion: 2
guid: 75b45c480953e4122a340e39a52bb914
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
namespace ET
{
public class GlobalComponentAwakeSystem: AwakeSystem<GlobalComponent>
{
public override void Awake(GlobalComponent self)
{
GlobalComponent.Instance = self;
self.Global = GameObject.Find("/Global").transform;
self.Unit = GameObject.Find("/Global/Unit").transform;
self.UI = GameObject.Find("/Global/UI").transform;
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 4cfd49fb001474252a4e9c0c6a5db6c9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -7,12 +7,13 @@ namespace ET
protected override async ETTask Run(EventType.AfterUnitCreate args)
{
// Unit View层
// 这里可以改成异步加载,demo就不搞了
GameObject bundleGameObject = (GameObject)ResourcesComponent.Instance.GetAsset("Unit.unity3d", "Unit");
GameObject prefab = bundleGameObject.Get<GameObject>("Skeleton");
GameObject go = UnityEngine.Object.Instantiate(prefab);
GameObject.DontDestroyOnLoad(go);
args.Unit.GameObject = go;
GameObject go = UnityEngine.Object.Instantiate(prefab, GlobalComponent.Instance.Unit, true);
go.transform.position = args.Unit.Position;
args.Unit.AddComponent<GameObjectComponent>().GameObject = go;
args.Unit.AddComponent<AnimatorComponent>();
await ETTask.CompletedTask;
}
......
......@@ -33,7 +33,7 @@ namespace ET
{
public static void Awake(this AnimatorComponent self)
{
Animator animator = self.GetParent<Unit>().GameObject.GetComponent<Animator>();
Animator animator = self.Parent.GetComponent<GameObjectComponent>().GameObject.GetComponent<Animator>();
if (animator == null)
{
......
......@@ -6,6 +6,16 @@
{
}
public struct ChangePosition
{
public Unit Unit;
}
public struct ChangeRotation
{
public Unit Unit;
}
public struct PingChange
{
public Scene ZoneScene;
......
......@@ -111,12 +111,15 @@ namespace ET
public long UnitId { get; set; }
[ProtoMember(2)]
public float X { get; set; }
public int ConfigId { get; set; }
[ProtoMember(3)]
public float Y { get; set; }
public float X { get; set; }
[ProtoMember(4)]
public float Y { get; set; }
[ProtoMember(5)]
public float Z { get; set; }
}
......
using UnityEngine;
using Quaternion = UnityEngine.Quaternion;
using Vector3 = UnityEngine.Vector3;
using System;
using MongoDB.Bson.Serialization.Attributes;
using UnityEngine;
namespace ET
{
public sealed class Unit: Entity
{
// 先放这里,去掉ViewGO,后面挪到显示层
public GameObject GameObject;
public int ConfigId;
[BsonIgnoreExtraElements]
public sealed class Unit: Entity
{
public int ConfigId; //配置表id
public UnitConfig Config
{
get
{
return UnitConfigCategory.Instance.Get(this.ConfigId);
}
}
public Vector3 Position
{
get
{
return GameObject.transform.position;
}
set
{
GameObject.transform.position = value;
}
}
[BsonIgnore]
public UnitConfig Config => UnitConfigCategory.Instance.Get(this.ConfigId);
public Quaternion Rotation
{
get
{
return GameObject.transform.rotation;
}
set
{
GameObject.transform.rotation = value;
}
}
private Vector3 position; //坐标
}
public Vector3 Position
{
get => this.position;
set
{
this.position = value;
Game.EventSystem.Publish(new EventType.ChangePosition() { Unit = this }).Coroutine();
}
}
[BsonIgnore]
public Vector3 Forward
{
get => this.Rotation * Vector3.forward;
set => this.Rotation = Quaternion.LookRotation(value, Vector3.up);
}
private Quaternion rotation;
public Quaternion Rotation
{
get => this.rotation;
set
{
this.rotation = value;
Game.EventSystem.Publish(new EventType.ChangeRotation() {Unit = this}).Coroutine();
}
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 762e0f6284e284dce99c41909e26c785
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
namespace ET
{
public class GlobalComponent: Entity
{
public static GlobalComponent Instance;
public Transform Global;
public Transform Unit;
public Transform UI;
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: d18ded40beb444ba2a1f8e97028953c3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
namespace ET
{
public class GameObjectComponent: Entity
{
public GameObject GameObject;
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 3d4aec20cfdbe40fdbed5c2555047f8e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -43,7 +43,7 @@ RenderSettings:
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 11
serializedVersion: 12
m_GIWorkflowMode: 0
m_GISettings:
serializedVersion: 2
......@@ -98,7 +98,7 @@ LightmapSettings:
m_TrainingDataDestination: TrainingData
m_LightProbeSampleCountMultiplier: 4
m_LightingDataAsset: {fileID: 0}
m_UseShadowmask: 0
m_LightingSettings: {fileID: 94338416}
--- !u!196 &4
NavMeshSettings:
serializedVersion: 2
......@@ -118,9 +118,72 @@ NavMeshSettings:
manualTileSize: 0
tileSize: 256
accuratePlacement: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
--- !u!850595691 &94338416
LightingSettings:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Settings.lighting
serializedVersion: 3
m_GIWorkflowMode: 0
m_EnableBakedLightmaps: 1
m_EnableRealtimeLightmaps: 1
m_RealtimeEnvironmentLighting: 1
m_BounceScale: 1
m_AlbedoBoost: 1
m_IndirectOutputScale: 1
m_UsingShadowmask: 0
m_BakeBackend: 0
m_LightmapMaxSize: 1024
m_BakeResolution: 40
m_Padding: 2
m_TextureCompression: 1
m_AO: 0
m_AOMaxDistance: 1
m_CompAOExponent: 0
m_CompAOExponentDirect: 0
m_ExtractAO: 0
m_MixedBakeMode: 1
m_LightmapsBakeMode: 1
m_FilterMode: 1
m_LightmapParameters: {fileID: 15204, guid: 0000000000000000f000000000000000, type: 0}
m_ExportTrainingData: 0
m_TrainingDataDestination: TrainingData
m_RealtimeResolution: 2
m_ForceWhiteAlbedo: 0
m_ForceUpdates: 0
m_FinalGather: 0
m_FinalGatherRayCount: 1024
m_FinalGatherFiltering: 1
m_PVRCulling: 1
m_PVRSampling: 1
m_PVRDirectSampleCount: 32
m_PVRSampleCount: 500
m_PVREnvironmentSampleCount: 500
m_PVREnvironmentReferencePointCount: 2048
m_LightProbeSampleCountMultiplier: 4
m_PVRBounces: 2
m_PVRMinBounces: 2
m_PVREnvironmentMIS: 0
m_PVRFilteringMode: 2
m_PVRDenoiserTypeDirect: 0
m_PVRDenoiserTypeIndirect: 0
m_PVRDenoiserTypeAO: 0
m_PVRFilterTypeDirect: 0
m_PVRFilterTypeIndirect: 0
m_PVRFilterTypeAO: 0
m_PVRFilteringGaussRadiusDirect: 1
m_PVRFilteringGaussRadiusIndirect: 5
m_PVRFilteringGaussRadiusAO: 2
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
m_PVRFilteringAtrousPositionSigmaIndirect: 2
m_PVRFilteringAtrousPositionSigmaAO: 1
--- !u!1 &251107983
GameObject:
m_ObjectHideFlags: 0
......@@ -198,6 +261,7 @@ MonoBehaviour:
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 0
--- !u!223 &251107987
Canvas:
m_ObjectHideFlags: 0
......@@ -362,6 +426,7 @@ MonoBehaviour:
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 0
--- !u!223 &493985073
Canvas:
m_ObjectHideFlags: 0
......@@ -425,6 +490,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 1784017108}
- {fileID: 630054496}
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
......@@ -444,6 +510,36 @@ MonoBehaviour:
- key: Unit
gameObject: {fileID: 1610378981859644, guid: cfaf4529ce2243c4c85126e9d008897b,
type: 3}
--- !u!1 &630054495
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 630054496}
m_Layer: 0
m_Name: Unit
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &630054496
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 630054495}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 575235020}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &872594939
GameObject:
m_ObjectHideFlags: 0
......@@ -521,6 +617,7 @@ MonoBehaviour:
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 0
--- !u!223 &872594943
Canvas:
m_ObjectHideFlags: 0
......@@ -711,6 +808,7 @@ MonoBehaviour:
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 0
--- !u!223 &1439952252
Canvas:
m_ObjectHideFlags: 0
......
......@@ -74,6 +74,7 @@
<Compile Include="Assets\Hotfix\Module\Message\SessionAcceptTimeoutComponentSystem.cs" />
<Compile Include="Assets\Hotfix\Scene\ZoneSceneFlagComponentSystem.cs" />
<Compile Include="Assets\Hotfix\Scene\ZoneSceneManagerComponentSystem.cs" />
<Compile Include="Assets\Hotfix\Unit\UnitSystem.cs" />
<None Include="Assets\Hotfix\Unity.Hotfix.asmdef" />
<Reference Include="UnityEngine">
<HintPath>/Applications/Unity/Hub/Editor/2020.2.2f1c1/Unity.app/Contents/Managed/UnityEngine/UnityEngine.dll</HintPath>
......
......@@ -76,6 +76,7 @@
<Compile Include="Assets\HotfixView\Helper\LoadConfigHelper.cs" />
<Compile Include="Assets\HotfixView\Unit\AnimatorComponentSystem.cs" />
<Compile Include="Assets\HotfixView\Scene\AfterCreateZoneScene_AddComponent.cs" />
<Compile Include="Assets\HotfixView\Global\GlobalComponentSystem.cs" />
<None Include="Assets\HotfixView\Unity.HotfixView.asmdef" />
<Reference Include="UnityEngine">
<HintPath>/Applications/Unity/Hub/Editor/2020.2.2f1c1/Unity.app/Contents/Managed/UnityEngine/UnityEngine.dll</HintPath>
......
......@@ -71,6 +71,8 @@
<Compile Include="Assets\ModelView\UI\UILoading\UILoadingComponent.cs" />
<Compile Include="Assets\ModelView\Module\UI\CanvasConfig.cs" />
<Compile Include="Assets\ModelView\Module\UI\UIEventAttribute.cs" />
<Compile Include="Assets\ModelView\Unit\GameObjectComponent.cs" />
<Compile Include="Assets\ModelView\Global\GlobalComponent.cs" />
<None Include="Assets\ModelView\Unity.ModelView.asmdef" />
<Reference Include="UnityEngine">
<HintPath>/Applications/Unity/Hub/Editor/2020.2.2f1c1/Unity.app/Contents/Managed/UnityEngine/UnityEngine.dll</HintPath>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册