289 lines
10 KiB
C#
Executable File
289 lines
10 KiB
C#
Executable File
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Cal.DataTable;
|
|
using MongoDB.Bson;
|
|
using UnityEngine;
|
|
|
|
namespace ET
|
|
{
|
|
public class UnitHelper
|
|
{
|
|
public static async ETTask<Unit> Query(Entity domain, long id)
|
|
{
|
|
try
|
|
{
|
|
Unit unit = EntityFactory.CreateWithId<Unit>(domain, id);
|
|
PlayerData data = await DBComponent.Instance.Query<PlayerData>(id);
|
|
if (data)
|
|
unit.AddComponent(data);
|
|
Character character = await DBComponent.Instance.Query<Character>(id);
|
|
if (character)
|
|
unit.AddComponent(character);
|
|
NumericComponent num = await DBComponent.Instance.Query<NumericComponent>(id);
|
|
if (num)
|
|
unit.AddComponent(num);
|
|
UnitSkillComponent unitSkill = await DBComponent.Instance.Query<UnitSkillComponent>(id);
|
|
if (unitSkill)
|
|
unit.AddComponent(unitSkill);
|
|
//!玩家设置
|
|
UserSetting setting = await DBComponent.Instance.Query<UserSetting>(id);
|
|
if (setting)
|
|
unit.AddComponent(setting);
|
|
//!任务
|
|
UnitTask unitTask = await DBComponent.Instance.Query<UnitTask>(id);
|
|
if (unitTask)
|
|
unit.AddComponent(unitTask);
|
|
//!背包
|
|
Bag bag = await DBComponent.Instance.Query<Bag>(id);
|
|
if (bag)
|
|
unit.AddComponent(bag);
|
|
UnitScene unitScene = await DBComponent.Instance.Query<UnitScene>(id);
|
|
if (unitScene)
|
|
unit.AddComponent(unitScene);
|
|
StarSoulBag starSoulBag = await DBComponent.Instance.Query<StarSoulBag>(id);
|
|
if (starSoulBag)
|
|
unit.AddComponent(starSoulBag);
|
|
Pet pet = await DBComponent.Instance.Query<Pet>(id);
|
|
if (pet)
|
|
unit.AddComponent(pet);
|
|
unit.AddComponent<Combat>();
|
|
|
|
return unit;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static async ETTask<T> Query<T>(int zone, long id) where T : Entity
|
|
{
|
|
try
|
|
{
|
|
long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(zone, SceneType.UserCache.ToString()).SceneId;
|
|
U2M_GetComponent getComponent =
|
|
(U2M_GetComponent) await MessageHelper.CallActor(mapInstanceId,
|
|
new M2U_GetComponent() { UserId = id, type = typeof (T).Name });
|
|
if (getComponent.component == null) return null;
|
|
T t = getComponent.component as T;
|
|
return t;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static void Save<T>(Unit unit) where T : Entity
|
|
{
|
|
Entity entity = unit.GetComponent<T>();
|
|
int zone = entity.DomainZone();
|
|
if (zone == 0)
|
|
{
|
|
Log.Error($"zone == 0 when {entity.GetType()}");
|
|
zone = 1;
|
|
}
|
|
|
|
long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(zone, SceneType.UserCache.ToString()).SceneId;
|
|
MessageHelper.SendActor(mapInstanceId, new M2U_WriteComponent() { Id = entity.Id, type = typeof (T).Name, component = entity });
|
|
}
|
|
|
|
public static void SaveComponenet<T>(T t) where T : Entity
|
|
{
|
|
int zone = t.DomainZone();
|
|
if (zone == 0)
|
|
{
|
|
Log.ErrorDetail($"zone == 0 when {t.GetType()}");
|
|
zone = 1;
|
|
}
|
|
|
|
// await DBComponent.Instance.Save(t);
|
|
long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(zone, SceneType.UserCache.ToString()).SceneId;
|
|
MessageHelper.SendActor(mapInstanceId, new M2U_WriteComponent() { Id = t.Id, type = typeof (T).Name, component = t });
|
|
}
|
|
|
|
public static void SaveComponenet<T>(int zone, T t) where T : Entity
|
|
{
|
|
if (zone == 0)
|
|
{
|
|
Log.ErrorDetail($"zone == 0 when {t.GetType()}");
|
|
zone = 1;
|
|
}
|
|
|
|
// await DBComponent.Instance.Save(t);
|
|
long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(zone, SceneType.UserCache.ToString()).SceneId;
|
|
MessageHelper.SendActor(mapInstanceId, new M2U_WriteComponent() { Id = t.Id, type = typeof (T).Name, component = t });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置位置信息以及人物信息
|
|
/// </summary>
|
|
/// <param name="unit"></param>
|
|
/// <param name="unitInfo"></param>
|
|
/// <param name="pvpUnitCharacter"></param>
|
|
public static void SetUnitInfo(Unit unit, UnitPosInfo unitInfo)
|
|
{
|
|
UnitScene unitScene = unit.GetComponent<UnitScene>();
|
|
if (unitScene == null)
|
|
{
|
|
Log.Error($"unitScene == null where id = {unit.Id}");
|
|
return;
|
|
}
|
|
|
|
unitInfo.X = unitScene.X;
|
|
unitInfo.Y = unitScene.Y;
|
|
unitInfo.YAngle = unitScene.YAngle;
|
|
}
|
|
|
|
public static async ETTask AddComponentFromDB(Unit unit, int jobId)
|
|
{
|
|
int zone = unit.DomainZone();
|
|
long id = unit.Id;
|
|
//!玩家数据
|
|
var data = await UnitHelper.Query<PlayerData>(zone, id);
|
|
if (data == null)
|
|
{
|
|
data = unit.AddComponent<PlayerData>();
|
|
data.IsNew = true;
|
|
data.UpdateMinstoryRecord(Sys_SceneId.Scene_MainStory1 - 1);
|
|
data.SkillPointKV = new KeyValuePair<int, int>(1, 1);
|
|
}
|
|
else
|
|
unit.AddComponent(data);
|
|
|
|
Character character = await UnitHelper.Query<Character>(zone, id);
|
|
if (character == null)
|
|
character = unit.AddComponent<Character>();
|
|
else
|
|
unit.AddComponent(character);
|
|
|
|
var num = await UnitHelper.Query<NumericComponent>(zone, id);
|
|
if (num == null)
|
|
{
|
|
num = unit.AddComponent<NumericComponent>();
|
|
CharacterHelper.InitData(num);
|
|
num.Set(NumericType.SkinId, jobId);
|
|
num.Set(NumericType.Energy, 1000);
|
|
}
|
|
else
|
|
unit.AddComponent(num);
|
|
|
|
//!exp
|
|
int energy = num.GetAsInt(NumericType.Energy);
|
|
data.ForbidExp = energy <= 0;
|
|
|
|
UnitSkillComponent unitSkill = await UnitHelper.Query<UnitSkillComponent>(zone, id);
|
|
if (unitSkill == null)
|
|
{
|
|
unitSkill = unit.AddComponent<UnitSkillComponent>();
|
|
JobType jobType = JobHelper.GetJobType(jobId);
|
|
unitSkill.InitSkill(jobType);
|
|
}
|
|
else
|
|
unit.AddComponent(unitSkill);
|
|
|
|
//!玩家设置
|
|
UserSetting setting = await UnitHelper.Query<UserSetting>(zone, id);
|
|
if (setting == null)
|
|
{
|
|
setting = unit.AddComponent<UserSetting>();
|
|
}
|
|
else
|
|
unit.AddComponent(setting);
|
|
|
|
setting.SetCD(MathHelper.RoundToInt(CharacterHelper.GetSkillCD(num.Get(NumericType.Spd))));
|
|
//!宠物
|
|
Pet pet = await UnitHelper.Query<Pet>(zone, id);
|
|
if (pet == null)
|
|
{
|
|
pet = unit.AddComponent<Pet>();
|
|
}
|
|
else
|
|
unit.AddComponent(pet);
|
|
|
|
if (pet.petId == 0)
|
|
{
|
|
pet.petId = 2101;
|
|
}
|
|
|
|
UnitScene unitScene = await UnitHelper.Query<UnitScene>(zone, id);
|
|
if (unitScene == null)
|
|
{
|
|
unitScene = UnitSceneFactory.Create(Game.Scene, id, new Vector2(0, 0), 0, Sys_SceneId.Scene_Beach * 100 + 1);
|
|
unit.AddComponent(unitScene);
|
|
}
|
|
else
|
|
unit.AddComponent(unitScene);
|
|
|
|
//!任务
|
|
UnitTask unitTask = await UnitHelper.Query<UnitTask>(zone, id);
|
|
if (unitTask == null)
|
|
{
|
|
unitTask = unit.AddComponent<UnitTask>();
|
|
}
|
|
else
|
|
unit.AddComponent(unitTask);
|
|
|
|
//!背包
|
|
Bag bag = await UnitHelper.Query<Bag>(zone, id);
|
|
if (bag == null)
|
|
{
|
|
bag = unit.AddComponent<Bag>();
|
|
}
|
|
else
|
|
unit.AddComponent(bag);
|
|
|
|
if (bag.ItemDic.Count == 0)
|
|
{
|
|
bag.InitBag();
|
|
}
|
|
|
|
bag.CheckSlot();
|
|
|
|
Store store = await StoreComponent.Instance.Query(unit.Id);
|
|
//!初始化仓库
|
|
if (store == null)
|
|
StoreComponent.Instance.Init(unit.Id).Coroutine();
|
|
store.CheckSlot();
|
|
//!背包
|
|
StarSoulBag StarSoulBag = await UnitHelper.Query<StarSoulBag>(zone, id);
|
|
if (StarSoulBag == null)
|
|
{
|
|
StarSoulBag = unit.AddComponent<StarSoulBag>();
|
|
}
|
|
else
|
|
unit.AddComponent(StarSoulBag);
|
|
}
|
|
|
|
public static async ETTask SendUnitInfo(Unit unit)
|
|
{
|
|
Log.Debug($"【{UserComponent.Instance.Get(unit.Id)?.NickName} ({unit.Id})】已经进入游戏");
|
|
PlayerData data = unit.GetComponent<PlayerData>();
|
|
|
|
var num = unit.GetComponent<NumericComponent>();
|
|
CharacterHelper.ResetAddNumeric(unit, num);
|
|
data.IsBattleIdle = false;
|
|
UnitScene unitScene = unit.GetComponent<UnitScene>();
|
|
if (unitScene == null)
|
|
{
|
|
Log.Error($"unitScene == null where id = {unit.Id}");
|
|
return;
|
|
}
|
|
|
|
if (unitScene.MapId / 100 == Sys_SceneId.Scene_BattleIdle)
|
|
{
|
|
unitScene.MapId = Sys_SceneId.Scene_MainCity * 100 + 1;
|
|
}
|
|
|
|
//!+任务相关
|
|
|
|
//!+发送Boss消息
|
|
Game.EventSystem.Publish(new EventType.AfterEnterGame() { unit = unit });
|
|
}
|
|
}
|
|
} |