加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 设计游戏对象

发布时间:2020-12-15 03:42:45 所属栏目:百科 来源:网络整理
导读:我最近开始使用微软XNA和C#,为自己的娱乐活动做一个小游戏.我的问题是设计一个游戏对象和继承它的对象.我将把游戏对象定义为可以在屏幕上渲染的东西.所以为此,我决定做一个基类,所有其他需要渲染的对象将继承,称为GameObject.下面的代码是我做的类: class G
我最近开始使用微软XNA和C#,为自己的娱乐活动做一个小游戏.我的问题是设计一个游戏对象和继承它的对象.我将把游戏对象定义为可以在屏幕上渲染的东西.所以为此,我决定做一个基类,所有其他需要渲染的对象将继承,称为GameObject.下面的代码是我做的类:
class GameObject
{
    private Model model = null;
    private float scale = 1f;
    private Vector3 position = Vector3.Zero;
    private Vector3 rotation = Vector3.Zero;
    private Vector3 velocity = Vector3.Zero;
    private bool alive = false;
    protected ContentManager content;

    #region Constructors
    public GameObject(ContentManager content,string modelResource)
    {
        this.content = content;
        model = content.Load<Model>(modelResource);
    }
    public GameObject(ContentManager content,string modelResource,bool alive)
        : this(content,modelResource)
    {
        this.alive = alive;
    }
    public GameObject(ContentManager content,bool alive,float scale)
        : this(content,modelResource,alive)
    {
        this.scale = scale;
    }
    public GameObject(ContentManager content,float scale,Vector3 position)
        : this(content,alive,scale)
    {
        this.position = position;
    }
    public GameObject(ContentManager content,Vector3 position,Vector3 rotation)
        : this(content,scale,position)
    {
        this.rotation = rotation;
    }
    public GameObject(ContentManager content,Vector3 rotation,Vector3 velocity)
        : this(content,position,rotation)
    {
        this.velocity = velocity;
    }
    #endregion
}

我省略了一些额外的方法,例如旋转,移动和绘制对象.现在如果我想创建另一个对象,像一艘船,我会创建一个Ship类,它将继承GameObject.下面的示例代码:

class Ship : GameObject
{
    private int num_missiles = 20; // the number of missiles this ship can have alive at any given time
    private Missile[] missiles;
    private float max_missile_distance = 3000f; // max distance a missile can be from the ship before it dies

    #region Constructors
    public Ship(ContentManager content,string modelResource)
        : base(content,modelResource)
    {
        InitShip();
    }
    public Ship(ContentManager content,bool alive)
        : base(content,alive)
    {
        InitShip();
    }
    public Ship(ContentManager content,float scale)
        : base(content,scale)
    {
        InitShip();
    }
    public Ship(ContentManager content,Vector3 position)
        : base(content,position)
    {
        InitShip();
    }
    public Ship(ContentManager content,Vector3 rotation)
        : base(content,rotation)
    {
        InitShip();
    }
    public Ship(ContentManager content,Vector3 velocity)
        : base(content,rotation,velocity)
    {
        InitShip();
    }
    #endregion
}

再次,我没有任何额外的特定于船的方法,比如射击导弹.你认为这样的设计是好的还是应该改进一下,或是完全改变?看起来孩子类的构造函数是凌乱的,但也许这是唯一的方法.我从来没有做过这样的事情,想知道我是否走路.

感谢大家留下的答案.他们都非常有帮助.似乎有一个普遍的共识,改变它来使用MVC模式将是最好的.我将进一步探讨如何做到这一点.我还将删除大部分的构造函数,并且只有一个构造函数,因为modelResource之后的所有参数都不需要创建对象,并且可以随后通过方法调用进行更改.

解决方法

个人来说,我发现绝大多数的构造函数都有一点点意义,但这是你的选择,没有什么根本的错误:)

关于从共同基地衍生游戏对象的一般策略,这是一个很常见的做事方式.标准,均匀.我倾向于开始使用类似于MVC的东西,拥有令人难以置信的轻量级的“模型”游戏对象,它们只包含数据.

我在XNA中看到的其他常见方法包括/你可能想要考虑的事情:

>实现一个IRenderable接口,而不是在基类或派生类中执行任何渲染代码.例如,您可能需要从未渲染的游戏对象 – waypoint或somesuch.
>你可以使你的基类抽象,你不可能想要实例化一个GameObject.

再次,小点.你的设计很好

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读