Esempio n. 1
0
    private void OnReqHit(UserToken token, SocketModel model)
    {
        ReqHit req = SerializeUtil.Deserialize <ReqHit>(model.message);

        if (req.bulletPos == null)
        {
            return;
        }

        Random r   = new Random();
        var    atk = r.Next(10, 20);

        AccountData acc   = CacheManager.instance.GetAccount(token.accountid);
        AccountData enemy = CacheManager.instance.GetAccount(req.enemy);

        if (acc.battleType == EBattle.Arena && acc.team == enemy.team)
        {
            return;
        }

        Battle battle = CacheManager.instance.GetBattle(acc.battleid, acc.battleType);

        if (battle == null)
        {
            return;
        }

        Tank t        = battle.GetTank(req.enemy);
        Tank attacker = battle.GetTank(acc.account);

        if (t != null && t.hp > 0 && attacker != null && attacker.hp > 0)
        {
            Console.WriteLine(Vector3.Distance(Tools.ToLunaVec3(req.bulletPos), t.pos));
            //校验子弹和敌人的坐标
            if (Vector3.Distance(Tools.ToLunaVec3(req.bulletPos), t.pos) > 2.5f)
            {
                return;
            }
            // Console.WriteLine(Time.time * 0.001f - attacker.lastHitTime);
            ////校验两次命中时间
            // if (Time.time * 0.001f - attacker.lastHitTime < 0.7f) return;

            attacker.lastHitTime = Time.time * 0.001f;

            t.hp -= atk;
            //伤害统计
            acc.hurt += atk;

            NotifyHit notify = new NotifyHit();
            notify.id = req.enemy;

            notify.damage = atk;

            MsgSender.SendAll <NotifyHit>(battle.accounts, (int)MsgID.NotifyHit, notify);
            //死亡
            if (t.hp <= 0)
            {
                t.hp = 0;
                acc.killCount++;
                enemy.deathCount++;

                //通知所有人有个坦克死了
                NotifyDeath not = new NotifyDeath();
                not.id = req.enemy;

                MsgSender.SendAll <NotifyDeath>(battle.accounts, (int)MsgID.NotifyDeath, not);
                if (battle.type == EBattle.Survival)
                {
                    SurvivalTankDeath(acc, enemy, battle);
                }
                else
                {
                    ArenaTankDeath(acc, enemy, battle);
                }
            }
        }
    }