Пример #1
0
        //Initializes the variables
        public DrawingPanel(World w, GameController ctr)
        {
            controller     = ctr;
            playerNum      = controller.GetPlayerNum();
            beamFrameCount = 0;
            DoubleBuffered = true;
            world          = w;
            string dir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;

            background       = Image.FromFile(dir + "\\..\\Resources/Images/Background.png");
            wall             = Image.FromFile(dir + "\\..\\Resources/Images/WallSprite.png");
            blueTank         = Image.FromFile(dir + "\\..\\Resources/Images/BlueTank.png");
            blueTurret       = Image.FromFile(dir + "\\..\\Resources/Images/BlueTurret.png");
            darkBlueTank     = Image.FromFile(dir + "\\..\\Resources/Images/DarkTank.png");
            darkBlueTurret   = Image.FromFile(dir + "\\..\\Resources/Images/DarkTurret.png");
            greenTank        = Image.FromFile(dir + "\\..\\Resources/Images/GreenTank.png");
            greenTurret      = Image.FromFile(dir + "\\..\\Resources/Images/GreenTurret.png");
            lightGreenTank   = Image.FromFile(dir + "\\..\\Resources/Images/LightGreenTank.png");
            lightGreenTurret = Image.FromFile(dir + "\\..\\Resources/Images/LightGreenTurret.png");
            orangeTank       = Image.FromFile(dir + "\\..\\Resources/Images/OrangeTank.png");
            orangeTurret     = Image.FromFile(dir + "\\..\\Resources/Images/OrangeTurret.png");
            redTank          = Image.FromFile(dir + "\\..\\Resources/Images/RedTank.png");
            redTurret        = Image.FromFile(dir + "\\..\\Resources/Images/RedTurret.png");
            purpleTank       = Image.FromFile(dir + "\\..\\Resources/Images/PurpleTank.png");
            purpleTurret     = Image.FromFile(dir + "\\..\\Resources/Images/PurpleTurret.png");
            yellowTank       = Image.FromFile(dir + "\\..\\Resources/Images/YellowTank.png");
            yellowTurret     = Image.FromFile(dir + "\\..\\Resources/Images/YellowTurret.png");
            purpleShot       = Image.FromFile(dir + "\\..\\Resources/Images/shot_violet.png");
            blueShot         = Image.FromFile(dir + "\\..\\Resources/Images/shot_blue.png");
            redShot          = Image.FromFile(dir + "\\..\\Resources/Images/shot_red_new.png");
            yellowShot       = Image.FromFile(dir + "\\..\\Resources/Images/shot-yellow.png");
            brownShot        = Image.FromFile(dir + "\\..\\Resources/Images/shot-brown.png");
            greenShot        = Image.FromFile(dir + "\\..\\Resources/Images/shot-green.png");
            greyShot         = Image.FromFile(dir + "\\..\\Resources/Images/shot_grey.png");
            whiteShot        = Image.FromFile(dir + "\\..\\Resources/Images/shot-white.png");

            //Sets the background to black
            this.BackColor = Color.Black;
        }
Пример #2
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);
        }