예제 #1
0
        /// <summary>
        /// Handler for the controller's WorldLoaded event. Sets this form and drawingPanel's world to the loaded world
        /// Also enables KeyPreview
        /// </summary>
        private void SetWorld()
        {
            world = controller.GetWorld();
            drawingPanel.SetWorld(controller.GetWorld());

            // Enable the global form to capture key presses
            KeyPreview = true;
        }
예제 #2
0
        /// <summary>
        /// Once network sends player ID, we send this to the drawing panel
        /// This allows us to know which area to center the view on
        /// </summary>
        /// <param name="info"></param>
        private void InitializeWithID(int info)
        {
            //Set up Drawing Panel with the created world and player ID
            panel.SetWorld(theController.GetWorld());
            panel.SetPlayerId(info);
            theWorld = theController.GetWorld();

            //Set boolean to true so that other events are activated
            worldExists = true;
        }
예제 #3
0
        public Form1(GameController ctl)
        {
            //Set up the general handlers and variables of the form
            InitializeComponent();
            theController              = ctl;
            theWorld                   = theController.GetWorld();
            theController.UpdateWorld += OnFrame;

            // Set up the form.
            panel          = new DrawingPanel(theWorld);
            panel.Location = new Point(0, menuSize);
            panel.Size     = new Size(viewSize, viewSize);
            this.Controls.Add(panel);

            // Set the window size
            ClientSize = new Size(viewSize, viewSize + menuSize);

            //Save our boxes to variables
            IPName     = hostText;
            playerName = playerText;

            // Set up key and mouse handlers
            this.KeyDown += HandleKeyDown;
            this.KeyUp   += HandleKeyUp;

            //Allow enter button to connect to server
            AcceptButton = connectButton;

            //Set up handlers for controls
            panel.MouseMove             += OnMouseMove;
            panel.MouseClick            += HandleMouseClick;
            theController.PlayerIDGiven += InitializeWithID;
            theController.Error         += DisplayErrorMessage;
        }
예제 #4
0
        /// <summary>
        /// Subscribes to the events and initializes the variables needed in the constructor
        /// </summary>
        public TankWarsView(GameController ctl)
        {
            InitializeComponent();
            controller = ctl;

            FormClosed += OnExit;
            world       = controller.GetWorld();
            controller.UpdateArrived += OnFrame;
            controller.Error         += ShowError;
            controller.Connected     += HandleConnected;

            ClientSize = new Size(viewSize, viewSize + menuSize);


            drawingPanel            = new DrawingPanel(world, controller);
            drawingPanel.Location   = new Point(0, menuSize);
            drawingPanel.Size       = new Size(viewSize, viewSize);
            drawingPanel.MouseDown += HandleMouseDown;
            drawingPanel.MouseUp   += HandleMouseUp;
            drawingPanel.MouseMove += HandleMouseMovement;

            Controls.Add(drawingPanel);


            this.KeyDown += HandleKeyDown;
            this.KeyUp   += HandleKeyUp;
        }
예제 #5
0
        public Form1(GameController ctl)
        {
            InitializeComponent();
            theController = ctl;
            theWorld      = theController.GetWorld();
            theController.RegisterServerUpdateHandler(OnFrame);          //Register OnFrame as the controller event handler
            theController.RegisterConnectionErrorHandler(UnlockMenuBar); //Register UnlockMenuBar as the controller event handler

            ClientSize            = new Size(Constant.ClientSize, Constant.ClientSize + 35);
            drawingPanel          = new DrawingPanel(theWorld);
            drawingPanel.Location = new Point(0, 35);
            drawingPanel.Size     = new Size(Constant.ViewSize, Constant.ViewSize);

            drawingPanel.BackColor = Color.Black;
            this.Controls.Add(drawingPanel);

            // Set KeyPreview object to true to allow the form to process
            // the key before the control with focus processes it.
            this.KeyPreview = true;

            this.KeyDown           += new KeyEventHandler(Form1_KeyDown);
            this.KeyUp             += new KeyEventHandler(Form1_KeyUp);
            drawingPanel.MouseMove += new MouseEventHandler(Form1_MouseMove);
            drawingPanel.MouseDown += new MouseEventHandler(Form1_MouseDown);
            drawingPanel.MouseUp   += new MouseEventHandler(Form1_MouseUp);
            FormClosed             += OnExit;
        }
예제 #6
0
        /// <summary>
        /// Handler for the controllers UpdateArrived event that essentially draws everything each frame
        /// </summary>
        private void OnFrame()
        {
            try
            {
                //Sets the world to the controller's world
                world = controller.GetWorld();
                //Passes this world into the drawing panel, so that the MethodInvoker calls OnPaint to redraw on each frame in the form
                MethodInvoker invoker = new MethodInvoker(() => this.Invalidate(true));
                this.Invoke(invoker);
            }

            //Catches an ObjectDisposedException and calls the Close method in the controller class
            catch (ObjectDisposedException e)
            {
                controller.Close();
            }
        }
예제 #7
0
        /// <summary>
        /// Constructor for the View.
        /// </summary>
        /// <param name="ctl"></param>
        public GameView(GameController ctl)
        {
            // Initalize
            InitializeComponent();
            FormClosed   += OnExit;
            theController = ctl;
            theWorld      = theController.GetWorld();
            HelpPanel.Hide();


            // Register handlers for the controller's events
            theController.Error         += ShowError;
            theController.UpdateArrived += OnFrame;
            theController.Connected     += HandleConnected;


            // Set up key handlers
            this.KeyDown += HandleKeyDown;
            this.KeyUp   += HandleKeyUp;
        }
예제 #8
0
        /// <summary>
        /// This method is invoked when the DrawingPanel needs to be re-drawn
        /// </summary>
        protected override void OnPaint(PaintEventArgs e)
        {
            //Gets the world based on the world of the controller
            world = controller.GetWorld();

            //Gets the playerNum based on the controller's player number
            playerNum = controller.GetPlayerNum();

            //Only attempts to draw if the world contains the tanks
            if (world.GetTanks().Count > 0)
            {
                //Locks the thread with the world as the key to draw everything in one thread to avoid changes while drawing
                lock (world)
                {
                    //Locks the player's view in the center based on ther user's controlled tank, if the tank is not dead
                    if (world.GetTanks().TryGetValue(playerNum, out Tank tank) && !tank.GetDead())
                    {
                        //Sets the player's X and Y coordinate
                        playerX = tank.GetLocation().GetX();
                        playerY = tank.GetLocation().GetY();

                        //Centers the player's view
                        double ratio          = (double)viewSize / (double)world.Size;
                        int    halfSizeScaled = (int)(world.Size / 2.0 * ratio);

                        double inverseTranslateX = -WorldSpaceToImageSpace(world.Size, playerX) + halfSizeScaled;
                        double inverseTranslateY = -WorldSpaceToImageSpace(world.Size, playerY) + halfSizeScaled;

                        e.Graphics.TranslateTransform((float)inverseTranslateX, (float)inverseTranslateY);
                    }
                    else
                    {
                        //If the tank is not dead, then it sets the location as the last place of death of the tank and center's the player's view
                        double ratio          = (double)viewSize / (double)world.Size;
                        int    halfSizeScaled = (int)(world.Size / 2.0 * ratio);

                        double inverseTranslateX = -WorldSpaceToImageSpace(world.Size, playerX) + halfSizeScaled;
                        double inverseTranslateY = -WorldSpaceToImageSpace(world.Size, playerY) + halfSizeScaled;

                        e.Graphics.TranslateTransform((float)inverseTranslateX, (float)inverseTranslateY);
                    }



                    //Doesn't draw anything if the world size is less than equal to 0
                    if (world.Size <= 0)
                    {
                        return;
                    }
                    //Draws the background
                    DrawObjectWithTransform(e, null, world.Size, 0, 0, 0, BackgroundDrawer);

                    //Draws all the walls in the world
                    foreach (Wall w in world.GetWalls().Values.ToList())
                    {
                        for (double i = w.GetStartingPoint().GetX(); i <= w.GetEndingPoint().GetX(); i = i + 50)
                        {
                            for (double j = w.GetStartingPoint().GetY(); j <= w.GetEndingPoint().GetY(); j = j + 50)
                            {
                                //Draws all the walls in the condition that the starting point is smaller than ending points of the walls
                                DrawObjectWithTransform(e, w, world.Size, i, j, 0, WallDrawer);
                            }
                        }

                        for (double i = w.GetEndingPoint().GetX(); i <= w.GetStartingPoint().GetX(); i = i + 50)
                        {
                            for (double j = w.GetEndingPoint().GetY(); j <= w.GetStartingPoint().GetY(); j = j + 50)
                            {
                                //Draws all the walls in the condition that the starting point is greater than ending points of the walls
                                DrawObjectWithTransform(e, w, world.Size, i, j, 0, WallDrawer);
                            }
                        }
                    }

                    //Draws all the powerups
                    foreach (Powerups p in world.GetPowerups().Values)
                    {
                        //Draws the outside and inside circle for the powerups
                        DrawObjectWithTransform(e, p, world.Size, p.GetLocation().GetX(), p.GetLocation().GetY(), 0, PowerupDrawer);
                        DrawObjectWithTransform(e, p, world.Size, p.GetLocation().GetX(), p.GetLocation().GetY(), 0, PowerupDrawerInsideCircle);
                    }

                    //Draws all the tanks in the world
                    foreach (Tank t in world.GetTanks().Values)
                    {
                        //Draws the tank
                        DrawObjectWithTransform(e, t, world.Size, t.GetLocation().GetX(), t.GetLocation().GetY(), t.GetOrientation().ToAngle(), TankDrawer);

                        //Draws the turret
                        DrawObjectWithTransform(e, t, world.Size, t.GetLocation().GetX(), t.GetLocation().GetY(), t.GetAiming().ToAngle(), TurretDrawer);

                        //Draws the name
                        DrawObjectWithTransform(e, t, world.Size, t.GetLocation().GetX(), t.GetLocation().GetY() + 60, 0, NameDrawer);

                        //Draws the health
                        DrawObjectWithTransform(e, t, world.Size, t.GetLocation().GetX(), t.GetLocation().GetY() - 40, 0, HealthDrawer);
                    }

                    //Draws all the projectiles in the world
                    foreach (Projectile p in world.GetProjectile().Values.ToList())
                    {
                        //Draws the projectile
                        DrawObjectWithTransform(e, p, world.Size, p.GetLocation().GetX(), p.GetLocation().GetY(), p.GetOrientation().ToAngle(), ProjectileDrawer);
                    }

                    //Draws all the beams in the world
                    foreach (Beams b in world.GetBeams().Values.ToList())
                    {
                        //Draws the beams
                        DrawObjectWithTransform(e, b, world.Size, b.GetOrigin().GetX(), b.GetOrigin().GetY(), b.GetDirection().ToAngle(), BeamDrawer);
                        //Removes the beams after 20 iterations
                        beamFrameCount++;
                        if (beamFrameCount == 20)
                        {
                            world.GetBeams().Remove(b.GetBeamID());
                            beamFrameCount = 0;
                        }
                    }
                }
            }
            //Calls the base
            base.OnPaint(e);
        }
예제 #9
0
        /// <summary>
        /// Main form application. Sets controller, world, and clientsize. Then adds events for controller and finally adds form components
        /// </summary>
        public Form1(GameController controller)
        {
            //Initialize and set controller to parameter and world to controller's world
            InitializeComponent();
            this.controller = controller;
            this.world      = controller.GetWorld();

            //Sets clientsize for form
            ClientSize = new Size(viewSize, viewSize + menuSize);

            //Adds listeners to events for controller-view handshake
            controller.OnUpdate    += OnFrame;
            controller.IDLoaded    += SetID;
            controller.WorldLoaded += SetWorld;

            // Place and add the start button
            startButton          = new Button();
            startButton.Location = new Point(245, 5);
            startButton.Size     = new Size(70, 20);
            startButton.Text     = "Start";
            startButton.Click   += StartClick;
            this.Controls.Add(startButton);

            // Place and add the name label
            nameLabel          = new Label();
            nameLabel.Text     = "Name:";
            nameLabel.Location = new Point(5, 10);
            nameLabel.Size     = new Size(40, 15);
            this.Controls.Add(nameLabel);

            // Place and add the name textbox
            nameText           = new TextBox();
            nameText.Text      = "player";
            nameText.MaxLength = 16;
            nameText.Location  = new Point(50, 5);
            nameText.Size      = new Size(70, 15);
            this.Controls.Add(nameText);

            // Place and add the host label
            hostLabel          = new Label();
            hostLabel.Text     = "Host:";
            hostLabel.Location = new Point(125, 10);
            hostLabel.Size     = new Size(40, 15);
            this.Controls.Add(hostLabel);

            // Place and add the host textbox
            hostText          = new TextBox();
            hostText.Text     = "";
            hostText.Location = new Point(165, 5);
            hostText.Size     = new Size(70, 15);
            this.Controls.Add(hostText);

            // Place and add the drawing panel with event when beam gets fired
            drawingPanel          = new DrawingPanel(world);
            drawingPanel.Location = new Point(0, menuSize);
            drawingPanel.Size     = new Size(viewSize, viewSize);
            drawingPanel.SetViewSize(viewSize);
            this.Controls.Add(drawingPanel);
            controller.BeamFired += drawingPanel.DrawBeam;

            //Set up control events for keys and mouse inputs
            this.KeyDown           += Moved;
            drawingPanel.MouseDown += Shoot;
            drawingPanel.MouseUp   += StopShoot;
            drawingPanel.MouseMove += Aim;
            this.KeyUp             += StopMove;
        }