private void Update() { if (stateGame == StateGame.Playing) { time += Time.deltaTime; if (time >= GameConfig.Instance.Timeday) { dateGame = dateGame + 1; SetDateUI(); this.PostEvent(EventID.NextDay); time = 0; } if (isBeingAttack && dateGame >= dateEnemyAttack && !isAttacking && lsGoldMinePlayer.Count > 0) { if (lsEnemyAttackGoldMine.Count <= 0) { int a = UnityEngine.Random.Range(0, lsGoldMineEnemy.Count); lsGoldMineEnemy[a].AttackPlayer(); dateEnemyAttack = dateGame + (long)(GameConfig.Instance.TimeDestroy / GameConfig.Instance.Timeday); } } if (dateGame >= dateUpGoldMine && lsGoldMineEnemy.Count > 0) { int landUpMax = (lsGoldMineEnemy.Count / GameConfig.Instance.LandDiv); if (landUpMax < 1) { landUpMax = 1; } int landUp; if (landUpMax >= GameConfig.Instance.LandUP) { landUp = UnityEngine.Random.Range(GameConfig.Instance.LandUP, landUpMax); } else { landUp = UnityEngine.Random.Range(landUpMax, GameConfig.Instance.LandUP); } int numberGoldMine = 0; if (numberGoldMine < landUp) { for (int i = 0; i < lsGoldMineEnemy.Count; i++) { if (UnityEngine.Random.Range(0f, 1f) >= 0.5f) { lsGoldMineEnemy[i].AddLevel(); numberGoldMine++; } } } dateUpGoldMine = dateGame + (long)(GameConfig.Instance.TimeUp); } if (lsGoldMineEnemy.Count <= 0) { stateGame = StateGame.Finished; UIManager.Instance.panelVictory.SetActive(true); } if (lsGoldMinePlayer.Count <= 0) { stateGame = StateGame.Finished; UIManager.Instance.panelGameOver.SetActive(true); } if (isAttacking) { if (isAttackGoldMineEnemy) { if (itemSelectHero != null) { if (Input.GetMouseButtonUp(0) && !UIManager.Instance.panelLetGo.activeSelf) { Vector3 posIns = UIManager.Instance.cameraAttack.ScreenToWorldPoint(Input.mousePosition); posIns.z = 0f; if (posIns.y > 0) { posIns.y = 0; } if (posIns.y < lsPosHero[0].position.y) { posIns.y = lsPosHero[0].position.y; } ThrowHero(itemSelectHero.houseHero, itemSelectHero.countHero, posIns); itemSelectHero.gameObject.SetActive(false); itemSelectHero = null; numberThrowHero--; } } else { if (Input.GetMouseButtonUp(0) && !UIManager.Instance.panelLetGo.activeSelf) { if (UIManager.Instance.lsItemHeroAttack[0].gameObject.activeSelf || UIManager.Instance.lsItemHeroAttack[1].gameObject.activeSelf || UIManager.Instance.lsItemHeroAttack[2].gameObject.activeSelf) { Vector3 posIns = UIManager.Instance.cameraAttack.ScreenToWorldPoint(Input.mousePosition); posIns.z = 0f; if (posIns.y > 0) { posIns.y = 0; } if (posIns.y < lsPosHero[0].position.y) { posIns.y = lsPosHero[0].position.y; } ItemHeroAttack item; if (UIManager.Instance.lsItemHeroAttack[0].gameObject.activeSelf) { item = UIManager.Instance.lsItemHeroAttack[0]; } else if (UIManager.Instance.lsItemHeroAttack[1].gameObject.activeSelf) { item = UIManager.Instance.lsItemHeroAttack[1]; } else { item = UIManager.Instance.lsItemHeroAttack[2]; } ThrowHero(item.houseHero, item.countHero, posIns); item.gameObject.SetActive(false); itemSelectHero = null; numberThrowHero--; } } } if (lsEnemy.Count <= 0) { EndAttack(); GolEnemyBeingAttack.DeleteHero(); dateEnemyAttack = dateGame + (long)(GameConfig.Instance.TimeDestroy / GameConfig.Instance.Timeday); ScenesManager.Instance.GoToScene(1, () => { GolEnemyBeingAttack.info.typeGoleMine = TypeGoldMine.Player; GolEnemyBeingAttack.SetSpriteBox(0); lsGoldMineEnemy.Remove(GolEnemyBeingAttack); lsGoldMinePlayer.Add(GolEnemyBeingAttack); if (lsEnemyAttackGoldMine.Count > 0) { for (int i = 0; i < lsEnemyAttackGoldMine.Count; i++) { lsEnemyAttackGoldMine[i].isPause = false; } } }, () => { UIManager.Instance.panelWarring.SetActive(true); UIManager.Instance.detailWarring.GetWarring(3, "You have conquered " + GolEnemyBeingAttack.info.name); }); } else if (lsHero.Count <= 0 && numberThrowHero <= 0) { EndAttack(); if (goldMineInSide != null && GolEnemyBeingAttack != null && goldMineInSide.info.ID == GolEnemyBeingAttack.info.ID) { if (lsGoldMinePlayer.Count > 0) { int idGoldMineCheck = 0; float disGoldPlayer = Vector3.Distance(lsGoldMinePlayer[0].transform.position, GolEnemyBeingAttack.transform.position); for (int i = 1; i < lsGoldMinePlayer.Count; i++) { if (disGoldPlayer > Vector3.Distance(lsGoldMinePlayer[i].transform.position, GolEnemyBeingAttack.transform.position)) { idGoldMineCheck = i; } } castlePlayer.transform.localPosition = lsGoldMinePlayer[idGoldMineCheck].transform.position; castlePlayer.posMove = lsGoldMinePlayer[idGoldMineCheck].transform.position; } } else { Vector3 posGoldMine = Vector3.zero; posGoldMine = GolEnemyBeingAttack.transform.position; Vector3 posMoveEnd = posTriggerGoldMine - (posGoldMine - posTriggerGoldMine).normalized; castlePlayer.transform.localPosition = posMoveEnd; castlePlayer.posMove = posMoveEnd; } ScenesManager.Instance.GoToScene(1, () => { if (lsEnemyAttackGoldMine.Count > 0) { for (int i = 0; i < lsEnemyAttackGoldMine.Count; i++) { lsEnemyAttackGoldMine[i].isPause = false; } } }, () => { UIManager.Instance.panelWarring.SetActive(true); UIManager.Instance.detailWarring.GetWarring(4, "You lose"); }); } } else { if (lsHero.Count <= 0) { EndAttack(); GolHeroBeingAttack.DeleteHero(); if (isBreak) { GolHeroBeingAttack.info.typeGoleMine = TypeGoldMine.Enemy; GolHeroBeingAttack.SetSpriteBox(maxLevelHouse); lsGoldMinePlayer.Remove(GolHeroBeingAttack); lsGoldMineEnemy.Add(GolHeroBeingAttack); for (int i = 0; i < lsEnemyAttackGoldMine.Count; i++) { GolHeroBeingAttack.InstantiateEnemy(lsEnemyAttackGoldMine[i].infoHero.ID , lsEnemyAttackGoldMine[i].infoHero.countHero, i); } foreach (Hero h in lsEnemyAttackGoldMine) { Destroy(h.gameObject); } lsEnemyAttackGoldMine.Clear(); isBreak = false; } else { ScenesManager.Instance.GoToScene(1, () => { GolHeroBeingAttack.info.typeGoleMine = TypeGoldMine.Enemy; GolHeroBeingAttack.SetSpriteBox(maxLevelHouse); lsGoldMinePlayer.Remove(GolHeroBeingAttack); lsGoldMineEnemy.Add(GolHeroBeingAttack); for (int i = 0; i < lsEnemyAttackGoldMine.Count; i++) { GolHeroBeingAttack.InstantiateEnemy(lsEnemyAttackGoldMine[i].infoHero.ID , lsEnemyAttackGoldMine[i].infoHero.countHero, i); } foreach (Hero h in lsEnemyAttackGoldMine) { Destroy(h.gameObject); } lsEnemyAttackGoldMine.Clear(); }, () => { UIManager.Instance.panelWarring.SetActive(true); UIManager.Instance.detailWarring.GetWarring(1, GolHeroBeingAttack.info.name + " is invaded"); }); } } else if (lsEnemy.Count <= 0) { EndAttack(); ScenesManager.Instance.GoToScene(1, () => { foreach (Hero h in lsEnemyAttackGoldMine) { Destroy(h.gameObject); } lsEnemyAttackGoldMine.Clear(); }, () => { UIManager.Instance.panelWarring.SetActive(true); UIManager.Instance.detailWarring.GetWarring(2, GolHeroBeingAttack.info.name + " is protected successfully"); }); } } } } else if (stateGame == StateGame.Finished) { if (Input.GetMouseButtonDown(0) && (UIManager.Instance.panelGameOver.activeSelf || UIManager.Instance.panelVictory.activeSelf)) { UIManager.Instance.panelGameOver.SetActive(false); UIManager.Instance.panelVictory.SetActive(false); } } }
public void AttackGoldMineEnemy() { BeginAttack(); UIManager.Instance.panelReleaseAttack.SetActive(true); GameManager.Instance.numberThrowHero = 3; for (int i = 0; i < lsHeroGoldMine.Count && i < 3; i++) { if (lsHeroGoldMine[i].infoHero.countHero > 0) { if (lsHeroGoldMine[i].infoHero.ID != 2 && lsHeroGoldMine[i].infoHero.ID != 1) { Hero hero = Instantiate(GameManager.Instance.lsPrefabsEnemy[lsHeroGoldMine[i].infoHero.ID - 1], GameManager.Instance.lsPosEnemy[i]); hero.gameObject.name = "Enemy"; hero.SetInfoHero(); hero.infoHero.capWar = 0; hero.countHeroStart = lsHeroGoldMine[i].infoHero.countHero; hero.AddHero(lsHeroGoldMine[i].infoHero.countHero); hero.goldMine = this; hero.idGoldMine = i; GameManager.Instance.lsEnemy.Add(hero); } else if (lsHeroGoldMine[i].infoHero.ID == 2) { float XADD = -0.5f; for (int j = 0; j < 3; j++) { Hero hero = Instantiate(GameManager.Instance.lsPrefabsEnemy[1], GameManager.Instance.lsPosEnemy[i]); if (j == 1) { hero.transform.position += new Vector3(0, -0.5f, 0f); } else { hero.transform.position += new Vector3(XADD, 0f, 0f); } XADD += 0.5f; hero.gameObject.name = "Enemy"; hero.SetInfoHero(); hero.infoHero.capWar = 0; hero.AddHero(lsHeroGoldMine[i].infoHero.countHero / 3); hero.goldMine = this; hero.idGoldMine = i; GameManager.Instance.lsEnemy.Add(hero); } } else if (lsHeroGoldMine[i].infoHero.ID == 1) { float XADD = -0.5f; for (int j = 0; j < 4; j++) { Hero hero = Instantiate(GameManager.Instance.lsPrefabsEnemy[0], GameManager.Instance.lsPosEnemy[i]); if (j == 1) { hero.transform.position += new Vector3(0, -0.5f, 0f); } else if (j == 3) { hero.transform.position += new Vector3(0, 0.5f, 0f); } else { hero.transform.position += new Vector3(XADD, 0f, 0f); } XADD += 0.5f; hero.gameObject.name = "Enemy"; hero.SetInfoHero(); hero.infoHero.capWar = 0; hero.AddHero(lsHeroGoldMine[i].infoHero.countHero / 4); hero.goldMine = this; hero.idGoldMine = i; GameManager.Instance.lsEnemy.Add(hero); } } } } int countHero = 0; Castle castle = GameManager.Instance.castlePlayer; for (int i = 0; i < 3; i++) { ItemHeroAttack item = UIManager.Instance.lsItemHeroAttack[i]; if (i < castle.lsHouseRelease.Count && castle.lsHouseRelease[i].info.countHero > 0) { countHero++; item.gameObject.SetActive(true); item.countHero = castle.lsHouseRelease[i].info.countHero; item.houseHero = castle.lsHouseRelease[i]; item.iconHero.sprite = UIManager.Instance.lsSprAvatarHero[castle.lsHouseRelease[i].info.idHero - 1]; item.txtCountHero.text = UIManager.Instance.lsItemHeroAttack[i].countHero.ToString(); } else { item.gameObject.SetActive(false); } } GameManager.Instance.numberThrowHero = countHero; }