예제 #1
0
    private void Attack()
    {
        var attackingUnitsEntities = attackingUnits.Entities
                                     .Where(u => u.IsNotNullAndAlive())
                                     .Take(attackingUnits.GetEntitiesCount());

        foreach (var unit in attackingUnitsEntities)
        {
            var attackingComponent    = unit.Get <AttackingComponent>();
            var unitAttackComponent   = unit.Get <AttackComponent>();
            var unitMovementComponent = unit.Get <MovementComponent>();

            var targetUnit = attackingComponent.TargetEntity;
            if (!targetUnit.IsNotNullAndAlive())
            {
                AttackHelper.StopAttack(unit);
                return;
            }

            var targetMovementComponent = targetUnit.Get <MovementComponent>();

            if (targetMovementComponent == null || !targetMovementComponent.IsObjectAlive)
            {
                AttackHelper.StopAttack(unit);
                return;
            }


            if (attackingComponent.TargetEntity.Get <UnitComponent>() != null &&
                AttackHelper.CanAttack(unitAttackComponent, unitMovementComponent, targetMovementComponent))
            {
                unitAttackComponent.LastAttackTime = DateTime.Now;
                ServerClient.Communication.AttackSender.attacks.Add(
                    new AttackInfo(
                        unit.Get <UnitComponent>().Guid,
                        attackingComponent.TargetEntity.Get <UnitComponent>().Guid,
                        (int)unitAttackComponent.AttackDamage));
            }
            if (attackingComponent.TargetEntity.Get <BuildingComponent>() != null)
            {
                var enemyBuildTransform = attackingComponent.TargetEntity.Get <BuildingComponent>().Object.transform;
                var attackRange         = Math.Max(enemyBuildTransform.lossyScale.x, enemyBuildTransform.lossyScale.z) * 5;
                if (attackingComponent.TargetEntity.Get <BuildingComponent>().Tag == BuildingTag.Base)
                {
                    attackRange *= 6;
                }

                if (AttackHelper.CanBuildingAttack(unitAttackComponent,
                                                   unitMovementComponent,
                                                   targetMovementComponent,
                                                   (int)attackRange))
                {
                    unitAttackComponent.LastAttackTime = DateTime.Now;
                    ServerClient.Communication.AttackSender.attacks.Add(new AttackInfo(unit.Get <UnitComponent>().Guid,
                                                                                       attackingComponent.TargetEntity.Get <BuildingComponent>().Guid,
                                                                                       (int)unitAttackComponent.AttackDamage));
                }
            }
        }
    }
    private void FindAvailableFights()
    {
        var unitsPositionsEntities = unitsPositions.Entities
                                     .Where(e => e.IsNotNullAndAlive() &&
                                            e.Get <MovementComponent>().IsObjectAlive);
        var enemyUnitsPositionsEntities = enemyUnits.Entities
                                          .Where(e => e.IsNotNullAndAlive() &&
                                                 e.Get <MovementComponent>().IsObjectAlive);

        foreach (var allyUnit in unitsPositionsEntities)
        {
            foreach (var enemyUnit in enemyUnitsPositionsEntities)
            {
                var allyUnitAttackComponent    = allyUnit.Get <AttackComponent>();
                var allyUnitMovementComponent  = allyUnit.Get <MovementComponent>();
                var enemyUnitMovementComponent = enemyUnit.Get <MovementComponent>();

                if (AttackHelper.CanAttack(
                        allyUnitAttackComponent,
                        allyUnitMovementComponent,
                        enemyUnitMovementComponent))
                {
                    AttackHelper.CreateAttackEvent(allyUnit, enemyUnit);
                    break;
                }
            }
        }
    }