Esempio n. 1
0
 /// <summary>
 /// Handler for the controller's IDLoaded event. Call's drawingPanel's SetID so we know where to center camera
 /// </summary>
 private void SetID()
 {
     drawingPanel.SetID(controller.GetPlayerID());
 }
Esempio n. 2
0
        /// <summary>
        /// This method is invoked when the DrawingPanel needs to be re-drawn
        /// When first centering our view, a try catch is used to skip the first time Onpaint is invoked.
        /// </summary>
        protected override void OnPaint(PaintEventArgs e)
        {
            // Stopwatch for calculating FPS
            sw.Start();

            lock (theWorld)
            {
                // Centering View (provided code)
                try
                {
                    double playerX = theWorld.Tanks[theController.GetPlayerID()].location.GetX();
                    double playerY = theWorld.Tanks[theController.GetPlayerID()].location.GetY();

                    // calculate view/world size ratio
                    double ratio          = (double)900 / (double)theWorld.size;
                    int    halfSizeScaled = (int)(theWorld.size / 2.0 * ratio);

                    double inverseTranslateX = -WorldSpaceToImageSpace(theWorld.size, playerX) + halfSizeScaled;
                    double inverseTranslateY = -WorldSpaceToImageSpace(theWorld.size, playerY) + halfSizeScaled;

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

                catch { }  // Do nothing

                // Draw background
                Image     backGround = GetSprite("Background.png");
                Rectangle size       = new Rectangle(0, 0, theWorld.size, theWorld.size);
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                e.Graphics.DrawImage(backGround, size);
            }

            // Drawing the walls
            lock (theWorld.Walls)
            {
                foreach (Wall wall in theWorld.Walls.Values)
                {
                    // The points of the walls
                    double p1x = wall.GetP1().GetX();
                    double p1y = wall.GetP1().GetY();

                    double p2x = wall.GetP2().GetX();
                    double p2y = wall.GetP2().GetY();

                    DrawUntilEndPoint(e, wall, p1x, p1y, p2x, p2y);
                }
            }

            // Tanks
            lock (theWorld.Tanks)
            {
                // Draw the players
                foreach (Tank tank in theWorld.Tanks.Values.ToList())
                {
                    // Draw only if alive, Server will send the tank JSON with full health when it respawns.
                    if (tank.hitPoints != 0)
                    {
                        // Tank body
                        DrawObjectWithTransform(e, tank, theWorld.size, tank.location.GetX(), tank.location.GetY(), tank.orientation.ToAngle(), TankDrawer);

                        // Turret
                        DrawObjectWithTransform(e, tank, theWorld.size, tank.location.GetX(), tank.location.GetY(), tank.GetAiming().ToAngle(), TurretDrawer);

                        // Name and Score
                        DrawObjectWithTransform(e, tank, theWorld.size, tank.location.GetX(), tank.location.GetY(), tank.orientation.ToAngle(), StringDrawer);

                        // HP bar
                        DrawObjectWithTransform(e, tank, theWorld.size, tank.location.GetX(), tank.location.GetY(), tank.orientation.ToAngle(), HPDrawer);

                        // Draw FPS
                        if (tank.ID == theController.GetPlayerID() && theController.showFPS)
                        {
                            DrawObjectWithTransform(e, tank, theWorld.size, tank.location.GetX() - 420, tank.location.GetY() - 410, 0, FPSDrawer);
                        }
                    }

                    else if (tank.hitPoints == 0)
                    {
                        DrawObjectWithTransform(e, tank, theWorld.size, tank.location.GetX(), tank.location.GetY(), tank.orientation.ToAngle(), DeathDrawer);
                    }
                }
            }

            // Load the power-ups
            lock (theWorld.PowerUps)
            {
                // Draw the powerups
                foreach (PowerUp pow in theWorld.PowerUps.Values.ToList())
                {
                    if (!pow.died)
                    {
                        DrawObjectWithTransform(e, pow, theWorld.size, pow.location.GetX(), pow.location.GetY(), 0, PowerupDrawer);
                    }
                }
            }

            // Loading the Projectiles
            lock (theWorld.Projectiles)
            {
                foreach (Projectile proj in theWorld.Projectiles.Values.ToList())
                {
                    if (!proj.died)
                    {
                        DrawObjectWithTransform(e, proj, theWorld.size, proj.location.GetX(), proj.location.GetY(), proj.orientation.ToAngle() - 90, ProjectileDrawer);
                    }
                }
            }

            // Load Beams
            lock (theWorld.Beams)
            {
                foreach (Beam b in theWorld.Beams.Values.ToList())
                {
                    DrawObjectWithTransform(e, b, theWorld.size, b.origin.GetX(), b.origin.GetY(), b.direction.ToAngle() - 45, BeamDrawer);
                    FPSCount += FPS;
                }

                // Clearing the Beams after a certain amount of frames
                if (FPSCount >= 1200)
                {
                    theWorld.Beams.Clear();
                    FPSCount = 0;
                }
            }


            // Do anything that Panel (from which we inherit) needs to do
            base.OnPaint(e);

            // Calculate FPS with the send rate
            double sendRate = sw.ElapsedMilliseconds;

            FPS = Math.Ceiling(1000 / sendRate);

            sw.Restart();
        }