I’m making a game for my final project in an XNA class I’m taking, it’s going to be a 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. 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
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. Is the enemy class completely unnecessary?
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.