gpt4 book ai didi

c# - 实体组件系统 c#

转载 作者:太空宇宙 更新时间:2023-11-03 12:25:06 24 4
gpt4 key购买 nike

我目前正在使用 C# 创建一个 2D 游戏引擎。目前,我正在实现一个实体组件系统。

我的结构如下:

  • 实体类:包含IGameComponent 的列表,您可以按类类型添加、删除和删除任何组件。 (即 entity.RemoveComponent(typeof(Transform));。它还包含一个父实体和一个子实体列表。
  • IGameComponent 接口(interface):目前,它只是一个空接口(interface)。 (注意:组件只包含数据,不包含功能)
  • 实体池:包含游戏中所有事件对象的列表,它还用于创建和销毁实体。

到目前为止一切都很好。

但是,我遇到了一个问题。由于组件只包含数据,我需要一种方法来初始化、更新和渲染组件,我不想只向 GameComponent 类添加一堆虚拟方法,但我不知道有任何其他方法可以解决它。

我有哪些选择?

编辑:

我已经看到 Unity 使用像“SendMessage”这样的方法,我只能假设它使用反射来调用方法。我应该实现类似的东西吗?

最佳答案

我不知道你是否还需要它,但我几年前做过类似的东西,它可能对你有帮助。它是用基于 MonoGame/XNA 的 C# 编写的

你的 GameObject 类可以看起来像这样

public class GameObject
{
List<GameObjectComponent> _goComponent = new List<GameObjectComponent();

public T GetComponent<T>() where T : GameObjectComponent
{
foreach (GameObjectComponent goc in _goComponent)
if (goc.GetType().Equals(typeof(T)))
return (T)goc;
return null;
}

public void AddComponent(GameObjectComponent gameObjectComponent)
{
_goComponent.Add(gameObjectComponent);
gameObjectComponent.gameObject = this;
gameObjectComponent.Init();
}

public virtual void Update(GameTime gameTime)
{
foreach (GameObjectComponent _goc in _goComponent)
_goc.Update(gameTime);
}

public static void Instantiate(GameObject gameObject)
{
Scene._AddedGO.Add(gameObject);
}

public static void Destroy(GameObject gameObject)
{
Scene._RemoveGO.Add(gameObject);
}

}

GameObjectComponent 类似于 Unity3D 中的 MonoBehaivior

  public class GameObjectComponent
{

public GameObject gameObject;

public GameObjectComponent()
{

}

public virtual void Init()
{

}

public virtual void Update(GameTime gameTime)
{

}
}

然后你像这样继承其他类:

public class Sprite : GameObjectComponent
{
public Texture2D texture;
public Vector2 origin = Vector2.Zero;
public Rectangle rect;
public Rectangle sourceRect;
public Color color = Color.White;
public float rotation = 0f;
private float layerDepth = 0f;
public int scale = 1;

public Sprite()
{

}

public void Load(string path)
{
texture = Setup.ContentDevice.Load<Texture2D>(path);
}

}

现在你终于可以创建你的玩家游戏对象了

class Player : GameObjectComponent
{
float speed = 150f;
KeyboardState keyState;

float pos_X;
float pos_Y;
int rect_X;
int rect_Y;

public Player(float x, float y, int rx, int ry)
{
pos_X = x;
pos_Y = y;
rect_X = rx;
rect_Y = ry;
}

public override void Init()
{
Sprite sprite = new Sprite();
gameObject.AddComponent(sprite);

gameObject.GetComponent<Sprite>().Load("Sprites/MainGuySpriteSheet_0");
gameObject.GetComponent<Sprite>().scale = 1;
gameObject.GetComponent<Sprite>().rect = new Rectangle(46, 0, 32, 36);


Transform transform = new Transform();
gameObject.AddComponent(transform);
// gameObject.GetComponent<Transform>().position = new Vector2(Screen.width / 2 - gameObject.GetComponent<Sprite>().rect.Width, Screen.height / 2 - gameObject.GetComponent<Sprite>().rect.Height);
gameObject.GetComponent<Transform>().position = new Vector2(pos_X, pos_Y - 32 * (gameObject.GetComponent<Sprite>().scale - 1));

RectCollider collider = new RectCollider();
gameObject.AddComponent(collider);
gameObject.GetComponent<RectCollider>().Set(gameObject.GetComponent<Sprite>(), gameObject.GetComponent<Transform>());

SpriteRenderer render = new SpriteRenderer();
gameObject.AddComponent(render);
gameObject.GetComponent<SpriteRenderer>().layer = 1;
gameObject.GetComponent<SpriteRenderer>().Set(gameObject.GetComponent<Sprite>());
}

public override void Update(GameTime gameTime)
{
//movex = transform.position.X -= 25 * gameTime.DeltaTime();


if (Keyboard.GetState().IsKeyDown(Keys.Left))
gameObject.GetComponent<Transform>().Move(-speed * gameTime.DeltaTime(), 0);

else if (Keyboard.GetState().IsKeyDown(Keys.Right))
gameObject.GetComponent<Transform>().Move(speed * gameTime.DeltaTime(), 0);

else if (Keyboard.GetState().IsKeyDown(Keys.Down))
gameObject.GetComponent<Transform>().Move(0, speed * gameTime.DeltaTime());

else if (Keyboard.GetState().IsKeyDown(Keys.Up))
gameObject.GetComponent<Transform>().Move(0, -speed * gameTime.DeltaTime());

if (Keyboard.GetState().IsKeyDown(Keys.Space) && !keyState.IsKeyDown(Keys.Space))
{
GameObject tomato = new GameObject();
tomato.AddComponent(new Tomato());
tomato.GetComponent<Transform>().position = gameObject.GetComponent<Transform>().position;
GameObject.Instantiate(tomato);
}

if (Keyboard.GetState().IsKeyDown(Keys.Q) && !keyState.IsKeyDown(Keys.Q))
{
SceneManager.LoadScene(new AnotherOne());
}

keyState = Keyboard.GetState();


gameObject.GetComponent<Transform>().position.Y = MathHelper.Clamp(gameObject.GetComponent<Transform>().position.Y, 0, Screen.bounds.Height - gameObject.GetComponent<Sprite>().rect.Height * gameObject.GetComponent<Sprite>().scale);
gameObject.GetComponent<Transform>().position.X = MathHelper.Clamp(gameObject.GetComponent<Transform>().position.X, 0, Screen.bounds.Width - gameObject.GetComponent<Sprite>().rect.Width * gameObject.GetComponent<Sprite>().scale);
}

}

我希望它不会太困惑并能对您有所帮助。为了更清楚,我在这里留下了一个指向 git 的链接:https://github.com/Memorix101/MonoGame_ComponentSystem

干杯,Memorix101 :)

关于c# - 实体组件系统 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45397374/

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