-
Notifications
You must be signed in to change notification settings - Fork 4
TNTDamageComponent
The TNTDamageComponent
is a specialized game component designed to facilitate damage application and knock-back effects within a particular radius of an entity in a game world. It operates within the LibGDX framework and is specifically geared for games that require area-of-effect damage, often referred to as "splash" damage. This component uses underlying components such as CombatStatsComponent
and HitboxComponent
to execute its functionalities.
To use the TNTDamageComponent
, you must first add the component to a game entity. Here's a simple example of how to attach it:
TNTDamageComponent tntDamage = new TNTDamageComponent(targetLayer, knockbackForce, radius);
entity.addComponent(tntDamage);
Entities that wish to use TNTDamageComponent
must also have a CombatStatsComponent
to draw the base damage from. This is critical for ensuring that the damage dealt to target entities is consistent and can be easily managed.
CombatStatsComponent combatStats = new CombatStatsComponent(health, baseAttack);
entity.addComponent(combatStats);
One primary application is for projectile entities. For instance, in a game where a tower throws an area-effect bomb, the bomb projectile would carry a TNTDamageComponent
. When the projectile reaches its target location, it triggers, applying damage and possibly knock-back to all entities within a certain radius.
Another entity that might use this is the specialized "TNT Tower" in a tower defense game. Unlike a standard defense tower that shoots projectiles, a TNT Tower could use this component to apply area-of-effect damage when it detects an enemy in its range.
The target entities must also have a HitboxComponent
for the damage and knock-back effects to apply, as the TNTDamageComponent
uses collision detection to identify which entities are within its effect radius. Without a HitboxComponent
, the target entity won't be considered in the collision event, and thus no damage or effects will be applied to it.
HitbocComponent hitbox = new HitboxComponent();
target.addComponent(hitbox);
The component offers a high degree of customization:
-
targetLayer
: Specify which layer of entities are valid targets (e.g. PhysicsLayer.NPC). -
knockbackForce
: Set the magnitude of the knock-back effect. -
radius
: Define the radius within which the damage and knock-back effect will apply.
-
applyTNTDamage
method scans nearby entities and checks whether they should be affected by the damage or knock-back. It fetches all nearby entities within a specific radius, excluding itself, and calls another method,applyDamage
, for each eligible target. -
applyDamage
method handles the actual application of damage and knock-back based on the properties of the source and target entities. It performs several checks to make sure the affected entity fits within the definedtargetLayer
and that all the required components are available. If the target entity has aCombatStatsComponent
, it applies the damage. If it has aPhysicsComponent
andknockbackForce
is set, it applies the knock-back effect.
- The component listens for a specific event ("TNTDamageStart").
- When triggered, it identifies all entities in the vicinity based on the specified radius.
- For each such entity, it applies damage and potentially a knock-back effect based on the entity's characteristics and the properties set in the
TNTDamageComponent
.
The code is designed to be plugged into an entity-component-system architecture, making it reusable and maintainable.
- Ensure that all entities that are supposed to interact with
TNTDamageComponent
have aHitboxComponent
. - While attaching this component to an entity, make sure to also attach a
CombatStatsComponent
to the same entity. - Clearly define the
targetLayer
to ensure that the component interacts only with the intended set of entities.
TNTDamageComponent had been tested according to the TNTDamageComponent Test Plan