1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using GameConfig; using TEngine; using UnityEngine; using Object = System.Object;
namespace GameLogic { public class EntityDataControl : Singleton<EntityDataControl> { private static readonly Dictionary<EnumProjectile, Type> _projectileLogicMap = new() { { EnumProjectile.EntityProjectileHitscanLogic, typeof(EntityProjectileHitscanLogic) }, { EnumProjectile.EntityProjectileBallisticLogic, typeof(EntityProjectileBallisticLogic) }, { EnumProjectile.EntityEnergyPylonLogic, typeof(EntityEnergyPylonLogic) }, { EnumProjectile.EntityEMPGeneratorLogic, typeof(EntityEMPGeneratorLogic) }, { EnumProjectile.EntityProjectileWobblingHomingLogic, typeof(EntityProjectileWobblingHomingLogic) } }; private static readonly Dictionary<Type, Action<int, int, Action<Entity>, object>> _logicMap = new() { { typeof(EntityPlayerLogic), (id, serial, cb, data) => ShowEntity<EntityPlayerLogic>(id, serial, cb, data) }, { typeof(EntityRocketPlatformLogic), (id, serial, cb, data) => ShowEntity<EntityRocketPlatformLogic>(id, serial, cb, data) }, { typeof(EntityTowerBaseLogic), (id, serial, cb, data) => ShowEntity<EntityTowerBaseLogic>(id, serial, cb, data) }, { typeof(EntityTowerLevelLogic), (id, serial, cb, data) => ShowEntity<EntityTowerLevelLogic>(id, serial, cb, data) }, { typeof(EntityTowerAttackerLogic), (id, serial, cb, data) => ShowEntity<EntityTowerAttackerLogic>(id, serial, cb, data) }, { typeof(EntityTowerPreviewLogic), (id, serial, cb, data) => ShowEntity<EntityTowerPreviewLogic>(id, serial, cb, data) }, { typeof(EntityRadiusLogic), (id, serial, cb, data) => ShowEntity<EntityRadiusLogic>(id, serial, cb, data) }, { typeof(EntityEnemyLogic), (id, serial, cb, data) => ShowEntity<EntityEnemyLogic>(id, serial, cb, data) }, { typeof(EntityProjectileHitscanLogic), (id, serial, cb, data) => ShowEntity<EntityProjectileHitscanLogic>(id, serial, cb, data) }, { typeof(EntityParticleAutoHideLogic), (id, serial, cb, data) => ShowEntity<EntityParticleAutoHideLogic>(id, serial, cb, data) }, { typeof(EntityHPBarLogic), (id, serial, cb, data) => ShowEntity<EntityHPBarLogic>(id, serial, cb, data) }, { typeof(EntityProjectileBallisticLogic), (id, serial, cb, data) => ShowEntity<EntityProjectileBallisticLogic>(id, serial, cb, data) }, { typeof(EntityHideSelfProjectileLogic), (id, serial, cb, data) => ShowEntity<EntityHideSelfProjectileLogic>(id, serial, cb, data) }, { typeof(EntityProjectileLogic), (id, serial, cb, data) => ShowEntity<EntityProjectileLogic>(id, serial, cb, data) }, { typeof(EntityEnergyPylonLogic), (id, serial, cb, data) => ShowEntity<EntityEnergyPylonLogic>(id, serial, cb, data) }, { typeof(EntityEMPGeneratorLogic), (id, serial, cb, data) => ShowEntity<EntityEMPGeneratorLogic>(id, serial, cb, data) }, { typeof(EntityProjectileWobblingHomingLogic), (id, serial, cb, data) => ShowEntity<EntityProjectileWobblingHomingLogic>(id, serial, cb, data) }, { typeof(EntityPlasmaLanceLogic), (id, serial, cb, data) => ShowEntity<EntityPlasmaLanceLogic>(id, serial, cb, data) }, { typeof(EntityAnimationLogic), (id, serial, cb, data) => ShowEntity<EntityAnimationLogic>(id, serial, cb, data) }, }; public static Type GetProjectileLogicType(EnumProjectile projectileType) { if (_projectileLogicMap.TryGetValue(projectileType, out var logicType)) { return logicType; }
Log.Error("未找到对应的 Projectile 逻辑类型: {0}", projectileType); return null; } public static void ShowEntity<TLogic>(int entityId, int serialId, Action<Entity> onShowSuccess, object userData = null) where TLogic : EntityLogic { string pathName = AssetsDataLoader.Instance.GetItemConfig(entityId).ResourcesName; if (pathName == null) { Log.Error("EntityDataControl ShowEntity pathName is null, entityId: {0}", entityId); return; }
GameObject gameObject = PoolManager.Instance.GetGameObject(pathName); Entity entity = gameObject.GetComponent<Entity>();
TLogic entityLogic = gameObject.GetComponent<TLogic>();
if (entityLogic == null) { throw new Exception($"GameObject {gameObject.name} 没有组件 {typeof(TLogic).Name}"); }
entity.OnInit(entityId, serialId, pathName, entityLogic); entityLogic.OnInit(userData);
GameModule.Entity.AddToDic(serialId, entity); entity.OnShow(userData);
onShowSuccess?.Invoke(entity); }
public void ShowEntity(ShowEntityEventData entityData) { if (!_logicMap.TryGetValue(entityData.LogicType, out var action)) { throw new InvalidOperationException($"未知的逻辑类型: {entityData.LogicType.FullName}"); }
Log.Debug("ShowEntity entityId:{0} ", entityData.EntityId);
Action<Entity> callback = entity => { entityData.OnShowSuccess?.Invoke(entity); entityData.Clear(); };
action.Invoke(entityData.EntityId, entityData.SerialId, callback, entityData.UserData); }
public void HideEntity(Entity entity) { if (entity == null) return;
GameModule.Entity.HideEntity(entity); }
public void HideAllEntities() { Log.Debug("HideAllEntities"); GameModule.Entity.HideAllEntity(); }
public IEnumerable<Entity> GetAllEntities() { return GameModule.Entity.GetAllEntities(); }
public void Clear() { } }
public class EntityShowOptionsArgs { public int EntityId; public int ManualSerialId; public bool AutoGenerateSerialId = true; public Type LogicType; public Action<Entity> OnShowSuccess; public object UserData;
public EntityShowOptionsArgs( int entityId, int manualSerialId, bool autoGenerateSerialId, Type logicType, Action<Entity> onShowSuccess, object userData) { EntityId = entityId; ManualSerialId = manualSerialId; AutoGenerateSerialId = autoGenerateSerialId; LogicType = logicType; this.OnShowSuccess = onShowSuccess; UserData = userData; } }
public class ShowEntityEventData : IMemory { public int EntityId; public int SerialId; public Type LogicType; public Object UserData; public Action<Entity> OnShowSuccess = null;
public void Clear() { EntityId = 0; SerialId = 0; LogicType = null; UserData = null; OnShowSuccess = null; } } }
|