示例#1
0
        const string Format_finalAttack = "finalAttack@{0}:{1}:{2}";  //参数:对象-掉落个数-击退方向(未单位化)

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="paramName"></param>
        /// <param name="value"></param>
        public void updateData(string paramName, string value, PlayerData player)
        {
            switch (paramName)
            {
            case "DValue":
                int data = int.Parse(value);
                lastModifyTime_DValue = ServerFunction.getTimeStamp_milSeconds();
                //最后一击
                if (D_Value >= maxDValue && data > 0)
                {
                    D_Value = 0.5f * maxDValue;
                    string t = getFinalAttack(player.Pos);
                    msgQueue.Enqueue(t);
                }
                D_Value += data;
                break;

            case "Score":
                int data2 = int.Parse(value);
                //产生消息
                string t2 = getSacrifice(this, carryScore, data2);
                msgQueue.Enqueue(t2);
                //完成一次献祭
                Score     += carryScore;
                carryScore = 0;
                break;

            default:
                break;
            }
        }
示例#2
0
        /// <summary>
        /// 将事件放入待回答队列
        /// </summary>
        void AddEvent(Event_waitSend t)
        {
            Event_waitAnswer t_a = new Event_waitAnswer();

            t_a.data    = t;
            t_a.endTime = ServerFunction.getTimeStamp_milSeconds() + maxWaitTime_event;
            answerEventList.Add(t.index, t_a);
        }
示例#3
0
 public void getDamage(int damage, Vector3 shootPos)
 {
     lastModifyTime_DValue = ServerFunction.getTimeStamp_milSeconds();
     //最后一击
     if (D_Value >= maxDValue && damage > 0)
     {
         D_Value = 0.5f * maxDValue;
         string t = getFinalAttack(shootPos);
         msgQueue.Enqueue(t);
     }
     D_Value += damage;
 }
示例#4
0
        public GamePlay(Room rm)
        {
            name = rm.name;

            Players = new PlayerData[maxPlayer];
            for (int i = 0; i < maxPlayer; i++)
            {
                Players[i] = new PlayerData();
            }
            //初始化随机种子
            Random rd = new Random();

            rand = rd.Next(1, 1000);
            //初始化玩家数据
            Players[0].Camp   = 0;
            Players[0].client = rm.RoomOwner;
            Players[1].Camp   = 1;
            Players[1].client = rm.OP_client;

            Players[0].client.socket.BeginReceive(Players[0].client.data, 0, Players[0].client.data.Length, SocketFlags.None, new AsyncCallback(Callback_Receive), Players[0]);
            Players[1].client.socket.BeginReceive(Players[1].client.data, 0, Players[1].client.data.Length, SocketFlags.None, new AsyncCallback(Callback_Receive), Players[1]);
            Players[0].client.isGame = true;
            Players[1].client.isGame = true;

            Thread_game = new Thread(Game);
            Thread_game.IsBackground = true;
            Thread_game.Start();

            for (int i = 0; i < maxPlayer; i++)
            {
                Players[i].gameplayRoom = this;
                SendInitData(Players[i]);
            }
            for (int i = 0; i < Stronghold.Length; i++)
            {
                Stronghold[i] = new StrongholdInfo();
            }

            lastTime = ServerFunction.getTimeStamp_milSeconds();

            //初始化单位信息字典
            for (int i = 0; i < UnitDic.Length; i++)
            {
                UnitDic[i] = new Dictionary <string, UnitInfo>();
            }
        }
示例#5
0
        /// <summary>
        /// 更新
        /// </summary>
        public void Update(float deltaTime)
        {
            long currentStamp = ServerFunction.getTimeStamp_milSeconds();

            //DValue更新
            if (lastModifyTime_DValue + config.MaxRecoverTime_DValue < currentStamp)
            {
                if (D_Value > 0)
                {
                    D_Value -= (deltaTime * RecoverRate_DValue);
                    if (D_Value < 0)
                    {
                        D_Value = 0;
                    }
                    if (D_Value > maxDValue)
                    {
                        D_Value = maxDValue;
                    }
                }
            }
        }
示例#6
0
        /// <summary>
        /// 游戏
        /// </summary>
        /// <param name="rm"></param>
        public void Game()
        {
            //等待玩家初始化完成
            while (true)
            {
                int t = 0;
                for (int i = 0; i < maxPlayer; i++)
                {
                    if (isReady[i])
                    {
                        t++;
                    }
                }
                if (t == maxPlayer)
                {
                    //开始游戏
                    for (int i = 0; i < maxPlayer; i++)
                    {
                        Players[i].client.Send("BG");
                    }
                    break;
                }
            }

            while (!isFinish)
            {
                //更新玩家数据
                for (int i = 0; i < Players.Length; i++)
                {
                    Players[i].Update(deltaTime);
                }
                //更新单位状态
                if (UpdateBehavious != null)
                {
                    UpdateBehavious(Time, deltaTime);
                }

                //发送玩家事件
                int    c      = 0;
                string data_1 = "BM";
                for (int i = 0; i < Players.Length; i++)
                {
                    if (Players[i].msgQueue.Count > 0)
                    {
                        data_1 += Players[i].getMsg();
                        c++;
                    }
                }
                if (c > 0)
                {
                    BroadcastMsg(data_1);
                }

                //处理待发事件队列
                if (sendEventQueue.Count > 0)
                {
                    string t_msg = "GE";
                    while (sendEventQueue.Count > 0)
                    {
                        Event_waitSend t = sendEventQueue.Dequeue();
                        t_msg += t.ToString();
                        AddEvent(t);
                    }
                    BroadcastMsg(t_msg);
                }

                //处理待回答事件列表
                long currentTimeStamp = ServerFunction.getTimeStamp_milSeconds();
                if (answerEventList.Count > 0)
                {
                    Dictionary <int, Event_waitAnswer> .KeyCollection keysCollect = null;
                    keysCollect = answerEventList.Keys;
                    int[] keys = new int[keysCollect.Count];
                    keysCollect.CopyTo(keys, 0);
                    for (int i = 0; i < keys.Length; i++)
                    {
                        Event_waitAnswer t = answerEventList[keys[i]];
                        //超时
                        if (currentTimeStamp > t.endTime)
                        {
                            //如果超过一半玩家应答则予以回应
                            if (t.getAnswerNum() > maxPlayer * 0.5f)
                            {
                                int    t_answer = t.Answer();
                                string t_data   = string.Format("EA {0}:{1}", t.data.selfIndex, t_answer);
                                t.data.client.Send(t_data);
                            }
                            Console.WriteLine("Timeout");
                            answerEventList.Remove(keys[i]);
                        }
                    }
                }


                //获取玩家信息包
                string data = "PD ";
                for (int i = 0; i < Players.Length; i++)
                {
                    data += (getPlayerData(i));
                    if (i != Players.Length - 1)
                    {
                        data += "#";
                    }
                }

                //发送玩家信息包
                int j = 0;
                for (int i = 0; i < Players.Length; i++)
                {
                    if (Players[i].client.isLink)
                    {
                        Players[i].client.Send(data);
                    }
                    else
                    {
                        j++;
                    }
                }

                //检测是否所有玩家都已掉线
                if (j == maxPlayer)
                {
                    Console.WriteLine(name + "_房间结束游戏");
                    isFinish = true;
                    break;
                }

                //更新时间参数
                deltaTime = (currentTimeStamp - lastTime) / 1000.0f;
                lastTime  = currentTimeStamp;
                Time     += deltaTime;

                //检测是否结束游戏
                int t3 = 0;
                for (int i = 0; i < Players.Length; i++)
                {
                    if (Players[i].isEnd)
                    {
                        t3++;
                    }
                }
                if (t3 == Players.Length)
                {
                    //结束游戏
                    Console.WriteLine(name + "_房间结束游戏");
                    string data_EG = getEndGameData();
                    BroadcastMsg(data_EG);
                    break;
                }

                Thread.Sleep(50);
            }

            while (true)
            {
                //检测是否所有玩家都已退出
                int j = 0;
                for (int i = 0; i < Players.Length; i++)
                {
                    if (!Players[i].client.isLink && !Players[i].isLeftGameRoom)
                    {
                        Players[i].isLeftGameRoom = true;
                    }

                    if (Players[i].isLeftGameRoom)
                    {
                        j++;
                    }
                }

                if (j == maxPlayer)
                {
                    isFinish = true;
                    break;
                }

                Thread.Sleep(1000);
            }
        }