[英]I'm making a game, can/should I create an “Enemy” class?
I'm making a game for my final project in an XNA class I'm taking, it's going to be a FPS. 我正在为要参加的XNA课程的最终项目制作游戏,这将是FPS。 In order to generate enemies, I figured that I could write a class that imports a model and defines the random size and placement of the "Enemy", as well as his movements and actions.
为了产生敌人,我认为我可以编写一个类,以导入模型并定义“敌人”的随机大小和位置以及他的动作和动作。 I would then call that class from my Game.cs file.
然后,我将从Game.cs文件中调用该类。 However, I'm having some difficulty with this.
但是,我对此有些困难。
My main issue is that I'm not sure where/how to call the Enemy (which is a snowman) in the Game file. 我的主要问题是我不确定在“游戏”文件中在何处/如何调用敌人(雪人)。
Here's what I have for the Snowmen.cs (enemy) class 这是我的Snowmen.cs(敌人)课程的内容
public class Snowmen : Microsoft.Xna.Framework.Game
{
private Camera cam = new Camera();
Model snowMan;
Matrix[] snowManMatrix;
protected override void LoadContent()
{
snowMan = Content.Load<Model>( "Models\\snowman" );
snowManMatrix = new Matrix[ snowMan.Bones.Count ];
snowMan.CopyAbsoluteBoneTransformsTo( snowManMatrix );
}
public void DrawSnowMan(Model model, GameTime gameTime)
{
foreach (ModelMesh mesh in model.Meshes)
{
Matrix world, scale, translation;
scale = Matrix.CreateScale(0.02f, 0.02f, 0.02f);
translation = Matrix.CreateScale(0.0f, 0.7f, -4.0f);
world = scale * translation;
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = snowManMatrix[mesh.ParentBone.Index] * world;
effect.View = cam.viewMatrix;
effect.Projection = cam.projectionMatrix;
effect.EnableDefaultLighting();
}
mesh.Draw();
}
}
protected override void Draw(GameTime gameTime)
{
DrawSnowMan( snowMan, gameTime );
base.Draw(gameTime);
}
}
As of right now, My Game1.cs file is a functional skybox and also contains LoadContent()
and Draw()
methods. 截至目前,“我的Game1.cs”文件是一个功能齐全的天空盒,还包含
LoadContent()
和Draw()
方法。 Is the enemy class completely unnecessary? 敌人阶级是完全没有必要的吗?
You would probably want to use a DrawableGameObject 您可能要使用DrawableGameObject
public class Snowman : DrawableGameObject
{
private Camera cam = new Camera();
//Model snowMan;
//Matrix[] snowManMatrix;
public Model snowMan {get;set;};
public Matrix[] snowManMatrix{get;set;};
protected override void LoadContent()
{
//snowMan = Content.Load<Model>( "Models\\snowman" );
snowManMatrix = new Matrix[ snowMan.Bones.Count ];
snowMan.CopyAbsoluteBoneTransformsTo( snowManMatrix );
}
public void DrawSnowMan(Model model, GameTime gameTime)
{
foreach (ModelMesh mesh in model.Meshes)
{
Matrix world, scale, translation;
scale = Matrix.CreateScale(0.02f, 0.02f, 0.02f);
translation = Matrix.CreateScale(0.0f, 0.7f, -4.0f);
world = scale * translation;
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = snowManMatrix[mesh.ParentBone.Index] * world;
effect.View = cam.viewMatrix;
effect.Projection = cam.projectionMatrix;
effect.EnableDefaultLighting();
}
mesh.Draw();
}
}
protected override void Draw(GameTime gameTime)
{
DrawSnowMan( snowMan, gameTime );
}
}
In Game1.cs 在Game1.cs中
Initialize 初始化
Snowman snowDude= new Snowman();
Components.Add(snowDude);
And move 并移动
//snowDude = Content.Load<Model>( "Models\\snowman" );
snowDude.snowMan = Content.Load<Model>( "Models\\snowman" );
into Game1's LoadContent 进入Game1的LoadContent
Adding to Components means that snowDude's Update and Draw are called whenever Game1's is. 添加到组件意味着在Game1的任何时候调用snowDude的Update和Draw。 I am honestly not 100% on in what order though.
老实说,我不是100%按什么顺序来做。 I believe Load Content should be called as well but I think it is better practice (at least while you're learning) to import all of your sounds/animations/sprites in the same place (in your main file) so that you know exactly what is being called and when.
我相信也应该调用“加载内容”,但我认为(至少在学习时)将所有声音/动画/精灵导入同一位置(在主文件中)是一种更好的做法,这样您就可以确切知道什么叫什么,什么时候。
OR instead of Components.Add(snowDude); OR而不是Components.Add(snowDude);
You can just call snowDude.Draw(gametime) in Game1's Draw Method. 您可以只在Game1的Draw方法中调用snowDude.Draw(gametime)。 and snowDude.Update() in Game1's Update method.
和game1的Update方法中的snowDude.Update()。
And otherwise I agree with Brent. 否则我同意布伦特。 If there are only Snowmen then you should be fine with only one class but if you start adding more there will be a lot of overlap, so making an Enemy Class
如果只有雪人,那么只有一个班级就可以了,但是如果您开始添加更多的雪人,那将会有很多重叠,因此请制作一个敌人班级
public class Enemy: DrawableGameObject{}
and changing SnowMan to 并将SnowMan更改为
public class SnowMan:Enemy{}
should be appropriate. 应该适当。
If you only have snowmen than this is fine. 如果您只有雪人,那就很好。 When you add other enemies, or similar objects then adding a Parent class such as Enemy is useful both structurally, and it allows you to centralize your code and reduce/eliminate duplicate code.
当您添加其他敌人或类似的对象时,则添加诸如Enemy之类的Parent类在结构上都是有用的,它使您可以集中代码并减少/消除重复的代码。
If you expect your game to grow considerably, you might consider an entity system: 如果您希望自己的游戏发展迅速,可以考虑使用实体系统:
http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/ http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/
http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/ http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/
An entity system can help you overcome some of the design challenges inherent in traditional object-oriented solutions (Deep hierarchies, god objects, etc.) as well as keep your game flexible in the face of change. 实体系统可以帮助您克服传统的面向对象解决方案所固有的一些设计挑战(深层次结构,上帝对象等),并使您的游戏在面对变化时保持灵活性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.