bvn13

Untitled

Apr 10th, 2018
874
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using Commands;
  6. using Game;
  7.  
  8. public class BattleViewController : MonoBehaviour {
  9.     bool _surrender = false;
  10. #region Constants.
  11.     private const float SFX_VOLUME = 0.1f;
  12.  
  13.     // Constants for my tanks moving.
  14.     private const float MOVING_DURATION = 2;
  15.     private const float TANKS_START_MOVING_DURATION = 1;
  16.     private const float TANKS_END_MOVING_DURATION = 1;
  17.     private const float MOVING_SPEED_X = 550f;
  18.     private const float MOVING_SPEED_Y =  705f/1135f * MOVING_SPEED_X;//705/1135 - size of tile
  19.    
  20.     private const float DECORATIONS_PER_TURN = 0.66f;
  21.  
  22.     // Positions for battle turn label - it should be out of screen.
  23.     private readonly Vector3 BATTLE_TURN_LABEL_START_POSITION  = new Vector3(680, 0, -10);
  24.     private readonly Vector3 BATTLE_TURN_LABEL_FINISH_POSITION = new Vector3(-680, 0, -10);
  25.    
  26.     // Delay between shooting in fight.
  27.     private const float FIGHT_DELAY = 0.1f;
  28.    
  29.     // Sprite names for decorations.
  30.     private string[] DECORATIONS = {
  31.         "airplane1",
  32.         "airplane2",
  33.         "barrels",
  34.         "boxes",
  35.         "crater",
  36.         "factory1",
  37.         "factory2",
  38.         "garage",
  39.         "home1",
  40.         "home2",
  41.         "home3",
  42. //      "barn",
  43. //      "house",
  44. //      "house01",
  45. //      "house02",
  46. //      "pillbox",
  47. //      "pillbox01",
  48. //      "pillbox02",
  49. //      "pillbox03",
  50. //      "pillbox04",
  51. //      "pillbox05",
  52. //      "pillbox06",
  53. //      "wood01",
  54. //      "wood02",
  55. //      "wood03",
  56. //      "wood04",
  57. //      "tank_kaput",
  58. //      "hedgehogs",
  59.     };
  60.    
  61. #endregion
  62.    
  63.     private GameObject _enemyTracks;   
  64.     private GameObject _myTracks;
  65.    
  66.     public GameObject enemyTracks {
  67.         get {  
  68.             if (_enemyTracks == null) {
  69.                 _enemyTracks = transform.FindChild("BattleViewScene/EnemyTanks/Tracks").gameObject;
  70.             }
  71.             return _enemyTracks;
  72.         }
  73.     }
  74.    
  75.     public GameObject myTracks {
  76.         get {  
  77.             if (_myTracks == null) {
  78.                 _myTracks = transform.FindChild("BattleViewScene/MyObjects/Tracks").gameObject;
  79.             }
  80.             return _myTracks;
  81.         }
  82.     }
  83.    
  84.  
  85. #region Class fields.
  86.    
  87.     private bool waitingForMove = true;
  88.    
  89.     public GameObject decorationPrefab;
  90.    
  91.     private List<Tank> myTanks = new List<Tank>(5);
  92.     private List<Tank> enemyTanks = new List<Tank>(5);
  93.  
  94.     // Game objects to build tanks into.
  95.     private GameObject[] myTanksContainers;
  96.    
  97.     // Game objects to build tanks into.
  98.     private GameObject[] enemyTanksContainers = new GameObject[5];
  99.    
  100.     /// <summary>
  101.     /// Used for moving tanks tween.
  102.     /// </summary>
  103.     AnimationCurve movingTanksCurve = new AnimationCurve(
  104.         new Keyframe(0, 0),
  105.         new Keyframe(0.5f, 0.5f, 2, 2),
  106.         new Keyframe(1, 1)
  107.         );
  108.  
  109.     // Explosions.
  110.     private GameObject[] enemyExplosions;
  111.     private Vector3[] originalEnemyExplosionPositions;
  112.    
  113.     private GameObject[] myExplosions;
  114.     private Vector3[] originalMyExplosionPositions;
  115.  
  116.     private bool isInitialized;
  117.    
  118.     private Transform movingContainer;
  119.     private Transform enemyTanksParent;
  120.     private Transform myTanksParent;
  121.    
  122.     public GameObject lootPrefab;
  123.     public GameObject tankPrefab;
  124.     public GameObject repairBtnPrefab;
  125.     public GameObject revivalBtnPrefab;
  126.     public GameObject craterPrefab;
  127.    
  128.     // Dropped loot.
  129.     private ArrayList items = new ArrayList();
  130.    
  131.     private List<UISprite> decorations = new List<UISprite>();
  132.     private Transform decorationsContainer;
  133.    
  134.     private int[] playerFormation;
  135.    
  136.     public UILabel testLabel;
  137.    
  138.     private UILabel ironBoxLabel;
  139.     private UILabel boxLabel;
  140.     private UILabel coinsLabel;
  141.     private UILabel turnLabel;
  142.    
  143.     private UILabel battleTurnLabel;
  144.    
  145.     private GameObject moveButton;
  146.    
  147. #endregion
  148.    
  149.     void Init() {
  150.         testLabel.gameObject.active = false;
  151.         if (isInitialized)
  152.             return;
  153.         isInitialized = true;
  154.        
  155.         movingContainer = transform.FindChild("BattleViewScene/MovingLayer");
  156.         enemyTanksParent = transform.FindChild("BattleViewScene/EnemyTanks");
  157.         myTanksParent = transform.FindChild("BattleViewScene/MyObjects");
  158.         decorationsContainer = movingContainer.FindChild("Decorations");
  159.        
  160.         Transform t = transform;
  161.         ironBoxLabel = t.FindChild("BattleViewHUD/Top/IronBoxBar/Label").GetComponent<UILabel>();
  162.         coinsLabel = t.FindChild("BattleViewHUD/Top/MoneyBar/Label").GetComponent<UILabel>();
  163.         boxLabel = t.FindChild("BattleViewHUD/Top/BoxBar/Label").GetComponent<UILabel>();
  164.         turnLabel = t.FindChild("BattleViewHUD/Top/BattleTurnsBar/Label").GetComponent<UILabel>();
  165.         battleTurnLabel = t.FindChild("BattleViewHUD/BattleTurnLabel").GetComponent<UILabel>();
  166.         moveButton = t.FindChild("BattleViewHUD/MoveBtnGo/MoveBtn").gameObject;
  167.     }
  168.    
  169.     private void InitDecorations() {
  170.         float decorationsCount = (GameManager.Instance.Battle.TotalTurns + 3) * DECORATIONS_PER_TURN * 2;
  171.         float dx = MOVING_SPEED_X * MOVING_DURATION / DECORATIONS_PER_TURN;
  172.         float dy = MOVING_SPEED_Y * MOVING_DURATION / DECORATIONS_PER_TURN;
  173.         GameObject decoration;
  174.         UISprite sprite;
  175.         Transform decorationTransform;
  176.        
  177.         System.Random random = GameManager.RandomGenerator;
  178.        
  179.         while (decorations.Count < decorationsCount) {
  180.             decoration = (GameObject) GameObject.Instantiate(decorationPrefab);
  181.             decorationTransform = decoration.transform;        
  182.             decorationTransform.parent = decorationsContainer;
  183.            
  184.             sprite = decorationTransform.GetComponent<UISprite>();
  185.             decorations.Add(sprite);
  186.         }
  187.  
  188.         // Top line of decorations.
  189. //      Vector3 pos = new Vector3(0, 300);
  190.         Vector3 pos = new Vector3(-700, 83);
  191.         int i = 0;
  192.         float y;
  193.         int maxYRandom = 100;
  194.         for (; i < decorationsCount / 2; ++i) {
  195.             sprite = decorations[i];
  196.             sprite.spriteName = DECORATIONS[random.Next(0, DECORATIONS.Length)];
  197.             sprite.MakePixelPerfect();
  198.             y = pos.y;
  199. //          pos.y += random.Next(maxYRandom);
  200.             sprite.transform.localPosition = pos;
  201.            
  202.             pos.x += dx;
  203.             pos.y = y + dy;
  204.         }
  205.         // Bottom line of decorations.
  206. //      pos = new Vector3(500, -100);
  207.         pos = new Vector3(400, -317);
  208.         for (; i < decorationsCount; ++i) {
  209.             sprite = decorations[i];
  210.             sprite.spriteName = DECORATIONS[random.Next(0, DECORATIONS.Length)];
  211.             sprite.MakePixelPerfect();
  212.             y = pos.y;
  213. //          pos.y -= random.Next(maxYRandom);
  214.             sprite.transform.localPosition = pos;
  215.            
  216.             pos.x += dx;
  217.             pos.y = y + dy;
  218.         }
  219.     }
  220.    
  221.     void PlayInfantrySFX(UISpriteAnimationExtended animation, int frameIndex) {
  222.         SoundManager.PlaySFXFromGroup("Machinegun", null, SFX_VOLUME);
  223.     }
  224.    
  225.     void PlayMortarSFX(UISpriteAnimationExtended animation, int frameIndex) {
  226.         SoundManager.PlaySFXFromGroup("Mortar", null, SFX_VOLUME);
  227.     }
  228.    
  229.     void PlayPtrSFX(UISpriteAnimationExtended animation, int frameIndex) {
  230.         SoundManager.PlaySFXFromGroup("PTR", null, SFX_VOLUME + 0.2f);
  231.     }
  232.    
  233.     void OnEnable() {
  234.         Debug.Log("BattleViewController.OnEnable");
  235.         Init();
  236.         movingContainer.localPosition = Vector3.zero;
  237.         enemyTanksParent.localPosition = Vector3.zero;
  238.         battleTurnLabel.transform.localPosition = BATTLE_TURN_LABEL_START_POSITION;
  239.         DestroyCraters();
  240.     }
  241.     void OnDisable() {
  242.         Debug.Log("BattleViewController.OnDisable");
  243.         foreach (GameObject item in items)
  244.         {
  245.             NGUITools.Destroy(item);
  246.         }
  247.         items.Clear();
  248.     }
  249.    
  250. #region Game events.
  251.  
  252.     public void StartNewBattle() {
  253.         Debug.Log("StartNewBattle" + Time.time);
  254.         UILabel lvl = transform.FindChild("BattleViewHUD/Top/lvlExpBar/lvlZoomer/lvl").GetComponent<UILabel>();
  255.         UIFilledSprite exp = transform.FindChild("BattleViewHUD/Top/lvlExpBar/exp/Foreground").GetComponent<UIFilledSprite>();
  256.         lvl.text = GameManager.Instance.Human.Lvl.ToString();
  257.         //Debug.Log("expfill" + GameManager.Instance.Human.ExpFill);
  258.         exp.fillAmount = GameManager.Instance.Human.ExpFill;
  259.         transform.FindChild("BattleViewHUD/BottomRight/AttackButton").gameObject.SetActiveRecursively(false);
  260.         SetButtonsActive(false);
  261.        
  262.         // Set tiles size and position.
  263. //      Transform tiles = movingContainer.FindChild("Tiles");
  264.        
  265. //      UIDiagonalSprite sp = tiles.GetComponent<UIDiagonalSprite>();
  266. //      Debug.Log("StartNewBattle.sp.innerUV.width: " + sp.innerUV.width/tiles.localScale.x*sp.atlas.pixelSize);
  267.        
  268.         // Size
  269.         float width  = Screen.width + GameManager.Instance.Battle.TotalTurns * MOVING_SPEED_X * (MOVING_DURATION + TANKS_START_MOVING_DURATION);
  270.         float height = Screen.height + GameManager.Instance.Battle.TotalTurns * MOVING_SPEED_Y * (MOVING_DURATION + TANKS_START_MOVING_DURATION);
  271. //      tiles.localScale = new Vector3(width, height, 1);
  272. //      tiles.localPosition = new Vector3((width /*- Screen.width*/)/2, (height /*- Screen.height*/) / 2, 1);
  273.        
  274.         UpdateLabels();
  275.        
  276.         MainUIController.Instance.SetDarknessVisible(false);
  277.        
  278.         List<CardData> cards = GameManager.Instance.Human.GetBattleLine1();
  279.         Tank tank;
  280.        
  281.         if (cards.Count > myTanks.Count) {
  282.             while (cards.Count > myTanks.Count) {
  283.                 // create moar tanks;
  284.                 tank = NGUITools.AddChild(myTanksParent.gameObject, tankPrefab).GetComponent<Tank>();
  285.                 tank.GetComponent<UIButtonMessage>().target = gameObject;
  286.                 tank.HideAddText();
  287.                 tank.isEnemy = false;
  288.                 myTanks.Add(tank);
  289.                
  290.                 GameObject tankTracksConteiner =  NGUITools.AddChild(myTracks);
  291.                 tankTracksConteiner.name = "traks_" +myTanks.Count;            
  292.                 tank.tracksContainer = tankTracksConteiner;
  293.             }
  294.         }
  295.        
  296.         for (int i = 0; i < cards.Count; ++i) {
  297.             myTanks[i].SetTankActive(false);
  298.         }
  299.        
  300.         // Initialize decorations.
  301. //      InitDecorations();
  302.        
  303.        
  304.         playerFormation = FormationsManager.GetCurrentFormation();
  305.         for (int i = 0; i < cards.Count; ++i) {
  306.                 Debug.Log("myCard: " + cards[i].name);
  307.                 myTanks[i].InitWith(cards[i], 0, i);
  308.                 myTanks[i].transform.localPosition = new Vector3 (-5*100, -3*100,0) + GetTankPosition(playerFormation, i, false);
  309.                 myTanks[i].SetTankActive(true);
  310.                 myTanks[i].HideAddText();
  311.                 myTanks[i].image.gameObject.active = false;
  312.                 myTanks[i].lvl.gameObject.active = false;
  313.                 myTanks[i].nameLabel.gameObject.active  = false;
  314.                 myTanks[i].AimRoot.SetActiveRecursively(false);
  315.                 myTanks[i].Bufs.Clear();
  316.                
  317.                 myTanks[i].updateTracks();
  318.            
  319.             }
  320.  
  321.         StartCoroutine(StartMovingMyTanks(0.0f));
  322.        
  323.         VisualEffectManager.CreateEffect("CloudsFX", new Vector3(0, 0, -5), transform);
  324.         //StartCoroutine(StartMovingEnemyTanks(0.0f));
  325.     }
  326.    
  327.     public void ScaleGO(GameObject go, float duration, float startScaleKoef)
  328.     {
  329.         Debug.Log ("ScaleGO", go);
  330.         Vector3 targetScale = go.transform.localScale * startScaleKoef;
  331.         TweenScale.Begin(go, duration, targetScale);
  332.     }
  333.    
  334.     // Called when new battle turn starts.
  335.     public void StartBattleTurn()
  336.     {
  337.         _surrender = false;
  338.     //  enemyTanksParent.gameObject.SetActiveRecursively(false);
  339.         moveButton.SetActiveRecursively(false);
  340.         //moveButton.GetComponent<UISprite>().color = new Color(0.0f, 0.0f, 0.0f, 0.0f);
  341.         transform.FindChild("BattleViewHUD/BottomRight/AttackButton").GetComponent<UIButtonMessage>().enabled = false;
  342.         Debug.Log("StartBattleTurn");
  343.         GameManager.Instance.Human.ActionInLocation++;
  344.         waitingForMove = true;
  345.        
  346.         playerFormation = FormationsManager.GetCurrentFormation();
  347.         // Show my tanks.
  348.         if (GameManager.Instance.Battle.CurrentTurn == 1)
  349.         {
  350. //          List<CardData> cards = GameManager.Instance.Human.GetBattleLine1();
  351. //          for (int i = 0; i < cards.Count; ++i) {
  352. //              Debug.Log("myCard: " + cards[i].name);
  353. //              myTanks[i].InitWith(cards[i], 0, i);
  354. //              myTanks[i].transform.localPosition = GetTankPosition(playerFormation, i, false);
  355. //              myTanks[i].SetTankActive(true);
  356. //              myTanks[i].HideAddText();
  357. //              myTanks[i].image.gameObject.active = false;
  358. //              myTanks[i].lvl.gameObject.active = false;
  359. //              myTanks[i].nameLabel.gameObject.active  = false;
  360. //              myTanks[i].AimRoot.SetActiveRecursively(false);
  361. //              myTanks[i].Bufs.Clear();
  362. //          }
  363.         }
  364.         else
  365.             foreach (var tank in myTanks)
  366.             {
  367.                 tank.Bufs.Clear();
  368.                 tank.AimRoot.SetActiveRecursively(false);
  369.                 tank.HideAddText();
  370.                 tank.ShowButtons(true);
  371.                 if (tank.Card.hp <= 0)
  372.                 {
  373.                     var reserve = GameManager.Instance.Human.GetLineCards(1 - tank.Line)[tank.Index];
  374.                     if (reserve.hp > 0)
  375.                     {
  376.                         tank.InitWith(reserve, 1 - tank.Line, tank.Index);
  377.                     }
  378.                     else
  379.                         tank.SetTankActive(false);
  380.                 }
  381.             }
  382.        
  383. //      StartMovingMyTanks();
  384.         UpdateLabels();
  385.         StartCoroutine( StartMovingEnemyTanks(0));
  386.         ShowBattleTurnLabel();
  387.     }
  388.    
  389. #endregion
  390.    
  391. #region Button handlers.
  392.     void OnAttackClicked(GameObject sender) {
  393.         Debug.Log("OnAttackClicked", sender);
  394.         SoundManager.PlaySFXFromGroup("Attack");
  395.         transform.FindChild("BattleViewHUD/BottomRight/AttackButton").GetComponent<UIFramedButtonExt>().disableButton();
  396.         SetButtonsActive(false);
  397.         foreach (var tank in myTanks) {
  398.             tank.ShowButtons(false);
  399.         }
  400.         StartCoroutine(AnimateFight());
  401.         foreach (var t in enemyTanks)
  402.             t.DisableAim();
  403.     }
  404.    
  405.     void OnMoveClicked(GameObject sender) {
  406.         //moveButton.SetActiveRecursively(false);
  407.         //moveButton.GetComponent<UISprite>().color = new Color(0.0f, 1.0f, 0.0f, 0.0f);
  408.         Debug.Log("OnMoveClicked", sender);
  409.        
  410.         SoundManager.PlaySFXFromGroup("TanksMove");
  411.        
  412.         transform.FindChild("BattleViewHUD/BottomRight/AttackButton").GetComponent<UIFramedButtonExt>().disableButton();
  413.         ServerEmulation.EmulateStartBattleTurn();
  414.     }
  415.    
  416.     void OnDefendClicked(GameObject sender) {
  417.         SetButtonsActive(false);
  418.     }
  419.    
  420.     void OnCapitulirenClicked(GameObject sender) {
  421.         _surrender = true;
  422.         ShowDefeatScreen(null);
  423.     }
  424.    
  425.     void OnTankClicked(GameObject sender) {
  426.         Debug.Log("BattleViewController.OnTankClicked");
  427.     }
  428. #endregion
  429.    
  430.     public GUITexture Mockup
  431.     {
  432.         get
  433.         {
  434.             return UnityUtils.FindUIComponent<GUITexture>("Mockup");
  435.         }
  436.     }
  437.    
  438.     Rect mockupPixelInset;
  439.    
  440.     #region Animation coroutines.  
  441.    
  442.     void AnimateMyMove(Vector3 offset, UITweener.OnFinished onFinished) {
  443.         Vector3 pos;
  444.         // Tank with this index will start moving with random delay.
  445.         int  delayedTankIndex = GameManager.RandomGenerator.Next(0, 2);
  446.         TweenPosition tween;
  447.         for (int i = 0; i < myTanks.Count; ++i) {
  448.             pos = myTanks[i].transform.localPosition;
  449.             pos += offset;
  450.             tween = TweenPosition.Begin(myTanks[i].gameObject, 1.0f, pos);
  451.             tween.animationCurve = movingTanksCurve;
  452.             // There is little issue in NGUI tweens: tween start with previous delay value, even if we change it after Begin().
  453.             // This new delay will be used for next tween execution, so random will be used anyway.
  454.             if (delayedTankIndex == i) {
  455.                 tween.delay = GameManager.RandomGenerator.Next(1, 6) * 0.1f;
  456.                 tween.onFinished = onFinished;
  457.             } else {
  458.                 tween.delay = 0;
  459.             }
  460.         }
  461.         waitingForMove = false;
  462.     }
  463.    
  464.     /// <summary>
  465.     /// Raises the animation finished event.
  466.     /// </summary>
  467.     /// <param name='animation'>
  468.     /// Animation that finished.
  469.     /// </param>
  470.     void OnAnimationFinished(UISpriteAnimationExtended animation) {
  471.         // random delay: 2-4 sec
  472.         float delay;
  473.         if (waitingForMove) {
  474.             delay = 2 + 0.1f * GameManager.RandomGenerator.Next(20);
  475.         } else {
  476.             delay = 0.5f;
  477.         }
  478.         animation.Reset(delay);
  479.     }
  480.  
  481. #endregion 
  482.    
  483.     void ShowEnemyTanks(bool visible)
  484.     {
  485.         foreach(Tank tank in enemyTanks) {
  486.             if (visible) {
  487.                 tank.FadeIn();
  488.             }
  489.             tank.SetTankActive(visible);
  490.             tank.HideAddText();
  491.         }
  492.     }
  493.    
  494.     /*
  495.     IEnumerator AnimateEnemyFire(MoveResponse move) {
  496.         int count = playerFormation.Length;
  497.         CardData card;
  498.         int damage;
  499.         Tank tank;
  500.         for (int i = 0; i < enemyTanks.Count; ++i) {
  501. //      foreach(Tank tank in enemyTanks) {
  502.             tank = enemyTanks[i];
  503.             tank.Attack();
  504.             SoundManager.PlaySFXFromGroup("Tank");
  505.            
  506.             // Damage my tank.         
  507.             tank = myTanks[i];
  508.             card =  tank.Card;
  509.             damage = card.maxHp / (GameManager.Instance.Battle.TotalTurns + 1);
  510.             tank.Damage(damage);
  511.            
  512.             yield return new WaitForSeconds(0.2f + 0.01f * GameManager.RandomGenerator.Next(5));
  513.         }
  514.     }
  515.     */
  516.    
  517.     IEnumerator AnimateTankExplosions(List<Tank> tanks, int deltaHP)
  518.     {
  519.         for (int i = 0; i < deltaHP; ++i) {
  520.             VisualEffectManager.CreateEffect("Explosion01", Vector3.zero, tanks[i].transform);
  521.             yield return new WaitForSeconds(0.15f);
  522.         }
  523.         for (int i = 0; i < deltaHP; ++i) {
  524.             if (tanks[i].isEnemy) {
  525.                 tanks[i].tankParts = Tank.buildAtGameObject("burned_c_enemy_", tanks[i]);
  526.                 tanks[i].FadeOut();
  527.             } else {
  528.                 tanks[i].tankParts = Tank.buildAtGameObject("burned_c_", tanks[i]);
  529.             }
  530.         }
  531.     }
  532.    
  533.     void SetButtonsActive(bool val) {
  534. //      Debug.Log("SetButtonsActive " + val);
  535.         UnityUtils.FindUIComponent<UIButtonMessage>("BottomRight/AttackButton", transform).enabled = val;
  536.         for (int i = 0; i < myTanks.Count; ++i) {
  537.             myTanks[i].SetButtonActive(val);
  538.         }
  539.     }
  540.    
  541.     private void ShowVictoryScreen(BattleResult result) {
  542.         var controller = MainUIController.Instance.VictoryScreen.GetComponent<VictoryScreenController>();
  543.         controller.SetTrophies(result.trophies, result.coins);
  544.         DialogsController.AddToQueue(UIState.Victory, result);
  545.     }
  546.    
  547.     private void ShowDefeatScreen(UITweener tween) {
  548.         DialogsController.AddToQueue(UIState.Loose, null);
  549.     }
  550.    
  551.     float rnd() {
  552.         return UnityEngine.Random.Range(0.0f, 1.0f);
  553.     }
  554.    
  555. //! private void StartMovingMyTanks() {
  556. //!     SetButtonsActive(false);
  557. //!    
  558. //!     for (int i = 0; i < playerFormation.Length; ++i) {
  559. //!         myTanks[i].StartDustMovingAnim(0);
  560. //! //      myTanks[i].korpusMove(TANKS_START_MOVING_DURATION);
  561. //!     }
  562. //!    
  563. //!     Vector3 startPos = - new Vector3(MOVING_SPEED_X * TANKS_START_MOVING_DURATION, MOVING_SPEED_Y * TANKS_START_MOVING_DURATION, 0.0f);
  564. //!    
  565. //!     myTanksParent.localPosition = startPos;
  566. //!     TweenPosition tween = TweenPosition.Begin(myTanksParent.gameObject,
  567. //!                                                 TANKS_START_MOVING_DURATION, Vector3.zero, 0.0f);
  568. //!     tween.onFinished = FinishMyTanksMoving;
  569. //! }
  570.    
  571.     //! sl func
  572.     private IEnumerator StartMovingMyTanks(float delay) {
  573.         yield return new WaitForSeconds(delay);
  574.            
  575.         SetButtonsActive(false);
  576.        
  577.         float timeLast = -1;
  578.        
  579.         for (int i = 0; i < playerFormation.Length; ++i) {
  580.             myTanks[i].StartDustMovingAnim(0);
  581.            
  582.             float t =  myTanks[i].beginBattleMove( 100, 0, 0.5f, 3.0f*rnd()).x;
  583.             if (t >= timeLast) {
  584.                 timeLast = t;
  585.             }
  586.         }
  587.        
  588.    
  589. //!     Vector3 pos = movingContainer.localPosition + new Vector3 (- 3*5*35, - 3*3*35, 0);
  590. //!    
  591. //!     Debug.Log ("timeLast =" +timeLast);
  592. //!    
  593. //!     TweenPosition tweenN = TweenPosition.Begin(movingContainer.gameObject,  timeLast*0.65f, pos,timeLast*0.35f);
  594. //!    
  595. //!     tweenN.animationCurve = new AnimationCurve (new Keyframe(0.0f,0.0f), new Keyframe(1.0f,1.0f));
  596.        
  597.    
  598. //!     StartCoroutine(OnFinishMovingMyTanks(timeLast));
  599.        
  600.        
  601.     }
  602.    
  603. //! private void FinishMyTanksMoving(UITweener tween) {
  604. //!     for (int i = 0; i < playerFormation.Length; ++i)
  605. //!         if (myTanks[i].Card.hp > 0)
  606. //!     {
  607. //!         myTanks[i].korpusMove(12);
  608. //!         myTanks[i].StopDustMovingAnim();
  609. //!        
  610. //!     }
  611. //!     SetButtonsActive(true);
  612. //!    
  613. //! }
  614.    
  615.     //! sl func
  616.     IEnumerator StartMovingEnemyTanks (float time)
  617.     {
  618.         yield return new WaitForSeconds(time);
  619.         CreateEnemyTanks ();
  620.         //! Transform enemyTanksParent = transform.FindChild("BattleViewScene/MovingLayer/EnemyTanks");
  621.         //! enemyTanksParent.gameObject.active = true;
  622.         //! enemyTanksParent.localPosition = enemyTanksParent.localPosition +  new Vector3(+3*5*35.0f, +3*3*35.0f, 0.0f);
  623.        
  624.        
  625.         float timeLast = -1;
  626.    
  627.         List<int> itemsToRemove = new List<int> ();
  628.        
  629.         //for (int i = 0; i < playerFormation.Length; ++i) {
  630.         for (int i = 0; i < enemyTanks.Count; i++) {
  631.             if (enemyTanks [i].Card.hp > 0) {
  632.                 enemyTanks [i].StartDustMovingAnim (0);
  633.                 float t = enemyTanks [i].beginBattleMove (100, 0, 0.5f, 3.0f * rnd ()).x;
  634.                 if (t >= timeLast) {
  635.                     timeLast = t;
  636.                 }
  637.             } else {
  638.                 //NGUITools.Destroy (enemyTanks [i].gameObject);
  639.                 //enemyTanks.RemoveAt (i);
  640.                 itemsToRemove.Add (i);
  641.             }
  642.         }
  643.        
  644.         for (int i=itemsToRemove.Count-1; i>=0; i--) {
  645.             NGUITools.Destroy (enemyTanks [itemsToRemove[i]].gameObject);
  646.             enemyTanks.RemoveAt (itemsToRemove [i]);
  647.         }
  648.        
  649.         StartCoroutine (OnFinishMovingEnemyTanks (timeLast));
  650.     }
  651.    
  652. //! private void StartMovingEnemyTanks() {
  653. //!     CreateEnemyTanks();
  654. //!     enemyTanksParent.gameObject.active = true;
  655. //!     for (int i = 0; i < playerFormation.Length; ++i) {
  656. //!         enemyTanks[i].korpusMove2();
  657. //!     }
  658. //!    
  659. //!     Vector3 startPos = new Vector3(MOVING_SPEED_X * TANKS_START_MOVING_DURATION, MOVING_SPEED_Y * TANKS_START_MOVING_DURATION, 0.0f);
  660. //!    
  661. //!     transform.FindChild("BattleViewScene/EnemyTanks").localPosition = startPos;
  662. //!     TweenPosition tween = TweenPosition.Begin(transform.FindChild("BattleViewScene/EnemyTanks").gameObject,
  663. //!                                                 TANKS_START_MOVING_DURATION, Vector3.zero, 0.0f);
  664. //!     tween.onFinished = OnFinishEnemyMoving;
  665. //! }
  666.    
  667.     //! sl func
  668.     IEnumerator OnFinishMovingEnemyTanks(float time ) {
  669.         yield return new WaitForSeconds(time);
  670.         UpdateLabels();
  671.        
  672. //      ShowBattleTurnLabel();
  673.         StartCoroutine(showAims());
  674.        
  675.     }
  676.    
  677. //! private void OnFinishInitMoving(UITweener tween) {
  678. //!     Vector3 pos = new Vector3 (- MOVING_SPEED_X * MOVING_DURATION, - MOVING_SPEED_Y * MOVING_DURATION, 0);
  679. //!    
  680. //!     /*foreach(Tank tank in enemyTanks) {
  681. //!         enemyPos = tank.transform.localPosition;
  682. //!         enemyPos -= pos;
  683. //!         tank.transform.localPosition = enemyPos;
  684. //!     }*/
  685. //!    
  686. //!//       for (int i = 0; i < playerFormation.Length; ++i) {
  687. //!//           myTanks[i].korpusMove(12);
  688. //!//       }
  689. //!
  690. //!    
  691. //!     pos += movingContainer.localPosition;
  692. //!    
  693. //!     TweenPosition tweenN = TweenPosition.Begin(movingContainer.gameObject, MOVING_DURATION, pos);
  694. //!     tweenN.onFinished = OnFinishEndMoving;
  695. //! }
  696. //! private void OnFinishEndMoving(UITweener tween) {
  697. //!     for (int i = 0; i < playerFormation.Length; ++i)
  698. //!         if (myTanks[i].Card.hp > 0)
  699. //!     {
  700. //!         myTanks[i].korpusMove(12);
  701. //!         myTanks[i].StopDustMovingAnim();
  702. //!        
  703. //!     }
  704. //!     Vector3 pos = new Vector3 (- MOVING_SPEED_X * TANKS_END_MOVING_DURATION,  - MOVING_SPEED_Y * TANKS_END_MOVING_DURATION, 0);    
  705. //!     pos += movingContainer.localPosition;      
  706. //!     TweenPosition tweenP = TweenPosition.Begin(movingContainer.gameObject, TANKS_END_MOVING_DURATION, pos);
  707. //!     TweenPosition tweenN = TweenPosition.Begin(transform.FindChild("BattleViewScene/MyObjects").gameObject, TANKS_END_MOVING_DURATION, transform.FindChild("BattleViewScene/MyObjects").localPosition - new Vector3(MOVING_SPEED_X * TANKS_END_MOVING_DURATION, MOVING_SPEED_Y * TANKS_END_MOVING_DURATION, 0.0f), 0.0f);
  708. //!     tweenN.onFinished = OnFinishMoving;
  709. //! }
  710.    
  711. //! private void OnFinishMoving(UITweener tween) {
  712. //!     UpdateLabels();
  713. //!    
  714. //!
  715. //!     ShowBattleTurnLabel();
  716. //!     CreateEnemyTanks();
  717. //!     enemyTanksParent.gameObject.active = true;
  718. //!     enemyTanksParent.localPosition = new Vector3(MOVING_SPEED_X * TANKS_START_MOVING_DURATION, MOVING_SPEED_Y * TANKS_START_MOVING_DURATION, 0.0f);
  719. //!     TweenPosition tweenNew = TweenPosition.Begin(enemyTanksParent.gameObject, TANKS_START_MOVING_DURATION, - new Vector3( MOVING_SPEED_X * 2.5f, MOVING_SPEED_Y * 2.5f, 0.0f));
  720. //!     tweenNew.onFinished = OnFinishEnemyMoving;
  721. //!     for (int i = 0; i < playerFormation.Length; ++i) {
  722. //!         enemyTanks[i].korpusMove2();
  723. //!     }
  724. //! }
  725.    
  726. //! private void OnFinishEnemyMoving(UITweener tween) {
  727. //!     StartCoroutine(showAims());
  728. //!    
  729. //!     FinishMyTanksMoving(tween);
  730. //! }
  731.    
  732.     private IEnumerator showAims ()
  733.     {
  734.         Debug.Log ("showaims");
  735.         SoundManager.PlaySFXFromGroup ("EnemyDetected", null, 0.5f);
  736.         //for (int i = 0; i < playerFormation.Length; ++i) {
  737.         for (int i = 0; i < enemyTanks.Count; ++i) {
  738.             if (enemyTanks [i].Card.hp > 0) {
  739.                 yield return new WaitForSeconds(0.1f);
  740.                 enemyTanks [i].ShowAim ();
  741.                 //  enemyTanks[i].StopDustMovingAnim();
  742.                 //  enemyTanks[i].korpusMove2();
  743.             }
  744.         }
  745.         transform.FindChild ("BattleViewHUD/BottomRight/AttackButton/TweenTarget/Icon").GetComponent<UISprite> ().spriteName = "attack_icon";
  746.         transform.FindChild ("BattleViewHUD/BottomRight/AttackButton/TweenTarget/Background").GetComponent<UISprite> ().color = new Color (0.59f, 0.0f, 0.0f, 0.1f);
  747.         transform.FindChild ("BattleViewHUD/BottomRight/AttackButton").GetComponent<UIButtonMessage> ().functionName = "OnAttackClicked";
  748.         //transform.FindChild("BattleViewHUD/BottomRight/AttackButton").gameObject.SetActiveRecursively(true);
  749.         transform.FindChild ("BattleViewHUD/BottomRight/AttackButton").GetComponent<UIFramedButtonExt> ().enableButton ();
  750.     }
  751.    
  752.     private void CreateEnemyTanks ()
  753.     {
  754.         //Transform enemyTanksParent = transform.FindChild("BattleViewScene/EnemyTanks");
  755.         //enemyTanksParent.gameObject.SetActiveRecursively(true);
  756.         Battle battle = GameManager.Instance.Battle;
  757.         Debug.Log ("Location ID = " + GameManager.Instance.Human.LocationId + " Sector ID + " + battle.SectorID);
  758.         CardData[] cards = ContentManager.Instance.getTurnData (GameManager.Instance.Human.LocationId, battle.SectorID, battle.CurrentTurn, GameManager.Instance.Human.ActionInLocation).pveBattle.deck.ToArray ();//GameManager.Instance.Battle.TurnData.enemy.cards;
  759.        
  760.         Tank enemyTank;
  761.         if (cards.Length > enemyTanks.Count) {
  762.             Transform enemyTanksParent = transform.FindChild ("BattleViewScene/EnemyTanks");
  763.             while (cards.Length > enemyTanks.Count) {
  764.                 // create moar tanks;
  765.                 enemyTank = NGUITools.AddChild (enemyTanksParent.gameObject, tankPrefab).GetComponent<Tank> ();
  766.                 enemyTank.HideAddText ();
  767.                 enemyTank.SetButtonActive (false);
  768.                 enemyTank.isEnemy = true;
  769.                 enemyTank.Bufs.Clear ();
  770.                 enemyTank.Bufs.Add (new BufData () {type = BufType.Attack, value = 0.5f});
  771.                 enemyTanks.Add (enemyTank);
  772.                
  773.                 GameObject tankTracksConteiner = NGUITools.AddChild (enemyTracks); 
  774.                 tankTracksConteiner.name = "traks_" + enemyTanks.Count;            
  775.                 enemyTank.tracksContainer = tankTracksConteiner;
  776.             }
  777.         }
  778.        
  779.         foreach (Tank tank in enemyTanks) {
  780.             tank.SetTankActive (false);
  781.         }
  782.        
  783.         int[] formation = ContentManager.Instance.getTurnData (GameManager.Instance.Human.LocationId, battle.SectorID, battle.CurrentTurn, GameManager.Instance.Human.ActionInLocation).pveBattle.formation.positions;//GameManager.Instance.Battle.TurnData.enemy.formation.positions;// FormationsManager.GetCurrentFormation();
  784.         int count = cards.Length;
  785.         for (int i = 0; i < count; ++i) {
  786.             //NB. Dirty trick - set hp to maxHP.
  787.             cards [i].hp = cards [i].maxHp;
  788.             enemyTanks [i].InitWith (cards [i], 0, i);
  789.             enemyTanks [i].transform.localPosition = new Vector3 (5 * 100, 3 * 100, 0) + GetTankPosition (formation, i, true);
  790.             enemyTanks [i].updateTracks ();
  791.             enemyTanks [i].AimRoot.SetActiveRecursively (false);
  792.         }
  793.        
  794.         ShowEnemyTanks (true);
  795.         for (int i = 0; i < count; ++i) {
  796.             enemyTanks [i].image.gameObject.active = false;
  797.             enemyTanks [i].lvl.gameObject.active = false;
  798.             enemyTanks [i].nameLabel.gameObject.active = false;
  799.             enemyTanks [i].AimRoot.SetActiveRecursively (false);
  800.         }
  801.     }
  802.    
  803.     /*public const float HIGHLIGHT_DELAY = 0.1f;
  804.     public const float TanksMoves.Instance.DELAY_BEFORE_FIRE = 0.1f;
  805.     private const float TanksMoves.Instance.DELAY_BEFORE_DAMAGE = 0.2f;
  806.     private const float TanksMoves.Instance.DELAY_AFTER_DAMAGE = 0.1f;
  807.     private const float TanksMoves.Instance.DELAY_AFTER_END = 0.1f;*/
  808.  
  809.     void CheckIsTankAlive (Tank _attacked, int i, Tank attacker)
  810.     {
  811.         //TanksMoves.Instance.
  812.         if (i<0)
  813.             return;
  814.         if (_attacked.Card.hp > 0) {
  815.             //_attacked.HighlightTank(true, TanksMoves.Instance.HIGHLIGHT_DELAY, new Color(0.0f, 0.0f, 0.0f, 0.0f));
  816.            
  817.         }
  818.         else
  819.         {
  820.             //_attacked.HighlightTank(true, 0.0f, new Color(0.0f, 0.0f, 0.0f, 0.0f));
  821.            
  822.             // Enemy tank is dead.
  823.             VisualEffectManager.CreateEffect("Explosion01", Vector3.zero, _attacked.transform);
  824.             if (_attacked.isEnemy)
  825.             {
  826.                 _attacked.tankParts = Tank.buildAtGameObject("burned_c_enemy_", _attacked);
  827.                 _attacked.FadeOut();
  828.                
  829.                 GameObject crater = (GameObject) GameObject.Instantiate(craterPrefab);
  830.                 Transform craterTransform = crater.transform;
  831.                 Vector3 scale = craterTransform.localScale;
  832.                 craterTransform.parent = decorationsContainer.FindChild("Craters");
  833.                 craterTransform.localScale = scale;
  834.                
  835.                 Vector3 pos = _attacked.transform.localPosition;
  836.                 pos.x += GameManager.RandomGenerator.Next(50) - 25;
  837.                 pos.y += GameManager.RandomGenerator.Next(50) - 25;
  838.                
  839.                 craterTransform.localPosition = pos;
  840.             }
  841.             else
  842.             {
  843.                 _attacked.tankParts = Tank.buildAtGameObject("burned_c_", _attacked);
  844.             }
  845.            
  846.             // Loot
  847.            
  848.             if (_attacked.isEnemy)
  849.             {
  850.                 LootData loot = GameManager.Instance.GenerateLoot();
  851.                 int coinsCount = (int)(loot.coins / (playerFormation.Length * 10));
  852.                 if (loot.coins > 0) {
  853.                     for (int k = 0; k < coinsCount; ++k) {
  854.                         //Debug.Log("zzzzzzzzz" + (_attacked.gameObject.transform.localPosition + _attacked.gameObject.transform.parent.transform.localPosition).ToString());
  855.                         DropItem(_attacked.gameObject.transform.localPosition + _attacked.gameObject.transform.parent.transform.localPosition, "coin");
  856.                     }
  857.                 }
  858.                 if (loot.box > 0) {
  859.                     DropItem(_attacked.gameObject.transform.localPosition + _attacked.gameObject.transform.parent.transform.localPosition, "box");
  860.                 }
  861.                
  862.                 if (loot.ironBox > 0) {
  863.                     DropItem(_attacked.gameObject.transform.localPosition + _attacked.gameObject.transform.parent.transform.localPosition, "iron-box");
  864.                 }
  865.                
  866.                 DropStar(_attacked.gameObject.transform, attacker.transform);
  867.                 DropWrench(_attacked.gameObject.transform, attacker.transform);
  868.                 UpdateLabels();
  869.             }
  870.         }
  871.     }
  872.    
  873.    
  874.     int ChooseEnemyTankForAttack(List<Tank> enemyTanks, int attackerId, List<Tank> myTanks) {
  875.        
  876.         int diff = 1;
  877.         if(myTanks.Count == enemyTanks.Count)
  878.             diff = 0;
  879.         else
  880.         {
  881.             if(enemyTanks.Count > myTanks.Count)
  882.                 diff = -1;
  883.         }
  884.        
  885.         int returnedID = 0;
  886.         int i = 0;
  887.         for (i=0; i<5; i++)
  888.         {
  889.             int tmpid = attackerId - diff;
  890.             if (tmpid - i >= 0 && tmpid - i < enemyTanks.Count && enemyTanks[tmpid - i] != null && enemyTanks[tmpid - i].Card.hp > 0)
  891.             {
  892.                 returnedID = tmpid -i;
  893.                 break;
  894.             }
  895.            
  896.             if (tmpid + i >=0 && tmpid + i < enemyTanks.Count && enemyTanks[tmpid + i] != null && enemyTanks[tmpid + i].Card.hp > 0)
  897.             {
  898.                 returnedID = tmpid + i;
  899.                 break;
  900.             }
  901.         }
  902.         Debug.Log("at" + attackerId + " ret " + returnedID + " " + enemyTanks.Count + " i=" + i);
  903.         return returnedID;
  904.     }
  905.    
  906.    
  907.     private IEnumerator AnimateNormalAttack (Tank _attacker, List<Tank> enemiesList, int actorIndex, List<Tank> myList)
  908.     {
  909.         float timeLength = 0.0f;
  910.        
  911.         var _attacked = enemiesList [ChooseEnemyTankForAttack (enemiesList, actorIndex, myList)];
  912.         yield return new WaitForSeconds(0);
  913.         if (_attacked != null) {
  914. //          _attacker.HighlightTank(true, TanksMoves.Instance.HIGHLIGHT_DELAY, new Color(1.0f, 1.0f, 1.0f, 1.0f));
  915. //          _attacked.HighlightTank(true, TanksMoves.Instance.HIGHLIGHT_DELAY, new Color(1.0f, 0.0f, 0.0f, 1.0f));
  916.            
  917. //          VisualEffect effect = VisualEffectManager.CreateEffect("ColorFrameFX", Vector3.zero, _attacked.transform);
  918. //          ColorFrameFX frame = (ColorFrameFX) effect;
  919. //          frame.transform.rotation = _attacked.transform.rotation;
  920. //          frame.transform.Rotate(50,10,-41);
  921. //          frame.Color = Color.red;
  922.            
  923.             VisualEffect effect;
  924.             SWFClipFX swf;         
  925.            
  926.             /* //было
  927.             effect = VisualEffectManager.CreateEffect ("GreenFrameFX", Vector3.zero, _attacker.transform);
  928.             swf = (SWFClipFX)effect;
  929.             swf.FPS = TanksMoves.Instance.HIGHLIGHT_GREEN_FPS;
  930.             yield return new WaitForSeconds(TanksMoves.Instance.HIGHLIGHT_DELAY);
  931.             */
  932.            
  933.            
  934.            
  935.             HighlightFX greenFrame = HighlightFX.CreateFX(HighlightFX._FRAME_KIND.GREEN, _attacker.gameObject);
  936.             //greenFrame.
  937.             Debug.Log("!!!! Starting green 1 - begin");
  938.             yield return (greenFrame.showInState());
  939.             Debug.Log("!!!! Starting green 1 - end");
  940.            
  941.             //UnityEditor.EditorApplication.isPaused = true;
  942.            
  943.            
  944. //          effect = VisualEffectManager.CreateEffect("ColorFrameFX", Vector3.zero, _attacker.transform);
  945. //          frame = (ColorFrameFX) effect;
  946. //          frame.transform.rotation = _attacker.transform.rotation;
  947. //          frame.transform.Rotate(50,10,-41);
  948. //          frame.Color = Color.green;
  949.            
  950.             /* //было
  951.             effect = VisualEffectManager.CreateEffect ("RedFrameFX", Vector3.zero, _attacked.transform);
  952.             swf = (SWFClipFX)effect;
  953.             swf.FPS = TanksMoves.Instance.HIGHLIGHT_RED_FPS;
  954.            
  955.             yield return new WaitForSeconds(TanksMoves.Instance.HIGHLIGHT_DELAY + TanksMoves.Instance.DELAY_BEFORE_FIRE);
  956.             */
  957.  
  958.             HighlightFX redFrame = HighlightFX.CreateFX(HighlightFX._FRAME_KIND.RED, _attacked.gameObject);
  959.             Debug.Log("!!!! Starting red 1 - begin");
  960.             yield return (redFrame.showInState());
  961.             Debug.Log("!!!! Starting red 1 - end");
  962.            
  963.             // We will kill enemy tank with 2 shots.
  964.    
  965.             _attacker.Attack ();
  966.             var cardProto = _attacker.Card.GetPrototype ();
  967.             SoundManager.PlaySFXFromGroup (string.IsNullOrEmpty (cardProto.sfx) ? "Tank" : cardProto.sfx);
  968.            
  969.             // Damage enemy tank.          
  970.             //tank = enemyTanks[i];
  971.             if (_attacked.Card.hp > 0) {
  972.                
  973.                 yield return new WaitForSeconds(TanksMoves.Instance.DELAY_BEFORE_DAMAGE);
  974.                
  975.                 _attacked.Damage (ServerEmulation.GetDamage (_attacker.Card, _attacker.Bufs, _attacked.Card, _attacked.Bufs));
  976.                
  977.                 VisualEffectManager.CreateEffect ("Explosion01", Vector3.zero, _attacked.transform);
  978.                
  979.                 yield return new WaitForSeconds(TanksMoves.Instance.DELAY_AFTER_DAMAGE);
  980.                
  981.                 CheckIsTankAlive (_attacked, enemiesList.IndexOf (_attacked), _attacker);
  982.             }
  983.            
  984. //          _attacker.HighlightTank(true, TanksMoves.Instance.HIGHLIGHT_DELAY, new Color(1.0f, 1.0f, 1.0f, 0.0f));
  985.            
  986.             Debug.Log("!!!! Starting green 2 - begin");
  987.             yield return (redFrame.showOutState());
  988.             Debug.Log("!!!! Starting green 2 - end");
  989.             Debug.Log("!!!! Starting red 2 - begin");
  990.             yield return (greenFrame.showOutState());
  991.             Debug.Log("!!!! Starting red 2 - end");
  992.             yield return new WaitForSeconds(TanksMoves.Instance.HIGHLIGHT_DELAY + TanksMoves.Instance.DELAY_AFTER_END);
  993.  
  994.         }
  995.     }
  996.    
  997.     static int Strongest(Tank x, Tank y)
  998.     {
  999.         if (x.Card.hp > y.Card.hp) return -1; else if (x.Card.hp == y.Card.hp) return 0; return -1;
  1000.     }
  1001.     static int Weakest(Tank x, Tank y)
  1002.     {
  1003.         if (x.Card.hp > y.Card.hp) return -1; else if (x.Card.hp == y.Card.hp) return 0; return -1;
  1004.     }
  1005.    
  1006.     private IEnumerator AnimateSkill (Tank _attacker, List<Tank> friendsList, List<Tank> enemiesList)
  1007.     {
  1008.         float timeLength = 0.0f;
  1009.    
  1010.         // TODO: Split it into server-view functions
  1011.         if (_attacker.Card.hp > 0) {
  1012.        
  1013.             // Get targets
  1014.             List<Tank> targets = new List<Tank> ();
  1015.             List<Tank> enemies = new List<Tank> (enemiesList);
  1016.             List<Tank> friends = new List<Tank> (friendsList);
  1017.            
  1018.             bool isEnemy = false;
  1019.            
  1020.             int actorIndex = friendsList.IndexOf (_attacker);
  1021.             friends.Remove (_attacker);
  1022.             targets.Add (_attacker);
  1023.            
  1024.             //if(actorIndex > enemiesList.Count)
  1025.             //actorIndex = actorIndex % enemiesList.Count;
  1026.             var targetslist = _attacker.Card.skill.GetPrototype ().targets;
  1027.             foreach (var type in targetslist) {
  1028.                 Debug.Log ("SKILL: " + type);
  1029.                 Tank target = null;
  1030.                 switch (type) {
  1031.                 case SkillTargetType.Self:
  1032.                     target = _attacker;
  1033.                     break;
  1034.                 case SkillTargetType.NearestEnemy:
  1035.                     target = enemiesList [ChooseEnemyTankForAttack (enemiesList, actorIndex, friendsList)];//.FindNearest (actorIndex, (obj) => {
  1036.                         //return obj.Card.hp > 0 && enemies.Contains (obj);});
  1037.                     isEnemy = true;
  1038.                     break;
  1039.                 case SkillTargetType.RandomEnemy:
  1040.                     Debug.LogWarning ("RANDOM ENEMY SKILL!!");
  1041.                     enemies.Shuffle ();
  1042.                     target = enemies.Find ((obj) => {
  1043.                         return obj.Card.hp > 0;});
  1044.                     isEnemy = true;
  1045.                     break;
  1046.                 case SkillTargetType.WeakestEnemy:
  1047.                     enemies.Sort (Weakest);
  1048.                     target = enemies.Find ((obj) => {
  1049.                         return obj.Card.hp > 0;});
  1050.                     isEnemy = true;
  1051.                     break;
  1052.                 case SkillTargetType.StrongestEnemy:
  1053.                     enemies.Sort (Strongest);
  1054.                     target = enemies.Find ((obj) => {
  1055.                         return obj.Card.hp > 0;});
  1056.                     isEnemy = true;
  1057.                     break;
  1058.                 case SkillTargetType.NearestFriend:
  1059.                     target = friendsList.FindNearest (actorIndex, (obj) => {
  1060.                         return obj.Card.hp > 0 && friends.Contains (obj);});
  1061.                     break;
  1062.                 case SkillTargetType.RandomFriend:
  1063.                     friends.Shuffle ();
  1064.                     target = friends.Find ((obj) => {
  1065.                         return obj.Card.hp > 0;});
  1066.                     break;
  1067.                 case SkillTargetType.WeakestFriend:
  1068.                     friends.Sort (Weakest);
  1069.                     target = friends.Find ((obj) => {
  1070.                         return obj.Card.hp > 0;});
  1071.                     break;
  1072.                 case SkillTargetType.StrongestFriend:
  1073.                     friends.Sort (Strongest);
  1074.                     target = friends.Find ((obj) => {
  1075.                         return obj.Card.hp > 0;});
  1076.                     break;
  1077.                 }
  1078.                 targets.Add (target);
  1079.                 if (target != null) {
  1080.                     /*
  1081.                     int targetIndex = enemies.IndexOf (target);
  1082.                     if (targetIndex > 0)
  1083.                         enemies.RemoveAt (targetIndex);
  1084.                     else {
  1085.                         targetIndex = friends.IndexOf (target);
  1086.                         if (targetIndex > 0)
  1087.                             friends.RemoveAt (targetIndex);
  1088.                     }
  1089.                     */
  1090.                     if (isEnemy)
  1091.                         enemies.Remove (target);
  1092.                     else
  1093.                         friends.Remove(target);
  1094.                 }
  1095.             }
  1096.            
  1097.            
  1098.             // play effects
  1099.             /*
  1100.             var fx = VisualEffectManager.CreateEffect ("SkillFX", new Vector3 (0, 0, -100), _attacker.transform) as SkillFX;
  1101.             fx.skill = _attacker.Card.skill;
  1102.             fx.Targets = targets;
  1103.             */
  1104.            
  1105.             // Get random skill FX
  1106.            
  1107.             int skillIndex = GameManager.RandomGenerator.Next (2);
  1108.             string nameFx = "";
  1109.             if (skillIndex == 0) {
  1110.                 name = "TipaSkillFX";
  1111.             } else {
  1112.                 name = "SWFSkill2FX";
  1113.             }
  1114.            
  1115.             VisualEffect fx = VisualEffectManager.CreateEffect (name, new Vector3 (0, 0, -100), _attacker.transform);
  1116.             Debug.Log ("TYPE OF SKILL: " + fx.GetType ());
  1117.             timeLength = fx.timeLength();
  1118.            
  1119.             if (fx.GetType ().ToString () == "SWFClipFX") {
  1120.                 SWFClipFX clip = (SWFClipFX)fx;
  1121.                 //clip.skill = VisualEffectManager.CreateEffect ("SkillFX", new Vector3 (0, 0, -100), _attacker.transform) as SkillFX;
  1122.                 clip.skill = _attacker.Card.skill;
  1123.                 clip.Targets = targets;
  1124.                 /*foreach (var t in targets) {
  1125.                     Debug.Log ("-> Skill target: " + t.Card.name);
  1126.                 }*/
  1127.                 /*while (clip.skill.gameObject.active)
  1128.                     yield return new WaitForEndOfFrame();
  1129.                     */
  1130.                    
  1131.                 timeLength = clip.timeLength();
  1132.             }
  1133.            
  1134.             /*while (fx.gameObject.active)
  1135.                 yield return new WaitForEndOfFrame();
  1136.             */
  1137.            
  1138.             Debug.Log("WAITING for "+timeLength+" sec");
  1139.             //UnityEditor.EditorApplication.isPaused = true;
  1140.            
  1141.             yield return new WaitForSeconds(timeLength);
  1142.                    
  1143.             foreach (var target in targets) {
  1144.                 CheckIsTankAlive (target, enemiesList.IndexOf (target), _attacker);
  1145.             }
  1146. //          Debug.Log("Skill animated");
  1147.         }
  1148.     }
  1149.    
  1150.     private IEnumerator AnimateFight ()
  1151.     {      
  1152.        
  1153.         //TRYING TO MAKE RANDOM TURNS
  1154.         List<int> myT = new List<int> ();
  1155.         List<int> eT = new List<int> ();
  1156.        
  1157.         for (int i = 0; i < playerFormation.Length; ++i) {
  1158.             myT.Add (i);
  1159.         }
  1160.        
  1161.         for (int i = 0; i < enemyTanks.Count; i++) {
  1162.             eT.Add (i);
  1163.         }
  1164.         List<int> myTF = new List<int> ();
  1165.         List<int> eTF = new List<int> ();
  1166.        
  1167.         while (myT.Count > 0) {
  1168.             int position = GameManager.RandomGenerator.Next (0, myT.Count);
  1169.             myTF.Add (myT [position]);
  1170.             myT.RemoveAt (position);
  1171.         }
  1172.        
  1173.         while (eT.Count > 0) {
  1174.             int position = GameManager.RandomGenerator.Next (0, eT.Count);
  1175.             eTF.Add (eT [position]);
  1176.             eT.RemoveAt (position);
  1177.         }
  1178.         //TRYING TO MAKE RANDOM TURNS
  1179.        
  1180.         int myCount = myTanks.Count;
  1181.         int enemyCount = enemyTanks.Count;
  1182.        
  1183.         int currentTurn = 0;
  1184.        
  1185.         while (myCount > 0 && enemyCount > 0 && !_surrender) {
  1186.            
  1187.             //for(myTF)
  1188.            
  1189.             //myTF eTF != null
  1190.            
  1191.             foreach (var t in myTanks)
  1192.                 if(t != null && t.Card.hp <= 0)
  1193.                     myTF.Remove(myTanks.IndexOf (t));
  1194.            
  1195.  
  1196.            
  1197.            
  1198.             Tank myCard = myTanks[myTF [currentTurn%myTF.Count]] ;
  1199.             //Tank eACard = new Tank();//
  1200.            
  1201.             if (myCard.Card.hp > 0 && myCard.Card.skill != null && UnityEngine.Random.Range (0, 100) <= myCard.Card.skill.GetChance ())
  1202.             { // if use skill
  1203.                 yield return StartCoroutine(AnimateSkill(myCard, myTanks, enemyTanks));
  1204.                 /*
  1205.                 yield return new WaitForSeconds(
  1206.                     (TanksMoves.Instance.HIGHLIGHT_DELAY * 2
  1207.                     + TanksMoves.Instance.DELAY_BEFORE_FIRE
  1208.                     + TanksMoves.Instance.DELAY_AFTER_END
  1209.                     + TanksMoves.Instance.DELAY_BEFORE_DAMAGE
  1210.                     + TanksMoves.Instance.DELAY_AFTER_DAMAGE)*2.5f);
  1211.                     */
  1212.             }
  1213.             else
  1214.             {
  1215.                 StartCoroutine (AnimateNormalAttack (myCard, enemyTanks, myTF [currentTurn%myTF.Count], myTanks));
  1216.                 yield return new WaitForSeconds(
  1217.                     (TanksMoves.Instance.HIGHLIGHT_DELAY * 2
  1218.                     + TanksMoves.Instance.DELAY_BEFORE_FIRE
  1219.                     + TanksMoves.Instance.DELAY_AFTER_END
  1220.                     + TanksMoves.Instance.DELAY_BEFORE_DAMAGE
  1221.                     + TanksMoves.Instance.DELAY_AFTER_DAMAGE)*1.5f);
  1222.                    
  1223.  
  1224.             }
  1225.             //Debug.Log("bcount " + eTF.Count);
  1226.             foreach (var t in enemyTanks)
  1227.             {
  1228.                 //Debug.Log("hp " + t.Card.hp);
  1229.                 if(t != null && t.Card.hp <= 0)
  1230.                 {
  1231.                     eTF.Remove(enemyTanks.IndexOf (t));
  1232.                     //break;
  1233.                 }
  1234.             }
  1235.            
  1236.            
  1237.            
  1238.             enemyCount = enemyTanks.Count ((obj) => {
  1239.                         return obj.Card.hp > 0;});
  1240.            
  1241.             if(enemyCount <= 0)
  1242.                 break;
  1243.             //for(eT)
  1244.             Tank eCard = enemyTanks[eTF [currentTurn%eTF.Count]];
  1245.             //Tank myACard = new Tank();//
  1246.            
  1247.             //if (_surrender)
  1248.             //      continue;
  1249.            
  1250.             if (eCard.Card.hp > 0 && eCard.Card.skill != null && UnityEngine.Random.Range (0, 100) <= eCard.Card.skill.GetChance ())
  1251.             { // if use skill
  1252.                 yield return StartCoroutine(AnimateSkill(eCard, enemyTanks, myTanks));
  1253.                 /*yield return new WaitForSeconds(
  1254.                     (TanksMoves.Instance.HIGHLIGHT_DELAY * 2
  1255.                     + TanksMoves.Instance.DELAY_BEFORE_FIRE
  1256.                     + TanksMoves.Instance.DELAY_AFTER_END
  1257.                     + TanksMoves.Instance.DELAY_BEFORE_DAMAGE
  1258.                     + TanksMoves.Instance.DELAY_AFTER_DAMAGE)*2.5f);
  1259.                     */
  1260.             }
  1261.             else
  1262.             {              
  1263.                 StartCoroutine (AnimateNormalAttack (eCard, myTanks, eTF [currentTurn%eTF.Count], enemyTanks));
  1264.                 yield return new WaitForSeconds(
  1265.                     (TanksMoves.Instance.HIGHLIGHT_DELAY * 2
  1266.                     + TanksMoves.Instance.DELAY_BEFORE_FIRE
  1267.                     + TanksMoves.Instance.DELAY_AFTER_END
  1268.                     + TanksMoves.Instance.DELAY_BEFORE_DAMAGE
  1269.                     + TanksMoves.Instance.DELAY_AFTER_DAMAGE)*1.5f);
  1270.                    
  1271.             }
  1272.            
  1273.             myCount = myTanks.Count ((obj) => {
  1274.                 return obj.Card.hp > 0;});
  1275.            
  1276.             currentTurn++;
  1277.            
  1278.            
  1279.            
  1280.            
  1281.             /*for (int i = 0; i < playerFormation.Length; i++) {
  1282.                 Debug.Log (playerFormation.Length + " " + myTanks.Count + " " + enemyTanks.Count + " " + i);
  1283.                
  1284.                 int l = 0;
  1285.                 foreach (var t in enemyTanks) {
  1286.                     if (t != null)
  1287.                         Debug.Log (l + " y");
  1288.                     else
  1289.                         Debug.Log (l + " n");
  1290.                     l++;
  1291.                 }
  1292.                
  1293.                     int p = i % enemyTanks.Count;
  1294.                    
  1295.                    
  1296.                    
  1297.                 //for (int i = 0; i < n.Length; i++)
  1298.                 if (_surrender)
  1299.                     continue;
  1300.                 if (myTanks [i].Card.hp > 0 && enemyCount > 0) {
  1301.                    
  1302.                     //OUR TURN
  1303.                     if (myTanks [i].Card.skill != null && UnityEngine.Random.Range (0, 100) <= myTanks [i].Card.skill.GetChance ()) { // if use skill
  1304.                         yield return StartCoroutine(AnimateSkill(myTanks[myTF[i]], myTanks, enemyTanks));
  1305.                     } else {
  1306.                         if (myTanks [myTF [i]].Card.hp > 0) {
  1307.                            
  1308.                             Debug.Log ("11");
  1309.                             StartCoroutine (AnimateNormalAttack (myTanks [myTF [i]], enemyTanks, eTF [p]));
  1310.                             Debug.Log ("12");
  1311.                             yield return new WaitForSeconds(TanksMoves.Instance.HIGHLIGHT_DELAY * 2 + TanksMoves.Instance.DELAY_BEFORE_FIRE + TanksMoves.Instance.DELAY_AFTER_END + TanksMoves.Instance.DELAY_BEFORE_DAMAGE + TanksMoves.Instance.DELAY_AFTER_DAMAGE);
  1312.                         }
  1313.                     }
  1314.                     Debug.Log ("2");
  1315.                     enemyCount = enemyTanks.Count ((obj) => {
  1316.                         return obj.Card.hp > 0;});
  1317.                 }
  1318.                
  1319.                 //ENEMY TURN
  1320.                  //= 0;
  1321.                 //if (i > enemyTanks.Count)
  1322.                 ;
  1323.                
  1324.                 if (enemyTanks [p].Card.hp > 0 && myCount > 0) {
  1325.                     if (enemyTanks [p].Card.skill != null && UnityEngine.Random.Range (0, 100) <= enemyTanks [p].Card.skill.GetChance ()) { // if use skill
  1326.                         yield return StartCoroutine(AnimateSkill(enemyTanks[eTF[p]], enemyTanks, myTanks));
  1327.                     } else {
  1328.                         if (myTanks [eTF [p]].Card.hp > 0) {
  1329.                             StartCoroutine (AnimateNormalAttack (enemyTanks [eTF [p]], myTanks, myTF [i]));
  1330.                             yield return new WaitForSeconds(TanksMoves.Instance.HIGHLIGHT_DELAY * 2 + TanksMoves.Instance.DELAY_BEFORE_FIRE + TanksMoves.Instance.DELAY_AFTER_END + TanksMoves.Instance.DELAY_BEFORE_DAMAGE + TanksMoves.Instance.DELAY_AFTER_DAMAGE);
  1331.                         }
  1332.                     }
  1333.                     myCount = myTanks.Count ((obj) => {
  1334.                         return obj.Card.hp > 0;});
  1335.                 }
  1336.             }// end of for-i
  1337.             */
  1338.         }
  1339.        
  1340. //      bool waitForLevelup = false;
  1341. //      List<ExpReward> levelUps = new List<ExpReward>();
  1342. //      if (!_surrender) {
  1343. //          foreach (var tank in myTanks)
  1344. //              if (tank.Card.hp > 0)
  1345. //          {
  1346. //              int expReward = 5 + GameManager.Instance.Battle.CurrentTurn * 5;
  1347. ////                expReward = tank.Card.maxExp - tank.Card.exp;// use it for testing cards level up
  1348. //             
  1349. //              ExpReward reward = ServerEmulation.GetLevelUpResult(tank.Card, expReward);
  1350. //              tank.ExpReward(expReward);
  1351. //              if (reward.levelUp != null) {
  1352. //                  levelUps.Add(reward);
  1353. //                  VisualEffectManager.CreateEffect("LevelUpFX", Vector3.zero, tank.transform);
  1354. //                  waitForLevelup = true;
  1355. //              }
  1356. //              reward.Upgraded.CopyStatsTo(tank.Card);
  1357. //              if (waitForLevelup) {
  1358. //                  tank.Card.hp = tank.Card.maxHp;
  1359. //                  tank.UpdateHPBar();
  1360. //              }
  1361. //          }
  1362. //      }
  1363. //      if (!_surrender) {
  1364. //          yield return new WaitForSeconds(waitForLevelup ? 3.0f : 1.0f);
  1365. //      }
  1366.        
  1367.         yield return new WaitForSeconds(1.0f);
  1368.        
  1369. //      if (levelUps.Count > 0) {
  1370. //          CardsLevelUpViewController controller = MainUIController.Instance.CardsLevelUpView.GetComponent<CardsLevelUpViewController>();
  1371. //          controller.InitWith(levelUps);
  1372. //          DialogsController.AddToQueue(UIState.CardsLevelUp, null);
  1373. //      }
  1374.        
  1375. //      if (battle.CurrentTurn < battle.TotalTurns) {
  1376. //          foreach (var tank in myTanks) {
  1377. //              tank.ShowButtons(true);
  1378. //          }
  1379. //      }
  1380.        
  1381.         UpdateLabels ();
  1382.         Battle battle = GameManager.Instance.Battle;
  1383.         if (myCount > 0) {
  1384.             if (battle.CurrentTurn < battle.TotalTurns) {
  1385. //              //moveButton.
  1386. //              //moveButton.GetComponent<UISprite>().color = new Color(1.0f, 1.0f, 1.0f, 0.0f);
  1387. //              moveButton.SetActiveRecursively(true);
  1388. //             
  1389. //              //StartCoroutine(runButtonAnimation());
  1390. //              //transform.FindChild("BattleViewHUD/BottomRight/AttackButton").gameObject.SetActiveRecursively(true);
  1391. //             
  1392. //             
  1393. //              Debug.LogWarning ("Switching on attack button");
  1394. //              transform.FindChild("BattleViewHUD/BottomRight/AttackButton/TweenTarget/Background").GetComponent<UISprite>().color = new Color(0.0f, 0.47f, 0.0f, 0.1f);
  1395. //              transform.FindChild("BattleViewHUD/BottomRight/AttackButton/TweenTarget/Icon").GetComponent<UISprite>().spriteName = "go";
  1396. //              transform.FindChild("BattleViewHUD/BottomRight/AttackButton").GetComponent<UIButtonMessage>().functionName = "OnMoveClicked";
  1397. //              transform.FindChild("BattleViewHUD/BottomRight/AttackButton").GetComponent<UIButtonMessage>().enabled = true;
  1398. //              transform.FindChild("BattleViewHUD/BottomRight/AttackButton").GetComponent<UIFramedButtonExt>().enableButton();
  1399.                
  1400.                 //yield return new WaitForSeconds(2);
  1401.                 ServerEmulation.EmulateStartBattleTurn ();
  1402.             } else {
  1403.                 //TODO: show level up and victory screen
  1404.                 StartCoroutine (FinishBattle ());              
  1405. //              MainUIController.Instance.SetDarknessVisible(true, ShowMap);
  1406.                
  1407. //              // Finish battle
  1408. //              if (levelUps.Count <= 0) {
  1409. //                  // Show zone if there are no level ups
  1410. //                  MainUIController.Instance.SetDarknessVisible(true, ShowMap);
  1411. //              }
  1412.             }
  1413.         } else
  1414.             ShowDefeatScreen (null);
  1415.     }
  1416.    
  1417.     private void ShowZone(UITweener tween) {
  1418.         MainUIController.uiState = UIState.ZoneView;
  1419.     }
  1420.    
  1421.     private void ShowMap(UITweener tween) {
  1422.         MainUIController.uiState = UIState.Map;
  1423.     }
  1424.    
  1425.     IEnumerator runButtonAnimation()
  1426.     {
  1427.         yield return new WaitForSeconds(0.8f);
  1428.         transform.FindChild("BattleViewHUD/BottomRight/AttackButton").GetComponent<UIFramedButtonExt>().enableButton();
  1429.     }
  1430.    
  1431.     private void OnFinishButtonAnim1(UITweener tween) {
  1432.         Debug.Log ("OnFinishButtonAnim1", moveButton);
  1433.         TweenScale tweenN = TweenScale.Begin(moveButton.transform.parent.gameObject, 0.1f, new Vector3(1.0f, 1.0f, 1.0f));
  1434.         //tweenN.onFinished = OnFinishButtonAnim2;
  1435.     }
  1436.    
  1437.     private void OnFinishButtonColor(UITweener tween) {
  1438.         TweenColor tmp = TweenColor.Begin(moveButton, 0.3f, new Color(1.0f, 1.0f, 1.0f, 0.0f));
  1439.     }
  1440.    
  1441.  
  1442.     //
  1443.     private void DropItem(GameObject tank, string itemName) {
  1444.         DropItem(tank.transform.localPosition, itemName);
  1445.     }
  1446.    
  1447.     private void DropItem(Vector3 pos, string itemName) {
  1448.  
  1449.         GameObject item = (GameObject)GameObject.Instantiate(lootPrefab);
  1450.         items.Add(item);
  1451.        
  1452.         // set valid sprite
  1453.         UISprite sprite = item.GetComponent<UISprite>();
  1454.         sprite.spriteName = itemName;
  1455.         sprite.MakePixelPerfect();
  1456.        
  1457.         //Vector3 tankPos = tank.transform.localPosition;
  1458.        
  1459.         Vector3 originalScale = item.transform.localScale;
  1460.         item.transform.parent = movingContainer.gameObject.transform.parent;
  1461.         item.transform.localPosition = pos + new Vector3(0, 0, -25);
  1462.        
  1463.         // auto click to coin after 5 seconds
  1464.         iTween.MoveTo(item, iTween.Hash("onstart", "ClickToLoot", "onstarttarget", gameObject, "onstartparams", item,"delay", 2));
  1465.        
  1466.         MoveByArc dropScript = item.GetComponent<MoveByArc>();
  1467.         dropScript.dX = UnityEngine.Random.Range(-100, 100);
  1468.         dropScript.dY = UnityEngine.Random.Range(-100, -30);
  1469.        
  1470.         // set original scale
  1471.         item.transform.localScale = originalScale;
  1472. //      item.transform.localPosition = sectorTransform.localPosition;
  1473.        
  1474.         // coin is button - set target
  1475.         UIButtonMessage buttonMessage = item.GetComponent<UIButtonMessage>();
  1476.         buttonMessage.target = gameObject;
  1477.     }
  1478.    
  1479.     public void ClickToLoot(GameObject sender) {
  1480.         UISprite sprite = sender.GetComponent<UISprite>();
  1481.  
  1482.        
  1483.         Vector3 pos = sender.transform.localPosition;
  1484.         pos.y += 800;
  1485.        
  1486.         TweenPosition tween = TweenPosition.Begin(sender, 0.5f, pos);
  1487.         tween.onFinished = FinishLootMoving;
  1488.  
  1489.         // TODO: play sound
  1490.     }  
  1491.    
  1492.     private void FinishLootMoving(UITweener tween) {
  1493. //      Debug.Log ("FinishLootMoving " + tween.name);
  1494.         items.Remove(tween.gameObject);
  1495.         NGUITools.Destroy(tween.gameObject);
  1496.     }  
  1497.    
  1498.     public void ShowBattleResultScreen() {
  1499.         BattleResult result = GameManager.Instance.Battle.Result;
  1500.         if (result != null) {
  1501.             // Battle finished = handle result
  1502.             if (result.resultType == BattleResultType.Win) {
  1503.                 ShowVictoryScreen(result);
  1504.             } else {
  1505.                 ShowDefeatScreen(null);
  1506.             }
  1507.         }
  1508.     }
  1509.    
  1510.     private IEnumerator EmulateFinishBattle(){
  1511.         yield return new WaitForSeconds(3);
  1512.         ServerEmulation.EmulateFinishBattle();
  1513.     }
  1514.  
  1515.     // Returns position for tank in given formation and line position.
  1516.     private Vector3 GetTankPosition(int[] formation, int linePos, bool isEnemy) {
  1517.         // TODO: center 4 tanks formation
  1518.         Debug.Log(formation.Length + " " + linePos);
  1519.        
  1520.         int j = formation[linePos];//-1, 0, +1 - line in formation - back, center, front
  1521.         int i = linePos;// 0, 1, 2, 3, 4 - position inside formation.
  1522.         if (formation.Length == 3) {
  1523.             // Center 3 tanks formation.
  1524.             i++;
  1525.         }
  1526.         float x0;
  1527.         float y0;
  1528.         if (isEnemy) {
  1529.             j = -j;
  1530.             x0 = -10;// + MOVING_SPEED_X * MOVING_DURATION;// * (GameManager.Instance.Battle.CurrentTurn - 1);
  1531.             y0 = 210;//145;// + MOVING_SPEED_Y * MOVING_DURATION;// * (GameManager.Instance.Battle.CurrentTurn - 1);
  1532.         } else {
  1533.             x0 = -350;
  1534.             y0 = -30;
  1535.         }
  1536.        
  1537.         float ci =  100;
  1538.         float cj =  50;
  1539.         float k =  3.0f/5.0f;
  1540.        
  1541.         Vector3 beginPos = new Vector3(x0 + ci*i + cj*j, y0 + k*(-ci*i + cj*j), -1);
  1542.         return beginPos;
  1543.     }
  1544.    
  1545.     private void UpdateLabels() {
  1546.         Battle battle = GameManager.Instance.Battle;
  1547.         coinsLabel.text  = battle.Loot.coins.ToString();
  1548.         boxLabel.text = battle.Loot.box.ToString();
  1549.         ironBoxLabel.text = battle.Loot.ironBox.ToString();//battle.CurrentTurn + "/" + battle.TotalTurns;
  1550.         turnLabel.text = battle.CurrentTurn + "/" + battle.TotalTurns;     
  1551.     }
  1552.    
  1553.     private void ShowBattleTurnLabel() {
  1554.         Battle battle = GameManager.Instance.Battle;
  1555.         //TODO: L10n.
  1556.         battleTurnLabel.text = "Battle " + battle.CurrentTurn + "/" + battle.TotalTurns;
  1557.         battleTurnLabel.transform.localPosition = BATTLE_TURN_LABEL_START_POSITION;
  1558.         TweenPosition tween = TweenPosition.Begin(battleTurnLabel.gameObject, 0.5f, Vector3.zero);
  1559.         tween.onFinished = HideBattleTurnLabel;
  1560.         tween.method = UITweener.Method.EaseIn;//BounceIn;
  1561. //      Debug.Log("Location: " + GameManager.Instance.Human.LocationId + " Sector: " + " Turn: " + battle.CurrentTurn + " LocationAction: ");
  1562.         TurnData tmp = ContentManager.Instance.getTurnData(GameManager.Instance.Human.LocationId, battle.SectorID, battle.CurrentTurn, GameManager.Instance.Human.ActionInLocation);
  1563.         if(tmp != null)
  1564.             testLabel.text = tmp.ToString();
  1565.         else
  1566.             testLabel.text = "Null";
  1567.     }
  1568.    
  1569.     private void HideBattleTurnLabel(UITweener t) {
  1570.         // NB. We need to enable tween. There is no delay on next tween otherwise.
  1571.         t.enabled = false;
  1572.        
  1573.         TweenPosition tween = TweenPosition.Begin(battleTurnLabel.gameObject, 0.5f, BATTLE_TURN_LABEL_FINISH_POSITION, 1);
  1574.         tween.onFinished = ActivateButtonsAfterTween;
  1575.         tween.method = UITweener.Method.EaseOut;//BounceOut;
  1576.     }
  1577.    
  1578.     private void ActivateButtonsAfterTween(UITweener tween) {
  1579.         SetButtonsActive(true);
  1580.     }
  1581.  
  1582.     private IEnumerator FinishBattle() {
  1583.         bool waitForLevelup = false;
  1584.         List<ExpReward> levelUps = new List<ExpReward>();
  1585.         if (!_surrender) {
  1586.             foreach (var tank in myTanks)
  1587.                 if (tank.Card.hp > 0)
  1588.                 {
  1589.                     int expReward = 5 + GameManager.Instance.Battle.CurrentTurn * 5;
  1590.     //              expReward = tank.Card.maxExp - tank.Card.exp;// use it for testing cards level up
  1591.                    
  1592.                     ExpReward reward = ServerEmulation.GetLevelUpResult(tank.Card, expReward);
  1593.                     tank.ExpReward(expReward);
  1594.                     if (reward.levelUp != null) {
  1595.                         levelUps.Add(reward);
  1596.                         VisualEffectManager.CreateEffect("LevelUpFX", Vector3.zero, tank.transform);
  1597.                         waitForLevelup = true;
  1598.                     }
  1599.                     reward.Upgraded.CopyStatsTo(tank.Card);
  1600.                     if (waitForLevelup) {
  1601.                         tank.Card.hp = tank.Card.maxHp;
  1602.                         tank.UpdateHPBar();
  1603.                     }
  1604.                 }
  1605.         }
  1606.         if (waitForLevelup) {
  1607.             // wait for level up fx finished
  1608.             yield return new WaitForSeconds(2.0f);
  1609.         }
  1610.        
  1611.         if (levelUps.Count > 0) {
  1612.             CardsLevelUpViewController controller = MainUIController.Instance.CardsLevelUpView.GetComponent<CardsLevelUpViewController>();
  1613.             controller.InitWith(levelUps);
  1614.             DialogsController.AddToQueue(UIState.CardsLevelUp, null);
  1615.         }
  1616.        
  1617.         ServerEmulation.EmulateFinishBattle();
  1618.     }
  1619.    
  1620.     private void DestroyCraters() {
  1621.         Transform t = decorationsContainer.FindChild("Craters");
  1622.         int childCount = t.GetChildCount();
  1623.         for (int i = 0; i < childCount; ++i) {
  1624.             GameObject.Destroy(t.GetChild(i).gameObject);
  1625.         }
  1626.     }
  1627.    
  1628.     private void AirPlaneShadow() {
  1629.         float duration = 2;
  1630.         Vector3 startPosition = new Vector3(-Screen.width/2  - 100 , - Screen.height/2 - 100, -5);
  1631.         VisualEffect airplane = VisualEffectManager.CreateEffect("AirPlaneShadow", startPosition, movingContainer);
  1632.         airplane.duration = duration;
  1633.         Vector3 finishPosition = startPosition;
  1634.         finishPosition.x *= -1;
  1635.         finishPosition.y *= -1;
  1636.         TweenPosition.Begin(airplane.gameObject, duration, finishPosition);
  1637.     }
  1638.    
  1639.     private void DropStar(Transform t, Transform attacker) {
  1640.         Vector3 position = t.localPosition;
  1641.         position.z = -25;
  1642.         VisualEffect effect = VisualEffectManager.CreateEffect("StarIn", position, movingContainer);
  1643.        
  1644.         int rand;
  1645.            
  1646.         rand = GameManager.RandomGenerator.Next(100) - 50;
  1647.         if (rand > 0) {
  1648.             rand += 50;
  1649.         } else {
  1650.             rand -= 50;
  1651.         }
  1652.         position.x -= rand;
  1653.  
  1654.         rand = GameManager.RandomGenerator.Next(100) - 50;
  1655.         if (rand > 0) {
  1656.             rand += 50;
  1657.         } else {
  1658.             rand -= 50;
  1659.         }
  1660.         position.y -= rand;
  1661.        
  1662.         TweenPosition.Begin(effect.gameObject, 1, position);
  1663.        
  1664.         SWFClipFX swf = (SWFClipFX) effect;
  1665.         swf.duration = 3;
  1666.         swf.OnFinished = RemoveStar;
  1667.        
  1668.         effect.Assotiated.Add(attacker);
  1669.     }
  1670.    
  1671.     void RemoveStar(VisualEffect e) {
  1672.         Transform attacker = e.Assotiated[0];
  1673.         e.Assotiated.Clear();
  1674.         Vector3 position = e.transform.localPosition;
  1675.         VisualEffect effect = VisualEffectManager.CreateEffect("StarOut", position, movingContainer);
  1676.         position = attacker.localPosition;
  1677.         position.z = -25;
  1678.         TweenPosition.Begin(effect.gameObject, 1, position);
  1679.     }
  1680.    
  1681.     private void DropWrench(Transform t, Transform attacker) {
  1682.         Debug.Log("!!! WRENCH DROPPED");
  1683.                 //UnityEditor.EditorApplication.isPaused = true;
  1684.         Vector3 position = t.localPosition;
  1685.         position.z = -25;
  1686.         VisualEffect effect = VisualEffectManager.CreateEffect("WrenchIn", position, movingContainer);
  1687.        
  1688.         int rand;
  1689.            
  1690.         rand = GameManager.RandomGenerator.Next(100) - 50;
  1691.         if (rand > 0) {
  1692.             rand += 50;
  1693.         } else {
  1694.             rand -= 50;
  1695.         }
  1696.         position.x -= rand;
  1697.  
  1698.         rand = GameManager.RandomGenerator.Next(100) - 50;
  1699.         if (rand > 0) {
  1700.             rand += 50;
  1701.         } else {
  1702.             rand -= 50;
  1703.         }
  1704.         position.y -= rand;
  1705.        
  1706.         TweenPosition.Begin(effect.gameObject, 1, position);
  1707.        
  1708.         SWFClipFX swf = (SWFClipFX) effect;
  1709.         swf.duration = 3;
  1710.         swf.OnFinished = RemoveWrench;
  1711.        
  1712.         effect.Assotiated.Add(attacker);
  1713.     }
  1714.    
  1715.     void RemoveWrench(VisualEffect e) {
  1716.         Transform attacker = e.Assotiated[0];
  1717.                 //UnityEditor.EditorApplication.isPaused = true;
  1718.         e.Assotiated.Clear();
  1719.         Vector3 position = e.transform.localPosition;
  1720.         VisualEffect effect = VisualEffectManager.CreateEffect("WrenchOut", position, movingContainer);
  1721.         position = attacker.localPosition;
  1722.         position.z = -25;
  1723.         TweenPosition.Begin(effect.gameObject, 1, position);
  1724.     }
  1725.    
  1726. }
RAW Paste Data