Esempio n. 1
0
        /// <summary>
        /// 计算伤害
        /// </summary>
        private void onDamage(MobaClient client, int attackId, int skillId, int[] targetId)
        {
            //1.获取房间模型
            int       playerid = playerCache.GetId(client);
            FightRoom room     = fightCache.GetRoom(playerid);
            //2.判断是谁攻击 谁被攻击
            //攻击者的数据模型
            DogModel attackModel = null;

            if (attackId >= 0)
            {
                //攻击者的ID大于等于0 就是英雄攻击
                attackModel = room.GetHeroModel(attackId);
            }
            else if (attackId <= -10 && attackId >= -30)
            {
                //ID为-10~-30 防御塔攻击
                attackModel = room.GetBuildModel(attackId);
            }
            else if (attackId <= -1000)
            {
                //小兵攻击
                attackModel = room.GetDogModel(attackId);
            }
            //被攻击者的数据模型
            DogModel[] targetModels = new DogModel[targetId.Length];
            for (int i = 0; i < targetId.Length; i++)
            {
                if (targetId[i] >= 0)
                {
                    //被攻击者的ID大于等于0 就是英雄被攻击
                    targetModels[i] = room.GetHeroModel(targetId[i]);
                }
                else if (targetId[i] <= -10 && targetId[i] >= -30)
                {
                    //ID为-10~-30 防御塔被攻击
                    targetModels[i] = room.GetBuildModel(targetId[i]);
                }
                else if (targetId[i] <= -1000)
                {
                    //小兵被攻击
                    targetModels[i] = room.GetDogModel(targetId[i]);
                }
            }
            //3.根据技能ID判断出 是 普通攻击 还是特殊技能
            //4.根据伤害表 根据技能id获取ISKILL 调用damage 计算伤害
            ISkill             skill   = null;
            List <DamageModel> damages = null;

            //获取skill
            skill = DamageData.GetSkill(skillId);
            //计算出伤害
            damages = skill.Damage(skillId, 0, attackModel, targetModels);
            //6.给房间内的客户端广播数据模型
            room.Brocast(OpCode.FightCode, OpFight.Damage, 0, "有伤害产生", null, JsonMapper.ToJson(damages.ToArray()));
            //结算
            foreach (DogModel item in targetModels)
            {
                if (item.CurrHp <= 0)
                {
                    switch (item.ModelType)
                    {
                    case ModelType.DOG:
                        #region 小兵
                        //如果是英雄攻击导致死亡 那么就给奖励
                        if (attackModel.Id >= 0)
                        {
                            //加钱
                            ((HeroModel)attackModel).Money += 20;
                            //加经验
                            ((HeroModel)attackModel).Exp += 20;
                            //检测是否升级
                            if (((HeroModel)attackModel).Exp > ((HeroModel)attackModel).Level * 100)
                            {
                                //升级
                                ((HeroModel)attackModel).Level++;
                                ((HeroModel)attackModel).Points++;
                                //重置经验值
                                ((HeroModel)attackModel).Exp = 0;
                                HeroDataModel data = HeroData.GetHeroData(attackModel.Id);
                                //英雄成长属性 增加
                                ((HeroModel)attackModel).Attack  += data.GrowAttack;
                                ((HeroModel)attackModel).Defense += data.GrowDefense;
                                ((HeroModel)attackModel).MaxHp   += data.GrowHp;
                                ((HeroModel)attackModel).MaxMp   += data.GrowMp;
                            }
                            //给客户端发送 这个数据模型 attackModel 客户端更新
                            room.Brocast(OpCode.FightCode, OpFight.UpdateModel, 0, "更新数据模型", null,
                                         JsonMapper.ToJson((HeroModel)attackModel));
                        }
                        //移除小兵
                        room.RemoveDog(item);
                        #endregion
                        break;

                    case ModelType.BUILD:
                        #region 建筑
                        //判断是不是英雄击杀
                        if (attackModel.Id >= 0)
                        {
                            //加钱
                            ((HeroModel)attackModel).Money += 150;
                            //给客户端发attackModel
                            room.Brocast(OpCode.FightCode, OpFight.UpdateModel, 0, "更新数据模型", null, JsonMapper.ToJson((HeroModel)attackModel));
                        }
                        //检测防御塔是否可重生
                        if (((BuildModel)item).Rebirth)
                        {
                            //开启一个定时任务 在指定的事件之后复活
                            room.StartSchedule(DateTime.UtcNow.AddSeconds((double)((BuildModel)item).RebirthTime),
                                               () =>
                            {
                                //满状态
                                ((BuildModel)item).CurrHp = ((BuildModel)item).MaxHp;
                                //给客户端发送一个复活的消息 参数 item
                                room.Brocast(OpCode.FightCode, OpFight.Resurge, 1, "有模型复活", null, JsonMapper.ToJson((BuildModel)item));
                            });
                        }
                        //不重生 直接移除数据模型
                        else
                        {
                            room.RemoveBuild((BuildModel)item);
                        }
                        //游戏结束的判断
                        if (item.Id == -10)
                        {
                            onGameOver(room, 2);
                        }
                        else if (item.Id == -20)
                        {
                            onGameOver(room, 1);
                        }
                        #endregion
                        break;

                    case ModelType.HERO:
                        #region 英雄

                        //发奖励
                        if (attackModel.Id >= 0)
                        {
                            //加杀人数
                            ((HeroModel)attackModel).Kill++;
                            //加钱
                            ((HeroModel)attackModel).Money += 300;
                            //加经验
                            ((HeroModel)attackModel).Exp += 50;
                            //检测是否升级
                            if (((HeroModel)attackModel).Exp > ((HeroModel)attackModel).Level * 100)
                            {
                                //升级
                                ((HeroModel)attackModel).Level++;
                                ((HeroModel)attackModel).Points++;
                                //重置经验值
                                ((HeroModel)attackModel).Exp = 0;
                                HeroDataModel data = HeroData.GetHeroData(attackModel.Id);
                                //英雄成长属性 增加
                                ((HeroModel)attackModel).Attack  += data.GrowAttack;
                                ((HeroModel)attackModel).Defense += data.GrowDefense;
                                ((HeroModel)attackModel).MaxHp   += data.GrowHp;
                                ((HeroModel)attackModel).MaxMp   += data.GrowMp;
                            }
                            //给客户端发送 这个数据模型 attackModel 客户端更新
                            room.Brocast(OpCode.FightCode, OpFight.UpdateModel, 0, "更新数据模型", null, JsonMapper.ToJson((HeroModel)attackModel));
                        }
                        //目标英雄死亡
                        //加死亡数
                        ((HeroModel)item).Dead++;
                        //开启一个定时任务 在指定的事件之后复活
                        room.StartSchedule(DateTime.UtcNow.AddSeconds(((HeroModel)attackModel).Level * 5),
                                           () =>
                        {
                            //满状态
                            ((HeroModel)item).CurrHp = ((HeroModel)item).MaxHp;
                            //给客户端发送一个复活的消息 参数 item
                            room.Brocast(OpCode.FightCode, OpFight.Resurge, 0, "有模型复活", null, JsonMapper.ToJson((HeroModel)item));
                        });


                        #endregion
                        break;

                    default:
                        break;
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 响应伤害请求
        /// </summary>
        /// <param name="request"></param>
        private void OnDamage(MobaClient client, int attackId, int skillId, int[] targetIds)
        {
            //1、获取房间
            int       playerId = playerCache.GetId(client);
            FightRoom room     = fightCache.GetRoom(playerId, client);
            //2、谁攻击谁
            DogModel attackModel = null;

            if (attackId >= 0)
            {
                //英雄攻击
                attackModel = room.GetHeroModel(attackId);
            }
            else if (attackId <= -100 && attackId > -300)
            {
                //防御塔的攻击
                attackModel = room.GetBuildModel(attackId);
            }
            else if (attackId <= -1000)
            {
                //小兵的攻击
            }

            DogModel[] targetModels = new DogModel[targetIds.Length];
            for (int i = 0; i < targetIds.Length; i++)
            {
                int targetId = targetIds[i];
                if (targetId >= 0)
                {
                    //英雄攻击
                    targetModels[i] = room.GetHeroModel(targetId);
                }
                else if (targetId <= -100 && targetId > -300)
                {
                    //防御塔的攻击
                    targetModels[i] = room.GetBuildModel(targetId);
                }
                else if (targetId <= -1000)
                {
                    //小兵的攻击
                    targetModels[i] = room.GetDogModel(targetId);
                }
            }

            //3、根据技能id 普通攻击 特殊技能
            ISkill             skill   = null;
            List <DamageModel> damages = null;

            if (skillId == 1)
            {
                //普通攻击
                skill = DamageData.GetSkill(skillId);
                //4、根据技能id获取damage计算伤害
                damages = skill.Damage(skillId, 0, attackModel, targetModels);
                //5、检测死亡 给钱 给经验
            }
            else
            {
                //特殊技能
                skill = DamageData.GetSkill(skillId);
                //4、根据技能id获取damage计算伤害
                HeroModel  hero       = attackModel as HeroModel;
                SkillModel skillModel = hero.GetSkillModel(skillId);
                damages = skill.Damage(skillId, skillModel.Level, attackModel, targetModels);
                //5、检测死亡 给钱 给经验
            }

            //6、给房间内的客户端广播数据模型
            room.Brocast(OpCode.FightCode, OpFight.Damage, 0, "有伤害产生", null, JsonMapper.ToJson(damages.ToArray()));

            //结算
            foreach (DogModel item in targetModels)
            {
                if (item.CurrHp == 0)
                {
                    switch (item.ModelType)
                    {
                    case ModelType.HERO:

                        #region 英雄
                        if (attackModel.Id >= 0)
                        {
                            ((HeroModel)attackModel).Kill++;
                            ((HeroModel)attackModel).AddProprity(300, 50);
                            if (((HeroModel)attackModel).Exp > ((HeroModel)attackModel).Level * 100)
                            {
                                ((HeroModel)attackModel).Level++;
                                ((HeroModel)attackModel).SkillPoints++;
                                ((HeroModel)attackModel).Exp = 0;

                                HeroDataModel data = HeroData.GetHeroData(attackModel.Id);
                                ((HeroModel)attackModel).Attack  += data.GrowAttack;
                                ((HeroModel)attackModel).Defense += data.GrowDefens;
                                ((HeroModel)attackModel).MaxHp   += data.GrowHp;
                                ((HeroModel)attackModel).MaxMp   += data.GrowMp;
                            }
                            //给客户端发送数据模型 attackModel
                            room.Brocast(OpCode.FightCode, OpFight.UpdateModel, 0, "更新数据模型", null, JsonMapper.ToJson(attackModel as HeroModel));
                        }

                        //目标英雄死亡
                        ((HeroModel)item).Dead++;
                        //开启定时任务 复活
                        room.StartSchedule(DateTime.UtcNow.AddSeconds(
                                               (double)((HeroModel)item).Level * 5),
                                           () =>
                        {
                            ((HeroModel)item).CurrHp = ((HeroModel)item).MaxHp;
                            //给客户端发送复活消息
                            room.Brocast(OpCode.FightCode, OpFight.Resurge, 0, "有模型复活了", null, JsonMapper.ToJson(item as HeroModel));
                        });
                        #endregion
                        break;

                    case ModelType.BUILD:
                        #region 建筑
                        //判断是否英雄击杀
                        if (attackModel.Id >= 0)
                        {
                            ((HeroModel)attackModel).AddProprity(150, 0);
                            //给客户端发送数据模型 attackModel
                            room.Brocast(OpCode.FightCode, OpFight.UpdateModel, 0, "更新数据模型", null, JsonMapper.ToJson(attackModel as HeroModel));
                        }
                        //防御塔是否可以重生
                        if (((BuildModel)item).ReBirth)
                        {
                            //开启定时任务 复活塔
                            room.StartSchedule(DateTime.UtcNow.AddSeconds(
                                                   (double)(((BuildModel)item).ReBirthTime)),
                                               () =>
                            {
                                ((BuildModel)item).CurrHp = ((BuildModel)item).MaxHp;
                                //给客户端发送复活消息
                                room.Brocast(OpCode.FightCode, OpFight.Resurge, 1, "有模型复活了", null, JsonMapper.ToJson(item as BuildModel));
                            });
                        }
                        else
                        {
                            room.RemoveBuild((BuildModel)item);
                        }
                        //游戏结束判断
                        if (item.Id == -100)
                        {
                            //队伍2赢了
                            OnGameOver(room, 2);
                        }
                        else if (item.Id == -200)
                        {
                            //队伍1赢了
                            OnGameOver(room, 1);
                        }
                        #endregion
                        break;

                    case ModelType.DOG:
                        #region 小兵
                        //英雄攻击
                        if (attackModel.Id >= 0)
                        {
                            ((HeroModel)attackModel).AddProprity(20, 10);
                            if (((HeroModel)attackModel).Exp > ((HeroModel)attackModel).Level * 100)
                            {
                                ((HeroModel)attackModel).Level++;
                                ((HeroModel)attackModel).SkillPoints++;
                                ((HeroModel)attackModel).Exp = 0;

                                HeroDataModel data = HeroData.GetHeroData(attackModel.Id);
                                ((HeroModel)attackModel).Attack  += data.GrowAttack;
                                ((HeroModel)attackModel).Defense += data.GrowDefens;
                                ((HeroModel)attackModel).MaxHp   += data.GrowHp;
                                ((HeroModel)attackModel).MaxMp   += data.GrowMp;
                            }
                            //给客户端发送数据模型 attackModel
                            room.Brocast(OpCode.FightCode, OpFight.UpdateModel, 0, "更新数据模型", null, JsonMapper.ToJson(attackModel as HeroModel));
                        }
                        //移除小兵
                        room.RemoveDog(item);
                        #endregion
                        break;

                    default:
                        break;
                    }
                }
            }
        }