Esempio n. 1
0
        public Snake()
        {
            //initialize a world object with the default size of 150x150
            world = new SnakeUtilities.World(DEF_SIZE, DEF_SIZE);

            InitializeComponent();

            //Anchors the panel to the top left to handle any bugs on drawing when maximizing.
            drawingPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top)
                                                                       | System.Windows.Forms.AnchorStyles.Left));


            drawingPanel.SetWorld(world);

            drawingPanel.Size = new Size(world.width * SnakeUtilities.World.pixelsPerCell, world.height * SnakeUtilities.World.pixelsPerCell);

            ////Fake panel used solely to allow key input, when we have our drawing panel we will use that for the actual key press event.
            drawingPanel.Focus();

            //Used to catch key presses.
            KeyPreview = true;

            //Handles resizing of window. Not sure how this will work when we add the scoreboard, but I don't think it will be too hard to figure out and work around.
            AutoSizeMode = AutoSizeMode.GrowAndShrink;
            AutoSize     = true;

            //Defaults the client window to handle 150x150, until we get world size from server.
            ClientSize    = new Size(991, 800);
            StartPosition = FormStartPosition.CenterScreen;

            HostServer = null;

            this.KeyDown += new KeyEventHandler(Key_Press);

            this.FormClosing += Form1_FormClosing;
        }
 /// <summary>
 /// Pass in a reference to the world, so we can draw the objects in it
 /// </summary>
 /// <param name="_world"></param>
 public void SetWorld(SnakeUtilities.World _world)
 {
     world = _world;
 }
Esempio n. 3
0
        /// <summary>
        /// Used to recieve the first instructions from the server, specifically what to build in the world.
        /// </summary>
        /// <param name="state"></param>
        private void InitialReceive(SocketState state)
        {
            //I still need to figure out how to give a error message if the connection failed. This does not work
            if (state.getID() == -1 || state.theSocket == null)
            {
                MessageBox.Show("Connection with host was lost.", "Connection Failure", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                this.Invoke((Action)(() =>
                {
                    NameTextBox.Enabled = true;

                    ServerIPTextBox.Enabled = true;

                    ConnectButton.Enabled = true;

                    return;
                }));
            }

            try
            {
                //Used to change how big the world is based on what the server tells us
                int width  = 0;
                int height = 0;
                int ourID  = 0;

                Console.WriteLine("Server sent data. " + state.sb);

                //Parse the message here, get the id, get the width, then get the heigth and then mvoe on to receiving all data from the world.

                string[] theMsg = state.sb.ToString().Split('\n');

                // Console.WriteLine(":::" + theMsg[0] + "..." + theMsg[1] + theMsg[2]);

                //if we didn't get the whole message we should wait for more, so somehow check that we got id and world sizes.

                //Assures that we have recieved the initial data from the server, and that it is complete. Process it, and then remove it from the string builder.
                if (theMsg.Length >= 3 && state.sb.ToString()[state.sb.Length - 1] == '\n')
                {
                    ourID = int.Parse(theMsg[0]);


                    state.sb.Remove(0, theMsg[0].Length);

                    width = int.Parse(theMsg[1]);

                    state.sb.Remove(0, theMsg[1].Length);

                    height = int.Parse(theMsg[2]);


                    state.sb.Remove(0, theMsg[2].Length);
                }
                else
                {
                    //If message is not complete, we wait for more data
                    NetworkController.NetworkHandler.AwaitDataFromServer(state);
                    return;
                }


                Console.WriteLine("Our ID:" + ourID + " Our World Width and Height:" + width + "," + height);

                //Create new world based on the given h and w by server
                world = new SnakeUtilities.World(width, height);

                //Invoke so we can draw the new world on the panel

                //Need to add here how to resize the client window if the world size is too big, or maybe if it is even smaller we can resize it to scale.
                //Draw the new world with the designated size given.
                this.Invoke((Action)(() =>
                {
                    drawingPanel.Size = new Size((world.width * SnakeUtilities.World.pixelsPerCell) + SCB_OFFSET, (SnakeUtilities.World.pixelsPerCell * world.height));
                    drawingPanel.SetWorld(world);



                    //Size panelSize = drawingPanel.Size;
                    //int panWidth = panelSize.Width;
                    //int panHeight = panelSize.Height;
                    drawingPanel.Invalidate();
                }

                                     ));

                //Change call back to receive world to continuously take data and then call receive world to begin.
                state.callBack = ReceiveWorld;
                ReceiveWorld(state);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception:" + e.Message);
            }
        }