如何使一个物体与QUOT;可升级的QUOT;而呈现的形式 [英] How to make an object "scalable" while rendered in a form

查看:243
本文介绍了如何使一个物体与QUOT;可升级的QUOT;而呈现的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的渲染我的游戏在作为此示例中做了同样的方式一个WinForm:的的WinForms系列1:图形设备



在我的比赛,我有一些物体,比如我可以把已经和移动,在我的游戏世界中的矩形一旦创建。我在这里的项目是一个级别的编辑器。



我想要做的就是让每一个对象相当大或可扩展性(对不起,如果这不是正确的字)作为在每一个我们通常使用的软件做了同样的方式,我的意思是:



我有这样一个类:



<预类=郎-CS prettyprint-覆盖> 公共抽象类的游戏对象
{
保护Vector2 position_ = Vector2.Zero;
保护浮动rotation_ = 0.0;
保护Vector2 scale_ = Vector2.One;
保护浮动depth_ = 0.0;

保护布尔is_passable_ = TRUE;

保护的游戏物体(
Vector2 starting_position)
{
this.position_ = starting_position;
}

[DisplayName的(位置)]
公共虚拟Vector2位置
{
{返回position_; }
集合{position_ =价值; }
}

[BrowsableAttribute(假)]
公共抽象的矩形PositionRectangle
{
搞定;
}

[BrowsableAttribute(假)]
公共抽象的矩形SelectionRectangle
{
搞定;
}

[DisplayName的(缩放)]
公共抽象Vector2规模
{
搞定;
组;
}

[BrowsableAttribute(假)]
公众持股量的虚拟深度
{
{返回depth_; }
集合{depth_ =价值; }
}

[DisplayName的(IsPassable?)]
公共BOOL IsPassable
{
{返回is_passable_; }
集合{is_passable_ =价值; }
}

[BrowsableAttribute(假)]
公共抽象Vector2 TextureSize
{
搞定;
}

公共抽象无效更新(GameTime gameTime);
公共抽象无效抽奖(SpriteBatch spriteBatch);
}



一旦类实例化,表单里面我觉得做这样的事: (gameWrapper与样品创建控件绘制表格里面的游戏)

 私人无效gameWrapper_MouseClick_1(对象发件人,MouseEventArgsê )
{
Vector2 mouse_xy =新Vector2(e.Location.X,e.Location.Y);
游戏对象的obj = gameWrapper.GetObjectByPosition(mouse_xy);
propertyGrid1.SelectedObject = OBJ;
如果(OBJ!= NULL)
gameWrapper.SelectObject(OBJ);
,否则gameWrapper.Unselect();

propertyGrid1.Refresh();
}

在gameWrapper:

 公共选择对象(游戏对象OBJ)
{
名单,LT; Vector2> 4verticesList =新名单();
//
//代码添加4个顶点坐标SelectionRectangle到4verticesList
//

的foreach(Vector2 vertex_xy在4VerticesList)
DrawLittleRectangle的( vertex_xy);
}

这正是我所想的事情。绘制函数小按钮/矩形,然后点击处理它们。



有没有已经写一些代码来实现这一行为呢?其实我并不担心对象将如何调整,但只是到estetic按钮。


解决方案

我已经做到了!



主要功能:

 私人无效DrawObjectSelection(spriteBatch spriteBatch,矩形r)
{
R = Telecamera.Transform(R);

Vector2 V1 =新Vector2(r.X,r.Y);
Vector2 V2 =新Vector2(r.X + r.Width,r.Y);
Vector2 V3 =新Vector2(r.X,r.Y + r.Height);
Vector2 V4 =新Vector2(r.X + r.Width,r.Y + r.Height);

//本方矩形
DrawEmptyRectangle(spriteBatch,V1,V2,V3,V4);

// 4格在顶点
DrawFilledSquare(spriteBatch,V1);
DrawFilledSquare(spriteBatch,V2);
DrawFilledSquare(spriteBatch,V3);
DrawFilledSquare(spriteBatch,V4);

//其他4个在每边
浮动高度= v4.Y中间 - v1.Y;
浮动宽度= V2.X - V1.X;

DrawFilledSquare(spriteBatch,新Vector2(V1.X,v1.Y +身高/ 2));
DrawFilledSquare(spriteBatch,新Vector2(2.x版,v2.Y +身高/ 2));
DrawFilledSquare(spriteBatch,新Vector2(V1.X +宽/ 2,v1.Y));
DrawFilledSquare(spriteBatch,新Vector2(V1.X +宽/ 2,v4.Y));
}



有了这些功能:



 私人无效的DrawLine(
SpriteBatch spriteBatch,
浮动幅度,
Vector2点1,Vector2点2)
{
浮动角=(浮点)Math.Atan2(point2.Y - point1.Y,point2.X - point1.X);
浮长= Vector2.Distance(点1,点2);

spriteBatch.Draw(
grid_texture,
点1,
空,
selection_color,
角,
Vector2.Zero,
新Vector2(长度,宽度),
SpriteEffects.None,
0);
}

私人无效DrawEmptyRectangle(
SpriteBatch spriteBatch,
Vector2 V1,
Vector2 V2,
Vector2 V3,
Vector2 V4)
{
/ *
* V1 V2 ****
* * *
* * *
* V3 **** V4
* /

的DrawLine(spriteBatch,1.0F,V1,V2);
的DrawLine(spriteBatch,1.0F,V1,V3);
的DrawLine(spriteBatch,1.0F,V2,V4);
的DrawLine(spriteBatch,1.0F,V3,V4);
}

私人无效DrawFilledSquare(
SpriteBatch spriteBatch,
Vector2 c_pos)//随着中心位置
{
INT拉托= 8;

spriteBatch.Draw(
grid_texture,
新的Rectangle(
(INT)c_pos.X - 拉托/ 2,
(INT)c_pos.Y - 拉托/ 2,
拉托,
拉托),
selection_color);

}

当我想画它,我只需要一个矩形例如:

  // ... 

DrawObjectSelection(spriteBatch,Camera.Transform(gameObject1 .PositionRectangle));

// ...



唯一缺少的是处理点击在每平方。这是通过简单的做包含与鼠标位置COOR测试



结果的屏幕:




I am rendering my game in a Winform in the same way as done in this sample: WinForms Series 1: Graphics Device

In my game I have some object, for example a rectangle that I can already put and move, in my game world, once created. My project here is a level-editor.

What I want to do is to make every object "sizable" or "scalable" (sorry if this isn't the correct word) in the same way as done in every software we commonly use, I mean:

I have a class like:

public abstract class GameObject
{
    protected Vector2 position_ = Vector2.Zero;
    protected float rotation_ = 0.0f;
    protected Vector2 scale_ = Vector2.One;
    protected float depth_ = 0.0f;

    protected bool is_passable_ = true;

    protected GameObject(
        Vector2 starting_position)
    {
        this.position_ = starting_position;
    }

    [DisplayName("Position")]
    public virtual Vector2 Position
    {
        get { return position_; }
        set { position_ = value; }
    }

    [BrowsableAttribute(false)]
    public abstract Rectangle PositionRectangle
    {
        get;
    }

    [BrowsableAttribute(false)]
    public abstract Rectangle SelectionRectangle
    {
        get;
    }

    [DisplayName("Scale")]
    public abstract Vector2 Scale
    {
        get;
        set;
    }

    [BrowsableAttribute(false)]
    public virtual float Depth
    {
        get { return depth_; }
        set { depth_ = value; }
    }

    [DisplayName("IsPassable?")]
    public bool IsPassable
    {
        get { return is_passable_; }
        set { is_passable_ = value; }
    }

    [BrowsableAttribute(false)]
    public abstract Vector2 TextureSize
    {
        get;
    }

    public abstract void Update(GameTime gameTime);
    public abstract void Draw(SpriteBatch spriteBatch);
}

Once the class is instantiated, inside the form I thought to do something like: (gameWrapper is the control created with the sample to draw the game inside the form)

private void gameWrapper_MouseClick_1(object sender, MouseEventArgs e)
{
    Vector2 mouse_xy = new Vector2(e.Location.X, e.Location.Y);
    GameObject obj = gameWrapper.GetObjectByPosition(mouse_xy);
    propertyGrid1.SelectedObject = obj;
    if (obj != null)
        gameWrapper.SelectObject(obj);
    else gameWrapper.Unselect();

    propertyGrid1.Refresh();
 }

Inside gameWrapper:

public SelectObject(GameObject obj)
{
     List<Vector2> 4verticesList = new List();
     //
     // Code to add 4 vertices coordinates of the SelectionRectangle to 4verticesList
     //         

     foreach (Vector2 vertex_xy in 4VerticesList)
        DrawLittleRectangle(vertex_xy);
}

This is just what I thought to do. Draw little buttons/rectangles with a function and then handle clicks to them.

Is there already written some code to achieve this behaviour? Actually I'm not worried about how the object will resize, but just to the estetic buttons.

解决方案

I've done it!

The main function:

private void DrawObjectSelection(SpriteBatch spriteBatch, Rectangle r)
{
    r = Telecamera.Transform(r);

    Vector2 v1 = new Vector2(r.X, r.Y);
    Vector2 v2 = new Vector2(r.X + r.Width, r.Y);
    Vector2 v3 = new Vector2(r.X, r.Y + r.Height);
    Vector2 v4 = new Vector2(r.X + r.Width, r.Y + r.Height);

    //The side rectangle
    DrawEmptyRectangle(spriteBatch, v1, v2, v3, v4);

    //4 squares at vertices
    DrawFilledSquare(spriteBatch, v1);
    DrawFilledSquare(spriteBatch, v2);
    DrawFilledSquare(spriteBatch, v3);
    DrawFilledSquare(spriteBatch, v4);

    //the other 4 in the middle of every side
    float height = v4.Y - v1.Y;
    float width = v2.X - v1.X;

    DrawFilledSquare(spriteBatch, new Vector2(v1.X, v1.Y + height / 2));
    DrawFilledSquare(spriteBatch, new Vector2(v2.X, v2.Y + height / 2));
    DrawFilledSquare(spriteBatch, new Vector2(v1.X + width / 2, v1.Y));
    DrawFilledSquare(spriteBatch, new Vector2(v1.X + width / 2, v4.Y));
}

With these functions:

private void DrawLine(
    SpriteBatch spriteBatch,
    float width,
    Vector2 point1, Vector2 point2)
{
    float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
    float length = Vector2.Distance(point1, point2);

    spriteBatch.Draw(
        grid_texture,
        point1,
        null,
        selection_color,
        angle,
        Vector2.Zero,
        new Vector2(length, width),
        SpriteEffects.None,
        0);
}

private void DrawEmptyRectangle(
    SpriteBatch spriteBatch,
    Vector2 v1,
    Vector2 v2,
    Vector2 v3,
    Vector2 v4)
{
    /*
     * V1****V2
     * *      *
     * *      *
     * V3****V4
     */

    DrawLine(spriteBatch, 1.0f, v1, v2);
    DrawLine(spriteBatch, 1.0f, v1, v3);
    DrawLine(spriteBatch, 1.0f, v2, v4);
    DrawLine(spriteBatch, 1.0f, v3, v4);
}

private void DrawFilledSquare(
    SpriteBatch spriteBatch,
    Vector2 c_pos) //With center position
{
    int lato = 8;

    spriteBatch.Draw(
        grid_texture,
        new Rectangle(
            (int)c_pos.X - lato/2,
            (int)c_pos.Y - lato/2,
            lato,
            lato),
            selection_color);

}

When I want to draw it, I just need a rectangle, for example:

//...

DrawObjectSelection(spriteBatch, Camera.Transform(gameObject1.PositionRectangle));

//...

The only thing missing is Handling of clicks on every square. This is done by a simple "Contains" test with mouse position coor.

A screen of the result:

这篇关于如何使一个物体与QUOT;可升级的QUOT;而呈现的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆