예제 #1
0
        /// <summary>
        /// Начинает новую игру
        /// </summary>
        internal void NewGame(byte size)
        {
            var oldGame = CurrentGame;

            CurrentGame = new Game(size);
            GameChanged?.Invoke(oldGame, CurrentGame);
        }
예제 #2
0
        public void addApple()
        {
            Random rnd   = new Random();
            Point  apple = new Point();

            apple.X = rnd.Next(0, (BoardSize.Width - DOTSIZE) / DOTSIZE);
            apple.X = (DOTSIZE / 2) + (apple.X * DOTSIZE);
            apple.Y = rnd.Next(0, (BoardSize.Height - DOTSIZE) / DOTSIZE);
            apple.Y = (DOTSIZE / 2) + (apple.Y * DOTSIZE);
            Apples.Add(apple);
            GameChanged?.Invoke();
        }
예제 #3
0
        public void Move()
        {
            var point = Snake.Location[0];

            if (HitWall())
            {
                gameOver();
                return;
            }

            bool hit = false;

            if (HitApple())
            {
                Point apple = Apples.First((app) => app == Snake.Location[0]);
                Apples.Remove(apple);
                hit = true;
            }

            for (int z = Snake.Length - 1; z > 0; z--)
            {
                Snake.Location[z] = new Point(Snake.Location[z - 1].X, Snake.Location[z - 1].Y);
            }

            if (hit)  //this happens if we ate an apple
            {
                Snake.Location.Add(lastLocation);
                Snake.Length++;
            }

            if (SnakeDirection == Direction.Left)
            {
                Snake.Location[0] = new Point(point.X -= DOTSIZE, point.Y);
            }
            else if (SnakeDirection == Direction.Right)
            {
                Snake.Location[0] = new Point(point.X += DOTSIZE, point.Y);
            }
            else if (SnakeDirection == Direction.Up)
            {
                Snake.Location[0] = new Point(point.X, point.Y -= DOTSIZE);
            }
            else if (SnakeDirection == Direction.Down)
            {
                Snake.Location[0] = new Point(point.X, point.Y += DOTSIZE);
            }
            lastLocation = Snake.Location.Last();
            GameChanged?.Invoke();
        }
예제 #4
0
        private void Select(int newSelectedCardIndex)
        {
            if (newSelectedCardIndex == selectedIdx)
            {
                return;
            }

            for (int i = 0; i < container.Children.Count; ++i)
            {
                AnimateCard_SelectionChanged(i, newSelectedCardIndex, selectedIdx);
            }

            selectedIdx = newSelectedCardIndex;

            GameChanged?.Invoke(this, Game);
        }
예제 #5
0
        public string SlotClicked(Slot slot)
        {
            bool hasMoveSucceded = false;

            if (SrcOrDst == SlotClickPurpose.Source)
            {
                try
                {
                    hasMoveSucceded = Game.ChooseSource(slot.Id, out message);
                    if (hasMoveSucceded == true)
                    {
                        LastSlotIdSource = slot.Id;
                    }
                }
                catch (Exception e)
                {
                    return(e.Message);
                }
            }
            else //SlotIdDestination
            {
                try
                {
                    hasMoveSucceded = Game.ChooseDestination(slot.Id, out message);
                    if (hasMoveSucceded == true)
                    {
                        LastSlotIdDestination = slot.Id;
                        GameChanged?.Invoke(Game, new OnInGameEventArgs {
                            SlotIdSource = LastSlotIdSource, SlotIdDestination = LastSlotIdDestination
                        });
                    }
                }
                catch (Exception e)
                {
                    return(e.Message);
                }
            }
            updateSlotClickPurpose(hasMoveSucceded);
            return(message);
        }
예제 #6
0
 private void OnGameChanged(GameData data)
 {
     GameChanged?.Invoke(this, new GameChangedEventArgs(data));
 }
예제 #7
0
        public async Task OpenAsync(string userName, CancellationToken cancellationToken)
        {
            _cookieContainer   = new CookieContainer();
            _httpClientHandler = new HttpClientHandler {
                CookieContainer = _cookieContainer
            };

            _progressMessageHandler = new ProgressMessageHandler(_httpClientHandler);
            _progressMessageHandler.HttpSendProgress += MessageHandler_HttpSendProgress;

            _client = new HttpClient(_progressMessageHandler)
            {
                Timeout = TimeSpan.FromMinutes(PackageUploadTimelimitInMinutes)
            };
            _client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");

            var token = await AuthenticateUserAsync(userName, "", cancellationToken);

            _login = userName;

            _connection = new HubConnectionBuilder()
                          .WithUrl($"{ServerAddress}/sionline?token={token}", options =>
            {
                options.AccessTokenProvider = () => Task.FromResult(Convert.ToBase64String(Encoding.UTF8.GetBytes(_login)));
            })
                          .WithAutomaticReconnect()
                          .AddMessagePackProtocol()
                          .Build();

            _connection.Reconnecting += async e =>
            {
                if (Reconnecting != null)
                {
                    await Reconnecting(e);
                }
            };

            _connection.Reconnected += async s =>
            {
                if (Reconnected != null)
                {
                    await Reconnected(s);
                }
            };

            _connection.Closed += OnConnectionClosed;

            _connection.HandshakeTimeout = TimeSpan.FromMinutes(1);

            _connection.On <string, string>("Say", (user, text) => OnUI(() => Receieve?.Invoke(user, text)));
            _connection.On <SI.GameServer.Contract.GameInfo>("GameCreated", (gameInfo) => OnUI(() => GameCreated?.Invoke(ToSICoreGame(gameInfo))));
            _connection.On <int>("GameDeleted", (gameId) => OnUI(() => GameDeleted?.Invoke(gameId)));
            _connection.On <SI.GameServer.Contract.GameInfo>("GameChanged", (gameInfo) => OnUI(() => GameChanged?.Invoke(ToSICoreGame(gameInfo))));
            _connection.On <string>("Joined", (user) => OnUI(() => Joined?.Invoke(user)));
            _connection.On <string>("Leaved", (user) => OnUI(() => Leaved?.Invoke(user)));

            await _connection.StartAsync(cancellationToken);
        }
예제 #8
0
 private void OnGameChanged(GameDto gameDto)
 {
     GameChanged?.Invoke(this, gameDto);
 }
 void IGameEngineEventHandler.GameChanged(GameDto gameDto)
 {
     GameChanged?.Invoke(this, gameDto);
 }