我是 Monogame 的初学者,我刚刚完成了一个简单的 2D 游戏的游戏玩法部分。在我继续添加菜单等之前,我想要一些关于我组织项目的方式的反馈。除了 Monogame Basics 上的一个 YouTube 视频以及 Discord 上一些好心的陌生人的帮助,我完全利用了这个项目。我试图尽可能地简化代码。
Game1.cs
public class Game1 : Game
{
// declaring some stuff
enum GameState
{
MainMenu, Playing
}
GameState currentState;
public Game1()
{
AssetLoader.Content = Content;
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
graphics.ApplyChanges();
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
state_Playing = new State_Playing(GraphicsDevice);
state_mainMenu = new State_mainMenu(this);
currentState = GameState.Playing;
spriteBatch = new SpriteBatch(GraphicsDevice);
base.LoadContent();
currentState = GameState.Playing;
switch (currentState)
{
case GameState.MainMenu:
state_mainMenu.LoadContent(Content);
break;
case GameState.Playing:
state_Playing.LoadContent(Content);
break;
}
}
protected override void Update(GameTime gameTime)
{
switch (currentState)
{
case GameState.MainMenu:
state_mainMenu.Update(gameTime);
break;
case GameState.Playing:
state_Playing.Update(gameTime);
break;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
switch (currentState)
{
case GameState.MainMenu:
state_mainMenu.Draw(spriteBatch);
break;
case GameState.Playing:
state_Playing.Draw(spriteBatch);
break;
}
spriteBatch.End();
}
}
State_Playing.cs
class State_Playing
{
// declaring some stuff
public State_Playing(GraphicsDevice graphicsDevice)
{
this.graphicsDevice = graphicsDevice;
}
public void LoadContent(ContentManager content)
{
Texture2D someTexture = content.Load<Texture2D>("texture/Name")
AssetLoader.Load<Texture2D>("aTextureName", someTexture)
objectInstance = new objectName (
texture: AssetLoader.Get<Texture2D>("aTextureName"),
anotherVar: value );
Texture2D anotherTexture = content.Load<Texture2D>("texture/Name")
AssetLoader.Load<Texture2D>("anotherTextureName", anotherTexture)
anotherInstance = new anotherObject (
texture: AssetLoader.Get<Texture2D>("anotherTextureName"),
anotherVar: value );
}
public void Update(GameTime gameTime)
{
objectInstance.Update(gameTime);
anotherInstance.Update(gameTime);
}
public void Draw(SpriteBatch spriteBatch)
{
objectInstance.Draw(spriteBatch);
anotherInstance.Draw(
}
}