Guest User

Untitled

a guest
May 19th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.99 KB | None | 0 0
  1.     bool waiting = false;
  2.     bool finishedwaiting = false;
  3.     IEnumerator Wait(float dif)
  4.     {
  5.         waiting = true;
  6.         yield return new WaitForSeconds(dif);
  7.         waiting = false;
  8.         finishedwaiting = true;
  9.     }
  10.     //get's called every frame
  11.     public void ReadAIScript()
  12.     {
  13.         //starts at the beginning of the tree
  14.         BoxingNode currentNode = AITree.Nodes[AITree.FirstNodeIndex];
  15.         do
  16.         {
  17.             //don't do the rest of the tree if we're waiting
  18.             if (waiting == false)
  19.             {
  20.                 switch (currentNode.type)
  21.                 {
  22.                     //if the node is an equation
  23.                     case BoxingNode.NodeType.Equation:
  24.                         //what kind of value we're checking
  25.                         string ValueType = "";
  26.                         //both equation nodes
  27.                         for (int i = 0; i < 2; i++)
  28.                         {
  29.                             //the different situations. there will be more, i just don't need them yet
  30.                             switch (currentNode.EqNodes[i].Vchoice)
  31.                             {
  32.                                 case BoxingNode.EquationNodeButton.ValueChoices.enemyAttacking:
  33.                                     currentNode.EqNodes[i].InputBool = enemy.StartedPunching;
  34.                                     ValueType = "Bool";
  35.                                     break;
  36.                                 case BoxingNode.EquationNodeButton.ValueChoices.EnemyWeaving:
  37.                                     currentNode.EqNodes[i].InputBool = enemy.weavingL || enemy.weavingR;
  38.                                     ValueType = "Bool";
  39.                                     break;
  40.                                 case BoxingNode.EquationNodeButton.ValueChoices.SelfWeaving:
  41.                                     currentNode.EqNodes[i].InputBool = owner.weavingL || owner.weavingR;
  42.                                     ValueType = "Bool";
  43.                                     break;
  44.                                 case BoxingNode.EquationNodeButton.ValueChoices.enemyAttackDirection:
  45.                                     currentNode.EqNodes[i].InputAttackDirection = enemy.currentPunch.LimbIndex;
  46.                                     ValueType = "Direction";
  47.                                     break;
  48.                                 case BoxingNode.EquationNodeButton.ValueChoices.DistanceToEnemy:
  49.                                     currentNode.EqNodes[i].InputDouble = Vector3.Distance(owner.nav.transform.position, enemy.nav.transform.position);
  50.                                     ValueType = "Double";
  51.                                     break;
  52.                             }
  53.                         }
  54.                         //see the result of the equation node
  55.                         switch (ValueType)
  56.                         {
  57.                             case "Bool":
  58.                                 currentNode.EquationValue = currentNode.EqNodes[0].InputBool == currentNode.EqNodes[1].InputBool;
  59.                                 break;
  60.                             case "Direction":
  61.                                 currentNode.EquationValue = currentNode.EqNodes[0].InputAttackDirection == currentNode.EqNodes[1].InputAttackDirection;
  62.                                 break;
  63.                             case "Double":
  64.                                 switch (currentNode.typ)
  65.                                 {
  66.                                     case BoxingNode.EquationType.Equal:
  67.                                         currentNode.EquationValue = currentNode.EqNodes[0].InputDouble == currentNode.EqNodes[1].InputDouble;
  68.                                         break;
  69.                                     case BoxingNode.EquationType.Less:
  70.                                         currentNode.EquationValue = currentNode.EqNodes[0].InputDouble < currentNode.EqNodes[1].InputDouble;
  71.                                         break;
  72.                                     case BoxingNode.EquationType.Greater:
  73.                                         currentNode.EquationValue = currentNode.EqNodes[0].InputDouble > currentNode.EqNodes[1].InputDouble;
  74.                                         break;
  75.                                 }
  76.                                 break;
  77.                         }
  78.                         //set the correct next step depending on the result
  79.                         if (currentNode.EquationValue == true)
  80.                         {
  81.                             currentNode.NextStep = currentNode.connections[0];
  82.                         }
  83.                         else
  84.                         {
  85.                             currentNode.NextStep = currentNode.connections[1];
  86.                         }
  87.                         break;
  88.                         //follow the enemy
  89.                     case BoxingNode.NodeType.FollowEnemy:
  90.                         owner.nav.destination = enemy.nav.transform.position;
  91.                         break;
  92.                         //perform a specific move
  93.                     case BoxingNode.NodeType.Move:
  94.                         if (owner.Energy >= currentNode.move.energy)
  95.                             owner.anim.SetTrigger(currentNode.move.name);
  96.                         break;
  97.                         //do nothing
  98.                     case BoxingNode.NodeType.StandStill:
  99.                         owner.nav.destination = owner.nav.transform.position;
  100.                         break;
  101.                         //do a random punch
  102.                     case BoxingNode.NodeType.RandomMove:
  103.                         RandomPunch(currentNode.RandomPunches);
  104.                         break;
  105.                         //wait randomly between x and y seconds
  106.                     case BoxingNode.NodeType.WaitBetween:
  107.                         if (finishedwaiting == false)//this means that we haven't started waiting at all
  108.                         {//start waiting
  109.                             StartCoroutine(Wait(Random.Range((float)currentNode.EqNodes[0].InputDouble*10,(float)currentNode.EqNodes[1].InputDouble*10)/10));
  110.                         }
  111.                         else
  112.                         {//otherwise we'ev finished waiting
  113.                             finishedwaiting = false;
  114.                         }
  115.                         //if finishedwaiting is true, then the rest of the do while loop will continue, rather than entering an infinite loop of Wait coroutines
  116.                         break;
  117.                 }
  118.             }
  119.             //if this isn't an equation node or anything special, just tell it to keep going with it's first connected node
  120.             if (currentNode.NextStep == null)
  121.             {
  122.                 if (currentNode.connections.Count > 0)
  123.                 {
  124.                     currentNode.NextStep = currentNode.connections[0];
  125.                 }
  126.             }
  127.             //proceed
  128.             currentNode = currentNode.NextStep;
  129.         }
  130.         while (currentNode != null);
  131.         Rotate();
  132.     }
Add Comment
Please, Sign In to add comment