public void chooseActionMode(string actionMode) { if (strLastSelectSoldierName != "") { destroyAttackArrow(); } actionModeName = actionMode; if (selectedSoldier != null) { switch (actionModeName) { case "Attack": if (selectedSoldier.GetComponent <Unit>().m_pSoldier.getCurrentMoveStep() > 0) { actionModeName = "Attack"; GameObject newAttackArrow = Instantiate(attackArrow, selectedSoldier.transform.position, Quaternion.identity); newAttackArrow.name = selectedSoldier.name + "_attackArrow"; strLastSelectSoldierName = newAttackArrow.name; m_pUnit.setState(1); } break; case "Defense": actionModeName = "Defense"; int blockAttack = m_pUnit.getCurrentMoveStep(); m_pUnit.setDefense(blockAttack); m_pUnit.setCurrentMoveStep(0); m_pUnit.setState(2); GameObject floatingTextObj = Instantiate(floatingText, selectedSoldier.transform.position, Quaternion.identity); floatingTextObj.GetComponent <TextMesh>().text = "Block " + blockAttack.ToString() + " hit!"; floatingTextObj.GetComponent <TextMesh>().color = Color.white; SoundEffectManager.Instance.playAudio(12); break; case "LevelUP": actionModeName = "Defense"; m_pUnit.setState(3); break; case "Sell": int value = m_pUnit.getMoneyBySellingEntity(); if (GameManager.Instance.getTurn() == 0) { m_player1.setMoney(m_player1.getMoney() + m_pUnit.getMoneyBySellingEntity()); } else { m_player2.setMoney(m_player2.getMoney() + m_pUnit.getMoneyBySellingEntity()); } Destroy(selectedSoldier); m_pUnit.setState(4); Vector2 pos = new Vector2(cameraMain.transform.position.x + 2, cameraMain.transform.position.y - 1); GameObject popText = Instantiate(floatingText, pos, Quaternion.identity); popText.GetComponent <TextMesh>().text = "money+" + value.ToString(); SoundEffectManager.Instance.playAudio(0); break; } } }
// Update is called once per frame void Update() { if (transform.position.x == destination.x && transform.position.y == destination.y) { if (bMoving) { bMoving = false; whenUnitFirstArriveDestination(); } } else { bMoving = true; } Vector2 dir = new Vector2(destination.x - transform.position.x, destination.y - transform.position.y); Vector2 velocity = dir.normalized * speed * Time.deltaTime; velocity = Vector2.ClampMagnitude(velocity, dir.magnitude); transform.Translate(velocity); gameObject.GetComponent <SpriteRenderer>().sprite = UIManager.Instance.unitSprites[spriteIndex]; thisUnitHasBeenClicked = false; if (m_pSoldier != null && m_pSoldier.getCurrentHP() <= 0) { int money = m_pSoldier.getMoneyByKillingEnemy(); int xp = m_pSoldier.getXPByKillingEnemy(); if (unitAlign == 0) { m_player2.setMoney(money + m_player2.getMoney()); m_player2.setXP(xp + m_player2.getXP()); } else if (unitAlign == 1) { m_player1.setMoney(money + m_player1.getMoney()); m_player1.setXP(xp + m_player1.getXP()); } GameManager.Instance.modifyUnitDic(unitAlign, gameObject.name); Destroy(gameObject); } }
// Update is called once per frame void Update() { if (GameManager.Instance == null) { return; } int currentAlign = GameManager.Instance.getTurn(); if (currentAlign == 0) { if (m_Player1 == null) { return; } UIManager.Instance.alignImage.sprite = m_Player1.getAlignImage(); UIManager.Instance.money.text = m_Player1.getMoney().ToString(); UIManager.Instance.level.text = m_Player1.getLevel().ToString(); UIManager.Instance.xp.text = m_Player1.getXP().ToString(); m_Player1.checkLevel(); m_Player1.unlockSoldierTypes(); } else if (currentAlign == 1) { if (m_Player2 == null) { return; } UIManager.Instance.alignImage.sprite = m_Player2.getAlignImage(); UIManager.Instance.money.text = m_Player2.getMoney().ToString(); UIManager.Instance.level.text = m_Player2.getLevel().ToString(); UIManager.Instance.xp.text = m_Player2.getXP().ToString(); m_Player2.checkLevel(); m_Player2.unlockSoldierTypes(); } }
public void instantiateUnit(int nType) { if (GameManager.Instance.getTurn() == 0) { soldierUnit.GetComponent <Unit>().setEntity(nType, 0); if (soldierUnit.GetComponent <Unit>().m_pSoldier.getMoneyToBuyEntity() > m_player1.getMoney()) { return; } else { SoundEffectManager.Instance.playAudio(3); int nAlign = GameManager.Instance.getTurn(); int posXIndex = (GameManager.Instance.width - 1) / 2; int posYIndex = 0; if (nAlign == 0) { posYIndex = 0; } else { posYIndex = GameManager.Instance.height - 1; } Vector2 pos = GameManager.Instance.getTerrainPos(posXIndex, posYIndex); //GameObject pSoldierUnit = soldierUnit; GameObject pSoldierUnit = Instantiate(soldierUnit, pos, Quaternion.identity); //Instantiate(soldierUnit, pos, Quaternion.identity); pSoldierUnit.name = "nType_" + nType + "nAlign_" + nAlign + "Index_" + totalUnitNum; pSoldierUnit.GetComponent <Unit>().setEntity(nType, nAlign); pSoldierUnit.GetComponent <Unit>().indexX = posXIndex; pSoldierUnit.GetComponent <Unit>().indexY = posYIndex; if (nAlign == 1) { pSoldierUnit.GetComponent <SpriteRenderer>().color = new Color(253, 0, 255); } else { pSoldierUnit.GetComponent <SpriteRenderer>().color = new Color(255, 185, 0); } if (GameManager.Instance.getTurn() == 0) { GameManager.Instance.setSoldierObjectDic1(pSoldierUnit.name, pSoldierUnit); } else { GameManager.Instance.setSoldierObjectDic2(pSoldierUnit.name, pSoldierUnit); } GameManager.Instance.setUnitPosXIndex(pSoldierUnit.name, posXIndex); GameManager.Instance.setUnitPosYIndex(pSoldierUnit.name, posYIndex); GameManager.Instance.setSoldierAlign(pSoldierUnit.name, nAlign); GameManager.Instance.setSoldierType(pSoldierUnit.name, nType); //Debug.Log("pSoldierUnit.name: "+ pSoldierUnit.name+ " pSoldierUnit posXIndex: "+ GameManager.Instance.getUnitPosXIndex(pSoldierUnit.name) + " pSoldierUnit posYIndex: " + GameManager.Instance.getUnitPosYIndex(pSoldierUnit.name)); totalUnitNum++; m_player1.modifyMoney(-pSoldierUnit.GetComponent <Unit>().m_pSoldier.getMoneyToBuyEntity()); } } else if (GameManager.Instance.getTurn() == 1) { soldierUnit.GetComponent <Unit>().setEntity(nType, 1); if (soldierUnit.GetComponent <Unit>().m_pSoldier.getMoneyToBuyEntity() > m_player2.getMoney()) { return; } else { SoundEffectManager.Instance.playAudio(3); int nAlign = GameManager.Instance.getTurn(); int posXIndex = (GameManager.Instance.width - 1) / 2; int posYIndex = 0; if (nAlign == 0) { posYIndex = 0; } else { posYIndex = GameManager.Instance.height - 1; } Vector2 pos = GameManager.Instance.getTerrainPos(posXIndex, posYIndex); //GameObject pSoldierUnit = soldierUnit; GameObject pSoldierUnit = Instantiate(soldierUnit, pos, Quaternion.identity); //Instantiate(soldierUnit, pos, Quaternion.identity); pSoldierUnit.name = "nType_" + nType + "nAlign_" + nAlign + "Index_" + totalUnitNum; pSoldierUnit.GetComponent <Unit>().setEntity(nType, nAlign); pSoldierUnit.GetComponent <Unit>().indexX = posXIndex; pSoldierUnit.GetComponent <Unit>().indexY = posYIndex; if (nAlign == 1) { pSoldierUnit.GetComponent <SpriteRenderer>().color = new Color(253, 0, 255); } else { pSoldierUnit.GetComponent <SpriteRenderer>().color = new Color(255, 185, 0); } if (GameManager.Instance.getTurn() == 0) { GameManager.Instance.setSoldierObjectDic1(pSoldierUnit.name, pSoldierUnit); } else { GameManager.Instance.setSoldierObjectDic2(pSoldierUnit.name, pSoldierUnit); } GameManager.Instance.setUnitPosXIndex(pSoldierUnit.name, posXIndex); GameManager.Instance.setUnitPosYIndex(pSoldierUnit.name, posYIndex); GameManager.Instance.setSoldierAlign(pSoldierUnit.name, nAlign); GameManager.Instance.setSoldierType(pSoldierUnit.name, nType); //Debug.Log("pSoldierUnit.name: "+ pSoldierUnit.name+ " pSoldierUnit posXIndex: "+ GameManager.Instance.getUnitPosXIndex(pSoldierUnit.name) + " pSoldierUnit posYIndex: " + GameManager.Instance.getUnitPosYIndex(pSoldierUnit.name)); totalUnitNum++; m_player2.modifyMoney(-pSoldierUnit.GetComponent <Unit>().m_pSoldier.getMoneyToBuyEntity()); } } }