protected void CheckForNearbyTargets() { Collider2D[] colls = Physics2D.OverlapCircleAll(transform.position, 12f, 256); if (colls.Length > 0) { List <Collider2D> tempColList = new List <Collider2D>(); for (int i = 0; i < colls.Length; i++) { tempColList.Add(colls[i]); } //tempColList.Sort((q1, q2) => Vector3.Distance(q1.transform.position, transform.position).CompareTo(Vector3.Distance(q2.transform.position, transform.position))); for (int i = 0; i < tempColList.Count; i++) { if (Physics2D.RaycastAll(transform.position, tempColList[i].transform.position - transform.position, Vector2.Distance(transform.position, tempColList[i].transform.position), 8192).Length == 0) { targetEntity = tempColList[i].GetComponent <AttackableEntity>(); break; } } if (targetEntity != null) { inCombat = true; } } }
protected virtual void AttackTarget() { targetEntity.TakeDamage(AttackPower, ElementalResistances.PHYSICAL); attackTimer = 1.0f / AttackSpeed; if (targetEntity.CurrentHealth <= 0) { targetEntity = null; } }
void OnTriggerEnter2D(Collider2D other) { if (EnemyProjectile && (other.transform.tag == "Player" || other.transform.tag == "Minion" || other.transform.tag == "Wall")) { if (other.transform.tag != "Wall") { other.GetComponent <AttackableEntity>().TakeDamage(damageValue, damageType); } Destroy(gameObject); } else if (!EnemyProjectile) { AttackableEntity otherEntity = other.GetComponent <AttackableEntity>(); if (otherEntity != null) { otherEntity.TakeDamage(damageValue, damageType); Destroy(gameObject); } } }
protected override void Update() { base.Update(); UpdateHealthBar(); if (currentHealth <= 0) { Die(); } if (!empowered && DarkArcaneMagic.isActive) { print("Empowering " + transform.name); empowered = true; Empower(); } else if (empowered && !DarkArcaneMagic.isActive) { print("DeEmpowering " + transform.name); empowered = false; DeEmpower(); } if (targetEntity != null) { Debug.DrawLine(transform.position, targetEntity.transform.position, Color.green); } if (attackTimer > 0) { attackTimer -= Time.deltaTime; if (attackTimer < 0) { attackTimer = 0; } } if (targetEntity == null && !inCombat) { targetEntity = Player_Accessor_Script.DetailsScript; } //Not moving, move out. if (rgd2d.velocity.magnitude == 0) { //Collider2D[] nearby = Physics2D.OverlapCircleAll(transform.position, .25f, 2048); //for (int i = 0; i < nearby.Length; i++) //{ // if (nearby[i].GetComponent<Rigidbody2D>().velocity.magnitude == 0) // { // nearby[i].transform.position += (nearby[i].transform.position - transform.position).normalized * .015f; // transform.position += (transform.position - nearby[i].transform.position).normalized * .015f; // } //} } if (targetEntity == null) { rgd2d.velocity = Vector2.zero; targetEntity = Player_Accessor_Script.DetailsScript; if (inCombat) { inCombat = !inCombat; } } if (checkTimer < 5f) { checkTimer += Time.deltaTime; } if (checkTimer > 5f && targetEntity == null || targetEntity == Player_Accessor_Script.DetailsScript) { checkTimer = 0; CheckForNearbyTargets(); } if (inCombat) { if (targetEntity != null && CanSeeTarget()) { if (Vector2.Distance(transform.position, targetEntity.transform.position) > currentAttackRange) { rgd2d.velocity = (targetEntity.transform.position - transform.position).normalized * CharacterSpeed; } else { rgd2d.velocity = Vector2.zero; if (attackTimer <= 0) { AttackTarget(); } } } } else { //Pathing towards player if (CanSeePlayer() && Vector2.Distance(transform.position, Player_Accessor_Script.DetailsScript.transform.position) < 4) { if (Vector2.Distance(transform.position, Player_Accessor_Script.DetailsScript.transform.position) > distanceLimiter) { rgd2d.velocity = (Player_Accessor_Script.DetailsScript.transform.position - transform.position).normalized * Player_Accessor_Script.ControllerScript.PlayerSpeedMultiplier * Player_Accessor_Script.ControllerScript.PlayerSpeed; currentPath.Clear(); } else { rgd2d.velocity = Vector2.zero; currentPath.Clear(); } } else { if (currentPath == null) { currentPath = new List <Node>(); } if (graphManager == null) { graphManager = AIManager.instance; } if (currentPath.Count == 0) { currentPath = graphManager.Dijkstra(transform.position, Player_Accessor_Script.DetailsScript.transform.position); } else { Vector3 currTar = new Vector3(currentPath[0].x, currentPath[0].y); rgd2d.velocity = (currTar - transform.position).normalized * CharacterSpeed; if (Vector3.Distance(transform.position, currTar) < .5f) { currentPath.RemoveAt(0); } } } } }
private void Castle_Destroyed(AttackableEntity obj) { RemoveEntity(castle); }
private void OnPlayerConnected(GameObject player) { ae = player.GetComponent <AttackableEntity>(); ae.OnHealthChanged += UpdateHealth; UpdateHealth(ae.GetHealthInt()); }