| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using IwbZero.Expr;
- using IwbZero.ToolCommon.StringModel;
- using WeEngine.Enum;
- using WeEngine.Packages;
- namespace WeEngine.Functions
- {
- /// <summary>
- /// 随机选择情景流块
- /// </summary>
- public class RandomSelectFlowBlock : RunTimeBase,IFunction
- {
- public string Invoke(ExprObject exprObj)
- {
- var child = exprObj.GetChild(0);
- GetPackage(child.Expr);
- Select();
- return "TRUE";
- }
- private void Select()
- {
- var list = Rp.CurRoundInfo.Children.Select(a => (SceneFlowBlock)a).Where(a => a.BlockType == SceneFlowBlockType.Evolutionary).ToList();
- SceneFlowBlock selectBlock = list.FirstOrDefault();
- if (list.Count>1)
- {
- var randomResult = ExprModule.Evaluate($"IFunRandomSelection({Rp.CurRoundInfo.ControlRate})");
- if (randomResult.ValB())
- {
- selectBlock =
- list.FirstOrDefault(a => a.NodeState == NodeState.Running);
- }
- else
- {
- var noRunBlock = list.Where(a => a.NodeState == NodeState.New).ToList();
- if (noRunBlock.Any())
- {
- var random = new Random().Next(0, noRunBlock.Count - 1);
- selectBlock = noRunBlock[random];
- }
- }
- }
-
- if (selectBlock != null)
- {
- var sceneCount = Rp.CurRoundInfo.SceneCount;
- var flows = new List<NodeBase>();
- flows.AddRange(selectBlock.Children);
- sceneCount = sceneCount > flows.Count ? flows.Count : sceneCount;
- while (sceneCount > 0)
- {
- var id = SelectFlows(ref flows);
- var flow = selectBlock.Children.FirstOrDefault(a => a.Id == id);
- if (flow != null) flow.NodeState = NodeState.Running;
- sceneCount--;
- }
- }
- }
- private string SelectFlows(ref List<NodeBase> flows)
- {
- var random = new Random().Next(0, flows.Count - 1);
- var flow = flows[random];
- flows.Remove(flow);
- return flow.Id;
- }
- }
- }
|