예제 #1
0
    private void SetNoWallBuildingToTarget()
    {
        foreach (GameObject item in this.m_TargetsObjects)
        {
            if (item != null)
            {
                BuildingBasePropertyBehavior property = item.GetComponent <BuildingBasePropertyBehavior>();
                TilePosition destination = property.GetBuildingFirstActorPosition();
                //IgnoreTargetAndAttackScopeWeightStrategy findPathStrategy = new IgnoreTargetAndAttackScopeWeightStrategy(
                //	this.CharacterAI.BattleMapData, destination.Row, destination.Column, this.CharacterAI.AttackBehavior.AttackScope);
                KodoPathFindStrage findPathStrategy = new KodoPathFindStrage(
                    this.CharacterAI.BattleMapData, destination.Row, destination.Column);

                List <TilePosition> aStarPath;
                List <TilePosition> linePath = AStarPathFinder.CalculatePathTile(findPathStrategy, this.CharacterAI.BattleMapData.ActorObstacleArray,
                                                                                 this.m_CurrentPosition, destination, out aStarPath);

                TilePosition endPoint       = linePath[linePath.Count - 1];
                GameObject   targetBuilding = this.CharacterAI.BattleMapData.GetBulidingObjectFromActorObstacleMap(endPoint.Row, endPoint.Column);
                if (targetBuilding != null)
                {
                    property = targetBuilding.GetComponent <BuildingBasePropertyBehavior>();
                    if (property != null)
                    {
                        if (BattleEffectConfig.Instance.TargetEffectPrefab != null && this.CharacterAI.IsShowTarget)
                        {
                            GameObject targetEffect = GameObject.Instantiate(BattleEffectConfig.Instance.TargetEffectPrefab) as GameObject;
                            Vector3    offset       = targetEffect.transform.position;

                            Vector3 targetEffectPosition = property.AnchorTransform.position;
                            targetEffect.transform.position = targetEffectPosition + offset;
                            targetEffect.transform.parent   = BattleObjectCache.Instance.EffectObjectParent.transform;
                        }

                        KodoWalkState walkState = new KodoWalkState(this.CharacterAI.BattleMapData, endPoint, this.m_AIBehavior, targetBuilding);
                        walkState.SetPath(linePath);
                        this.m_AIBehavior.ChangeState(walkState);
                        break;
                    }
                }
            }
        }
    }
예제 #2
0
    public override void AICalculate()
    {
        if (this.m_Targets == null)
        {
            if (this.m_IsFindInstantly || this.m_AIBehavior.CanTakeResponse)
            {
                this.m_TargetsObjects = new List <GameObject>();
                this.m_Targets        = this.CharacterAI.BattleSceneHelper.GetBuildingsTransformOfCategory(this.CharacterAI.FavoriteCategory);
                if (this.m_Targets.Count == 0)
                {
                    this.m_Targets = this.CharacterAI.BattleSceneHelper.GetBuildingsTransformOfCategory(BuildingCategory.Any);
                }

                this.m_SearchIndex = 0;

                this.m_Targets.Sort((x, y) => {
                    float distanceX = Vector2.SqrMagnitude((Vector2)(x.position - this.m_AIBehavior.transform.position));
                    float distanceY = Vector2.SqrMagnitude((Vector2)(y.position - this.m_AIBehavior.transform.position));
                    return(distanceX.CompareTo(distanceY));
                }
                                    );

                foreach (Transform item in this.m_Targets)
                {
                    this.m_TargetsObjects.Add(item.parent.gameObject);
                }
            }
        }
        else if (this.m_SearchIndex == this.m_Targets.Count)
        {
            this.SetNoWallBuildingToTarget();
        }
        else
        {
            GameObject go = this.m_TargetsObjects[this.m_SearchIndex];
            while (this.m_SearchIndex < this.m_Targets.Count && go == null)
            {
                this.m_SearchIndex++;
                if (this.m_SearchIndex < this.m_Targets.Count)
                {
                    go = this.m_TargetsObjects[this.m_SearchIndex];
                }
            }
            if (this.m_SearchIndex == this.m_Targets.Count)
            {
                this.SetNoWallBuildingToTarget();
            }
            else
            {
                GameObject target = this.m_TargetsObjects[this.m_SearchIndex++];
                //IgnoreTargetAndAttackScopeWeightStrategy findPathStrategy = new IgnoreTargetAndAttackScopeWeightStrategy(
                //	this.CharacterAI.BattleMapData, destination.Row, destination.Column, this.CharacterAI.AttackBehavior.AttackScope);

                if (target != null)
                {
                    BuildingBasePropertyBehavior property = target.GetComponent <BuildingBasePropertyBehavior>();
                    if (property != null)
                    //if(property != null && property.BuildingType == BuildingType.Wall)
                    {
                        TilePosition destination = property.ActorPosition + property.ActorObstacleList[0];

                        KodoPathFindStrage findPathStrategy = new KodoPathFindStrage(
                            this.CharacterAI.BattleMapData, destination.Row, destination.Column);
                        List <TilePosition> aStarPath;
                        List <TilePosition> linePath = AStarPathFinder.CalculatePathTile(findPathStrategy, this.CharacterAI.BattleMapData.ActorObstacleArray,
                                                                                         this.m_CurrentPosition, destination, out aStarPath);

                        TilePosition endPoint       = linePath[linePath.Count - 1];
                        GameObject   targetBuilding = this.CharacterAI.BattleMapData.GetBulidingObjectFromActorObstacleMap(endPoint.Row, endPoint.Column);

                        if (targetBuilding != null)
                        {
                            property = targetBuilding.GetComponent <BuildingBasePropertyBehavior>();
                            if (property != null)
                            {
                                if (BattleEffectConfig.Instance.TargetEffectPrefab != null && this.CharacterAI.IsShowTarget)
                                {
                                    GameObject targetEffect = GameObject.Instantiate(BattleEffectConfig.Instance.TargetEffectPrefab) as GameObject;
                                    Vector3    offset       = targetEffect.transform.position;

                                    Vector3 targetEffectPosition = property.AnchorTransform.position;
                                    targetEffect.transform.position = targetEffectPosition + offset;
                                    targetEffect.transform.parent   = BattleObjectCache.Instance.EffectObjectParent.transform;
                                }

                                KodoWalkState walkState = new KodoWalkState(this.CharacterAI.BattleMapData, endPoint, this.m_AIBehavior, targetBuilding);
                                walkState.SetPath(linePath);
                                this.m_AIBehavior.ChangeState(walkState);
                            }
                        }
                    }
                }
            }
        }
    }