Home > Net >  Having trouble trying to manage/organise my Draw class in Monogame - or even trying to tell if it�
Having trouble trying to manage/organise my Draw class in Monogame - or even trying to tell if it�

Time:08-18

I'm trying to create a basic Touhou like game as practice for C#, OOP, and Monogame, and part of that is me trying to see if putting all my drawing functions into a seperate class is a good idea.

My concerns have rooted from performance - it seems very redudant to have to create a new GraphicsDeviceManager everytime an object is drawn/created, so I thought I could just have the draw function inherit the GraphicsDeviceManager and possibly even SpriteBatch from the main game class... however this has made my code a bit of a headache for me to organise properly.

Extra context: So far, I've only really made it so my player can move about, I want to flesh out my classes a lot more before I start putting it all together properly, because otherwise it can become a pain to manage.

So far, my classes are broken down as follows: Game1.c <- monogame default when created with the crossplatform template Player.cs <- Handles user input and inherits from the entity class Entity.cs <- base class for all entities, eg the player, bosses, bullets. Contains speed and rotation data, as well as functions for moving towards a certain point on the screen, as well as some collision handling. (Upon writing this, it seems like it might be best to move the speed and rotation data into GraphicsManager, but I'm not too sure). GraphicsManager <- Handles setting/changing textures as well as drawing. Entity.cs inherits from here for now, but I think there's a better way of doing this.

Once I fully flesh out the draw class and make sure entity works to my liking I'll start implementing bullets and bullet patterns.

And here are the relevant tidbits of each class:

GraphicsMangager.cs

public class GraphicsManager{
    public Texture2D texture;
    public Vector2 position;
    private GraphicsDeviceManager _graphics;

    private SpriteBatch _spriteBatch;

    public GraphicsManager(GraphicsDeviceManager g, SpriteBatch sb){
        _graphics = g;
        _spriteBatch = sb;
    }

    public void SetTexture(Texture2D newTexture){texture = newTexture);}

    pupublic void Draw(){
        _spriteBatch.Begin(samplerState: SamplerState.PointClamp);
        _spriteBatch.Draw(texture, position, Color.White);
        _spriteBatch.End();
        
    }

}
Entity.cs

public class Entity : GraphicsManager {
    public float speed;
    public float rotation = 0f;

    private Vector2 origin, dest;

    public int screenWidth, screenHeight;

    public Entity(int newscreenWidth, int newscreenHeight, Vector2 newPosition, float newSpeed) {
        screenHeight = newscreenHeight;
        screenWidth = newscreenWidth;
        position = newPosition;
        speed = newSpeed;
    }
}
Player.cs

public class Player : Entity {
    public Player(int newscreenWidth, int newscreenHeight, Vector2 newPosition, float newSpeed) : base(newscreenWidth, newscreenHeight, newPosition, newSpeed) { }
}

CodePudding user response:

If you want to share the same behaviour among other classes, then it is definetely the case of using inheritance, if not then it is case to use composition. Read more about when to choose composition or inheritance in this beatiful answer.

As your items should be drawn, then I think it is the case of inheritance. So in my view, it is okay to have such structure of code.

  • Related