Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //initialise the thing...
- public void Initialise()
- {
- Nodes = new List<RGNode>();
- Columns = new List<RGNodeColumn>();
- //go through num of acts, and generate each act
- for (int i = 0; i < ArmyData.Acts.Count; i++)
- {
- GenerateAct(i);
- }
- //add a start node and insert it at the beginning
- var startNode = new RGNode() { NodeType = NodeType.Start };
- var firstColumn = new RGNodeColumn();
- firstColumn.Nodes = new List<RGNode>();
- firstColumn.Nodes.Add(startNode);
- Nodes.Insert(0, startNode);
- Columns.Insert(0, firstColumn);
- var lastAct = GetLastAct(); // gets the last possible act
- var lastMove = GetLastMove(); // gets the last possible move number
- //add a last node and insert it at the end
- var lastColumn = new RGNodeColumn();
- lastColumn.Nodes = new List<RGNode>();
- var armies = ArmyData.levels.Where(n => n.CurrentAct == lastAct && n.CurrentMove == lastMove).ToList();
- var bossFight = armies.GetRandomElement();
- var lastNode = new RGNode()
- {
- NodeType = NodeType.End, ArmyData = bossFight,
- Reward = new BattleReward() { RewardClass = RewardClass.HighTierAugment }
- };
- lastColumn.Nodes.Add(lastNode);
- lastColumn.Act = lastAct;
- Columns.Add(lastColumn);
- Nodes.Add(lastNode);
- }
- private void GenerateAct(int actNum)
- {
- //just a wrapper to get the act out of ArmyData object because it has a very bad structure...
- Act act = GetActByCurrentAct(actNum);
- if (act == null)
- {
- // Handle act being null (e.g., log an error, return, or handle the exception in another way)
- return;
- }
- var maxShops = GetRandomRangeAsInt16(act.Shops);
- var maxEvents = GetRandomRangeAsInt16(act.Events);
- var moves = GetRandomRangeAsInt16(act.Moves);
- var cols = GenerateInitialColumns(actNum, moves);
- RandomlyAssignNodes(cols, NodeType.Event, maxEvents);
- RandomlyAssignNodes(cols, NodeType.Shop, maxShops);
- //all this method does is finds all remaining unassigned nodes, and assigns them combat node
- AssignUnassignedNodesToCombat(actNum);
- }
- //wrapper because lazy
- private short GetRandomRangeAsInt16(Vector2 range)
- {
- return Convert.ToInt16(UnityEngine.Random.Range(range.x, range.y));
- }
- private List<RGNodeColumn> GenerateInitialColumns(int actNum, int moves)
- {
- var columns = new List<RGNodeColumn>();
- for (int i = 0; i < moves; i++)
- {
- var act = actNum + 1;
- var move = i + 1;
- var column = new RGNodeColumn
- {
- Nodes = GenerateRandomNodes(actNum, i, moves),
- Act = act,
- Move = move
- };
- Nodes.AddRange(column.Nodes);
- if (i > 0 && i < moves - 1)
- {
- columns.Add(column);
- }
- Columns.Add(column);
- }
- return columns;
- }
- private List<RGNode> GenerateRandomNodes(int actNum, int colNum, int lastMoves)
- {
- var nodes = new List<RGNode>();
- var nodeCount = GetRandomRangeAsInt16(new Vector2(2, 3));
- for (int i = 0; i < nodeCount; i++)
- {
- nodes.Add(new RGNode() { NodeType = NodeType.Unassigned});
- }
- return nodes;
- }
- //this method goes through each column, shuffles the nodes, and assigns them a type
- private void RandomlyAssignNodes(List<RGNodeColumn> cols, NodeType nodeType, int maxCount)
- {
- int currentCount = 0;
- cols.Shuffle();
- foreach (var column in cols)
- {
- if (currentCount >= maxCount)
- {
- break;
- }
- //create a new list because we want to shuffle it
- var nodes = column.Nodes.ToList();
- //shuffle is an extension function - does exactly what it says, shuffles the list
- nodes.Shuffle();
- var node = nodes.FirstOrDefault(n => n.NodeType == NodeType.Unassigned);
- if (node != null)
- {
- currentCount++;
- node.NodeType = nodeType;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment