示例#1
0
    /// <summary>
    /// Перемещать фигуру AI
    /// </summary>
    private IEnumerator MovePieceAI(Piece piece, Cell origin, List <Cell> path)
    {
        activePiece = piece;

        SmoothMoveStrategy moveStrategy = new SmoothMoveStrategy(piece, board, path);

        foreach (Vector2 pos in moveStrategy.Move())
        {
            Vector2 realPos = ConvertToBoard(pos.x, pos.y);
            piece.SetPosition(realPos.x, realPos.y);

            if (moveStrategy.IsCompleted)
            {
                board.CompleteStep(strategy);
                UpdateTurn();

                destination = null;
                activePiece = null;

                if (!board.IsPlayerTurn)
                {
                    ai.MakeDecision();
                }

                yield break;
            }

            yield return(moveStrategy.Delay());
        }
    }
示例#2
0
文件: Brain.cs 项目: Grog84/AITest
 void TickBrain()
 {
     decisionMaker.MakeDecision();
 }
示例#3
0
文件: Enemy.cs 项目: dibarbet/HiveGen
    public override void Update()
    {
        Bullet target   = null;
        Enemy  attacked = null;

        //Debug.Log("CURRENT DECISION: " + curDecision);

        prevDecision = curDecision;

        //Determine if player tile changed
        prevPlayerTile = nTile;
        nTile          = Player.GetTileOn();

        //Only calculate distance change if new tile.
        if (nTile != prevPlayerTile)
        {
            //
        }
        //Get bullet if close to move to
        target = BulletVisibleDecision();
        PlayerHPDecision();
        attacked = FriendlyEnemyHasSight();
        EnemyHPDecision();
        //Only need to recalculate distance to player if the player changes tiles
        if (prevBulletDecision.value != bulletClose.value || nTile != prevPlayerTile || curDecision == "DONEMOVINGTOBULLET" || curDecision == "DONEMOVINGTOENEMY")
        {
            //Prevents too many decisions from happening
            if ((Time.time > nextActionTime))
            {
                nextActionTime = Time.time + period;

                //We have to perform the decision even distance result is the same, to make sure the enemy paths to the newest player position
                //If we did not, the enemy could just stay in range but never be hit by the enemy
                string dis = DistanceToPlayerDecision();
                if (dis == "FAR")
                {
                    if (curDecision != "MOVINGTOBULLET")// && curDecision != "MOVINGTOENEMY")
                    {
                        //Debug.Log("Making a decision");
                        curDecision = MakeDecision.MakeDecision(new List <AttributeValue <string> >()
                        {
                            playerDist, bulletClose, playerHPDecision, enemyAttackedDecision, thisHPDecision
                        });
                    }
                }
                else
                {
                    //Debug.Log("Making a decision");
                    curDecision = MakeDecision.MakeDecision(new List <AttributeValue <string> >()
                    {
                        playerDist, bulletClose, playerHPDecision, enemyAttackedDecision, thisHPDecision
                    });
                }
            }
        }



        //Execute decision

        if (curDecision == "CHASE")
        {
            GameManager.SpecialPathNode playerTile = Player.GetTileOn();
            if (DoDstarLite)
            {
                this.MoveToTileDstar(playerTile);
            }
            else
            {
                this.MoveToTile(playerTile);
            }

            //To make sure it keeps moving, rather than sitting on "CHASE"
            curDecision = "FINDINGPLAYER";
        }
        else if (curDecision == "STAY")
        {
            IsMoving = false;
        }
        else if (curDecision == "FINDBULLET")
        {
            if (target != null)
            {
                Debug.Log(target);
                if (DoDstarLite)
                {
                    this.MoveToTileDstar(target.GetTileOn());
                }
                else
                {
                    this.MoveToTile(target.GetTileOn());
                }
                curDecision = "MOVINGTOBULLET";
            }
        }
        else if (curDecision == "DEFENDGOAL")
        {
            print("ENEMY DEFENDING GOAL");
            GameManager.SpecialPathNode exitTile = GameManager.ExitTile;
            //Debug.Log("EXit: " + exitTile.X + ", " + exitTile.Y);
            if (DoDstarLite)
            {
                this.MoveToTileDstar(exitTile);
            }
            else
            {
                this.MoveToTile(exitTile);
            }
            curDecision = "DEFENDINGGOAL";
        }
        else if (curDecision == "DEFENDINGGOAL")
        {
            float dist = Vector3.Distance(this.transform.position, GameManager.ExitTile.tile.transform.position);
            if (dist < 2.0f)
            {
                IsMoving    = false;
                curDecision = "ATGOAL";
            }
        }
        else if (curDecision == "FINDENEMY")
        {
            if (attacked != null)
            {
                if (DoDstarLite)
                {
                    this.MoveToTileDstar(attacked.GetTileOn());
                }
                else
                {
                    this.MoveToTile(attacked.GetTileOn());
                }

                curDecision = "MOVINGTOENEMY";
            }
        }

        Position = transform.position;
        if (DoDstarLite)
        {
            if ((CurrentDstarNode != null) && IsAtGoal(CurrentDstarNode.tile.transform.position))
            {
                CurDstarIndex++;
                if (CurDstarIndex >= DstarPath.Count)
                {
                    Debug.Log("Final Node found");
                    //end of nodes
                    IsMoving = false;
                    if (curDecision == "MOVINGTOBULLET")
                    {
                        curDecision = "DONEMOVINGTOBULLET";
                    }
                    if (curDecision == "MOVINGTOENEMY")
                    {
                        curDecision = "DONEMOVINGTOENEMY";
                    }
                    CurrentDstarNode = null;
                }
                else
                {
                    GameManager.SpecialPathNode next = DstarPath[CurDstarIndex];
                    MoveToDstarNode(next);
                }
            }
            else
            {
                if (CurrentDstarNode != null)
                {
                    TileX = CurrentDstarNode.X;
                    TileY = CurrentDstarNode.Y;
                }
            }
        }
        else
        {
            if ((CurrentGoalNode != null) && IsAtGoal(CurrentGoalNode.Value.tile.transform.position))
            {
                LinkedListNode <GameManager.SpecialPathNode> next = CurrentGoalNode.Next;
                if (next == null)
                {
                    //Debug.Log("Final node found");
                    //end of list
                    IsMoving = false;
                    if (curDecision == "MOVINGTOBULLET")
                    {
                        curDecision = "DONEMOVINGTOBULLET";
                    }
                    if (curDecision == "MOVINGTOENEMY")
                    {
                        curDecision = "DONEMOVINGTOENEMY";
                    }
                }
                else
                {
                    MoveToNode(next);
                }
            }
            else
            {
                if (CurrentGoalNode != null)
                {
                    TileX = CurrentGoalNode.Value.X;
                    TileY = CurrentGoalNode.Value.Y;
                }
            }
        }

        if (IsMoving)
        {
            //Debug.Log("goal: " + GoalPos);
            transform.position = Vector3.MoveTowards(transform.position, GoalPos, Speed * Time.deltaTime);
        }

        if (DoDstarLite)
        {
            /*
             * string s_e = "";
             * foreach (GameManager.SpecialPathNode n in DstarPath)
             * {
             *  s_e += "(" + n.X + ", " + n.Y + ") ";
             *  n.tile.GetComponent<SpriteRenderer>().color = Color.blue;
             * }*/
        }
    }