protected override void FindPath() { List <TilePosition> aStarPath; List <TilePosition> linePath = AStarPathFinder.CalculatePathTile(this.FindPathStrategy, this.CharacterAI.BattleMapData.ActorObstacleArray, this.m_PreviousPosition, this.m_TargetPosition, out aStarPath); //this.CharacterAI.DrawPath(aStarPath); /* * Debug.Log("AStar Path Length is:" + aStarPath.Count); * foreach(TilePosition at in aStarPath) * { * Debug.Log("row:" + at.Row + " ,column:" + at.Column); * } * Debug.Log("Line Path Length is:" + linePath.Count); * foreach (TilePosition lt in linePath) * { * Debug.Log("row:" + lt.Row + " ,column:" + lt.Column); * } */ this.SetPath(linePath); if (linePath.Count == 0) { this.OnTargetReached(); } else { TilePosition targetTilePosition = linePath[linePath.Count - 1]; if (this.m_MapData.GetBulidingObjectFromActorObstacleMap(targetTilePosition.Row, targetTilePosition.Column) != this.m_Target) { Vector2 reachablePosition = (Vector2)PositionConvertor.GetWorldPositionFromActorTileIndex(linePath[linePath.Count - 2]); TilePosition targetTile = aStarPath[aStarPath.Count - 1]; for (int i = 1; i < aStarPath.Count; i++) { if (this.m_MapData.GetBulidingObjectFromActorObstacleMap(aStarPath[i].Row, aStarPath[i].Column) == this.m_Target) { targetTile = aStarPath[i]; break; } } Vector2 targetPosition = (Vector2)PositionConvertor.GetWorldPositionFromActorTileIndex(targetTile); if (Vector2.SqrMagnitude(targetPosition - reachablePosition) <= this.CharacterAI.AttackBehavior.AttackScopeSqr) { this.m_TargetPosition = targetTile; this.ShowTarget(); return; } } this.m_TargetPosition = targetTilePosition; this.m_Target = this.CharacterAI.BattleMapData.GetBulidingObjectFromActorObstacleMap (this.m_TargetPosition.Row, this.m_TargetPosition.Column); this.ShowTarget(); } }
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; } BombermanWalkState walkState = new BombermanWalkState(this.CharacterAI.BattleMapData, endPoint, this.m_AIBehavior, targetBuilding); walkState.SetPath(linePath); this.m_AIBehavior.ChangeState(walkState); break; } } } } }
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); } Debug.Log("Total count:" + this.m_Targets.Count); } } 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++]; if (target != null) { BuildingBasePropertyBehavior property = target.GetComponent <BuildingBasePropertyBehavior>(); if (property != null) { TilePosition destination = property.ActorPosition + property.ActorObstacleList[0]; 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 <BuildingPropertyBehavior>(); if (property != null && ((BuildingPropertyBehavior)property).BuildingType == BuildingType.Wall) { 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; } BombermanWalkState walkState = new BombermanWalkState(this.CharacterAI.BattleMapData, endPoint, this.m_AIBehavior, targetBuilding); walkState.SetPath(linePath); this.m_AIBehavior.ChangeState(walkState); } } } } } } }