예제 #1
0
    /// <summary>
    /// 战斗开始
    /// </summary>
    public void StartBattle()
    {
        if (IsBattleStarted)
        {
            return;
        }
        IsBattleStarted = true;
        IsoMap.Instance.HideGuardAreaMap();

        //计算每个建筑的百分比
        var allEntities = IsoMap.Instance.GetAllEntitiesByOwner(OwnerType.Defender);

        foreach (var tileEntity in allEntities)
        {
            if (EntityTypeUtil.IsAnyNonWallBuilding(tileEntity.entityType))
            {
                ++defenderBuildingCount;
            }
        }
        totalDefenderBuildingCount = defenderBuildingCount;

        //计算士兵数量
        foreach (var armyVo in DataCenter.Instance.Attacker.armies)
        {
            attackerArmyCount += armyVo.amount;

            var model = DataCenter.Instance.FindEntityModelById(armyVo.cid);
            if (EntityTypeUtil.IsCurer(model))
            {
                attackerCurerCount += armyVo.amount;
            }
        }
        //加上援军的士兵数量
        foreach (var donatedArmy in DataCenter.Instance.Attacker.donatedArmies)
        {
            attackerArmyCount += donatedArmy.amount;

            var model = DataCenter.Instance.FindEntityModelById(donatedArmy.cid);
            if (EntityTypeUtil.IsCurer(model))
            {
                attackerCurerCount += donatedArmy.amount;
            }
        }

        //备份进攻方和防守方的数据,提交服务器时使用
        attackerData = ProtoBuf.Serializer.DeepClone(DataCenter.Instance.Attacker);
        defenderData = ProtoBuf.Serializer.DeepClone(DataCenter.Instance.Defender);
    }
예제 #2
0
    protected override List <TileEntity> TryLockTargeters(out TilePoint?_targetPos, out LinkedList <IMoveGrid> _targetRoute)
    {
        _targetPos   = null;
        _targetRoute = null;

        var allTargeters = IsoMap.Instance.GetAllEntitiesByOwner(Attacker.GetTargetOwner()).RubySelect(tar =>
        {
            if (tar.IsDead())
            {
                return(false);
            }

            //  不是限定类型的目标则过滤掉
            if (EntityTypeUtil.IsAnyActor(tar.entityType) && Attacker.model.onlyAttackTargetType != EntityType.None && Attacker.model.onlyAttackTargetType != tar.entityType)
            {
                return(false);
            }

            return(true);
        });

        if (allTargeters.Count == 0)
        {
            return(null);
        }

        List <TileEntity> targets = null;

        //  1、有优先目标的士兵先筛选优先目标
        if (Entity.aiType != EntityAiType.Other)
        {
            targets = allTargeters.RubySelect(tar => { return(tar.entityType == (EntityType)Entity.aiType); });
        }

        //  2、没有优先目标 or 优先目标为空 则优先筛选援军
        if (targets == null || targets.Count == 0)
        {
            targets = allTargeters.RubySelect(tar => { return(tar.Friendly); });
        }

        //  3、没有援军则筛选默认的非墙建筑
        if (targets.Count == 0)
        {
            targets = allTargeters.RubySelect(tar => { return(EntityTypeUtil.IsAnyNonWallBuilding(tar.entityType)); });
        }

        //  4、未找到目标直接返回
        if (targets.Count == 0)
        {
            return(null);
        }

#if REALTIME_AI
        //float t1 = UnityEngine.Time.realtimeSinceStartup;
        //for (int i = 0; i < 5000; i++)
        //{
        //    FindTargetsNearestTimeCost(Attacker, targets);
        //}
        //float t2 = UnityEngine.Time.realtimeSinceStartup;
        //Debug.Log(t2 - t1);
        //Debug.Break();

        //  筛选最近目标(最小时间 or 最近距离)
        return(SelectTargeters(targets, out _targetPos, out _targetRoute));
#else
        //  从备选列表中筛选最近的目标
        IEnumerator <IsoGridTarget> iterator = FindTargetsNearestTimeCost(Attacker, targets);
        if (iterator == null)
        {
            return(null);
        }

        //  延迟处理
        DelayManager.Instance.AddDelayAi <IsoGridTarget>(Attacker, OnDelayLockTargetersCompleted, iterator);
        Entity.State = EntityStateType.Thinking;
        return(null);
#endif
    }
예제 #3
0
    /// <summary>
    /// 有单位死亡了
    /// </summary>
    /// <param name="entity"></param>
    public void OnEntityDestroy(TileEntity entity)
    {
        //如果死亡的是建筑
        if (EntityTypeUtil.IsAnyNonWallBuilding(entity.entityType))
        {
            defenderBuildingCount--;
            DestroyBuildingPercent = (float)(totalDefenderBuildingCount - defenderBuildingCount) / totalDefenderBuildingCount;
            //摧毁50% 获得一颗星
            if (!hasGotHalfDestroyStar && DestroyBuildingPercent > 0.5f)
            {
                hasGotHalfDestroyStar = true;
                BattleStar++;
            }
            Debug.Log("摧毁:" + DestroyBuildingPercent + "%");

            //摧毁基地 获得一颗星
            if (EntityTypeUtil.IsCenterBuilding(entity.model))
            {
                BattleStar++;
            }
            //摧毁100% 获得一颗星
            if (defenderBuildingCount == 0)
            {
                BattleStar++;
            }

            EventDispather.DispatherEvent(GameEvents.BATTLE_PROGRESS_CHANGE);
        }
        else if (entity.GetOwner() == OwnerType.Attacker && EntityTypeUtil.IsAnyActor(entity.entityType))
        {
            attackerArmyCount--;
            if (EntityTypeUtil.IsCurer(entity.model))
            {
                attackerCurerCount--;
            }
        }
        else if (entity.GetOwner() == OwnerType.Defender)
        {
            if (EntityTypeUtil.IsAnyActor(entity.entityType))
            {
                if (entity.Friendly)
                {
                    var found = false;
                    foreach (var killedDefenderDonatedArmy in killedDefenderDonatedArmies)
                    {
                        if (killedDefenderDonatedArmy.cid == entity.model.baseId)
                        {
                            killedDefenderDonatedArmy.amount++;
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        killedDefenderDonatedArmies.Add(new ArmyVO()
                        {
                            amount = 1, cid = entity.model.baseId
                        });
                    }
                }
            }
            else if (EntityTypeUtil.IsAnyTrap(entity.model))
            {
                brokenTraps.Add(entity.buildingVO.sid);
            }
        }
        CheckBattleEnd();
    }
예제 #4
0
    /// <summary>
    /// 类型获取对象列表
    /// </summary>
    /// <param name="ownerType"></param>
    /// <param name="aiType"></param>
    /// <param name="attacker"></param>
    /// <param name="includeFriend">是否包含友军(默认只获取建筑型目标)</param>
    /// <returns></returns>
    public List <TileEntity> GetEntitiesByTT(OwnerType ownerType, EntityAiType aiType, TileEntity attacker, bool includeFriend = false)
    {
        List <TileEntity> ret = new List <TileEntity>();

        foreach (TileEntity entity in m_entities[(int)ownerType])
        {
            if (entity == attacker)
            {
                continue;
            }

            if (entity.IsDead())
            {
                continue;
            }

            //  不是限定类型的目标则过滤掉
            if (EntityTypeUtil.IsAnyActor(entity.entityType) && attacker.model.onlyAttackTargetType != EntityType.None && attacker.model.onlyAttackTargetType != entity.entityType)
            {
                continue;
            }

            switch (aiType)
            {
            case EntityAiType.PriorToWall:
                if (entity.entityType == EntityType.Wall)
                {
                    ret.Add(entity);
                }
                break;

            case EntityAiType.PriorToTower:
                if (entity.entityType == EntityType.Tower)
                {
                    ret.Add(entity);
                }
                break;

            case EntityAiType.PriorToResource:
                if (entity.entityType == EntityType.Resource)
                {
                    ret.Add(entity);
                }
                break;

            default:
            {
                //  建筑目标
                if (EntityTypeUtil.IsAnyNonWallBuilding(entity.entityType))
                {
                    ret.Add(entity);
                }
                //  援军
                else if (includeFriend && entity.Friendly)
                {
                    ret.Add(entity);
                }
            }
            break;
            }
        }
        return(ret);
    }