Guest User

GameController

a guest
Jan 3rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 25.55 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. using System;
  7. using System.Xml;
  8. using System.Text;
  9. using System.IO;
  10.  
  11. #if UNITY_ANDROID
  12. using UnityEngine.Advertisements;
  13. #endif
  14.  
  15.  
  16.  
  17.  
  18. public class GameController : MonoBehaviour {
  19.    
  20.  
  21.     Touch touch;
  22.     float TouchTime;
  23.  
  24.     //Tiles
  25.     public LayerMask TileLayer;
  26.     public GameObject TilePrefab;
  27.  
  28.  
  29.     //public TileObject[] Tiles;
  30.     public Tile SelectedTile;
  31.     public int TilesActive = 0;
  32.  
  33.     public UIController UIController;
  34.     public Ressources Ressources;
  35.     public XMLManager XMLMannager;
  36.     public Buildings Buildings;
  37.     public RelativeAnimator RAnimator;
  38.  
  39.     public float Malus;
  40.  
  41.     public int SaveTimer;
  42.     public int defaultSaveTimer;
  43.     int Timer;
  44.  
  45.     public float AwayMoney;
  46.  
  47.     public bool OnMenu = false;
  48.     [Header("AllTiles")]
  49.     public List<TileObject> Tiles;
  50.  
  51.  
  52.  
  53.     public bool EmploisAdvert;
  54.     public bool MarchAdvert;
  55.     public bool TerrainValAdvert;
  56.  
  57.  
  58.     //Var utils
  59.     [Header("Variable")]
  60.     public float Money;
  61.     public float Population;
  62.     public float TerrainVal;
  63.     public float PopulationMax;
  64.     public float PopulationAuto;
  65.     public float SurplusElectrique;
  66.     public float SurplusHydrolique;
  67.     public float Pollution;
  68.     public float Emplois;
  69.     public float RisqueFeu;
  70.     public float RisqueCrime;
  71.     public float Marchandise;
  72.  
  73.     //Default var utils
  74.     [Header("Variable valeur par défaut")]
  75.     public float TilePrice;
  76.     public float MoneyDefault;
  77.     public float TerrainValDefault;
  78.     public float PopulationDefault;
  79.     public float PopulationMaxDefault;
  80.     public float EmploisDefault;
  81.     public float SurplusElectricDefault;
  82.     public float SurplusHydrolicDefault;
  83.     public float MarchandiseDefault;
  84.  
  85.  
  86.     private bool upgrade;
  87.     public float upgradePrice;
  88.     public bool loading;
  89.  
  90.  
  91.     // Use this for initialization
  92.     void Start () {
  93.         loading = false;
  94.         upgrade = false;
  95.         Tiles = new List<TileObject>();
  96.         Tiles.Capacity = 99999;
  97.         Buildings = this.gameObject.GetComponent<Buildings> ();
  98.         GameObject TilesFolder = GameObject.Find ("Tiles");
  99.         UIController = GameObject.Find ("Hud").GetComponent<UIController> ();
  100.         XMLMannager = this.gameObject.GetComponent<XMLManager> ();
  101.         Ressources = this.gameObject.GetComponent<Ressources> ();
  102.         Ressources.Start ();
  103.         XMLMannager.Start ();
  104.         if (PlayerPrefs.HasKey ("Money") && XMLMannager.CheckDoc() == true) {
  105.             LoadSave ();
  106.         } else {
  107.             XMLMannager.CreateSave ();
  108.  
  109.             GameObject obj = Instantiate (TilePrefab, Vector3.zero, Quaternion.identity, TilesFolder.transform) as GameObject;
  110.             Money = MoneyDefault;
  111.             TerrainVal = TerrainValDefault;
  112.             Population = PopulationDefault;
  113.             PopulationMax = PopulationMaxDefault;
  114.             SaveTimer = defaultSaveTimer;  
  115.             Emplois = EmploisDefault;
  116.             Marchandise = MarchandiseDefault;
  117.  
  118.  
  119.         }
  120.         if (PlayerPrefs.HasKey ("oT")) {
  121.             StartCoroutine (AwayAutoTimer ());
  122.         }
  123.         Timer = 0;
  124.         SaveTimer = 10;
  125.  
  126.  
  127.  
  128.  
  129.  
  130.         StartCoroutine (refresh ());
  131.  
  132.    
  133.     }
  134.    
  135.     // Update is called once per frame
  136.     void Update () {
  137.         RaycastHit hit;
  138.  
  139.         Ray ray;
  140.    
  141.         Physics.Raycast (new Vector3(0,0,0),new Vector3(0,0,0), out hit);
  142.  
  143.         if (Input.touchCount > 0) {
  144.             touch = Input.GetTouch (0);
  145.             if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended) {
  146.                 TouchTime = Time.time;
  147.             }
  148.         }
  149.            
  150.  
  151.         if (Input.touchCount > 0) {
  152.             if (touch.phase == TouchPhase.Stationary) {
  153.                 if (Time.time - TouchTime >= 0.3f) {
  154.                     if (Input.GetTouch (0).position.y > (0.15 * Screen.height) && Input.GetTouch (0).position.y < (0.83 * Screen.height)) {
  155.  
  156.                         if (OnMenu == false) {
  157.                             ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
  158.                             Physics.Raycast (ray, out hit);
  159.                         }
  160.                     }
  161.                 }
  162.             }
  163.         }
  164.  
  165.  
  166.         if (Application.isMobilePlatform == false && Input.touchCount <= 0) {
  167.             if (Input.GetMouseButton (0) || Input.GetMouseButton (1) || Input.GetMouseButton (2)) {
  168.                 if (Input.mousePosition.y > (0.15 * Screen.height) && Input.mousePosition.y < (0.83 * Screen.height)) {
  169.                     if (OnMenu == false) {
  170.                         ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  171.                         Physics.Raycast (ray, out hit);
  172.                     }
  173.                 }
  174.             }
  175.         }
  176.            
  177.         if (hit.transform != null && hit.transform.tag == "Tile") {
  178.            
  179.                 GameObject obj = hit.transform.gameObject;
  180.                 if (obj.tag == "Tile" && OnMenu != true) {
  181.                 if (obj.transform != null)
  182.                     if (obj.GetComponent<Tile> () != null) {
  183.                         obj.GetComponent<Tile> ().Select ();
  184.                     }
  185.                 }
  186.         }
  187.            
  188.         if (Input.GetKeyDown (KeyCode.LeftControl) && Input.GetKeyDown (KeyCode.S)) {
  189.             Save ();
  190.         }
  191.     }
  192.     public void LoadSave(){
  193.         if (PlayerPrefs.HasKey ("Money")) {
  194.             Money = PlayerPrefs.GetFloat ("Money");
  195.             XMLMannager.TileLoad ();
  196.         }
  197.         if (PlayerPrefs.HasKey ("TerrainVal"))
  198.             TerrainVal = PlayerPrefs.GetFloat ("TerrainVal");
  199.  
  200.  
  201.         if (PlayerPrefs.HasKey ("Population"))
  202.             Population = PlayerPrefs.GetFloat ("Population");
  203.  
  204.  
  205.         if (PlayerPrefs.HasKey ("PopulationMax"))
  206.             PopulationMax = PlayerPrefs.GetFloat ("PopulationMax");
  207.  
  208.  
  209.         if (PlayerPrefs.HasKey ("TilesActive"))
  210.             TilesActive = PlayerPrefs.GetInt ("TilesActive");
  211.  
  212.         if (PlayerPrefs.HasKey ("SaveTimer"))
  213.             SaveTimer = PlayerPrefs.GetInt ("SaveTimer");
  214.  
  215.     }
  216.  
  217.  
  218.  
  219.     public IEnumerator refresh() {
  220.         yield return new WaitForSeconds (1);
  221.         Malus = 1;
  222.  
  223.         if (Population < PopulationMax) {
  224.             Population = Population + PopulationAuto;
  225.         }
  226.  
  227.         if (Emplois <= 0 && Emplois > -(0.05f * PopulationMax)) {
  228.             if (EmploisAdvert == false) {
  229.                 UIController.KnobInfoDisplay ("Il n'y a plus d'emploi disponible, vous devez créer de nouvelles entreprise comme des commerces, ou des services publiques.");
  230.                 EmploisAdvert = true;
  231.             }
  232.         } else if (Emplois <= -(0.05f * PopulationMax)) {
  233.             UIController.KnobAlertDisplay ("Il n'y a plus d'emploi disponible, vous devez créer de nouvelles entreprise comme des commerces, ou des services publiques.");
  234.             StartCoroutine (UIController.LittleAlertDisplay ("Crise du chômage !!", Color.red));
  235.             Malus = 0.5f;
  236.         } else {
  237.             EmploisAdvert = false;
  238.         }
  239.  
  240.         if (Marchandise < 0 && Marchandise > -(0.03f * PopulationMax + MarchandiseDefault)) {
  241.             UIController.KnobInfoDisplay ("Les commerçants n'ont plus de marchandise à vendre, vous devez créer des industries.");
  242.         } else if (Marchandise <= -(0.03f * PopulationMax + MarchandiseDefault)) {
  243.             UIController.KnobAlertDisplay ("Les commerçants n'ont plus de marchandise à vendre, vous devez créer des industries.");
  244.             StartCoroutine (UIController.LittleAlertDisplay ("Crise de marchandise !!", Color.red));
  245.             Malus = 0.5f;
  246.         }  
  247.  
  248.         if (Marchandise < (1f * PopulationMax) && Marchandise > (1.25f * PopulationMax + MarchandiseDefault)) {
  249.             UIController.KnobInfoDisplay ("Les commerçants n'arrive pas a écoulé toute les marchandises. Construisez plus de commerces");
  250.         } else if (Marchandise >= (1.5f * PopulationMax + MarchandiseDefault)) {
  251.             UIController.KnobAlertDisplay ("Les commerçants n'arrive pas a écoulé toute les marchandises. Construisez plus de commerces");
  252.             StartCoroutine (UIController.LittleAlertDisplay ("Surplus de marchandise !!", Color.red));
  253.             Malus = 0.5f;
  254.         }
  255.  
  256.         if (SurplusElectrique < 0 && SurplusElectrique > - 100) {
  257.             UIController.KnobInfoDisplay ("Vos habitants subissent des coupures électrique. Vous devez améliorer votre réseau électrique");
  258.         } else if (SurplusElectrique <= -100) {
  259.             UIController.KnobAlertDisplay ("Vos habitants subissent des coupures électrique. Vous devez améliorer votre réseau électrique");
  260.             StartCoroutine (UIController.LittleAlertDisplay ("Pas d'éléctricité!!", Color.red));
  261.             Malus = 0.5f;
  262.         }
  263.  
  264.         if (SurplusHydrolique < 0 && SurplusHydrolique > - 100) {
  265.             UIController.KnobInfoDisplay ("Vos habitants subissent des coupures d'eau. Vous devez améliorer votre réseau hydrolique");
  266.         } else if (SurplusHydrolique <= -100) {
  267.             UIController.KnobAlertDisplay ("Vos habitants subissent des coupures d'eau. Vous devez améliorer votre réseau hydrolique");
  268.             StartCoroutine (UIController.LittleAlertDisplay ("Pas d'eau!!", Color.red));
  269.             Malus = 0.5f;
  270.         }
  271.  
  272.         int randomEvent = UnityEngine.Random.Range (0, 75000);
  273.         if (randomEvent <= Pollution) {
  274.             float popLost = UnityEngine.Random.Range (0, 0.4f * (float)(Population));
  275.             popLost = (float)Mathf.RoundToInt (popLost);
  276.             Population = Population - popLost;
  277.  
  278.             string sMaladie = "La pollution a rendu malade et a tué " + UIController.Suffixer(popLost) + " habitants. Vous devez créer des espaces verts";
  279.             UIController.KnobAlertDisplay (sMaladie);
  280.         }else if (randomEvent <= RisqueCrime) {
  281.             float moneyLost = UnityEngine.Random.Range (0, 0.5f * (float)(Money));
  282.             moneyLost = (float)Mathf.RoundToInt (moneyLost);
  283.             float popLost = UnityEngine.Random.Range (0, 0.2f * (float)(Population));
  284.             popLost = (float)Mathf.RoundToInt (popLost);
  285.             Money = Money - moneyLost;
  286.             Population = Population - popLost;
  287.  
  288.             string sCrime = "Un crime a eu lieu dans votre ville, vous avez perdu " + UIController.Suffixer(moneyLost) + " dollars et " + UIController.Suffixer(popLost) + " habitants. Construisez des commissariat.";
  289.             UIController.KnobAlertDisplay (sCrime);
  290.         }else if (randomEvent <= RisqueFeu) {
  291.  
  292.             TileObject BurnTile = Tiles [UnityEngine.Random.Range (0, Tiles.Count)];
  293.             while (BurnTile.statut != "Build") {
  294.                 BurnTile = Tiles [UnityEngine.Random.Range (0, Tiles.Count)];
  295.             }
  296.             SelectedTile = GameObject.Find(BurnTile.name).GetComponent<Tile>();
  297.             Destroy (SelectedTile.transform.GetChild (0).gameObject);
  298.             BuildInterface ("Ruines");
  299.             string sBat = "Votre " + BurnTile.builded.Name + " a pris feu et il ne reste que des cendres, pensez à construire des casernes de pompier.";
  300.  
  301.             UIController.KnobAlertDisplay (sBat);
  302.         }
  303.  
  304.         Money = Money + Population * (TerrainVal * Malus);;
  305.  
  306.  
  307.         UIController.UpdateUi ();
  308.         //TEMP
  309.         if (Timer == SaveTimer) {
  310.             Save ();
  311.             Timer = 0;
  312.         }
  313.         Timer++;
  314.         StartCoroutine (refresh ());
  315.     }
  316.  
  317.     public void AddPeople(){
  318.         if (Population +1 <= PopulationMax)
  319.         Population++;
  320.     }
  321.  
  322.     public void SelectTile(Tile tile){
  323.         SelectedTile = tile;
  324.     }
  325.  
  326.     public void TileAction(string action){
  327.         if (action == "Expand") {
  328.             if (Money >= TilePrice * ((TilesActive * 3f * TilesActive) + 1)) {
  329.                 SelectedTile.Expand ();
  330.                 Money = Money - TilePrice * ((TilesActive * 3f * TilesActive) + 1);
  331.                 UIController.CloseMenu ();
  332.                 TilesActive++;
  333.             } else {
  334.                 StartCoroutine (UIController.LittleAlertDisplay ("Argent insuffisant", Color.red));
  335.             }
  336.         } else if (action == "Upgrade") {
  337.             if (SelectedTile.Builded.nextIsEcological == true) {
  338.                 if (tryUpgrade (2) == true) {
  339.                     BuildingObject UpgradeObj = null;
  340.                     if (SelectedTile.Type == "Residentiel") {
  341.                         UpgradeObj = Buildings.Residentiels [SelectedTile.Builded.PosInList + 2];
  342.                     } else if (SelectedTile.Type == "Commercial") {
  343.                         UpgradeObj = Buildings.Commercials [SelectedTile.Builded.PosInList + 2];
  344.                     } else if (SelectedTile.Type == "Industriel") {
  345.                         UpgradeObj = Buildings.Industriels [SelectedTile.Builded.PosInList + 2];
  346.                     } else if (SelectedTile.Type == "Medical") {
  347.                         UpgradeObj = Buildings.Healths [SelectedTile.Builded.PosInList + 2];
  348.                     } else if (SelectedTile.Type == "Parcs") {
  349.                         UpgradeObj = Buildings.Parks [SelectedTile.Builded.PosInList + 2];
  350.                     } else if (SelectedTile.Type == "Education") {
  351.                         UpgradeObj = Buildings.Schools [SelectedTile.Builded.PosInList + 2];
  352.                     } else if (SelectedTile.Type == "Police") {
  353.                         UpgradeObj = Buildings.Polices [SelectedTile.Builded.PosInList + 2];
  354.                     } else if (SelectedTile.Type == "Pompier") {
  355.                         UpgradeObj = Buildings.Pompiers [SelectedTile.Builded.PosInList + 2];
  356.                     }
  357.  
  358.                     upgrade = true;
  359.                     Build (UpgradeObj);
  360.                 }
  361.             } else {
  362.                 if (tryUpgrade (1) == true) {
  363.                     BuildingObject UpgradeObj = null;
  364.                     if (SelectedTile.Type == "Residentiel") {
  365.                         UpgradeObj = Buildings.Residentiels [SelectedTile.Builded.PosInList + 1];
  366.                     } else if (SelectedTile.Type == "Commercial") {
  367.                         UpgradeObj = Buildings.Commercials [SelectedTile.Builded.PosInList + 1];
  368.                     } else if (SelectedTile.Type == "Industriel") {
  369.                         UpgradeObj = Buildings.Industriels [SelectedTile.Builded.PosInList + 1];
  370.                     } else if (SelectedTile.Type == "Medical") {
  371.                         UpgradeObj = Buildings.Healths [SelectedTile.Builded.PosInList + 1];
  372.                     } else if (SelectedTile.Type == "Parcs") {
  373.                         UpgradeObj = Buildings.Parks [SelectedTile.Builded.PosInList + 1];
  374.                     } else if (SelectedTile.Type == "Education") {
  375.                         UpgradeObj = Buildings.Schools [SelectedTile.Builded.PosInList + 1];
  376.                     } else if (SelectedTile.Type == "Police") {
  377.                         UpgradeObj = Buildings.Polices [SelectedTile.Builded.PosInList + 1];
  378.                     } else if (SelectedTile.Type == "Pompier") {
  379.                         UpgradeObj = Buildings.Pompiers [SelectedTile.Builded.PosInList + 1];
  380.                     }
  381.                    
  382.                     upgrade = true;
  383.                     Build (UpgradeObj);
  384.                 }
  385.             }
  386.  
  387.         } else if (action == "UpgradeEco") {
  388.             if (tryUpgrade (1) == true) {
  389.                 BuildingObject UpgradeObj = null;
  390.                 if (SelectedTile.Type == "Residentiel") {
  391.                     UpgradeObj = Buildings.Residentiels [SelectedTile.Builded.PosInList + 1];
  392.                 } else if (SelectedTile.Type == "Commercial") {
  393.                     UpgradeObj = Buildings.Commercials [SelectedTile.Builded.PosInList + 1];
  394.                 } else if (SelectedTile.Type == "Industriel") {
  395.                     UpgradeObj = Buildings.Industriels [SelectedTile.Builded.PosInList + 1];
  396.                 } else if (SelectedTile.Type == "Medical") {
  397.                     UpgradeObj = Buildings.Healths [SelectedTile.Builded.PosInList + 1];
  398.                 } else if (SelectedTile.Type == "Parcs") {
  399.                     UpgradeObj = Buildings.Parks [SelectedTile.Builded.PosInList + 1];
  400.                 } else if (SelectedTile.Type == "Education") {
  401.                     UpgradeObj = Buildings.Schools [SelectedTile.Builded.PosInList + 1];
  402.                 } else if (SelectedTile.Type == "Police") {
  403.                     UpgradeObj = Buildings.Polices [SelectedTile.Builded.PosInList + 1];
  404.                 } else if (SelectedTile.Type == "Pompier") {
  405.                     UpgradeObj = Buildings.Pompiers [SelectedTile.Builded.PosInList + 1];
  406.                 }
  407.  
  408.                 upgrade = true;
  409.                 Build (UpgradeObj);
  410.             }
  411.         }
  412.     }
  413.  
  414.  
  415.     public bool tryUpgrade(int indice){
  416.         upgradePrice = 0;
  417.  
  418.         if (SelectedTile.Type == "Residentiel") {
  419.             if (Buildings.Residentiels.Count - 1 >= SelectedTile.Builded.PosInList + indice) {
  420.                 BuildingObject UpgradeObj = Buildings.Residentiels [SelectedTile.Builded.PosInList + indice];
  421.                 //if (Money >= UpgradeObj.Price - SelectedTile.Builded.Price) {
  422.                 if (Money >= UpgradeObj.Price/2) {
  423.                     //upgradePrice = UpgradeObj.Price - SelectedTile.Builded.Price;
  424.                     upgradePrice = UpgradeObj.Price/2;
  425.                     return true;
  426.                 } else {
  427.                     upgradePrice = UpgradeObj.Price/2;
  428.                     return false;
  429.                     StartCoroutine (UIController.LittleAlertDisplay ("Argent insuffisant", Color.red));
  430.                 }
  431.  
  432.             } else {
  433.                 return false;
  434.                 StartCoroutine (UIController.LittleAlertDisplay ("Pas d'amélioration disponible", Color.yellow));
  435.             }
  436.         }else if(SelectedTile.Type == "Commercial"){
  437.             if (Buildings.Commercials.Count - 1 >= SelectedTile.Builded.PosInList + indice) {
  438.                 BuildingObject UpgradeObj = Buildings.Commercials [SelectedTile.Builded.PosInList + indice];
  439.                 if (Money >= UpgradeObj.Price - SelectedTile.Builded.Price) {
  440.                     upgradePrice = UpgradeObj.Price - SelectedTile.Builded.Price;
  441.                     return true;
  442.                 } else {
  443.                     upgradePrice = UpgradeObj.Price/2;
  444.                     return false;
  445.                     StartCoroutine (UIController.LittleAlertDisplay ("Argent insuffisant", Color.red));
  446.                 }
  447.  
  448.             } else {
  449.                 StartCoroutine (UIController.LittleAlertDisplay ("Pas d'amélioration disponible", Color.yellow));
  450.                 return false;
  451.  
  452.             }
  453.  
  454.         }else if(SelectedTile.Type == "Industriel"){
  455.             if (Buildings.Industriels.Count - 1 >= SelectedTile.Builded.PosInList + indice) {
  456.                 BuildingObject UpgradeObj = Buildings.Industriels [SelectedTile.Builded.PosInList + indice];
  457.                 if (Money >= UpgradeObj.Price - SelectedTile.Builded.Price) {
  458.                     upgradePrice = UpgradeObj.Price - SelectedTile.Builded.Price;
  459.                     return true;
  460.                 } else {
  461.                     upgradePrice = UpgradeObj.Price/2;
  462.                     return false;
  463.                     StartCoroutine (UIController.LittleAlertDisplay ("Argent insuffisant", Color.red));
  464.                 }
  465.  
  466.             } else {
  467.                 StartCoroutine (UIController.LittleAlertDisplay ("Pas d'amélioration disponible", Color.yellow));
  468.                 return false;
  469.  
  470.             }
  471.  
  472.         }else if(SelectedTile.Type == "Medical"){
  473.             if (Buildings.Healths.Count - 1 >= SelectedTile.Builded.PosInList + indice) {
  474.                 BuildingObject UpgradeObj = Buildings.Healths [SelectedTile.Builded.PosInList + indice];
  475.                 if (Money >= UpgradeObj.Price - SelectedTile.Builded.Price) {
  476.                     upgradePrice = UpgradeObj.Price - SelectedTile.Builded.Price;
  477.                     return true;
  478.                 } else {
  479.                     upgradePrice = UpgradeObj.Price/2;
  480.                     StartCoroutine (UIController.LittleAlertDisplay ("Argent insuffisant", Color.red));
  481.                     return false;
  482.  
  483.                 }
  484.  
  485.             } else {
  486.                 StartCoroutine (UIController.LittleAlertDisplay ("Pas d'amélioration disponible", Color.yellow));
  487.                 return false;
  488.  
  489.             }
  490.  
  491.         }else if(SelectedTile.Type == "Education"){
  492.             if (Buildings.Schools.Count - 1 >= SelectedTile.Builded.PosInList + indice) {
  493.                 BuildingObject UpgradeObj = Buildings.Schools [SelectedTile.Builded.PosInList + indice];
  494.                 if (Money >= UpgradeObj.Price - SelectedTile.Builded.Price) {
  495.                     upgradePrice = UpgradeObj.Price - SelectedTile.Builded.Price;
  496.                     return true;
  497.                 } else {
  498.                     upgradePrice = UpgradeObj.Price/2;
  499.                     StartCoroutine (UIController.LittleAlertDisplay ("Argent insuffisant", Color.red));
  500.                     return false;
  501.  
  502.                 }
  503.  
  504.             } else {
  505.                 StartCoroutine (UIController.LittleAlertDisplay ("Pas d'amélioration disponible", Color.yellow));
  506.                 return false;
  507.  
  508.             }
  509.  
  510.         }else if(SelectedTile.Type == "Police"){
  511.             if (Buildings.Polices.Count - 1 >= SelectedTile.Builded.PosInList + indice) {
  512.                 BuildingObject UpgradeObj = Buildings.Polices [SelectedTile.Builded.PosInList + indice];
  513.                 if (Money >= UpgradeObj.Price - SelectedTile.Builded.Price) {
  514.                     upgradePrice = UpgradeObj.Price - SelectedTile.Builded.Price;
  515.                     return true;
  516.                 } else {
  517.                     upgradePrice = UpgradeObj.Price/2;
  518.                     StartCoroutine (UIController.LittleAlertDisplay ("Argent insuffisant", Color.red));
  519.                     return false;
  520.  
  521.                 }
  522.  
  523.             } else {
  524.                 StartCoroutine (UIController.LittleAlertDisplay ("Pas d'amélioration disponible", Color.yellow));
  525.                 return false;
  526.  
  527.             }
  528.  
  529.         }else if(SelectedTile.Type == "Pompier"){
  530.             if (Buildings.Pompiers.Count - 1 >= SelectedTile.Builded.PosInList + indice) {
  531.                 BuildingObject UpgradeObj = Buildings.Pompiers [SelectedTile.Builded.PosInList + indice];
  532.                 if (Money >= UpgradeObj.Price - SelectedTile.Builded.Price) {
  533.                     upgradePrice = UpgradeObj.Price - SelectedTile.Builded.Price;
  534.                     return true;
  535.                 } else {
  536.                     upgradePrice = UpgradeObj.Price/2;
  537.                     StartCoroutine (UIController.LittleAlertDisplay ("Argent insuffisant", Color.red));
  538.                     return false;
  539.  
  540.                 }
  541.  
  542.             } else {
  543.                 StartCoroutine (UIController.LittleAlertDisplay ("Pas d'amélioration disponible", Color.yellow));
  544.                 return false;
  545.  
  546.             }
  547.  
  548.         }else {
  549.             StartCoroutine (UIController.LittleAlertDisplay ("Pas d'amélioration disponible", Color.yellow));
  550.             return false;
  551.         }
  552.     }
  553.  
  554.     public void Save(){
  555.         XMLMannager.Save ();
  556.         PlayerPrefs.SetFloat ("Money", Money);
  557.         PlayerPrefs.SetFloat ("TerrainVal", TerrainVal);
  558.         PlayerPrefs.SetFloat ("Population", Population);
  559.         PlayerPrefs.SetInt ("TilesActive", TilesActive);
  560.         PlayerPrefs.SetInt ("SaveTimer", SaveTimer);
  561.         PlayerPrefs.SetString ("Electrique", Ressources.ElecActual.name);
  562.         PlayerPrefs.SetString ("Hydrolique", Ressources.HydroActual.name);
  563.         PlayerPrefs.Save ();
  564.         UIController.LittleAlertDisplay ("Partie sauvegardé", Color.black);
  565.     }
  566.  
  567.  
  568.     public void BuildInterface(string BuildingName){
  569.         Buildings = this.gameObject.GetComponent<Buildings> ();
  570.         Buildings.Start ();
  571.         if (BuildingName == "Cabane") {
  572.             Build (Buildings.Cabane);
  573.         } else if (BuildingName == "Maisonette") {
  574.             Build (Buildings.Maisonette);
  575.         } else if (BuildingName == "PetiteClinique") {
  576.             Build (Buildings.PetiteClinique);
  577.         } else if (BuildingName == "Ferme") {
  578.             Build (Buildings.Ferme);
  579.         } else if (BuildingName == "Epicerie") {
  580.             Build (Buildings.Epicerie);
  581.         } else if (BuildingName == "Pavillon") {
  582.             Build (Buildings.Pavillon);
  583.         } else if (BuildingName == "Marché") {
  584.             Build (Buildings.Marché);
  585.         } else if (BuildingName == "Duplex") {
  586.             Build (Buildings.Duplex);
  587.         } else if (BuildingName == "Mine") {
  588.             Build (Buildings.Mine);
  589.         } else if (BuildingName == "PetitParc") {
  590.             Build (Buildings.PetitParc);
  591.         } else if (BuildingName == "Clinique") {
  592.             Build (Buildings.Clinique);
  593.         } else if (BuildingName == "Quartier") {
  594.             Build (Buildings.Quartier);
  595.         } else if (BuildingName == "Supérette") {
  596.             Build (Buildings.Supérette);
  597.         } else if (BuildingName == "Entrepot") {
  598.             Build (Buildings.Entrepot);
  599.         } else if (BuildingName == "Usine") {
  600.             Build (Buildings.Usine);
  601.         } else if (BuildingName == "Ruines") {
  602.             Build (Buildings.Ruines);
  603.         } else if (BuildingName == "EcolePrimaire") {
  604.             Build (Buildings.EcolePrimaire);
  605.         } else if (BuildingName == "PosteDePolice") {
  606.             Build (Buildings.PosteDePolice);
  607.         } else if (BuildingName == "PosteDePompier") {
  608.             Build (Buildings.PosteDePompier);
  609.         } else if (BuildingName == "PavillonEco") {
  610.             Build (Buildings.PavillonEco);
  611.         } else if (BuildingName == "College") {
  612.             Build (Buildings.College);
  613.         }
  614.     }
  615.  
  616.  
  617.     public void Build(BuildingObject BuildingObject){
  618.         if (Money >= BuildingObject.Price || upgrade == true) {
  619.            
  620.             if (upgrade == false) {
  621.                 Money = Money - BuildingObject.Price;
  622.             } else {
  623.                 Money = Money - upgradePrice;
  624.             }
  625.             upgrade = false;
  626.             StartCoroutine(RAnimator.BuildTile (SelectedTile.gameObject));
  627.             SelectedTile.TileBuild (BuildingObject);
  628.             UIController.CloseMenu ();
  629.             RefreshBuild ();
  630.         } else if (loading == true) {
  631.             SelectedTile.TileBuild (BuildingObject);
  632.             RefreshBuild ();
  633.         }else {
  634.             StartCoroutine(UIController.LittleAlertDisplay("Argent insuffisant", Color.red));
  635.         }
  636.     }
  637.  
  638.     public void RefreshBuild(){
  639.         PopulationMax = PopulationMaxDefault;
  640.         PopulationAuto = 0;
  641.         Emplois = EmploisDefault;
  642.         SurplusElectrique = Ressources.ElecActual.ressourcesPower;
  643.         SurplusHydrolique = Ressources.HydroActual.ressourcesPower;
  644.         TerrainVal = TerrainValDefault;
  645.         Marchandise = MarchandiseDefault;
  646.         int i = 0;
  647.         while (i != Tiles.Count) {
  648.             BuildingObject BuildingObject = Tiles [i].builded;
  649.             if (Tiles [i].builded != null) {
  650.                 PopulationMax = PopulationMax + BuildingObject.PopMax;
  651.                 PopulationAuto = PopulationAuto + BuildingObject.PopAuto;
  652.                 TerrainVal = TerrainVal + BuildingObject.TerrainVal;
  653.                 SurplusElectrique = SurplusElectrique + BuildingObject.Electric;
  654.                 SurplusHydrolique = SurplusHydrolique + BuildingObject.Hydrolic;
  655.                 Pollution = Pollution + BuildingObject.Pollution;
  656.                 Emplois = Emplois + BuildingObject.Emplois;
  657.                 RisqueFeu = RisqueFeu + BuildingObject.RisqueFeu;
  658.                 RisqueCrime = RisqueCrime + BuildingObject.RisqueCrime;
  659.                 Marchandise = Marchandise + BuildingObject.Marchandise;
  660.             }
  661.             i++;
  662.         }
  663.  
  664.     }
  665.  
  666.  
  667.     public void TileRemove(){
  668.         Destroy (SelectedTile.transform.GetChild (0).gameObject);
  669.         GameObject obj = Instantiate(SelectedTile.ActiveTilePrefab, SelectedTile.transform.position, Quaternion.identity, SelectedTile.transform) as GameObject;
  670.  
  671.         UIController.CloseMenu ();
  672.  
  673.         Money = Money + SelectedTile.Builded.Price;
  674.         SelectedTile.Building = "Active";
  675.         SelectedTile.Builded = null;
  676.         SelectedTile.Statut = "Active";
  677.         SelectedTile.Type = "None";
  678.         RefreshBuild ();
  679.     }
  680.  
  681.  
  682.     DateTime currentTime;
  683.     DateTime oldTime;
  684.     TimeSpan timeAway;
  685.     public float SecondesAway;
  686.  
  687.  
  688.     public void OnApplicationPause(bool paused)
  689.     {
  690.        
  691.         if(paused) // When Paused
  692.         {
  693.             oldTime = DateTime.Now;
  694.             PlayerPrefs.SetInt("oT", (int)oldTime.Ticks);
  695.         }
  696.         else // When Focused
  697.         {
  698.             StartCoroutine (AwayAutoTimer ());
  699.         }
  700.     }
  701.  
  702.     public void AwayUpdate(){
  703.         currentTime = DateTime.Now;
  704.         oldTime.AddTicks( PlayerPrefs.GetInt("oT"));
  705.         PlayerPrefs.DeleteKey ("oT");
  706.  
  707.         timeAway = currentTime.Subtract(oldTime);
  708.         SecondesAway = (float)TimeSpan.FromTicks (timeAway.Ticks).TotalSeconds;
  709.         if (SecondesAway >= 300) {
  710.  
  711.             if (SecondesAway >= 28800)
  712.                 SecondesAway = 28800;
  713.             AwayMoney = SecondesAway * Population * TerrainVal * 0.05f;
  714.             UIController.AwayMenu.SetActive (true);
  715.             GameObject.Find ("AwayMoney").GetComponent<Text> ().text = UIController.Suffixer(AwayMoney);
  716.         }
  717.     }
  718.  
  719.     IEnumerator AwayAutoTimer(){
  720.         yield return new WaitForSeconds (2.5f);
  721.         AwayUpdate ();
  722.  
  723.     }
  724.     #if UNITY_ANDROID
  725.     public void AwayRecolt(bool ads){
  726.         if (ads == true) {
  727.             ShowRewardedAdRecolt ();
  728.         } else {
  729.             Money = Money + AwayMoney;
  730.         }
  731.         UIController.CloseMenu ();
  732.         AwayMoney = 0;
  733.  
  734.     }
  735.  
  736.  
  737.     public void ShowRewardedAdRecolt()
  738.     {
  739.         if (Advertisement.IsReady("rewardedVideo"))
  740.         {
  741.             var options = new ShowOptions { resultCallback = HandleShowResultRecolt };
  742.             Advertisement.Show("rewardedVideo", options);
  743.         }
  744.     }
  745.  
  746.     private void HandleShowResultRecolt(ShowResult result)
  747.     {
  748.         switch (result)
  749.         {
  750.         case ShowResult.Finished:
  751.             //Debug.Log("The ad was successfully shown.");
  752.             Money = Money + (AwayMoney * 2);
  753.             break;
  754.         case ShowResult.Skipped:
  755.             Debug.Log ("The ad was skipped before reaching the end.");
  756.             UIController.LittleAlertDisplay ("Pub passé, pas de bonus", Color.red);
  757.             break;
  758.         case ShowResult.Failed:
  759.             Debug.LogError("The ad failed to be shown.");
  760.             UIController.LittleAlertDisplay ("Pas de pub revenez plus tard", Color.red);
  761.             break;
  762.         }
  763.     }
  764.     #endif
  765. }
Advertisement
Add Comment
Please, Sign In to add comment