private List <AttackableComponent> FindTarget(List <AttackableComponent> targets) { List <AttackableComponent> deadenemies = new List <AttackableComponent>(); foreach (AttackableComponent enemy in targets) { if (enemy == null) { deadenemies.Add(enemy); } else { if (Mathf.Abs((transform.position - enemy.transform.position).magnitude) > attackRange) { continue; } targetAttackableComponent = enemy; targetEnemy = enemy.GetComponent <MyObject>(); if (weapon != null) { weapon.transform.LookAt(enemy.transform); } return(deadenemies); } } return(deadenemies); }
// Take Damage public void Damage(Entity unit, uint damage) { if (!isAttackable(unit)) { return; } AttackableComponent component = GetAttackableComponent(unit); uint durability_points; // Validate & Set if (component.current < damage) { durability_points = 0; } else { durability_points = component.current - damage; } // component.current = durability_points; SetAttackableComponent(unit, component); }
// Heal public void Repair(Entity unit, uint heal) { if (!isAttackable(unit)) { return; } AttackableComponent component = GetAttackableComponent(unit); uint durabilityPoints; // if it's greater than uint.MaxValue than just SET IT to the maximum if (GameUtilities.CheckUintSumOverflow(component.current, heal)) { durabilityPoints = component.durability; } else { durabilityPoints = component.current + heal; // Else Set As It Is. } // Validate & Set if (durabilityPoints > component.durability) { component.current = component.durability; } else { component.current = durabilityPoints; } // SetAttackableComponent(unit, component); }
public void init(AttackableComponent target, MyObjectType damageType, float speed = 21f, float damage = 1) { this.target = target; this.damageType = damageType; this.speed = speed; this.damage = damage; lineRenderer = GetComponent <LineRenderer>(); }
// Reset current to maximum durability private void ResetDurability(Entity unit) { if (!isAttackable(unit)) { return; } // Remark: Just set some component data, without getting component data? AttackableComponent component = GetAttackableComponent(unit); component.current = component.durability; SetAttackableComponent(unit, component); }
// Use when entity is attackable private void SetDurability(Entity unit, uint durability) { if (!isAttackable(unit)) { return; } // Remark: Just set some component data, without getting component data AttackableComponent component = EntityManager.GetComponentData <AttackableComponent>(unit); component.durability = durability; SetAttackableComponent(unit, component); }
private void OnTriggerEnter(Collider other) { if (attackComponent == null) { attackComponent = GetComponentInParent <AttackComponent>(); } AttackableComponent attackableComponent = other.GetComponent <AttackableComponent>(); if (attackableComponent != null) { MyObject enemyObject = attackableComponent.GetComponent <MyObject>(); if (!enemyObject.placed) { // Building has not been placed return; } if (enemyObject != attackComponent.myObject) { if (enemyObject.team != attackComponent.myObject.team) { attackComponent.AddTargetInRange(attackableComponent, enemyObject.myObjectType); } } } else { attackableComponent = other.GetComponentInParent <AttackableComponent>(); if (attackableComponent != null) { MyObject enemyObject = attackableComponent.GetComponentInParent <MyObject>(); if (!enemyObject.placed) { // Building has not been placed return; } if (enemyObject != attackComponent.myObject) { if (enemyObject.team != attackComponent.myObject.team) { attackComponent.AddTargetInRange(attackableComponent, enemyObject.myObjectType); } } } } }
private void FindTargetEnemy() { List <AttackableComponent> deadenemies = new List <AttackableComponent>(); targetEnemy = null; targetAttackableComponent = null; if (effectiveTargets != null && effectiveTargets.Count > 0) { if (targetEnemy == null || targetAttackableComponent == null) { deadenemies = FindTarget(effectiveTargets); } foreach (AttackableComponent deadenemy in deadenemies) { effectiveTargets.Remove(deadenemy); } deadenemies.Clear(); } if (standardTargets != null && standardTargets.Count > 0) { if (targetEnemy == null || targetAttackableComponent == null) { deadenemies = FindTarget(standardTargets); } foreach (AttackableComponent deadenemy in deadenemies) { standardTargets.Remove(deadenemy); } deadenemies.Clear(); } if (ineffectiveTargets != null && ineffectiveTargets.Count > 0) { if (targetEnemy == null || targetAttackableComponent == null) { deadenemies = FindTarget(ineffectiveTargets); } foreach (AttackableComponent deadenemy in deadenemies) { ineffectiveTargets.Remove(deadenemy); } deadenemies.Clear(); } }
public void RemoveTargetInRange(AttackableComponent attackableComponent, MyObjectType targetObjectType) { AttackEffectiviness attackEffectiviness = GetEffectiviness(attackType, targetObjectType); switch (attackEffectiviness) { case AttackEffectiviness.Effective: effectiveTargets.Remove(attackableComponent); break; case AttackEffectiviness.Standard: standardTargets.Remove(attackableComponent); break; case AttackEffectiviness.Ineffective: ineffectiveTargets.Remove(attackableComponent); break; } }
private void OnTriggerExit(Collider other) { if (attackComponent == null) { attackComponent = GetComponentInParent <AttackComponent>(); } AttackableComponent attackableComponent = other.GetComponent <AttackableComponent>(); if (attackableComponent != null) { MyObject enemyObject = attackableComponent.GetComponent <MyObject>(); if (enemyObject != attackComponent.myObject) { if (enemyObject.team != attackComponent.myObject.team) { attackComponent.RemoveTargetInRange(attackableComponent, enemyObject.myObjectType); } } } else { attackableComponent = other.GetComponentInParent <AttackableComponent>(); if (attackableComponent != null) { MyObject enemyObject = attackableComponent.GetComponentInParent <MyObject>(); if (enemyObject != attackComponent.myObject) { if (enemyObject.team != attackComponent.myObject.team) { attackComponent.RemoveTargetInRange(attackableComponent, enemyObject.myObjectType); } } } } }
public void AddTargetInRange(AttackableComponent attackableComponent, MyObjectType targetObjectType) { if (effectiveTargets.Contains(attackableComponent) || standardTargets.Contains(attackableComponent) || ineffectiveTargets.Contains(attackableComponent)) { return; } AttackEffectiviness attackEffectiviness = GetEffectiviness(attackType, targetObjectType); switch (attackEffectiviness) { case AttackEffectiviness.Effective: effectiveTargets.Add(attackableComponent); break; case AttackEffectiviness.Standard: standardTargets.Add(attackableComponent); break; case AttackEffectiviness.Ineffective: ineffectiveTargets.Add(attackableComponent); break; } }
IEnumerator MoveTowardsTarget() { Vector3 startPos = transform.position; for (float i = 0; i <= 1f; i += Time.deltaTime * 2f) { transform.position = Vector3.Lerp(startPos, target.transform.position, i); lineRenderer.SetPosition(0, transform.position); lineRenderer.SetPosition(1, transform.position + transform.forward); if (Vector3.Distance(transform.position, target.transform.position) < 1f) { target.ReceiveDamage(damage * AttackComponent.GetDamageModifier(damageType, target.GetComponent <MyObject>().myObjectType)); if (hitSound != null) { Instantiate(hitSound, transform.position, Quaternion.identity); } target = null; //Destroy(gameObject); } yield return(null); } }
private void SetAttackableComponent(Entity entity, AttackableComponent c) => EntityManager.SetComponentData <AttackableComponent>(entity, c);