VPet/VPet-Simulator.Core/Display/MainLogic.cs

482 lines
18 KiB
C#
Raw Normal View History

2022-12-13 07:10:18 +00:00
using System;
2023-07-16 23:58:09 +00:00
using System.Collections.Generic;
using System.Linq;
2022-12-13 07:10:18 +00:00
using System.Threading.Tasks;
using System.Timers;
2022-12-14 18:17:13 +00:00
using System.Windows;
2023-08-10 16:07:28 +00:00
using System.Windows.Controls;
2023-07-16 23:58:09 +00:00
using System.Windows.Documents;
using static VPet_Simulator.Core.GraphInfo;
2022-12-13 07:10:18 +00:00
namespace VPet_Simulator.Core
{
public partial class Main
{
2023-01-26 15:15:37 +00:00
public const int TreeRND = 5;
2022-12-14 18:17:13 +00:00
2023-04-01 19:31:28 +00:00
/// <summary>
/// 处理说话内容
/// </summary>
public event Action<string> OnSay;
2023-06-14 11:14:04 +00:00
/// <summary>
/// 上次交互时间
/// </summary>
public DateTime LastInteractionTime { get; set; } = DateTime.Now;
2023-04-04 16:21:46 +00:00
/// <summary>
/// 事件Timer
/// </summary>
2022-12-13 07:10:18 +00:00
public Timer EventTimer = new Timer(15000)
{
AutoReset = true,
Enabled = true
};
2023-07-16 23:58:09 +00:00
/// <summary>
/// 说话,使用随机表情
/// </summary>
2023-09-27 07:13:48 +00:00
public void SayRnd(string text, bool force = false, string desc = null)
2023-05-26 16:05:59 +00:00
{
2023-09-27 07:13:48 +00:00
Say(text, Core.Graph.FindName(GraphType.Say), force, desc);
2023-05-26 16:05:59 +00:00
}
2023-01-27 17:44:57 +00:00
/// <summary>
/// 说话
/// </summary>
/// <param name="text">说话内容</param>
2023-09-27 07:13:48 +00:00
public void Say(string text, string graphname = null, bool force = false, string desc = null)
2023-01-26 15:15:37 +00:00
{
2023-05-25 11:02:13 +00:00
Task.Run(() =>
{
OnSay?.Invoke(text);
2023-08-13 16:31:37 +00:00
if (force || !string.IsNullOrWhiteSpace(graphname) && DisplayType.Type == GraphType.Default)//这里不使用idle是因为idle包括学习等
2023-07-16 23:58:09 +00:00
Display(graphname, AnimatType.A_Start, () =>
2023-05-25 11:02:13 +00:00
{
2023-09-27 07:13:48 +00:00
Dispatcher.Invoke(() =>
{
if (!string.IsNullOrWhiteSpace(desc))
MsgBar.MessageBoxContent.Children.Add(new TextBlock() { Text = desc, FontSize = 20, ToolTip = desc, HorizontalAlignment = System.Windows.HorizontalAlignment.Right });
MsgBar.Show(Core.Save.Name, text, graphname);
});
2023-07-16 23:58:09 +00:00
DisplayBLoopingForce(graphname);
2023-05-25 11:02:13 +00:00
});
else
2023-01-27 17:44:57 +00:00
{
2023-09-27 07:13:48 +00:00
Dispatcher.Invoke(() =>
{
if (!string.IsNullOrWhiteSpace(desc))
MsgBar.MessageBoxContent.Children.Add(new TextBlock() { Text = desc, FontSize = 20, ToolTip = desc, HorizontalAlignment = System.Windows.HorizontalAlignment.Right });
MsgBar.Show(Core.Save.Name, text);
});
2023-05-25 11:02:13 +00:00
}
});
2023-01-26 15:15:37 +00:00
}
2023-06-25 18:54:34 +00:00
int labeldisplaycount = 100;
int labeldisplayhash = 0;
Timer labeldisplaytimer = new Timer(10)
{
AutoReset = true,
};
double labeldisplaychangenum1 = 0;
double labeldisplaychangenum2 = 0;
/// <summary>
/// 显示消息弹窗Label
/// </summary>
/// <param name="text">文本</param>
/// <param name="time">持续时间</param>
public void LabelDisplayShow(string text, int time = 2000)
{
labeldisplayhash = text.GetHashCode();
Dispatcher.Invoke(() =>
{
LabelDisplay.Content = text;
LabelDisplay.Opacity = 1;
LabelDisplay.Visibility = Visibility.Visible;
labeldisplaycount = time / 10;
labeldisplaytimer.Start();
});
}
/// <summary>
/// 显示消息弹窗Lable,自动统计数值变化
/// </summary>
2023-07-01 07:46:08 +00:00
/// <param name="text">文本, 使用{0:f2}</param>
2023-06-25 18:54:34 +00:00
/// <param name="changenum1">变化值1</param>
/// <param name="changenum2">变化值2</param>
/// <param name="time">持续时间</param>
2023-07-01 07:46:08 +00:00
public void LabelDisplayShowChangeNumber(string text, double changenum1, double changenum2 = 0, int time = 2000)
2023-06-25 18:54:34 +00:00
{
if (labeldisplayhash == text.GetHashCode())
{
labeldisplaychangenum1 += changenum1;
labeldisplaychangenum2 += changenum2;
}
else
{
labeldisplaychangenum1 = changenum1;
labeldisplaychangenum2 = changenum2;
labeldisplayhash = text.GetHashCode();
}
Dispatcher.Invoke(() =>
{
2023-07-01 07:46:08 +00:00
LabelDisplay.Content = string.Format(text, labeldisplaychangenum1, labeldisplaychangenum2);
2023-06-25 18:54:34 +00:00
LabelDisplay.Opacity = 1;
LabelDisplay.Visibility = Visibility.Visible;
labeldisplaycount = time / 10;
labeldisplaytimer.Start();
});
}
2023-05-10 03:13:34 +00:00
/// <summary>
/// 根据消耗计算相关数据
/// </summary>
/// <param name="TimePass">过去时间倍率</param>
public void FunctionSpend(double TimePass)
2022-12-13 07:10:18 +00:00
{
2023-01-22 17:33:13 +00:00
Core.Save.CleanChange();
2023-06-03 21:51:58 +00:00
Core.Save.StoreTake();
2023-06-14 11:14:04 +00:00
double freedrop = (DateTime.Now - LastInteractionTime).TotalMinutes;
if (freedrop < 1)
freedrop = 0.25 * TimePass;
2023-06-14 11:14:04 +00:00
else
freedrop = Math.Sqrt(freedrop) * TimePass / 2;
2023-05-19 08:17:51 +00:00
switch (State)
2023-05-10 03:13:34 +00:00
{
2023-05-19 08:17:51 +00:00
case WorkingState.Sleep:
2023-08-17 14:47:40 +00:00
//睡觉不消耗
Core.Save.StrengthChange(TimePass * 2);
if (Core.Save.StrengthFood <= 25)
2023-05-19 08:17:51 +00:00
{
2023-08-17 14:47:40 +00:00
Core.Save.StrengthChangeFood(TimePass / 2);
2023-07-16 23:58:09 +00:00
}
2023-08-17 14:47:40 +00:00
else if (Core.Save.StrengthFood >= 75)
Core.Save.Health += TimePass * 2;
2023-07-16 23:58:09 +00:00
if (Core.Save.StrengthDrink >= 25)
{
2023-08-17 14:47:40 +00:00
Core.Save.StrengthChangeDrink(TimePass / 2);
2023-05-19 08:17:51 +00:00
}
2023-08-17 14:47:40 +00:00
else if (Core.Save.StrengthDrink >= 75)
Core.Save.Health += TimePass * 2;
LastInteractionTime = DateTime.Now;
2023-05-19 08:17:51 +00:00
break;
2023-07-16 23:58:09 +00:00
case WorkingState.Work:
2023-08-10 16:07:28 +00:00
var nowwork = nowWork;
2023-07-16 23:58:09 +00:00
var needfood = TimePass * nowwork.StrengthFood;
var needdrink = TimePass * nowwork.StrengthDrink;
double efficiency = 0;
int addhealth = -2;
2023-05-23 08:23:47 +00:00
if (Core.Save.StrengthFood <= 25)
2023-07-16 23:58:09 +00:00
{//低状态低效率
Core.Save.StrengthChangeFood(-needfood / 2);
efficiency += 0.25;
if (Core.Save.Strength >= needfood)
2023-05-23 08:23:47 +00:00
{
2023-07-16 23:58:09 +00:00
Core.Save.StrengthChange(-needfood);
efficiency += 0.1;
2023-05-23 08:23:47 +00:00
}
2023-07-16 23:58:09 +00:00
addhealth -= 2;
2023-05-23 08:23:47 +00:00
}
else
{
2023-07-16 23:58:09 +00:00
Core.Save.StrengthChangeFood(-needfood);
efficiency += 0.5;
2023-05-23 08:23:47 +00:00
if (Core.Save.StrengthFood >= 75)
2023-07-16 23:58:09 +00:00
addhealth += Function.Rnd.Next(1, 3);
2023-05-23 08:23:47 +00:00
}
2023-07-16 23:58:09 +00:00
if (Core.Save.StrengthDrink <= 25)
{//低状态低效率
Core.Save.StrengthChangeDrink(-needdrink / 2);
efficiency += 0.25;
if (Core.Save.Strength >= needdrink)
2023-05-23 08:23:47 +00:00
{
2023-07-16 23:58:09 +00:00
Core.Save.StrengthChange(-needdrink);
efficiency += 0.1;
2023-05-23 08:23:47 +00:00
}
2023-07-16 23:58:09 +00:00
addhealth -= 2;
2023-05-23 08:23:47 +00:00
}
else
{
2023-07-16 23:58:09 +00:00
Core.Save.StrengthChangeDrink(-needdrink);
efficiency += 0.5;
if (Core.Save.StrengthDrink >= 75)
addhealth += Function.Rnd.Next(1, 3);
2023-05-23 08:23:47 +00:00
}
2023-08-11 17:16:53 +00:00
if (addhealth > 0)
Core.Save.Health += addhealth * TimePass;
2023-11-20 15:46:19 +00:00
var addmoney = Math.Max(0, TimePass * (nowwork.MoneyBase * (efficiency) + Math.Pow(Core.Save.Level, 0.75) * nowwork.MoneyLevel * (efficiency - 0.5) * 2));
2023-07-16 23:58:09 +00:00
if (nowwork.Type == GraphHelper.Work.WorkType.Work)
Core.Save.Money += addmoney;
2023-05-23 08:23:47 +00:00
else
Core.Save.Exp += addmoney;
2023-07-16 23:58:09 +00:00
WorkTimer.GetCount += addmoney;
2023-08-18 10:02:25 +00:00
if (nowwork.Type == GraphHelper.Work.WorkType.Play)
{
LastInteractionTime = DateTime.Now;
Core.Save.FeelingChange(nowwork.Feeling * TimePass);
}
else
Core.Save.FeelingChange(-freedrop * nowwork.Feeling);
2023-07-16 23:58:09 +00:00
break;
2023-05-19 08:17:51 +00:00
default://默认
//饮食等乱七八糟的消耗
2023-07-16 23:58:09 +00:00
addhealth = -2;
2023-05-19 08:17:51 +00:00
if (Core.Save.StrengthFood >= 50)
{
2023-07-16 23:58:09 +00:00
Core.Save.StrengthChangeFood(-TimePass);
Core.Save.StrengthChange(TimePass);
2023-05-19 08:17:51 +00:00
if (Core.Save.StrengthFood >= 75)
2023-07-16 23:58:09 +00:00
addhealth += Function.Rnd.Next(1, 3);
2023-05-19 08:17:51 +00:00
}
else if (Core.Save.StrengthFood <= 25)
{
2023-07-24 01:53:18 +00:00
Core.Save.Health -= Function.Rnd.NextDouble() * TimePass;
2023-07-16 23:58:09 +00:00
addhealth -= 2;
2023-05-19 08:17:51 +00:00
}
2023-07-16 23:58:09 +00:00
if (Core.Save.StrengthDrink >= 50)
{
Core.Save.StrengthChangeDrink(-TimePass);
Core.Save.StrengthChange(TimePass);
if (Core.Save.StrengthDrink >= 75)
addhealth += Function.Rnd.Next(1, 3);
}
else if (Core.Save.StrengthDrink <= 25)
{
2023-07-24 01:53:18 +00:00
Core.Save.Health -= Function.Rnd.NextDouble() * TimePass;
2023-07-16 23:58:09 +00:00
addhealth -= 2;
}
if (addhealth > 0)
Core.Save.Health += addhealth * TimePass;
Core.Save.StrengthChangeFood(-TimePass);
Core.Save.StrengthChangeDrink(-TimePass);
2023-06-14 11:14:04 +00:00
Core.Save.FeelingChange(-freedrop);
2023-06-10 10:05:01 +00:00
break;
2023-05-10 03:13:34 +00:00
}
2023-05-19 08:17:51 +00:00
2023-05-10 03:13:34 +00:00
//if (Core.GameSave.Strength <= 40)
//{
// Core.GameSave.Health -= Function.Rnd.Next(0, 1);
//}
2023-05-23 08:23:47 +00:00
Core.Save.Exp += TimePass;
2023-05-10 03:13:34 +00:00
//感受提升好感度
if (Core.Save.Feeling >= 75)
{
if (Core.Save.Feeling >= 90)
{
Core.Save.Likability += TimePass;
}
2023-05-23 08:23:47 +00:00
Core.Save.Exp += TimePass * 2;
2023-05-10 03:13:34 +00:00
Core.Save.Health += TimePass;
}
else if (Core.Save.Feeling <= 25)
{
Core.Save.Likability -= TimePass;
Core.Save.Exp -= TimePass;
2023-05-10 03:13:34 +00:00
}
if (Core.Save.StrengthDrink <= 25)
{
Core.Save.Health -= Function.Rnd.Next(0, 1) * TimePass;
Core.Save.Exp -= TimePass;
2023-05-10 03:13:34 +00:00
}
else if (Core.Save.StrengthDrink >= 75)
Core.Save.Health += Function.Rnd.Next(0, 1) * TimePass;
2023-06-18 22:20:06 +00:00
FunctionSpendHandle?.Invoke();
2023-05-10 03:13:34 +00:00
var newmod = Core.Save.CalMode();
if (Core.Save.Mode != newmod)
{
2023-08-10 16:07:28 +00:00
//切换显示动画
playSwitchAnimat(Core.Save.Mode, newmod);
2023-05-10 03:13:34 +00:00
Core.Save.Mode = newmod;
}
//看情况播放停止工作动画
if (Core.Save.Mode == GameSave.ModeType.Ill && State == WorkingState.Work)
{
WorkTimer.Stop();
2023-05-10 03:13:34 +00:00
}
}
2023-08-10 16:07:28 +00:00
private void playSwitchAnimat(GameSave.ModeType before, GameSave.ModeType after)
{
if (!(DisplayType.Type == GraphType.Default || DisplayType.Type == GraphType.Switch_Down || DisplayType.Type == GraphType.Switch_Up))
2023-08-10 16:07:28 +00:00
{
return;
}
else if (before == after)
{
DisplayToNomal();
return;
}
else if (before < after)
{
Display(Core.Graph.FindGraph(Core.Graph.FindName(GraphType.Switch_Down), AnimatType.Single, before),
() => playSwitchAnimat((GameSave.ModeType)(((int)before) + 1), after));
}
else
{
Display(Core.Graph.FindGraph(Core.Graph.FindName(GraphType.Switch_Up), AnimatType.Single, before),
() => playSwitchAnimat((GameSave.ModeType)(((int)before) - 1), after));
}
}
2023-07-16 23:58:09 +00:00
/// <summary>
/// 状态计算Handle
/// </summary>
2023-06-18 22:20:06 +00:00
public event Action FunctionSpendHandle;
2023-07-16 23:58:09 +00:00
/// <summary>
/// 想要随机显示的接口 (return:是否成功)
/// </summary>
public List<Func<bool>> RandomInteractionAction = new List<Func<bool>>();
2023-08-10 11:34:11 +00:00
public bool IsIdel => (DisplayType.Type == GraphType.Default || DisplayType.Type == GraphType.Work) && !isPress;
2023-07-16 23:58:09 +00:00
/// <summary>
/// 每隔指定时间自动触发计算 可以关闭EventTimer后手动计算
/// </summary>
2023-07-18 18:09:51 +00:00
public void EventTimer_Elapsed()
2023-05-19 08:17:51 +00:00
{
2023-01-08 16:57:10 +00:00
//所有Handle
TimeHandle?.Invoke(this);
2023-01-21 14:16:13 +00:00
if (Core.Controller.EnableFunction)
2023-01-20 12:42:00 +00:00
{
2023-06-08 11:44:41 +00:00
FunctionSpend(0.05);
2023-01-20 12:42:00 +00:00
}
2023-01-21 14:16:13 +00:00
else
2023-01-20 12:42:00 +00:00
{
2023-06-05 09:18:27 +00:00
//Core.Save.Mode = GameSave.ModeType.Happy;
2023-05-09 23:16:58 +00:00
//Core.GameSave.Mode = GameSave.ModeType.Ill;
2023-06-05 09:18:27 +00:00
Core.Save.Mode = NoFunctionMOD;
2023-01-20 12:42:00 +00:00
}
2023-01-19 15:26:58 +00:00
2023-01-08 16:57:10 +00:00
//UIHandle
2023-08-10 11:34:11 +00:00
Dispatcher.Invoke(() => TimeUIHandle?.Invoke(this));
2023-01-08 16:57:10 +00:00
2023-08-10 11:34:11 +00:00
if (IsIdel)
2023-07-16 23:58:09 +00:00
switch (Function.Rnd.Next(Math.Max(20, Core.Controller.InteractionCycle - CountNomal)))
{
case 0:
case 1:
case 2:
//显示移动
DisplayMove();
break;
2023-08-10 11:34:11 +00:00
case 3:
2023-07-16 23:58:09 +00:00
case 4:
case 5:
//显示待机
DisplayIdel();
break;
2023-07-18 18:09:51 +00:00
case 6:
2023-07-16 23:58:09 +00:00
DisplayIdel_StateONE();
break;
2023-07-18 18:09:51 +00:00
case 7:
DisplaySleep();
break;
case 8:
2023-08-10 11:34:11 +00:00
case 9:
case 10:
2023-07-16 23:58:09 +00:00
//给其他显示留个机会
var list = RandomInteractionAction.ToList();
for (int i = Function.Rnd.Next(list.Count); 0 != list.Count; i = Function.Rnd.Next(list.Count))
{
var act = list[i];
if (act.Invoke())
{
break;
}
else
{
list.RemoveAt(i);
}
}
2023-08-10 11:34:11 +00:00
break;
2022-12-14 18:17:13 +00:00
}
2023-01-19 15:26:58 +00:00
2022-12-13 07:10:18 +00:00
}
2022-12-14 18:17:13 +00:00
/// <summary>
/// 定点移动位置向量
/// </summary>
2023-07-16 23:58:09 +00:00
public Point MoveTimerPoint = new Point(0, 0);
2022-12-14 18:17:13 +00:00
/// <summary>
/// 定点移动定时器
/// </summary>
2023-07-23 22:57:06 +00:00
public Timer MoveTimer = new Timer();
2023-01-24 06:56:16 +00:00
/// <summary>
/// 设置计算间隔
/// </summary>
/// <param name="Interval">计算间隔</param>
public void SetLogicInterval(int Interval)
{
EventTimer.Interval = Interval;
}
private Timer SmartMoveTimer = new Timer(20 * 60)
{
AutoReset = true,
};
/// <summary>
/// 是否启用智能移动
/// </summary>
private bool SmartMove;
/// <summary>
/// 设置移动模式
/// </summary>
/// <param name="AllowMove">允许移动</param>
2023-04-04 16:21:46 +00:00
/// <param name="smartMove">启用智能移动</param>
2023-01-24 06:56:16 +00:00
/// <param name="SmartMoveInterval">智能移动周期</param>
public void SetMoveMode(bool AllowMove, bool smartMove, int SmartMoveInterval)
{
2023-04-03 18:11:12 +00:00
MoveTimer.Enabled = false;
2023-01-24 06:56:16 +00:00
if (AllowMove)
{
2023-07-23 22:57:06 +00:00
MoveTimerSmartMove = true;
2023-01-24 06:56:16 +00:00
if (smartMove)
{
SmartMoveTimer.Interval = SmartMoveInterval;
SmartMoveTimer.Start();
SmartMove = true;
}
else
{
2023-04-04 09:32:53 +00:00
SmartMoveTimer.Enabled = false;
2023-01-24 06:56:16 +00:00
SmartMove = false;
}
}
else
{
2023-07-23 22:57:06 +00:00
MoveTimerSmartMove = false;
2023-01-24 06:56:16 +00:00
}
}
2023-05-19 08:17:51 +00:00
/// <summary>
/// 当前状态
/// </summary>
public WorkingState State = WorkingState.Nomal;
/// <summary>
2023-07-16 23:58:09 +00:00
/// 当前状态辅助ID
/// </summary>
public int StateID = 0;
/// <summary>
2023-08-10 16:07:28 +00:00
/// 当前工作
/// </summary>
public GraphHelper.Work nowWork => Core.Graph.GraphConfig.Works[StateID];
/// <summary>
2023-05-19 08:17:51 +00:00
/// 当前正在的状态
/// </summary>
public enum WorkingState
{
/// <summary>
/// 默认:啥都没干
/// </summary>
Nomal,
/// <summary>
2023-07-16 23:58:09 +00:00
/// 正在干活/学习中
2023-05-19 08:17:51 +00:00
/// </summary>
2023-07-16 23:58:09 +00:00
Work,
2023-05-19 08:17:51 +00:00
/// <summary>
2023-07-16 23:58:09 +00:00
/// 睡觉
2023-05-19 08:17:51 +00:00
/// </summary>
2023-07-16 23:58:09 +00:00
Sleep,
2023-05-19 08:17:51 +00:00
/// <summary>
2023-07-16 23:58:09 +00:00
/// 旅游中
2023-05-19 08:17:51 +00:00
/// </summary>
2023-07-16 23:58:09 +00:00
Travel,
2023-05-19 08:17:51 +00:00
/// <summary>
2023-07-16 23:58:09 +00:00
/// 其他状态,给开发者留个空位计算
2023-05-19 08:17:51 +00:00
/// </summary>
2023-07-16 23:58:09 +00:00
Empty,
2023-05-19 08:17:51 +00:00
}
2022-12-13 07:10:18 +00:00
}
}