/// <summary>
        /// This is kind of like our event loop, where everything is drawn.
        /// The world is the key to lock on, where processing cubes and 
        /// drawing them are done in locked fashion so as to not change
        /// the underlying data unecessarily.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void AgCubioPaint(object sender, PaintEventArgs e)
        {
            //If the game hasn't started yet, show the inital setup form
            if (!GameRunning)
            {
                return;
            }
            else if (GameRunning)
            {
                if (ServerConnected)
                {
                    try
                    {
                        //Compute the x and y offset, based on where the player cube is and how big it is.
                        int center_x = this.Width / 2;
                        int center_y = this.Height / 2;
                        Cube player_cube = world.GetPlayerCube();

                        //If the player cube isn't in the world anymore, we know it's game over
                        if (player_cube == null)
                        {
                            Console.WriteLine("Game over!");
                            GameRunning = false;
                            GameOverForm game_over = new GameOverForm(this, player_mass, player_name);
                            game_over.ShowDialog(this);

                            return;
                        }
                        player_mass = (int)world.GetPlayerMass();
                        //world.Scale = (this.Width / (player_cube.Width * 10));

                        lock (world)
                        {

                            //Draw the player cube first
                            DrawCube(player_cube, e);

                            foreach (Cube cube in world.cubes.Values)
                            {
                                if (cube == player_cube) continue;
                                DrawCube(cube, e);
                            }

                            System.Drawing.Font drawFont = new System.Drawing.Font("Arial", (int)(10 * world.Scale));
                            System.Drawing.SolidBrush nameBrush = new System.Drawing.SolidBrush(Color.FromName("black"));

                            e.Graphics.DrawString("Frames per second: " + CalculateFrameRate(), drawFont, nameBrush, new PointF(this.Width - 300, 50));
                            e.Graphics.DrawString("Player mass: " + (int)player_mass, drawFont, nameBrush, new PointF(this.Width - 300, 75));
                        }
                        
                        //Check to see if the player cube is where we told it to go. If not, send a move request again.
                        if (player_cube.X != dest_x || player_cube.Y != dest_y)
                        {
                            SendMoveRequest(dest_x, dest_y);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    Invalidate();
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// This is kind of like our event loop, where everything is drawn.
        /// The world is the key to lock on, where processing cubes and
        /// drawing them are done in locked fashion so as to not change
        /// the underlying data unecessarily.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void AgCubioPaint(object sender, PaintEventArgs e)
        {
            //If the game hasn't started yet, show the inital setup form
            if (!GameRunning)
            {
                return;
            }
            else if (GameRunning)
            {
                if (ServerConnected)
                {
                    try
                    {
                        //Compute the x and y offset, based on where the player cube is and how big it is.
                        int  center_x    = this.Width / 2;
                        int  center_y    = this.Height / 2;
                        Cube player_cube = world.GetPlayerCube();

                        //If the player cube isn't in the world anymore, we know it's game over
                        if (player_cube == null)
                        {
                            Console.WriteLine("Game over!");
                            GameRunning = false;
                            GameOverForm game_over = new GameOverForm(this, player_mass, player_name);
                            game_over.ShowDialog(this);

                            return;
                        }
                        player_mass = (int)world.GetPlayerMass();
                        //world.Scale = (this.Width / (player_cube.Width * 10));

                        lock (world)
                        {
                            //Draw the player cube first
                            DrawCube(player_cube, e);

                            foreach (Cube cube in world.cubes.Values)
                            {
                                if (cube == player_cube)
                                {
                                    continue;
                                }
                                DrawCube(cube, e);
                            }

                            System.Drawing.Font       drawFont  = new System.Drawing.Font("Arial", (int)(10 * world.Scale));
                            System.Drawing.SolidBrush nameBrush = new System.Drawing.SolidBrush(Color.FromName("black"));

                            e.Graphics.DrawString("Frames per second: " + CalculateFrameRate(), drawFont, nameBrush, new PointF(this.Width - 300, 50));
                            e.Graphics.DrawString("Player mass: " + (int)player_mass, drawFont, nameBrush, new PointF(this.Width - 300, 75));
                        }

                        //Check to see if the player cube is where we told it to go. If not, send a move request again.
                        if (player_cube.X != dest_x || player_cube.Y != dest_y)
                        {
                            SendMoveRequest(dest_x, dest_y);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    Invalidate();
                }
            }
        }