예제 #1
0
        private void GameForm_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            if (Options.smoothing)
            {
                g.SmoothingMode = SmoothingMode.HighQuality;
            }

            if (Options.pixelOffset)
            {
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            }

            GameLoop.Manage(g);
            Text = originalText + GameLoop.fps + " FPS";
            Application.DoEvents();
            Invalidate(false);
        }
예제 #2
0
        public static async void ConnectAsync()
        {
            if (IsConnected())
            {
                return;
            }

            remotePlayers.Clear();

            connection = new HubConnectionBuilder()
                         .WithUrl(scheme + host + path + "/?name=" + Options.name)
                         .WithAutomaticReconnect()
                         .Build();

            connection.Closed += (error) =>
            {
                GameState.Instance.State = ClientState.Menu;
                return(Task.CompletedTask);
            };

            connection.On <DateTime, LevelType, int>("StartGame", (startAt, levelType, spawnPos) =>
            {
                GameLoop.StartGame(startAt, levelType, spawnPos);
                foreach (var player in remotePlayers)
                {
                    player.Value.Spawn();
                    GameObject.Instantiate(player.Value);
                }
            });

            connection.On <string, PlayerStats>("OnNewConnection", (connectionId, stats) =>
            {
                if (connectionId == connection.ConnectionId)
                {
                    return;
                }

                RemotePlayer player = new RemotePlayer()
                {
                    name     = stats.name,
                    isReady  = stats.isReady,
                    tankType = stats.tankType,
                };

                if (remotePlayers.TryAdd(connectionId, player))
                {
                    RemotePlayerChange(remotePlayers.Count - 1, player);
                }
            });

            connection.On <string>("OnDisconnectedConnection", (connectionId) =>
            {
                if (connectionId == connection.ConnectionId)
                {
                    return;
                }

                int index = FindIndex(connectionId);
                if (remotePlayers.TryRemove(connectionId, out RemotePlayer player))
                {
                    RemotePlayerChange(index, null);
                    player.Despawn();
                    player.Destroy();
                }
            });

            connection.On <string, string>("ReceiveMessage", (name, message) =>
            {
                MessageReceive(name, message);
            });

            connection.On <string, string>("OnSetName", (connectionId, name) =>
            {
                if (remotePlayers.TryGetValue(connectionId, out RemotePlayer player))
                {
                    player.name = name;
                    RemotePlayerChange(FindIndex(connectionId), player);
                }
            });

            connection.On <string>("OnOverrideName", (name) =>
            {
                Options.name = name;
            });

            connection.On <string, bool>("OnSetIsReady", (connectionId, isReady) =>
            {
                if (remotePlayers.TryGetValue(connectionId, out RemotePlayer player))
                {
                    player.isReady = isReady;
                    RemotePlayerChange(FindIndex(connectionId), player);
                }
            });

            connection.On <string, TankType>("OnSetTankType", (connectionId, type) =>
            {
                if (remotePlayers.TryGetValue(connectionId, out RemotePlayer player))
                {
                    player.tankType = type;
                }
            });

            connection.On <LevelType>("OnSetLevelType", (levelType) =>
            {
                LevellTypeChange(levelType);
            });

            connection.On <float, float, float, ProjectileStats>("OnCreateProjectile", (x, y, r, stats) =>
            {
                Transform tr = new Transform()
                {
                    position = new Vector2(x, y),
                    rotation = r,
                };

                Projectile projectile = new Projectile(tr)
                {
                    damage      = stats.damage,
                    speed       = stats.speed,
                    bounceAngle = stats.bounceAngle,
                    bounceCount = stats.bounceCount,
                };

                projectile.SetPosition(tr);
                GameObject.Instantiate(projectile);
            });

            connection.On <string, float, float, float, float>("OnPositionUpdate", (connectionId, x, y, r1, r2) =>
            {
                if (remotePlayers.TryGetValue(connectionId, out RemotePlayer player))
                {
                    player.SetPosition(x, y, r1, r2);
                }
            });

            connection.On("DisconnectClient", () =>
            {
                DisconnectAsync();
            });

            try
            {
                await connection.StartAsync();

                SetNameAsync(Options.name);
                SetTankTypeAsync(GameState.Instance.tankType);

                GameState.Instance.State = ClientState.Connected;
                Debug.WriteLine("Connection started");
            }
            catch (Exception e)
            {
                GameState.Instance.State = ClientState.Menu;
                Debug.WriteLine("Error connecting: " + e.Message);
            }
        }