zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Server/Hotfix/Game/System/User/PetSystem.cs

384 lines
14 KiB
C#
Executable File

using Cal;
using Cal.DataTable;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ET
{
public class PetAwakeSystem: AwakeSystem<Pet>
{
public override void Awake(Pet self)
{
self.level = 1;
self.name = "宠物";
self.actionEndTime = 0;
self.Load();
}
}
public class PetLoadSystem: LoadSystem<Pet>
{
public override void Load(Pet self)
{
self.Load();
}
}
public class PetDeserializeSystem: DeserializeSystem<Pet>
{
public override void Deserialize(Pet self)
{
self.Load();
}
}
public class PetChangeSystem: ChangeSystem<Pet>
{
public override void Change(Pet self)
{
UnitHelper.SaveComponenet(self);
Unit unit = self.GetParent<Unit>();
M2C_SyncPet proto = new()
{
UnitId = self.Id,
petId = self.petId,
exp = self.exp,
isShow = self.isShow,
level = self.level,
name = self.name,
intimacy = self.intimacy,
};
BrocastComponent brocastComponent = unit.GetComponent<BrocastComponent>();
brocastComponent?.Brocast(proto);
}
}
public class PetDestroySystem: DestroySystem<Pet>
{
public override void Destroy(Pet self)
{
self.petId = 0;
self.exp = 0;
self.intimacy = 0;
self.actionEndTime = 0;
self.isShow = false;
self.petState = Pet.PetState.Idle;
self.addToCharacter.Clear();
self.getAttributeFunc = null;
}
}
public static class PetSystem
{
public static void Load(this Pet self)
{
self.getAttributeFunc = self.GetAttribute;
self.Init();
}
private static void Init(this Pet self)
{
}
private static float GetAttribute(this Pet self, AttributeType attributeType)
{
float bound = 0f;
try
{
PetConfig petConfig = PetConfigCategory.Instance.Get(self.petId);
foreach (PetConfig.AddAttibuteMap addAttibuteMap in petConfig.AddAttibuteMapArr)
{
if (addAttibuteMap.Key != (int) attributeType)
{
continue;
}
bound =addAttibuteMap.BaseValue+ addAttibuteMap.Value * self.level;
break;
}
}
catch (Exception e)
{
Log.Error(e);
}
return bound;
}
private static string AddLevel(this Pet self, int level)
{
self.level += level;
PetConfig petConfig = PetConfigCategory.Instance.Get(self.petId);
foreach (PetConfig.AddAttibuteMap addAttibuteMap in petConfig.AddAttibuteMapArr)
{
if (!self.addToCharacter.TryGetValue(addAttibuteMap.Key, out float old))
self.addToCharacter[addAttibuteMap.Key] = 0;
self.addToCharacter[addAttibuteMap.Key] = old + addAttibuteMap.Value * level;
}
CharacterHelper.SyncNumeric(self.GetParent<Unit>());
return null;
}
public static void AddExp(this Pet self, int exp)
{
exp += self.exp;
int addLevel = 0;
PetLevelConfig petLevelConfig = PetLevelConfigCategory.Instance.Get(self.petId);
while (exp > GetNeedExp(petLevelConfig, self.level))
{
exp -= GetNeedExp(petLevelConfig, self.level);
addLevel++;
}
self.exp = Math.Clamp(exp, 0, int.MaxValue);
int beyondLevelevel = self.level + addLevel - petLevelConfig.MaxLevel;
if (beyondLevelevel > 0)
addLevel -= beyondLevelevel;
if (addLevel > 0)
self.AddLevel(addLevel);
Game.EventSystem.Change(self);
}
private static int GetNeedExp(PetLevelConfig petLevelConfig, int level)
{
return petLevelConfig.Exp + petLevelConfig.ExpByLevel * level;
}
public static void AddIntimacy(this Pet self, int intimacy)
{
self.intimacy += intimacy;
Game.EventSystem.Change(self);
}
public static void AddActive(this Pet self, int active)
{
PlayerData data = self.Parent.GetComponent<PlayerData>();
data.petActive += active;
}
public static string ChangeName(this Pet self, string name)
{
if (!FilterCharHelper.IsInvalid(name))
{
return "昵称不能超过7位,不能含有特殊字符";
}
self.name = name;
Log.Info($"宠物改名:{self.name}");
Game.EventSystem.Change(self);
return null;
}
/// <summary>
/// 探险
/// </summary>
/// <param name="self"></param>
/// <returns></returns>
public static string Explore(this Pet self)
{
if (self.petState is not (Pet.PetState.Idle))
{
return "行动中";
}
PlayerData data = self.Parent.GetComponent<PlayerData>();
if (data.petActive <= 0) return "活跃度不足";
const int index = (int) PetExploreConfigCategory.PetActionType.Explore;
var configList = PetExploreConfigCategory.Instance.GetListByType((PetExploreConfigCategory.PetActionType) index);
int petActionIndexFlag = IntFlagHelper.GetFlag(data.petActionIndexFlag, index - 1, 2, 3);
if (petActionIndexFlag >= configList.Count)
return "次数达到上限";
PetExploreConfig config = configList[petActionIndexFlag];
self.actionEndTime = Game.TimeInfo.ClientNow() + config.Time;
data.petActive--;
self.petState = Pet.PetState.Explore;
return null;
}
/// <summary>
/// 嬉戏
/// </summary>
/// <param name="self"></param>
/// <returns></returns>
public static string Play(this Pet self)
{
if (self.petState is not (Pet.PetState.Idle))
{
return "行动中";
}
PlayerData data = self.Parent.GetComponent<PlayerData>();
if (data.petActive <= 0) return "活跃度不足";
const int index = (int) PetExploreConfigCategory.PetActionType.Play;
var configList = PetExploreConfigCategory.Instance.GetListByType((PetExploreConfigCategory.PetActionType) index);
int petActionIndexFlag = IntFlagHelper.GetFlag(data.petActionIndexFlag, index - 1, 2, 3);
if (petActionIndexFlag >= configList.Count)
return "次数达到上限";
PetExploreConfig config = configList[petActionIndexFlag];
self.actionEndTime = Game.TimeInfo.ClientNow() + config.Time;
data.petActive--;
self.petState = Pet.PetState.Play;
return null;
}
/// <summary>
/// 锻炼
/// </summary>
/// <param name="self"></param>
/// <returns></returns>
public static string Experience(this Pet self)
{
if (self.petState is not (Pet.PetState.Idle))
{
return "行动中";
}
PlayerData data = self.Parent.GetComponent<PlayerData>();
if (data.petActive <= 0) return "活跃度不足";
const int index = (int) PetExploreConfigCategory.PetActionType.Experience;
var configList = PetExploreConfigCategory.Instance.GetListByType((PetExploreConfigCategory.PetActionType) index);
int petActionIndexFlag = IntFlagHelper.GetFlag(data.petActionIndexFlag, index - 1, 2, 3);
if (petActionIndexFlag >= configList.Count)
return "次数达到上限";
PetExploreConfig config = configList[petActionIndexFlag];
self.actionEndTime = Game.TimeInfo.ClientNow() + config.Time;
data.petActive--;
self.petState = Pet.PetState.Experience;
return null;
}
public static string GetActionReword(this Pet self)
{
PetExploreConfigCategory.PetActionType type;
switch (self.petState)
{
case Pet.PetState.Play:
type = PetExploreConfigCategory.PetActionType.Play;
break;
case Pet.PetState.Experience:
type = PetExploreConfigCategory.PetActionType.Experience;
break;
case Pet.PetState.Explore:
type = PetExploreConfigCategory.PetActionType.Explore;
break;
default:
return "宠物没有开始此玩法呢";
}
long now = TimeHelper.ClientNow();
if (now < self.actionEndTime)
return "宠物忙碌中!";
Unit unit = self.GetParent<Unit>();
PlayerData data = unit.GetComponent<PlayerData>();
int index = (int) type;
var configList = PetExploreConfigCategory.Instance.GetListByType((PetExploreConfigCategory.PetActionType) index);
int petActionIndexFlag = IntFlagHelper.GetFlag(data.petActionIndexFlag, index - 1, 2, 3);
if (petActionIndexFlag >= configList.Count)
return "系统错误,次数达到上限";
PetExploreConfig config = configList[petActionIndexFlag];
switch (type)
{
case PetExploreConfigCategory.PetActionType.Explore:
//给掉落
using (UnOrderMultiMapComponent<long, (int, int)> rewordMapComponent = UnOrderMultiMapComponent<long, (int, int)>.Create())
{
int parentId = config.EndRewordArr.FirstOrDefault(t => t.PetId == self.petId).IncressValue;
if (parentId == 0) return "系统错误";
DropHelper.Drop(unit, data, BattleType.None, parentId, rewordMapComponent.MultiMap, 1, "宠物探险",
false);
using ListComponent<string> listComponent = ListComponent<string>.Create();
foreach (KeyValuePair<long, List<(int, int)>> kp in rewordMapComponent.MultiMap.GetDictionary())
{
foreach ((int id, int count) in kp.Value)
{
listComponent.List.Add($"[color=#ffff00][/color]获得了[color=#226655]{BagHelper.GetName(id)}x{count}[/color]");
}
}
Chat.Instance.SendSystemCahtNoBrocast(unit, listComponent.List);
}
break;
case PetExploreConfigCategory.PetActionType.Play:
//给亲密度
self.AddIntimacy(config.EndRewordArr[0].IncressValue);
break;
case PetExploreConfigCategory.PetActionType.Experience:
//给经验
self.AddExp(config.EndRewordArr[0].IncressValue);
break;
default:
throw new ArgumentOutOfRangeException(nameof (type), type, null);
}
self.petState = Pet.PetState.Idle;
petActionIndexFlag++;
IntFlagHelper.SetFlagValue(ref data.petActionIndexFlag, index - 1, petActionIndexFlag, 2, 3);
Log.Info($"{data.petActionIndexFlag}");
return null;
}
public static string QuickEnd(this Pet self)
{
long time = self.actionEndTime - TimeHelper.ClientNow();
int second = (int) (time / 1000);
if (second <= 0)
return "已经结束";
int voucher = second / 20;
if (voucher <= 0)
return "即将结束";
var ret = CharacterHelper.ReduceMoney(self.GetParent<Unit>(), CharacterHelper.MoneyType.Voucher, voucher);
if (ret != null)
return ret;
self.actionEndTime = TimeHelper.ClientNow();
return null;
}
public static int GetQuickEndPrice(this Pet self)
{
long time = self.actionEndTime - TimeHelper.ClientNow();
int second = (int) (time / 1000);
if (second <= 0)
return 0;
int voucher = second / 20;
if (voucher <= 0)
return 0;
return voucher;
}
public static string UpgradePet(this Pet self, int index)
{
try
{
if (self.petState != Pet.PetState.Idle)
return "宠物忙碌中";
PetConfig petConfig = PetConfigCategory.Instance.Get(self.petId);
int petId = petConfig.UpgradeIdArr[index];
if (self.level < petConfig.NeedLevel || self.intimacy < petConfig.NeedIntimacy)
return "条件不足";
self.level = 1;
self.exp = 0;
self.intimacy = 0;
self.actionEndTime = 0;
self.isShow = false;
self.petState = Pet.PetState.Idle;
self.addToCharacter.Clear();
self.petId = petId;
Game.EventSystem.Change(self);
}
catch (Exception e)
{
Log.Error(e);
}
return null;
}
}
}