Skip to content

TNTDamageComponent

Mohamad Dabboussi edited this page Sep 10, 2023 · 7 revisions

TNTDamageComponent Documentation

Overview

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.

How To Use It

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);

Required Components

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);

Typical Use-Cases

Projectiles

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.

TNT Tower

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.

Target Requirements

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);

Customization

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.

Functionality

Applying Damage and Knock-back

  • 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 defined targetLayer and that all the required components are available. If the target entity has a CombatStatsComponent, it applies the damage. If it has a PhysicsComponent and knockbackForce is set, it applies the knock-back effect.

Overall Flow

  1. The component listens for a specific event ("TNTDamageStart").
  2. When triggered, it identifies all entities in the vicinity based on the specified radius.
  3. 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.

Best Practices

  • Ensure that all entities that are supposed to interact with TNTDamageComponent have a HitboxComponent.
  • 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.

Test Plan

TNTDamageComponent had been tested according to the TNTDamageComponent Test Plan

Clone this wiki locally