private void StartupGame(GameSession session)
        {
            session.State = GameState.LoadState;
            session.Players.ForEach(p => p.State = PlayerState.Loading);
            try
            {
                var mapLoader = new MapFromImageParser(session.SelectedMap);
                session.MapData = mapLoader.ParseImage();

                session.MapHandler = new ServerMap(session);

                if (session.Players.Count == 1)
                {
                    AddRace(session, 1, 0, 0);
                    //AddRace(session, 0, 1, 0);
                    //AddRace(session, 2, 2, 1);
                    //AddRace(session, 3, 3, 1);
                }

                session.GameLoop = new GameLoop(_gr, session);
            }
            catch (Exception ex)
            {
                TStuffLog.Error(ex.ToString());
            }
            session.Players.ForEach(p => p.User.Send(RequestNames.GetMapData, session.MapData));
        }
        private void ProcessGame(double delta, double total)
        {
            switch (_session.State)
            {
            case GameState.LoadState:
                DoLoadStep(delta);
                break;

            case GameState.PlayState:
                try
                {
                    PreUpdate(delta);
                    Update(delta);
                    PostUpdate(delta);
                }
                catch (Exception ex)
                {
                    TStuffLog.Error(ex.ToString());
                }
                break;

            case GameState.FinishState:
                break;
            }
        }
        public void Update(double delta)
        {
            var targetToRemove = Targets.Where(a => a.IsDead || !IsInDistance(X, Y, a.X, a.Y)).ToList();

            targetToRemove.ForEach(w =>
            {
                Targets.Remove(w);
                hasChanged = true;
            });

            if (_fireCooldown > 0)
            {
                _fireCooldown -= delta;
            }

            if (Targets.Count < TowerData.MaxTargets)
            {
                try
                {
                    SearchTarget();
                }
                catch (Exception ex)
                {
                    TStuffLog.Error(ex.ToString());
                }
            }

            Targets.ForEach(t =>
            {
                if (_fireCooldown <= 0)
                {
                    _fireCooldown = TowerData.FireSpeed;
                    AttackTarget(t);
                }
                IsFire     = true;
                hasChanged = true;
            });

            if (Targets.Count == 0)
            {
                IsFire     = false;
                hasChanged = true;
            }
        }
        private void SendUpdate()
        {
            if (_fastUpdateObjectQueue.Count == 0)
            {
                return;
            }
            var fu = _fastUpdateObjectQueue.Pop();

            if (fu == null)
            {
                return;
            }

            _session.Players.ForEach(p =>
            {
                try
                {
                    p.User.Send(RequestNames.FastUpdate, fu);
                }
                catch (Exception ex) { TStuffLog.Error(ex.StackTrace); }
            });
        }
예제 #5
0
        public void LeaveGame(Connection connection)
        {
            var session = GetSessionByConnection(connection);
            var user    = _gr.User.GetUserByConnection(connection);

            if (session != null)
            {
                var player = (session?.Players).SingleOrDefault(a => a.User.Id == user.Id);
                if (player == null)
                {
                    return;
                }
                if (player.Owner)
                {
                    CancelGame(session);
                }
                else
                {
                    session.Players.Remove(player);
                    foreach (var team in session.Teams)
                    {
                        team.Value.Remove(player);
                    }
                    try
                    {
                        user.Send(RequestNames.GameNotification,
                                  new GameNotifications {
                            Notification = GameNotificationType.GameCanceled
                        });
                    }
                    catch (Exception ex) { TStuffLog.Error("Notification GameCancle " + ex); }
                    session.Players.ForEach(p =>
                    {
                        p.User.Send(RequestNames.GameJoinInfo, GetGameJoinInfo(session));
                    });
                }
            }
        }
        public GameLoop(GlobalRegister gr, GameSession session)
        {
            TStuffLog.Info("Start Gameloop");
            _gr      = gr;
            _session = session;
            InitializeGameLoopData();



            _startTime = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds;
            _lastTime  = _startTime;
            _gameTimer = new Timer(state =>
            {
                var delta = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds - _lastTime;
                var total = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds - _startTime;
                MaintainGameSession(delta);
                ProcessGame(delta, total);
                _lastTime = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds;
            }, null, 0, GConfig.D.LogicUpdateEachMillisecond);
            _startSendTime   = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds;
            _lastSendTime    = _startTime;
            _sendUpdateTimer = new Timer(state =>
            {
                var delta = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds - _lastSendTime;
                var total = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds - _startSendTime;
                try
                {
                    SendDataState(delta);
                }
                catch (Exception ex)
                {
                    TStuffLog.Error(ex.ToString());
                }
                _lastSendTime = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds;
            }, null, 0, GConfig.D.SendLoopMilliseconds);
        }
예제 #7
0
        private void CancelGame(GameSession session)
        {
            session.Players.ForEach(p =>
            {
                try
                {
                    p.User.CurrentSession = null;
                    p.User.Send(RequestNames.GameNotification,
                                new GameNotifications {
                        Notification = GameNotificationType.GameCanceled
                    });
                }
                catch (Exception ex)
                {
                    TStuffLog.Error(ex.ToString());
                }
            });

            session.Players.Clear();
            session.GameMobs.Clear();
            session.GameTower.Clear();
            session.Dispose();
            Sessions.Remove(session);
        }