Hello i make my 2d game with unity and i feel confuse about oop design. There is a 4 class in my game.
StageView : The view(scene) where the game logic run.
ObjectPool : The object pool that can manage the gameobjects, and it is the member field of stage view. (It is not a singleton)
Projectile : The projectile class that can attack the monster.
Monster : The monster class that could be attacked by projectile.
public class Monster : MonoBehaviour
{
private int hp;
public delegate void OnMobHitHandler(Projectile projectile, Monster monster, long damage);
public delegate void OnMobDieHandler(Monster monster);
public OnMobHitHandler OnMobHit;
public OnMobDieHandler OnMobDie;
public void OnTriggerEnter2D(Collider2D collision)
{
Projectile projectile = collision.gameObject.GetComponent<Projectile>();
hp -= projectile.Power;
OnMobHit?.Invoke(projectile, this, projectile.Power);
if(hp <= 0)
OnMobDie?.Invoke(this);
}
}
If the monster got hit, then the fragment object will be created. And if the monster die, then it must be released. But the fragment objects are managed by ObjectPool which is in StageView. (Also monster are managed by ObjectPool samely) So there are 2 choices in my head.
Make Init(Objectpool objectpool) Method and pass the objectpool to monster as a parameter. Then put "create fragment logic" and "Release(die) monster logic" in to the Monster class.
Expose the monster's callback event to the StageView like above example code. Their 2 logics must be in StageView's OnMonsterHit, OnMonsterDie.
I choiced second, because i want to loosen the dependencies of Monster class. (You know, if i don't loosen the dependencies then it's gonna be more complicate later) But sometimes, I think that it may seem more appropriate for the object to handle it internally rather than entrusting it to the outside. I'm very confused which one is more correct in OOP aspect.