C
C#6mo ago
Tycho

MonoGame - inheritance implementation.

Hello, all! I've been assigned a MonoGame assignment for school and wanted to challenge myself a bit more with some good inheritance implementation. Now, I'm not sure if I'm even using it correctly, which is why I wanted to ask you. Have I implemented it well, are there any upgrades to be made? There are two additional classes under the Obstacle class which I weren't able to add in a new 'file'. Thanks!
10 Replies
Tycho
Tycho6mo ago
BlazeBin - yvzwtancszds
A tool for sharing your source code with the world!
Stigl
Stigl6mo ago
I think the ChangeScreenSize is uselless, the window should resize automatically And also i think using Vector2 for the position is kindof useless since you can use just short (that only matters if you have a large amount of game objects) And also if you can, when you will publish it please replace the TakeDamage method with just making the health public. Calling method is a lot slower Same applies to things like Draw method inside Bullet etc. you can eighter call the function directly without having to cast it trough the child method, or just redirect it (it should be more optimized) like this public override void Draw(SpriteBatch pSpriteBatch) => base.Draw(pSpriteBatch);
Tycho
Tycho6mo ago
The "Draw" function requires Vector2
Asher
Asher6mo ago
is Game a class from a library you're using? or did you create it?
Tycho
Tycho6mo ago
Game is a standard library created for MonoGame
Asher
Asher6mo ago
in general from what I know most libraries or engines when creating some base class will expect you to override certain functions but if you really want to get a grasp on inheritence I'd argue you should be making the base class with an actual use case
Tycho
Tycho6mo ago
I "generally' understand inheritance but I more want to know if the way I'm implementing it is normally accepted
Asher
Asher6mo ago
I'm not a game dev but I'd assume most games use some sort of base class system to define for example enemies, maybe an interface that you need to implement
interface IEnemy
{
void DoAttack(); // this would be implemented for each enemy with their distinct attack
}
interface IEnemy
{
void DoAttack(); // this would be implemented for each enemy with their distinct attack
}
just to give an example if it's a base class you'd also add health maybe speed unless they're all stationary and that would give you alot of opportunity to learn how overriding works, how you pass references of a base class and possibly need to convert them to their original type etc etc as for your code, wouldn't change anything but again, not a game dev so take that with a grain of salt
Tycho
Tycho6mo ago
mhm, thank you
Stigl
Stigl6mo ago
well you can convert it but stiring it for long time could be bad etc. monogame does not