public void LocateEnemies() { if (this.isMoving) { return; } Collider[] colliders = Physics.OverlapSphere(this.transform.position, this.fieldOfViewRadius + ObtainRadius(this)); if (colliders.Length > 0) { foreach (Collider col in colliders) { CommonUnit unit = col.GetComponent <CommonUnit>(); if (unit == this) { continue; } if (unit != null) { if (unit.isEnemy) { if (!this.enemies.Contains(unit)) { this.enemies.Add(unit); } } } } } }
//---------------------------------- protected virtual void UpdateStatus() { if (this.selectedObjects.Count > 0 || this.boxSelectedObjects.Count > 0) { foreach (GameObject obj in this.unitManager.getAllObjects()) { if (obj == null) { this.unitManager.getRemoveList().Add(obj); continue; } if (this.selectedObjects.Contains(obj) || this.boxSelectedObjects.Contains(obj)) { CommonUnit unit = obj.GetComponent <CommonUnit>(); if (unit == null) { this.unitManager.getRemoveList().Add(obj); continue; } if (!unit.isEnemy) { if (unit.isStandingBy) { unit.SetAttackStandby(); } if (unit.isAttacking) { unit.SetAttack(); } if (unit.isSplitting) { unit.SetDeselect(); unit.SetAttackCancel(); } if (unit.isSelected) { unit.SetSelect(); } } } } } else { foreach (GameObject obj in this.unitManager.getAllObjects()) { if (obj == null) { this.unitManager.getRemoveList().Add(obj); continue; } CommonUnit unit = obj.GetComponent <CommonUnit>(); unit.SetDeselect(); } } }
public float ObtainRadius(CommonUnit unit) { if (unit == null) { return(0f); } Renderer renderer = unit.GetComponent <Renderer>(); return(renderer.bounds.extents.magnitude / 2f); }
public void ActionTick() { if (!this.isTakingDamage) { if (this.isMoving || this.isAttacking) { NavMeshAgent agent = this.GetComponent <NavMeshAgent>(); if (agent.ReachedDestination()) { SetStopMoving(); } if (this.enemies.Count <= 0) { LocateEnemies(); } if (this.enemies.Count > 0) { if (this.enemies[0] != null) { this.enemyTarget = this.enemies[0]; } } if (this.enemyTarget != null && this.enemyTarget.isEnemy) { if (Vector3.Distance(this.transform.position, this.enemyTarget.transform.position) <= ObtainRadius(this) + this.attackRadius) { if (this.enemyTarget.currentHealth > 0) { SetAttack(); if (this.attackCooldownTimer <= 0f) { this.enemyTarget.TakeDamage(this.attackPower); } } } else { agent.stoppingDistance = ObtainRadius(this.enemyTarget) + this.attackRadius; agent.SetDestination(this.enemyTarget.transform.position); } } else { if (agent.ReachedDestination()) { SetAttackCancel(); if (this.isSelected) { SetSelect(); } } } } } }
//---------------------------------- protected void MergeOrder() { if (Input.GetKeyDown(KeyCode.D)) { if (this.selectedObjects.Count > 0) { bool enemyCheck = false; foreach (GameObject obj in this.selectedObjects) { CommonUnit unit = obj.GetComponent <CommonUnit>(); if (unit.isEnemy) { enemyCheck = true; } } if (!enemyCheck) { List <GameObject> pairedUnitsList = new List <GameObject>(); for (int i = 0; i < this.selectedObjects.Count - 1; i++) { if (pairedUnitsList.Contains(this.selectedObjects[i])) { continue; } CommonUnit unpairedUnit = this.selectedObjects[i].GetComponent <CommonUnit>(); for (int j = i + 1; j < this.selectedObjects.Count; j++) { if (pairedUnitsList.Contains(this.selectedObjects[j])) { continue; } CommonUnit undeterminedUnit = this.selectedObjects[j].GetComponent <CommonUnit>(); if (unpairedUnit.level == undeterminedUnit.level) { unpairedUnit.DisableSelection(); unpairedUnit.SetDeselect(); unpairedUnit.SetMerging(); undeterminedUnit.DisableSelection(); undeterminedUnit.SetDeselect(); undeterminedUnit.SetMerging(); this.mergeManager.mergeGroups.Add(new MergeGroup(this.selectedObjects[i], this.selectedObjects[j])); pairedUnitsList.Add(this.selectedObjects[i]); pairedUnitsList.Add(this.selectedObjects[j]); break; } } } } this.selectedObjects.Clear(); } } }
// Use this for initialization protected void Start() { GameObject gameObject = GameObject.Find("Unit Manager"); if (gameObject != null) { this.unitManager = gameObject.GetComponent <CommonUnitManager>(); if (this.unitManager == null) { Debug.LogError("Common: Unit Manager is not created in the editor."); } else { this.unitManager.getAllObjects().Add(this.gameObject); } } Renderer renderer = this.GetComponent <Renderer>(); this.initialColor = renderer.material.color; if (this.initialColor.Equals(Color.black)) { this.initialColor = Color.white; } Vector3 size = renderer.bounds.size; this.fieldOfViewRadius = 2.5f; this.attackRadius = Mathf.Ceil(((size / 2f).magnitude)); if (this.attackCooldown <= 3f) { this.attackCooldown = 3f; } this.canBeSelected = true; this.level = 1; this.attackPower = 1; this.maxHealth = 5; this.currentHealth = 5; //this.isEnemy = false; this.isDead = false; this.isTakingDamage = false; this.enemies = new List <CommonUnit>(); this.enemyTarget = null; }
public MergeGroup(GameObject owner, GameObject merger) { this.owner = owner; this.merger = merger; this.elapsedTime = 0f; this.ownerOrigin = owner.transform.position; this.mergerOrigin = merger.transform.position; this.initialLocalScale = owner.transform.localScale; this.newLocalScale = this.initialLocalScale * 1.4f; this.center = this.ownerOrigin + ((this.mergerOrigin - this.ownerOrigin) / 2f); CommonUnit ownerUnit = owner.GetComponent <CommonUnit>(); CommonUnit mergerUnit = merger.GetComponent <CommonUnit>(); ownerUnit.MultiplyAttributes(2); mergerUnit.MultiplyAttributes(2); }
protected void Update() { if (this.splitGroups.Count > 0) { for (int i = 0; i < this.splitGroups.Count; i++) { SplitGroup group = this.splitGroups[i]; if (group.elapsedTime < 1f) { group.owner.transform.position = Vector3.Lerp(group.center, group.ownerTarget, group.elapsedTime); group.clone.transform.position = Vector3.Lerp(group.center, group.cloneTarget, group.elapsedTime); group.elapsedTime += Time.deltaTime; this.splitGroups[i] = group; //TODO(Thompson): Test to see if "ref" keyword works. //MovePosition(ref group); //UpdateTime(ref group); } else { if (!this.removeList.Contains(group)) { this.removeList.Add(group); } } } } if (this.removeList.Count > 0) { foreach (SplitGroup group in this.removeList) { if (this.splitGroups.Contains(group)) { CommonUnit unit = group.owner.GetComponent <CommonUnit>(); unit.EnableSelection(); unit.SetNotSplitting(); unit = group.clone.GetComponent <CommonUnit>(); unit.EnableSelection(); unit.SetNotSplitting(); this.splitGroups.Remove(group); } } this.removeList.Clear(); } }
protected void Update() { if (this.mergeGroups.Count > 0) { for (int i = 0; i < this.mergeGroups.Count; i++) { MergeGroup group = this.mergeGroups[i]; if (group.elapsedTime < 1f) { group.owner.transform.position = Vector3.Lerp(group.ownerOrigin, group.center, group.elapsedTime); group.merger.transform.position = Vector3.Lerp(group.mergerOrigin, group.center, group.elapsedTime); group.owner.transform.localScale = Vector3.Lerp(group.initialLocalScale, group.newLocalScale, group.elapsedTime); group.merger.transform.localScale = Vector3.Lerp(group.initialLocalScale, group.newLocalScale, group.elapsedTime); group.elapsedTime += Time.deltaTime; this.mergeGroups[i] = group; } else { this.removeList.Add(this.mergeGroups[i]); } } } if (this.removeList.Count > 0) { foreach (MergeGroup group in this.removeList) { if (this.mergeGroups.Contains(group)) { CommonUnit unit = group.owner.GetComponent <CommonUnit>(); unit.EnableSelection(); unit.SetNotMerging(); if (this.unitManager.getAllObjects().Contains(group.merger)) { this.unitManager.getAllObjects().Remove(group.merger); } GameObject.Destroy(group.merger); this.mergeGroups.Remove(group); } } this.removeList.Clear(); } }
//---------------------------------- protected void SplitOrder() { if (Input.GetKeyDown(KeyCode.S)) { if (this.selectedObjects.Count > 0) { bool enemyCheck = false; foreach (GameObject obj in this.selectedObjects) { CommonUnit unit = obj.GetComponent <CommonUnit>(); if (unit.isEnemy) { enemyCheck = true; } } if (!enemyCheck) { foreach (GameObject owner in this.selectedObjects) { CommonUnit unit = owner.GetComponent <CommonUnit>(); if (unit.level == 1) { GameObject duplicate = GameObject.Instantiate <GameObject>(this.commonUnitPrefab); duplicate.transform.position = owner.transform.position; unit.SetDeselect(); unit.DisableSelection(); unit.SetSplitting(); unit = duplicate.GetComponent <CommonUnit>(); unit.SetDeselect(); unit.DisableSelection(); unit.SetSplitting(); unit.initialColor = Color.white; this.unitManager.getAllObjects().Add(duplicate); this.splitManager.splitGroups.Add(new SplitGroup(owner, duplicate)); } } } this.selectedObjects.Clear(); } } }
public void IdleActions() { LocateEnemies(); if (this.enemies.Count > 0) { if (this.enemies[0] != null) { this.enemyTarget = this.enemies[0]; if (this.enemyTarget.currentHealth > 0) { Vector3 enemyPosition = this.enemies[0].transform.position; if (Vector3.Distance(this.transform.position, enemyPosition) <= ObtainRadius(this) + this.fieldOfViewRadius) { NavMeshAgent agent = this.GetComponent <NavMeshAgent>(); agent.stoppingDistance = ObtainRadius(this.enemyTarget) + this.attackRadius; agent.SetDestination(this.enemyTarget.transform.position); if (!this.isAttacking) { SetAttack(); } } else { if (this.isAttacking) { SetAttackCancel(); } } } } else { this.enemies.RemoveAt(0); } } else { SetNoEnemyTarget(); } }
//---------------------------------- //protected void AttackOrder() { // if (Input.GetKeyDown(KeyCode.A)) { // if (!this.attackStandingByFlag) { // if (this.selectedObjects.Count > 0) { // this.attackStandingByFlag = true; // foreach (GameObject obj in this.selectedObjects) { // CommonUnit unit = obj.GetComponent<CommonUnit>(); // if (!unit.isEnemy) { // unit.SetAttackStandby(); // } // } // //this.selectedObjects.Clear(); // } // } // } // if (this.attackStandingByFlag) { // if (Input.GetMouseButtonDown(0)) { // this.attackStandingByFlag = false; // foreach (GameObject obj in this.selectedObjects) { // CommonUnit unit = obj.GetComponent<CommonUnit>(); // unit.SetAttackCancel(); // } // this.selectedObjects.Clear(); // } // else if (Input.GetMouseButtonDown(1)) { // this.attackStandingByFlag = false; // Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // RaycastHit[] hits = Physics.RaycastAll(ray); // //bool hasOrderedAttackTarget = false; // foreach (RaycastHit hit in hits) { // GameObject obj = hit.collider.gameObject; // if (obj.name.Equals("Floor")) { // foreach (GameObject selected in this.selectedObjects) { // CommonUnit unit = selected.GetComponent<CommonUnit>(); // if (!unit.isEnemy) { // unit.SetAttackCancel(); // unit.SetNewDestination(hit.point); // unit.SetAttack(); // } // } // //hasOrderedAttackTarget = true; // break; // } // } // //if (hasOrderedAttackTarget) { // // this.selectedObjects.Clear(); // //} // } // } //} //---------------------------------- protected void MoveOrder() { if (Input.GetMouseButtonDown(1)) { if (this.selectedObjects.Count > 0) { for (int i = 0; i < this.selectedObjects.Count; i++) { if (this.selectedObjects[i] == null) { this.selectedObjects.RemoveAt(i); } } Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit[] hits = Physics.RaycastAll(ray); foreach (RaycastHit hit in hits) { GameObject obj = hit.collider.gameObject; if (obj.name.Equals("Floor")) { foreach (GameObject select in this.selectedObjects) { CommonUnit unit = select.GetComponent <CommonUnit>(); if (!unit.isEnemy) { unit.SetAttackCancel(); unit.SetNoEnemyTarget(); unit.enemies.Clear(); unit.SetStartMoving(); unit.SetNewDestination(hit.point); } } break; } } } //this.selectedObjects.Clear(); } }
public void SetNoEnemyTarget() { this.enemyTarget = null; }
//---------------------------------- protected virtual void SelectOrder() { if (Input.GetMouseButtonDown(0)) { if (this.selectedObjects.Count > 0) { foreach (GameObject obj in this.selectedObjects) { CommonUnit unit = obj.GetComponent <CommonUnit>(); unit.SetDeselect(); } this.selectedObjects.Clear(); } Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit[] hits = Physics.RaycastAll(ray); bool hasHitUnit = false; foreach (RaycastHit hit in hits) { GameObject obj = hit.collider.gameObject; //Units of type CommonUnit must have this tag set in the editor. if (obj.tag.Equals(this.unitTagName)) { hasHitUnit = true; if (!this.selectedObjects.Contains(obj)) { CommonUnit unit = obj.GetComponent <CommonUnit>(); unit.SetSelect(); this.selectedObjects.Add(obj); } break; } } if (!hasHitUnit) { this.selectedObjects.Clear(); } } if (Input.GetMouseButton(0)) { foreach (GameObject obj in this.unitManager.getAllObjects()) { if (obj == null) { this.unitManager.getRemoveList().Add(obj); continue; } Vector2 screenPoint = Camera.main.WorldToScreenPoint(obj.transform.position); screenPoint.y = Screen.height - screenPoint.y; if (Selection.selectionArea.Contains(screenPoint) && !this.boxSelectedObjects.Contains(obj)) { this.boxSelectedObjects.Add(obj); if (!this.selectedObjects.Contains(obj)) { this.selectedObjects.Add(obj); } CommonUnit unit = obj.GetComponent <CommonUnit>(); unit.SetSelect(); } else if (!Selection.selectionArea.Contains(screenPoint) && this.boxSelectedObjects.Contains(obj)) { this.boxSelectedObjects.Remove(obj); if (this.selectedObjects.Contains(obj)) { this.selectedObjects.Remove(obj); } CommonUnit unit = obj.GetComponent <CommonUnit>(); unit.SetDeselect(); } } } if (Input.GetMouseButtonUp(0)) { if (this.boxSelectedObjects.Count > 0) { foreach (GameObject obj in this.boxSelectedObjects) { if (!this.selectedObjects.Contains(obj)) { CommonUnit unit = obj.GetComponent <CommonUnit>(); unit.SetSelect(); this.selectedObjects.Add(obj); } } this.boxSelectedObjects.Clear(); } } }