Skip to main content
Cleanup
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Unity: Decoupling Components

I am working on my first Tower Defense game and I got stuck with architecture. I found some past Q&A this post"Should the entity handle his own movement?" and, read about Entity Component approach, and decided to stick with it.

What I want to achieve. I want to have different scripts that are responsible for different behaviors. For Example If unit contains IEnemy script than it will follow predifined path of the level. If it has IDamagable script it can receive damage, If it has Health script than the damage will affect health. So inexample:

  • if a unit contains an IEnemy script then it will follow predefined path through the level.

  • If it has an IDamageable script it can receive damage.

  • If it has a Health script then the damage will affect health.

In order to create friendly unit all I have to do is throw away IEnemythe IEnemy script and add IFriendlyUnitthe IFriendlyUnit script. 

In order to do that, I decided to have some storage and a manager of that storage. ManagerThe manager will have methods like heal(healthPoints)Heal(healthPoints), removeArmorPoints(armorPoints)RemoveArmorPoints(armorPoints),addArmorPoints(armorPoints) AddArmorPoints(armorPoints), DecreaseHealthBy(damage)DecreaseHealthBy(damage).

But the problem is that If I want to have Damagablea Damageable script which has only one method ReceiveDamage() thanReceiveDamage() then it needs to have a reference to the StateManagerStateManager in order to inflict damage and Itit breaks the design because Itit forces me to add the StateManagerStateManager script to the GameObjectGameObject in order to remove some healthpointshealth points and I cannot easily add independend Damagablean independent Damageable script to the object.

I want to decouple my code as much as possible and stick to SOLID principles, but seems like I am either over engineering or choosing the wrong way.

Will be very happy to read your recommendations. Thank you in advance.

Unity: Decoupling Components

I am working on my first Tower Defense game and I got stuck with architecture. I found this post and read about Entity Component approach and decided to stick with it.

What I want to achieve. I want to have different scripts that are responsible for different behaviors. For Example If unit contains IEnemy script than it will follow predifined path of the level. If it has IDamagable script it can receive damage, If it has Health script than the damage will affect health. So in order to create friendly unit all I have to do is throw away IEnemy script and add IFriendlyUnit script. In order to do that I decided to have some storage and a manager of that storage. Manager will have methods like heal(healthPoints), removeArmorPoints(armorPoints),addArmorPoints(armorPoints), DecreaseHealthBy(damage).

But the problem is that If I want to have Damagable script which has only one method ReceiveDamage() than it needs to have a reference to the StateManager in order to inflict damage and It breaks the design because It forces me to add the StateManager script to the GameObject in order to remove some healthpoints and I cannot easily add independend Damagable script to the object.

I want to decouple my code as much as possible and stick to SOLID principles but seems like I am either over engineering or choosing the wrong way.

Will be very happy to read your recommendations. Thank you in advance.

Decoupling Components

I am working on my first Tower Defense game and I got stuck with architecture. I found some past Q&A "Should the entity handle his own movement?", read about Entity Component approach, and decided to stick with it.

I want to have different scripts that are responsible for different behaviors. For example:

  • if a unit contains an IEnemy script then it will follow predefined path through the level.

  • If it has an IDamageable script it can receive damage.

  • If it has a Health script then the damage will affect health.

In order to create friendly unit all I have to do is throw away the IEnemy script and add the IFriendlyUnit script. 

In order to do that, I decided to have some storage and a manager of that storage. The manager will have methods like Heal(healthPoints), RemoveArmorPoints(armorPoints), AddArmorPoints(armorPoints), DecreaseHealthBy(damage).

But the problem is that If I want to have a Damageable script which has only one method ReceiveDamage() then it needs to have a reference to the StateManager in order to inflict damage and it breaks the design because it forces me to add the StateManager script to the GameObject in order to remove some health points and I cannot easily add an independent Damageable script to the object.

I want to decouple my code as much as possible and stick to SOLID principles, but seems like I am either over engineering or choosing the wrong way.

Source Link

Unity: Decoupling Components

I am working on my first Tower Defense game and I got stuck with architecture. I found this post and read about Entity Component approach and decided to stick with it.

What I want to achieve. I want to have different scripts that are responsible for different behaviors. For Example If unit contains IEnemy script than it will follow predifined path of the level. If it has IDamagable script it can receive damage, If it has Health script than the damage will affect health. So in order to create friendly unit all I have to do is throw away IEnemy script and add IFriendlyUnit script. In order to do that I decided to have some storage and a manager of that storage. Manager will have methods like heal(healthPoints), removeArmorPoints(armorPoints),addArmorPoints(armorPoints), DecreaseHealthBy(damage).

But the problem is that If I want to have Damagable script which has only one method ReceiveDamage() than it needs to have a reference to the StateManager in order to inflict damage and It breaks the design because It forces me to add the StateManager script to the GameObject in order to remove some healthpoints and I cannot easily add independend Damagable script to the object.

I want to decouple my code as much as possible and stick to SOLID principles but seems like I am either over engineering or choosing the wrong way.

Will be very happy to read your recommendations. Thank you in advance.