public void SetTarget(GameObject tar) { target = tar; if (target != null) { UnitStatSystem tarStat = target.GetComponent <UnitStatSystem>(); if (tarStat != null) { foreach (GameObject uni in unitList) { UnitCombat com = uni.GetComponent <UnitCombat>(); com.SetTarget(tarStat); } } UnitGroupSystem tarGro = target.GetComponent <UnitGroupSystem>(); if (tarGro != null) { foreach (GameObject uni in unitList) { UnitCombat com = uni.GetComponent <UnitCombat>(); UnitStatSystem tarLitStat = tarGro.GetUnitList()[Random.Range(0, tarGro.GetUnitList().Count)].GetComponent <UnitStatSystem>(); com.SetTarget(tarLitStat); } } } }
void UnselectUnit() { if (selectedUnit != null) { selectedUnit.SetSelect(false); } selectedUnit = null; }
void TrainingUnit() { if (isTraining) { cancelUI.gameObject.SetActive(true); barUI.gameObject.SetActive(true); if (cancelUI.CheckClick()) { cancelUI.SetClick(false); isTraining = false; } if (trainTimeNow >= trainTimeMax) { isTraining = false; GameObject unit = Instantiate(unitTrainNow, spawnPoint.transform.position, spawnPoint.transform.rotation) as GameObject; UnitGroupSystem group = unit.GetComponent <UnitGroupSystem>(); if (group != null) { group.SetOwner(build.GetOwner()); } WorkerUnit worker = unit.GetComponent <WorkerUnit>(); if (worker != null) { worker.SetOwner(this.build.GetOwner()); } } trainTimeNow += Time.deltaTime; barUI.SetProgress(trainTimeNow / trainTimeMax); } else { cancelUI.gameObject.SetActive(false); barUI.gameObject.SetActive(false); VRPlayerIndicator player = null; foreach (GameObject plays in GameObject.FindGameObjectsWithTag("Player")) { VRPlayerIndicator ind = plays.GetComponent <VRPlayerIndicator>(); if (ind.GetPlayerNumber() == stat.GetOwner()) { player = ind; } } for (int i = 0; i < trainUI.Length; i++) { if (trainUI[i].CheckClick() && player.GetEther() >= trainCost[i]) { isTraining = true; unitTrainNow = trainUnit[i]; trainTimeMax = trainTime[i]; trainTimeNow = 0; player.ChangeEther(-trainCost[i]); barUI.SetProgress(0); trainUI[i].SetClick(false); Debug.Log("Start Training!!!"); } } } }
void CheckUnitOBJ(GameObject hitObject) { UnitStatSystem unit = hitObject.GetComponent <UnitStatSystem>(); if (unit != null) { Debug.Log("SELECT UNIT"); if (unit.GetUnitType() == UnitStatSystem.UnitObjectType.Building) { Debug.Log("SELECT Building"); BuildingSystem buil = hitObject.GetComponent <BuildingSystem>(); if (player.GetPlayerNumber() == buil.GetOwner()) { Debug.Log("Select Own Building"); UnselectUnit(); UnselectWorker(); if (buil != selectedBuilding) { UnselectBuilding(); SelectBuilding(buil); } } } if (unit.GetUnitType() == UnitStatSystem.UnitObjectType.Units) { WorkerUnit wor = unit.GetComponent <WorkerUnit>(); if (wor != null) { if (player.GetPlayerNumber() == wor.GetOwner()) { UnselectUnit(); UnselectBuilding(); if (wor != selectedWorker) { UnselectWorker(); SelectWorker(wor); } } } } if (unit.GetOwner() != player.GetPlayerNumber() && selectedUnit != null) { if (!AllianceSystem.CheckAlly(player.GetPlayerNumber(), unit.GetOwner())) { Debug.Log("ATTACK"); selectedUnit.SetAutoAttaack(true); selectedUnit.SetTarget(unit.gameObject); } } } if (hitObject.tag == "Soldier Group") { Debug.Log("SELECT SOLDIER"); UnitGroupSystem unitGro = hitObject.GetComponent <UnitGroupSystem>(); if (player.GetPlayerNumber() == unitGro.GetOwner()) { Debug.Log("Select own Soldier"); UnselectBuilding(); UnselectWorker(); if (unitGro != selectedUnit) { UnselectUnit(); SelectUnit(unitGro); } } if (unitGro.GetOwner() != player.GetPlayerNumber() && selectedUnit != null) { if (!AllianceSystem.CheckAlly(player.GetPlayerNumber(), unitGro.GetOwner())) { Debug.Log("ATTACK"); selectedUnit.SetAutoAttaack(true); selectedUnit.SetTarget(unitGro.gameObject); } } } }
void SelectUnit(UnitGroupSystem uni) { selectedUnit = uni; selectedUnit.SetSelect(true); }
private void CheckArmy() { foreach (UnitGroupSystem army in armyList) { if (!army) { armyList.Remove(army); Destroy(army.gameObject); } } foreach (UnitGroupSystem army in armyList) { GameObject target = null; Collider[] hitColliders = Physics.OverlapSphere(army.transform.position, 50); int i = 0; while (i < hitColliders.Length) { UnitStatSystem tarStat = hitColliders[i].GetComponent <UnitStatSystem>(); if (tarStat && tarStat.GetAlive() && tarStat.GetOwner() != playerStat.GetPlayerNumber() && !AllianceSystem.CheckAlly(playerStat.GetPlayerNumber(), tarStat.GetOwner())) { target = tarStat.gameObject; } else { UnitGroupSystem tarGroup = hitColliders[i].GetComponent <UnitGroupSystem>(); if (tarGroup && tarGroup.GetOwner() != playerStat.GetPlayerNumber() && !AllianceSystem.CheckAlly(playerStat.GetPlayerNumber(), tarGroup.GetOwner())) { target = tarGroup.gameObject; } } i++; } if (target && !army.HaveTarget()) { army.SetAutoAttaack(true); army.SetTarget(target); } else { if (armyList.Count >= 3) { UnitStatSystem[] tarG = FindObjectsOfType <UnitStatSystem>(); foreach (UnitStatSystem tarDum in tarG) { if (tarDum && tarDum.GetAlive() && tarDum.GetOwner() != playerStat.GetPlayerNumber() && !AllianceSystem.CheckAlly(playerStat.GetPlayerNumber(), tarDum.GetOwner())) { target = tarDum.gameObject; } } if (target) { army.GetComponent <UnitMove>().OrderMove(target.transform.position); } } else { GameObject posDummy = Instantiate(aiPointDummy, aiStronghold.transform.position, aiStronghold.transform.rotation) as GameObject; posDummy.transform.Rotate(new Vector3(0, Random.Range(135f, 225f), 0)); posDummy.transform.position += posDummy.transform.forward * Random.Range(40, 50); army.GetComponent <UnitMove>().OrderMove(posDummy.transform.position); //army.GetComponent<UnitMove>().OrderMove(Vector3.one); } } } }
private void Moving() { Vector3 movement = Vector3.zero; Vector3 adjustedPos = new Vector3(); if (isColEvd) { adjustedPos = new Vector3(evdPoints[0].transform.position.x, transform.position.y, evdPoints[0].transform.position.z); } else { adjustedPos = new Vector3(_targetPos.x, transform.position.y, _targetPos.z); } if (_targetPos != Vector3.one) { Quaternion targetRot = Quaternion.LookRotation(adjustedPos - transform.position); // transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, rotSpeed * Time.deltaTime); movement = _curSpeed * Vector3.forward; movement = transform.TransformDirection(movement); } if (isColEvd) { } else { //Debug.Log(Vector3.Distance(adjustedPos, transform.position)); if (Vector3.Distance(adjustedPos, transform.position) < targetBuffer) { _curSpeed -= deceleration * Time.deltaTime; if (_curSpeed <= 0) { _targetPos = Vector3.one; } } } if (needAni) { _animator.SetFloat("Speed", movement.sqrMagnitude); } movement *= Time.deltaTime; _charController.Move(movement); if (isColEvd) { CollisionEvading(); } else { RaycastHit hit; GameObject hitObject = null; Debug.DrawRay(checkColPoint.transform.position + (transform.up * 2), checkColPoint.transform.TransformDirection(Vector3.forward) * colCheckDistance, Color.blue); if (Physics.Raycast(checkColPoint.transform.position + transform.up, checkColPoint.transform.TransformDirection(Vector3.forward), out hit, colCheckDistance)) { Debug.Log("Raycast Hit"); hitObject = hit.transform.gameObject; UnitMove colUnit = hitObject.gameObject.GetComponent <UnitMove>(); if (colUnit) { UnitStatSystem colUnitStat = colUnit.GetComponent <UnitStatSystem>(); if (colUnitStat && colUnitStat.GetOwner() == stat.GetOwner()) { if (colUnit.GetCurrentSpeed() < _curSpeed) { if (evdPoints.Count == 0) { //StartColEvade(); } } } else { UnitGroupSystem colGroup = hitObject.GetComponent <UnitGroupSystem>(); if (colGroup && colGroup.GetOwner() == stat.GetOwner()) { if (evdPoints.Count == 0) { //StartColEvade(); } } } } else { if (evdPoints.Count == 0) { //StartColEvade(); } } } else { isColEvd = false; } } }