Пример #1
0
        /// <summary>
        /// This method is invoked when the DrawingPanel is invalidated and needs to be redrawn
        /// </summary>
        /// <param name="e">Graphics for drawing objects</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            //If the walls aren't imported, it shouldn't try to draw anything so it just returns
            if (!TheController.wallsDone)
            {
                return;
            }

            //Store the player tank location
            double playerX = TheController.GetPlayerTank().Location.GetX();
            double playerY = TheController.GetPlayerTank().Location.GetY();

            //Calculate view/world size ratio
            double ratio          = (double)Constants.ViewSize / (double)TheController.TheWorld.worldSize;
            int    halfSizeScaled = (int)(TheController.TheWorld.worldSize / 2.0 * ratio);

            //Amount to translate the x and y coordinates
            double inverseTranslateX = -WorldSpaceToImageSpace(TheController.TheWorld.worldSize, playerX) + halfSizeScaled;
            double inverseTranslateY = -WorldSpaceToImageSpace(TheController.TheWorld.worldSize, playerY) + halfSizeScaled;

            //Translates the image and draws it
            e.Graphics.TranslateTransform((float)inverseTranslateX, (float)inverseTranslateY);
            e.Graphics.DrawImage(background, 0, 0, TheController.TheWorld.worldSize, TheController.TheWorld.worldSize);


            lock (TheController.TheWorld)
            {
                // Draw the walls
                foreach (Wall wall in TheController.TheWorld.Walls.Values)
                {
                    int Width  = (int)Math.Abs(wall.EndPoint1.GetX() - wall.EndPoint2.GetX());
                    int Height = (int)Math.Abs(wall.EndPoint1.GetY() - wall.EndPoint2.GetY());

                    int MinX = (int)Math.Min(wall.EndPoint1.GetX(), wall.EndPoint2.GetX()) - Constants.WallSize / 2;
                    int MinY = (int)Math.Min(wall.EndPoint1.GetY(), wall.EndPoint2.GetY()) - Constants.WallSize / 2;

                    //Call DrawObjectWithTransform on each individual portion of the wall
                    for (int i = 0; i <= Width; i += Constants.WallSize)
                    {
                        for (int j = 0; j <= Height; j += Constants.WallSize)
                        {
                            DrawObjectWithTransform(e, wall, TheController.TheWorld.worldSize, MinX + i, MinY + j, 0, WallDrawer);
                        }
                    }
                }

                // Draw the players
                foreach (Tank tank in TheController.TheWorld.Tanks.Values)
                {
                    tank.Orientation.Normalize();
                    tank.Aiming.Normalize();
                    DrawObjectWithTransform(e, tank, TheController.TheWorld.worldSize, tank.Location.GetX(), tank.Location.GetY(), tank.Orientation.ToAngle(),
                                            TankDrawer);
                    DrawObjectWithTransform(e, tank, TheController.TheWorld.worldSize, tank.Location.GetX(), tank.Location.GetY(), tank.Aiming.ToAngle(),
                                            TurretDrawer);
                    DrawObjectWithTransform(e, tank, TheController.TheWorld.worldSize, tank.Location.GetX(), tank.Location.GetY(), 0,
                                            NameDrawer);
                    DrawObjectWithTransform(e, tank, TheController.TheWorld.worldSize, tank.Location.GetX(), tank.Location.GetY(), 0,
                                            HealthDrawer);
                }

                // Draw the powerups
                foreach (PowerUp pow in TheController.TheWorld.PowerUps.Values)
                {
                    DrawObjectWithTransform(e, pow, TheController.TheWorld.worldSize, pow.Location.GetX(), pow.Location.GetY(), 0, PowerUpDrawer);
                }

                // Draw the beams
                foreach (Beam beam in TheController.TheWorld.Beams.Values)
                {
                    DrawObjectWithTransform(e, beam, TheController.TheWorld.worldSize, beam.Origin.GetX(), beam.Origin.GetY(), beam.Orientation.ToAngle(), BeamDrawer);
                }

                // Draw the projectiles
                foreach (Projectile proj in TheController.TheWorld.Projectiles.Values)
                {
                    proj.Orientation.Normalize();
                    DrawObjectWithTransform(e, proj, TheController.TheWorld.worldSize, proj.Location.GetX(), proj.Location.GetY(), proj.Orientation.ToAngle(), ProjectileDrawer);
                }

                // If the there are any beams that have been out for too long it removes them
                if (TheController.TheWorld.Beams.Count != 0)
                {
                    foreach (Beam b in TheController.TheWorld.Beams.Values.ToList())
                    {
                        if (b.beamFrames == Constants.BeamFrameLength)
                        {
                            TheController.TheWorld.Beams.Remove(b.ID);
                        }
                    }
                }
            }

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