Example #1
0
        public static NetOutgoingMessage CreateGameMessage()
        {
            var msg = Networking.CreateMessage();

            msg.Write((byte)MainScreen.Side.GAME_SIDE);
            msg.Write((byte)MessageType.GAME);

            return(msg);
        }
Example #2
0
        public void KillEntity(int id)
        {
            var msg = Networking.CreateMessage();

            msg.Write((byte)GameScreen.ServerHeaders.KILL_ENTITY);

            msg.Write(id);
            Networking.SendMessage(msg, NetDeliveryMethod.ReliableOrdered, 1);

            NPCs.RemoveAt(id);
        }
Example #3
0
        public static void SendGameMessage(Action <NetOutgoingMessage> func,
                                           NetDeliveryMethod method = NetDeliveryMethod.ReliableOrdered,
                                           int channel = 0)
        {
            var msg = Networking.CreateMessage();

            msg.Write((byte)MainScreen.Side.GAME_SIDE);
            msg.Write((byte)MessageType.GAME);

            func(msg);

            Networking.SendMessage(msg, method, channel);
        }
Example #4
0
        public void SetTile(Tile tile, int x, int y, bool report = true, bool update = false)
        {
            if (!InBounds(x, y))
            {
                return;
            }

            var chunk = chunks[x / Chunk.size, y / Chunk.size];

            if (chunk == null)
            {
                return;
            }


            /*
             * Tile thisTile = GetTile(x, y);
             *
             * if (Networking.IsServer && report && tile.id == 0 && thisTile.id != 0)
             * {
             *  var item = new Item(this, thisTile);
             *
             *  item.transform.position = new Vector2((x + 0.25f) * Tile.size, (y + 0.25f) * Tile.size);
             *
             *  SpawnEntity(item);
             * }
             */


            chunk.SetTile(tile, x % Chunk.size, y % Chunk.size);

            if (Networking.IsServer && update)
            {
                toUpdate.Add(new Point(x, y));
                UpdateOthers(x, y);
            }


            if (report)
            {
                //Networking.SendMessage($"ST/{x},{y},{tile.id}");
                var msg = Networking.CreateMessage();

                msg.Write((byte)GameScreen.ServerHeaders.SET_TILE);
                msg.Write(x);
                msg.Write(y);
                msg.Write(tile.id);

                Networking.SendMessage(msg, NetDeliveryMethod.ReliableOrdered, 0);
            }
        }
Example #5
0
        public void SpawnEntity(NPC npc)
        {
            var msg = Networking.CreateMessage();

            msg.Write((byte)GameScreen.ServerHeaders.SPAWN_ENTITY);

            msg.Write((int)npc.GetNPCType());
            msg.Write(npc.transform.position.X);
            msg.Write(npc.transform.position.Y);
            msg.Write(npc.Tag);

            Networking.SendMessage(msg, NetDeliveryMethod.ReliableOrdered, 1);

            NPCs.Add(npc);
        }
Example #6
0
        public BoredGame(MainScreen _mainScreen, int _seed)
        {
            mainScreen = _mainScreen;
            seed       = _seed;

            Button resignBtn = new Button(this)
            {
                Position = new Point(412, 512),
                Size     = new Point(100, 50),
                Font     = Main.MediumFont,
                Text     = "Quit Game"
            };

            resignBtn.ClickEvent += (object s) =>
            {
                resignBtn.active = false;

                var msg = Networking.CreateMessage();
                msg.Write((byte)MainScreen.Side.GAME_SIDE);
                msg.Write((byte)MessageType.RESIGN);
                Networking.SendMessage(msg);

                var wait = new WaitingOverlay("You requested to quit the game. Waiting for a response...");

                wait.action += (bool quit) =>
                {
                    if (quit)
                    {
                        if (Networking.IsServer)
                        {
                            mainScreen.gameSide = new GameChoose(mainScreen);
                        }
                        else
                        {
                            mainScreen.gameSide = new WaitForChoose(mainScreen);
                        }
                    }
                    else
                    {
                        resignBtn.active = true;
                    }
                };

                overlay = wait;
            };

            components.Add(resignBtn);
        }
Example #7
0
        public void ReceiveMessage(NetIncomingMessage msg)
        {
            if (overlay != null && overlay.TakeControl && overlay is ICanReceive ics)
            {
                ics.ReceiveMessage(msg);
            }
            else
            {
                var messageType = (MessageType)msg.ReadByte();

                if (messageType == MessageType.RESIGN)
                {
                    var optionOverlay = new OptionsOverlay("Your opponent requested to quit the game." + Environment.NewLine + Environment.NewLine + "Do you accept?", new string[] { "Yes", "No" });
                    optionOverlay.action += (string ret) =>
                    {
                        var newMsg = Networking.CreateMessage();
                        newMsg.Write((byte)MainScreen.Side.GAME_SIDE);

                        if (ret == "Yes")
                        {
                            newMsg.Write(true);

                            if (Networking.IsServer)
                            {
                                mainScreen.gameSide = new GameChoose(mainScreen);
                            }
                            else
                            {
                                mainScreen.gameSide = new WaitForChoose(mainScreen);
                            }
                        }
                        else
                        {
                            newMsg.Write(false);
                        }


                        Networking.SendMessage(newMsg);
                    };

                    overlay = optionOverlay;
                }
                else
                {
                    RelayMessage2(msg);
                }
            }
        }
Example #8
0
        public override void ClientUpdate(GameTime gameTime)
        {
            if (me)
            {
                var msg = Networking.CreateMessage();

                msg.Write((byte)GameScreen.ClientHeaders.INPUT);
                msg.Write(input.Horizontal());
                msg.Write(input.Jump());

                Networking.SendMessage(msg, NetDeliveryMethod.ReliableSequenced, 1);

                world.camera.position = transform.Centre;
                //world.camera.Follow(transform.Centre, 1f);
            }
        }
Example #9
0
        public void GameEnd(string message)
        {
            if (Networking.IsServer)
            {
                var yesNo = new OptionsOverlay(message + Environment.NewLine + Environment.NewLine + "Play again?", new string[] { "Yes", "No" });
                yesNo.action += (string s) =>
                {
                    var msg = Networking.CreateMessage();
                    msg.Write((byte)MainScreen.Side.GAME_SIDE);

                    if (s == "Yes")
                    {
                        msg.Write(true);

                        ResetGame();
                    }
                    else
                    {
                        msg.Write(false);

                        mainScreen.gameSide = new GameChoose(mainScreen);
                    }

                    Networking.SendMessage(msg);
                };
                overlay = yesNo;
            }
            else
            {
                var waiting = new WaitingOverlay(message + Environment.NewLine + Environment.NewLine + "Waiting for host to make a decision...");

                waiting.action += (bool s) =>
                {
                    if (s)
                    {
                        ResetGame();
                        //overlay = null;
                    }
                    else
                    {
                        mainScreen.gameSide = new WaitForChoose(mainScreen);
                    }
                };

                overlay = waiting;
            }
        }
Example #10
0
        private void ButtonClick(object s)
        {
            string value = (s as Button).Text;

            int seed = Main.random.Next(int.MaxValue);
            //Console.WriteLine(white);

            var msg = Networking.CreateMessage();

            msg.Write((byte)MainScreen.Side.GAME_SIDE);
            msg.Write(value);
            //msg.Write(value + "," + (white ? "B" : "W"));
            msg.Write(seed);
            Networking.SendMessage(msg);

            mainScreen.LoadFromString(value, seed);
        }
Example #11
0
        public WaitingScreen(string text)
        {
            label = new Label(this)
            {
                Position  = new Point(Main.screenWidth / 2, Main.screenHeight / 2),
                Font      = Main.MediumFont,
                Text      = text,
                TextAlign = Label.TextAlignEnum.CENTRE_CENTRE
            };

            button = new Button(this)
            {
                Font = Main.MediumFont,
                Text = "Cancel",
                Size = new Point(100, 50)
            };

            button.ClickEvent += (object s) =>
            {
                Networking.Stop();
                Main.main.Screen = new ChooseScreen();
            };

            components.Add(label);
            components.Add(button);

            Networking.OnConnect += () =>
            {
                var msg = Networking.CreateMessage();
                msg.Write(Properties.Settings.Default.username);
                Networking.SendMessage(msg);

                /*
                 * button.active = false;
                 * label.Text = "You are now connected to " + PlayerConnection.connection.username + "!";
                 * explosion = new Explosion();
                 * explosion.Finished += () =>
                 * {
                 *  animDone = true;
                 *  //main.screen = new MainScreen();
                 * };
                 */
            };
        }
Example #12
0
        void ServerReceive(NetIncomingMessage msg)
        {
            ClientHeaders header = (ClientHeaders)msg.ReadByte();

            switch (header)
            {
            case ClientHeaders.SET_TILE:
                int sX   = msg.ReadInt32();
                int sY   = msg.ReadInt32();
                int sSet = msg.ReadInt32();

                world.SetTile(new Tile(sSet), sX, sY, false, true);

                break;

            case ClientHeaders.INPUT:
                int  hor  = msg.ReadInt32();
                bool jump = msg.ReadBoolean();

                OtherInput.hor = hor;
                OtherInput.PushJump(jump);

                break;

            case ClientHeaders.CHUNK_REQUEST:
                int cX = msg.ReadInt32();
                int cY = msg.ReadInt32();

                var newMsg = Networking.CreateMessage();

                newMsg.Write((byte)ServerHeaders.SEND_CHUNK);
                newMsg.Write(cX);
                newMsg.Write(cY);
                newMsg.Write(world.chunks[cX, cY].ToBytes());

                Networking.SendMessage(newMsg, NetDeliveryMethod.ReliableOrdered, 2);

                break;
            }
        }
Example #13
0
        public static void RequestChunk(int x, int y)
        {
            if (!Networking.Connected)
            {
                return;
            }

            Point p = new Point(x, y);

            if (requested.Contains(p))
            {
                return;
            }

            requested.Add(p);

            var requestMsg = Networking.CreateMessage();

            requestMsg.Write((byte)GameScreen.ClientHeaders.CHUNK_REQUEST);
            requestMsg.Write(x);
            requestMsg.Write(y);

            Networking.SendMessage(requestMsg, NetDeliveryMethod.ReliableOrdered, 2);
        }
Example #14
0
        //static readonly Matrix matrix = Matrix.CreateTranslation(left, 0, 0);

        public ChatScreen()
        {
            Matrix = Matrix.CreateTranslation(left, 0, 0);

            const int boxHeight = 50;
            const int btnWidth  = 50;

            components.Add(new Label(this)
            {
                Position  = new Point(10, barHeight / 2),
                Font      = Main.MediumFont,
                Text      = Settings.username,
                TextAlign = Label.TextAlignEnum.CENTRE_LEFT
            });

            Button disconnectBtn = new Button(this)
            {
                Position = new Point(r.Right - 100, r.Y),
                Size     = new Point(100, barHeight),
                Font     = Main.MediumFont,
                Text     = "Disconnect"
            };

            disconnectBtn.ClickEvent += (object s) =>
            {
                Main.main.Screen = new ChooseScreen();
                Networking.Stop();
            };

            TextBox inputTxt = new TextBox(this, Main.main.Window)
            {
                Position = new Point(0, Main.screenHeight - boxHeight),
                Size     = new Point(r.Width - btnWidth, boxHeight),
                Font     = Main.SmallFont,
            };

            Button sendBtn = new Button(this)
            {
                Font     = Main.MediumFont,
                Text     = "Send",
                Position = new Point(r.Right - btnWidth, Main.screenHeight - boxHeight),
                Size     = new Point(btnWidth, boxHeight)
            };

            inputTxt.EnterPressed += () => sendBtn.PerformClick();

            sendBtn.ClickEvent += (object s) =>
            {
                if (inputTxt.Text != "")
                {
                    ShowMessage("(" + Properties.Settings.Default.username + "): " + inputTxt.Text);
                    var msg = Networking.CreateMessage();

                    msg.Write((byte)MainScreen.Side.CHAT_SIDE);
                    msg.Write(inputTxt.Text);
                    Networking.SendMessage(msg);
                    inputTxt.Text = "";
                }
            };

            components.Add(disconnectBtn);
            components.Add(sendBtn);
            components.Add(inputTxt);
        }