コード例 #1
0
ファイル: View.cs プロジェクト: tws2xa/RunningGame
        public void drawBkgEntity(Entity e)
        {
            //Pull out all required components
            PositionComponent posComp  = ( PositionComponent )e.getComponent(GlobalVars.POSITION_COMPONENT_NAME);
            DrawComponent     drawComp = ( DrawComponent )e.getComponent(GlobalVars.DRAW_COMPONENT_NAME);

            if (isInView(posComp))
            {
                if (g != null)
                {
                    Image img = drawComp.getImage();

                    //Get center instead of upper left
                    PointF drawPoint = posComp.getLocAsPoint();
                    drawPoint.X -= (posComp.width / 2.0f);
                    drawPoint.Y -= (posComp.height / 2.0f);

                    drawPoint.X -= this.x;
                    drawPoint.Y -= this.y;

                    drawPoint.X *= wRatio;
                    drawPoint.Y *= hRatio;


                    lock ( img ) {
                        //g.DrawImage(img, new RectangleF(0, 0, width, height), new RectangleF(x, y, width, height), GraphicsUnit.Pixel);
                        g.DrawImage(img, new RectangleF(x, y, width, height), new RectangleF(x, y, width, height), GraphicsUnit.Pixel);
                    }
                }
            }
        }
コード例 #2
0
        public void initialize(Level level, int id, Type entityType)
        {
            myEntType = entityType;

            //First create an entity of type entityType
            Entity e = ( Entity )Activator.CreateInstance(entityType, level, 0, 0);

            if (e.hasComponent(GlobalVars.ANIMATION_COMPONENT_NAME))
            {
                e.removeComponent(GlobalVars.ANIMATION_COMPONENT_NAME);
            }

            PositionComponent posComp  = ( PositionComponent )e.getComponent(GlobalVars.POSITION_COMPONENT_NAME);
            DrawComponent     drawComp = ( DrawComponent )e.getComponent(GlobalVars.DRAW_COMPONENT_NAME);

            this.level = level;


            if (posComp != null)
            {
                myWidth  = posComp.width;
                myHeight = posComp.height;
            }

            if (drawComp != null)
            {
                if (drawComp.sizeLocked)
                {
                    lock (drawComp.getImage())
                        myImg = new Bitmap(drawComp.getImage());
                }
                else
                {
                    lock (drawComp.getImage())
                        myImg = new Bitmap(drawComp.getImage(), new Size(( int )myWidth, ( int )myHeight));
                }

                //Make slightly transparent
                for (int x = 0; x < myImg.Width; x++)
                {
                    for (int y = 0; y < myImg.Height; y++)
                    {
                        if (myImg.GetPixel(x, y).A > protoAlpha)
                        {
                            myImg.SetPixel(x, y, Color.FromArgb(protoAlpha, myImg.GetPixel(x, y)));
                        }
                    }
                }
            }
            else
            {
                //Just solid blue box
                myImg = new Bitmap(( int )myWidth, ( int )myHeight);
                Graphics g = Graphics.FromImage(myImg);
                g.FillRectangle(Brushes.Blue, new Rectangle(0, 0, myImg.Size.Width, myImg.Size.Height));
            }

            initializeEntity(new Random().Next(Int32.MinValue, Int32.MaxValue), level);

            addMyComponents(0, 0);
        }
コード例 #3
0
ファイル: View.cs プロジェクト: tws2xa/RunningGame
        public void drawEntity(Entity e)
        {
            //Pull out all required components
            PositionComponent posComp  = ( PositionComponent )e.getComponent(GlobalVars.POSITION_COMPONENT_NAME);
            DrawComponent     drawComp = ( DrawComponent )e.getComponent(GlobalVars.DRAW_COMPONENT_NAME);

            if (e is ShooterBullet)
            {
                //Console.WriteLine( "In Draw ShooterBullet - Redraw: " + drawComp.needRedraw + ", In View: " + isInView( posComp ) );
            }

            if (drawComp.needRedraw)
            {
                if (isInView(posComp))
                {
                    if (g != null)
                    {
                        Image img = null;
                        //If size is locked, don't resize the image.
                        if (drawComp.sizeLocked && wRatio == 1 && hRatio == 1)
                        {
                            img = drawComp.getImage();
                        }
                        else
                        {
                            Size imageSize = new Size(( int )(posComp.width * wRatio), ( int )(posComp.height * hRatio));
                            img = new Bitmap(drawComp.getImage(), imageSize);
                        }

                        PointF drawPoint = posComp.getLocAsPoint();
                        drawPoint.X -= (posComp.width / 2.0f);
                        drawPoint.Y -= (posComp.height / 2.0f);

                        lock ( img ) {
                            g.DrawImageUnscaled(img, new Point(( int )drawPoint.X, ( int )drawPoint.Y));     //Draw the image to the view
                        }
                        //Health bar if need be
                        if (e.hasComponent(GlobalVars.HEALTH_COMPONENT_NAME))
                        {
                            HealthComponent healthComp = ( HealthComponent )e.getComponent(GlobalVars.HEALTH_COMPONENT_NAME);
                            if (healthComp.healthBar && (healthComp.showBarOnFull || !healthComp.hasFullHealth()))
                            {
                                int barHeight = 4;
                                int ySpace    = 3;
                                int xSpace    = 0;

                                int xLoc      = (( int )Math.Round(posComp.x - posComp.width / 2) + xSpace);
                                int yLoc      = (( int )Math.Round(posComp.y - posComp.height / 2) - barHeight - ySpace);
                                int fullWidth = (( int )Math.Round(posComp.width) - 2 * xSpace);

                                Rectangle backRect = new Rectangle(xLoc, yLoc, fullWidth, barHeight);
                                Rectangle foreRect = new Rectangle(xLoc, yLoc, ( int )Math.Round(fullWidth * healthComp.getHealthPercentage()), barHeight);

                                g.FillRectangle(healthComp.backHealthBarBrush, backRect);
                                g.FillRectangle(healthComp.foreHealthBarBrush, foreRect);
                                g.DrawRectangle(Pens.Black, backRect);   //Border
                            }
                        }

                        if (e is BasicGround & bkgEnt != null)
                        {
                            DrawComponent bkgDraw = ( DrawComponent )bkgEnt.getComponent(GlobalVars.DRAW_COMPONENT_NAME);
                            Graphics      graph   = Graphics.FromImage(bkgDraw.getImage());
                            lock ( img ) {
                                graph.DrawImageUnscaled(img, new Point(( int )drawPoint.X, ( int )drawPoint.Y));     //Draw the image to the view
                            }
                            drawComp.needRedraw = false;
                        }
                    }
                }
                if (GlobalVars.debugBoxes)
                {
                    g.DrawRectangle(Pens.Blue, posComp.x - posComp.width / 2, posComp.y - posComp.height / 2, posComp.width, posComp.height);

                    if (e.hasComponent(GlobalVars.COLLIDER_COMPONENT_NAME))
                    {
                        ColliderComponent colComp = (ColliderComponent)e.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME);
                        g.DrawRectangle(Pens.Red, colComp.getX(posComp) - colComp.width / 2, colComp.getY(posComp) - colComp.height / 2, colComp.width, colComp.height);
                    }
                }
            }
        }
コード例 #4
0
ファイル: View.cs プロジェクト: tws2xa/RunningGame
        public void Draw(Graphics mainG, List <Entity> entities)
        {
            //g.FillRectangle(bkgBrush, new Rectangle(0, 0, (int)width, (int)height)); //Clear

            if (!(level is RunningGame.Level_Editor.CreationLevel))
            {
                if (!hasDecreasedQuality)
                {
                    g.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; // or NearestNeighbour
                    g.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.None;
                    g.PixelOffsetMode    = System.Drawing.Drawing2D.PixelOffsetMode.None;
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
                    g.TextRenderingHint  = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;

                    mainG.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; // or NearestNeighbour
                    mainG.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.None;
                    mainG.PixelOffsetMode    = System.Drawing.Drawing2D.PixelOffsetMode.None;
                    mainG.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
                    mainG.TextRenderingHint  = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;

                    hasDecreasedQuality = true;
                }

                //Find background Entity first if need be
                if (bkgEnt == null)
                {
                    foreach (Entity e in entities)
                    {
                        if (e is BackgroundEntity)
                        {
                            bkgEnt = ( BackgroundEntity )e; //Find background entity
                        }
                    }
                }

                if (staticObjImg == null /*|| redrawStatics*/)
                {
                    if (seperateStaticObjImage)
                    {
                        staticObjImg = new Bitmap(( int )Math.Ceiling(level.levelWidth), ( int )Math.Ceiling(level.levelHeight));
                    }
                    else
                    {
                        foreach (Entity e in entities)
                        {
                            if (e is BackgroundEntity)
                            {
                                if (bkgEnt == null)
                                {
                                    bkgEnt = ( BackgroundEntity )e; //Find background entity
                                }
                                DrawComponent bkgDraw = ( DrawComponent )bkgEnt.getComponent(GlobalVars.DRAW_COMPONENT_NAME);
                                staticObjImg = ( Bitmap )bkgDraw.getImage();
                            }
                        }
                    }

                    //Draw static entities onto background
                    if (!GlobalVars.fullForegroundImage)
                    {
                        foreach (Entity ent in GlobalVars.groundEntities.Values)
                        {
                            DrawComponent grnDraw = ( DrawComponent )ent.getComponent(GlobalVars.DRAW_COMPONENT_NAME);

                            /*DrawComponent bkgDraw = (DrawComponent)bkgEnt.getComponent(GlobalVars.DRAW_COMPONENT_NAME);
                             *
                             * PositionComponent posComp = (PositionComponent)ent.getComponent(GlobalVars.POSITION_COMPONENT_NAME);
                             * PointF drawPoint = posComp.getPointF();
                             * drawPoint.X -= (posComp.width / 2.0f);
                             * drawPoint.Y -= (posComp.height / 2.0f);
                             *
                             * Graphics graph = Graphics.FromImage(bkgDraw.getImage());*/


                            PositionComponent posComp   = ( PositionComponent )ent.getComponent(GlobalVars.POSITION_COMPONENT_NAME);
                            PointF            drawPoint = posComp.getLocAsPoint();
                            drawPoint.X -= (posComp.width / 2.0f);
                            drawPoint.Y -= (posComp.height / 2.0f);

                            Graphics graph = Graphics.FromImage(staticObjImg);
                            lock (grnDraw.getImage()) {
                                graph.DrawImageUnscaled(grnDraw.getImage(), new Point(( int )drawPoint.X, ( int )drawPoint.Y));     //Draw the image to the view
                            }
                            grnDraw.needRedraw = false;
                            //redrawStatics = false;
                        }
                    }
                }

                //First, if there's a background entity, draw that!
                if (bkgEnt != null)
                {
                    drawBkgEntity(bkgEnt);
                }
                if (seperateStaticObjImage)
                {
                    drawStaticObjImage();
                }


                //If there's a grapple, draw it
                if (level.sysManager != null && level.sysManager.grapSystem.isGrappling)
                {
                    foreach (Entity e in GlobalVars.nonGroundEntities.Values)
                    {
                        if (e is GrappleEntity)
                        {
                            GrappleComponent grapComp = ( GrappleComponent )e.getComponent(GlobalVars.GRAPPLE_COMPONENT_NAME);

                            PointF start = grapComp.getFirstPoint();
                            PointF end   = grapComp.getLastPoint();

                            /*
                             * // Calc the pos relative to the view
                             * start.X -= this.x;
                             * start.Y -= this.y;
                             * end.X -= this.x;
                             * end.Y -= this.y;
                             *
                             * start.X *= wRatio;
                             * start.Y *= hRatio;
                             * end.X *= wRatio;
                             * end.Y *= hRatio;
                             */
                            g.DrawLine(GrapplePen, start, end);
                            break; //Should only be one - this'll save some time.
                        }
                    }
                }


                //For all applicable entities (Entities with required components)
                foreach (Entity e in entities)
                {
                    if (!(e is BackgroundEntity))
                    {
                        drawEntity(e);
                    }
                }

                if (level.sysManager.drawSystem.drawDebugStuff)
                {
                    for (int i = 0; i < level.sysManager.drawSystem.debugLines.Count - 1; i += 2)
                    {
                        g.DrawLine(new Pen(Color.Blue, 3), level.sysManager.drawSystem.debugLines[i], level.sysManager.drawSystem.debugLines[i + 1]);
                    }
                    level.sysManager.drawSystem.debugLines.Clear();
                    int circWidth = 4;
                    for (int i = 0; i < level.sysManager.drawSystem.debugPoints.Count; i++)
                    {
                        g.FillEllipse(Brushes.Green, ( int )Math.Round(level.sysManager.drawSystem.debugPoints[i].X - circWidth / 2), ( int )Math.Round(level.sysManager.drawSystem.debugPoints[i].Y) - circWidth / 2, circWidth, circWidth);
                    }
                    level.sysManager.drawSystem.debugPoints.Clear();
                }


                //mainG.DrawImage(drawImg, new Point((int)displayX, (int)displayY)); //Draw the view to the main window
                //mainG.DrawImageUnscaled(drawImg, new Point((int)displayX, (int)displayY)); //Draw the view to the main window
                mainG.DrawImage(drawImg, new RectangleF(displayX, displayY, displayWidth, displayHeight), new RectangleF(x, y, width, height), GraphicsUnit.Pixel);


                //Draw Border
                if (this.hasBorder)
                {
                    if (!this.borderFade)
                    {
                        mainG.DrawRectangle(new Pen(borderBrush, borderSize), new Rectangle(( int )(displayX), ( int )(displayY),
                                                                                            ( int )(displayWidth), ( int )(displayHeight)));
                    }
                    else
                    {
                        int alphaDiff = ( int )Math.Ceiling(255.0f / (borderSize - amntSolid));     //How much to decrease alpha per layer
                        //Draw the solid bit
                        //mainG.DrawRectangle(new Pen(borderBrush, amntSolid), new Rectangle((int)(displayX), (int)(displayY),
                        //(int)(displayWidth), (int)(displayHeight)));

                        int alphaVal = 255;
                        alphaVal -= alphaDiff;

                        for (int i = 0; i <= borderSize; i++)
                        {
                            if (alphaVal < 0)
                            {
                                alphaVal = 0;
                            }
                            Color tmpCol = Color.FromArgb(alphaVal, borderBrush.Color);
                            Pen   pen    = new Pen(new SolidBrush(tmpCol), 1);
                            mainG.DrawRectangle(pen, new Rectangle(( int )(displayX + i), ( int )(displayY + i),
                                                                   ( int )(displayWidth - 2 * i), ( int )(displayHeight - 2 * i))); alphaVal -= alphaDiff;
                            alphaVal -= alphaDiff;
                            if (alphaVal < 0)
                            {
                                alphaVal = 0;
                            }
                        }
                    }
                }
            }
            else
            {
                //For all applicable entities (Entities with required components)
                foreach (Entity e in entities)
                {
                    drawEntity(e);
                }
                mainG.DrawImage(drawImg, new RectangleF(displayX, displayY, displayWidth, displayHeight), new RectangleF(x, y, width, height), GraphicsUnit.Pixel);
            }
        }