Which approach to design components is better?
Let’s take for example some explodable object:
class Explode {
public GameObject explosionPrefab;
public bool explodeOnTimer;
public float timeToExplode;
public bool explodeOnCollision;
public bool explodeOnDamage;
public float minDamageToExplode;
public bool explodeOnDie;
//...
public void Start() {}
public void OnCollisionEnter(...) {}
public void OnTakeDamage(...) {}
public void OnDie(...) {}
//...
}
… or maybe…
class TimerExplode {
public float timeToExplode;
public void Start() {}
}
class CollisionExplode {
public void OnCollisionEnter(...) {}
}
class DamageExplode {
public float minDamageToExplode;
public void OnTakeDamage(...) {}
}
class DieExplode {
public void OnDie(...) {}
}
In other words, should I design small, more specialized components or bigger and more generic?
I know the first example is better in scenarios where I want some objects to explode on many different conditions - it’s only one component and few checkboxes.
But with the second example I can just add new condition/new event (for ex. ActivatorExplode to explode on activation by other object) as new component and don’t worry that new code could somehow affect or break behaviour of already created objects/prefabs. Ofcourse now there are many many small components and objects are getting messy.
Or maybe I don’t need all this conditions. Maybe I should add to my explodable object just Health component, which sends OnDie() message when health<=0, and Explode component which reacts to only this one message spawning explosion object. Health component could have all this conditions to decrease health value. Then to make timed bomb I’d just add TimedDamage component that will send OnTakeDamage() after few seconds.
But then, what if I want some object to explode only when its taken damage for example by flames? You can shoot it, you can break it and it dies but it only explode when you heat it with fire?
This questions aren’t only for this example, I’m asking about some general tips for: what level of functionality should I split into components? Should I make NPC component or rather NPC_Attack, NPC_Defense, NPC_Civilian, NPC_Soldier, NPC_PathFollower, …
… should I make a generic Grenade script or maybe TimedExplode, StickToWall, CollisionExplode, RadiusDamage, … to be able to create many different grenade and maybe bomb types? How big or how small this LEGO parts should be?
P.S.
Sorry for my bad english, still learning ![]()