C# “如何制作一个对象”;“可扩展”;以某种形式呈现时
我正在Winform中以与此示例相同的方式渲染游戏: 在我的游戏中,我有一些对象,例如一个矩形,一旦创建,我就可以在我的游戏世界中放置和移动它。我在这里的项目是一个级别编辑器 我想做的是让每个对象都像我们通常使用的每个软件一样具有“可伸缩性”或“可伸缩性”(如果这个词不正确,很抱歉),我的意思是: 我有一门课,像:C# “如何制作一个对象”;“可扩展”;以某种形式呈现时,c#,winforms,visual-studio-2010,xna,xna-4.0,C#,Winforms,Visual Studio 2010,Xna,Xna 4.0,我正在Winform中以与此示例相同的方式渲染游戏: 在我的游戏中,我有一些对象,例如一个矩形,一旦创建,我就可以在我的游戏世界中放置和移动它。我在这里的项目是一个级别编辑器 我想做的是让每个对象都像我们通常使用的每个软件一样具有“可伸缩性”或“可伸缩性”(如果这个词不正确,很抱歉),我的意思是: 我有一门课,像: 公共抽象类游戏对象 { 受保护向量2的位置=向量2.0; 受保护的浮动旋转=0.0f; 受保护矢量2比例=矢量2.1; 保护浮子深度_u0=0.0f; 受保护的布尔值是可通过的;
公共抽象类游戏对象
{
受保护向量2的位置=向量2.0;
受保护的浮动旋转=0.0f;
受保护矢量2比例=矢量2.1;
保护浮子深度_u0=0.0f;
受保护的布尔值是可通过的;
受保护的游戏对象(
矢量2(起始位置)
{
此位置=起始位置;
}
[显示名称(“位置”)]
公共虚拟向量2位置
{
获取{返回位置}
设置{position\=value;}
}
[浏览属性(错误)]
公共抽象矩形
{
得到;
}
[浏览属性(错误)]
公共抽象矩形选择矩形
{
得到;
}
[显示名称(“比例”)]
公共抽象向量2量表
{
得到;
设置
}
[浏览属性(错误)]
公共虚拟浮动深度
{
获取{返回深度}
设置{depth\=value;}
}
[显示名称(“是否可访问?”)]
公共场所可通行
{
get{return是可通过的}
集合{is_passable_=value;}
}
[浏览属性(错误)]
公共抽象向量2纹理化
{
得到;
}
公开摘要无效更新(游戏时间游戏时间);
公开摘要无效抽取(SpriteBatch-SpriteBatch);
}
一旦类被实例化,在表单中我想做如下事情:(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);
}
public SelectObject(GameObject obj)
{
List 4verticesList=新列表();
//
//将SelectionRectangle的4个顶点坐标添加到4Vertices列表的代码
//
foreach(矢量2顶点在4VerticesList中)
DrawLittleRectangle(顶点_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);
}
使用这些功能:
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));
//...
唯一缺少的是处理每个方块上的点击。这是通过使用鼠标位置coor进行简单的“Contains”测试来完成的
结果的屏幕:
我想你要找的是VC++中类似于
CRectTracker
()的东西。不幸的是,目前还没有C#等价物。你可以在下面的链接中找到初学者
我会随机猜测Graphics.ScaleTransform()就是答案。张贴代码而不是图片。