gpt4 book ai didi

c# - 在表单中呈现时如何使对象 "scalable"

转载 作者:太空狗 更新时间:2023-10-29 18:35:37 25 4
gpt4 key购买 nike

我在 Winform 中渲染我的游戏的方式与此示例中的方式相同:WinForms Series 1: Graphics Device

在我的游戏中,我有一些对象,例如一个矩形,一旦创建,我就可以在我的游戏世界中放置和移动它。我这里的项目是关卡编辑器。

我想做的是使每个对象“可缩放”或“可缩放”(抱歉,如果这个词不正确),就像我们常用的每个软件所做的一样,我的意思是: enter image description here

我有一个类:

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);
}

一旦类被实例化,我想在表单内做类似的事情:(gameWrapper 是用示例创建的控件,用于在表单内绘制游戏)

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();
}

在 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);
}

这正是我想做的。使用函数绘制小按钮/矩形,然后处理对它们的点击。

是否已经编写了一些代码来实现这种行为?实际上我并不担心对象将如何调整大小,而只是担心美观的按钮。

最佳答案

我做到了!

主要功能:

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));
}

具有这些功能:

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);

}

当我要绘制它时,我只需要一个矩形,例如:

//...

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

//...

唯一缺少的是处理每个方 block 上的点击。这是通过使用鼠标位置坐标的简单“包含”测试来完成的。

结果的屏幕:

A screen of the result

关于c# - 在表单中呈现时如何使对象 "scalable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20870285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com