samspade
Member
Let's say you're making a basic 2D metrovania style game and you have the following things which damage the player:
How would you structure the objects, inheritance and code wise? What would you consider objectively good or bad structure?
The one thing all the above share is the ability to damage the player through contact. I see a couple unshared elements - movement (state change) and hp (ability to die) - which can also be generalized as requiring an update.
I see a couple primary options for division.
Option 1 - Inheritance
Structure is done through inheritance. You would have a primary parent contain the code for damaging, immediate children for those that don't need hp, a sub parent for enemies with hp with children to contain their own movement. Movement code could be scripts if they were identical.
Example (tabs mean children):
enemy_parent (contains the damage player code)
Option 2 - Components
Structure is done through components. Where the damage masks and hp are separate objects created and connected to any object in the create event. Here you wouldn't need inheritance at all for hp or damage freeing up inheritance for use in other ways (e.g. similar movement code, or more complicated state changes with enemies, etc.).
Example:
damage mask (contains the damage player code)
hp obj (contains code to track hp and destroy connected obj)
Spikes (gets a damage mask attached)
Pillars of Fire (gets a damage mask attached)
Arrows (gets a damage mask attached)
Falling Spikes (gets a damage mask attached)
Stationary Enemy (gets a damage mask and hp obj attached)
Walking Enemy (gets a damage mask and hp obj attached)
Flying Enemy (gets a damage mask and hp obj attached)
Jumping Enemy (gets a damage mask and hp obj attached)
Preferences on the above or other choices entirely?
- Spikes (doesn't move, can't be killed)
- Pillars of Fire (move up then down, can't be killed)
- Arrows (move left to right, can't be killed)
- Falling Spikes (move down, can't be killed)
- Stationary Enemy (doesn't move, can be killed)
- Walking Enemy (moves back and forth, can be killed)
- Flying Enemy (flies back and forth, can be killed)
- Jumping Enemy (moves up then down, can be killed)
How would you structure the objects, inheritance and code wise? What would you consider objectively good or bad structure?
The one thing all the above share is the ability to damage the player through contact. I see a couple unshared elements - movement (state change) and hp (ability to die) - which can also be generalized as requiring an update.
I see a couple primary options for division.
Option 1 - Inheritance
Structure is done through inheritance. You would have a primary parent contain the code for damaging, immediate children for those that don't need hp, a sub parent for enemies with hp with children to contain their own movement. Movement code could be scripts if they were identical.
Example (tabs mean children):
enemy_parent (contains the damage player code)
Spikes
Pillars of Fire (movement code)
Arrows (movement code)
Falling Spikes (movement code)
enemy_with_hp_parent (contains hp code)
Pillars of Fire (movement code)
Arrows (movement code)
Falling Spikes (movement code)
enemy_with_hp_parent (contains hp code)
Stationary Enemy (movement code)
Walking Enemy (movement code)
Flying Enemy (movement code)
Jumping Enemy (movement code)
Walking Enemy (movement code)
Flying Enemy (movement code)
Jumping Enemy (movement code)
Option 2 - Components
Structure is done through components. Where the damage masks and hp are separate objects created and connected to any object in the create event. Here you wouldn't need inheritance at all for hp or damage freeing up inheritance for use in other ways (e.g. similar movement code, or more complicated state changes with enemies, etc.).
Example:
damage mask (contains the damage player code)
hp obj (contains code to track hp and destroy connected obj)
Spikes (gets a damage mask attached)
Pillars of Fire (gets a damage mask attached)
Arrows (gets a damage mask attached)
Falling Spikes (gets a damage mask attached)
Stationary Enemy (gets a damage mask and hp obj attached)
Walking Enemy (gets a damage mask and hp obj attached)
Flying Enemy (gets a damage mask and hp obj attached)
Jumping Enemy (gets a damage mask and hp obj attached)
Preferences on the above or other choices entirely?