Skip to content

Damage Texture Component

Isaac Robinson edited this page Aug 26, 2023 · 1 revision

The DamageTextureComponent is a custom component used to add varying textures to an entity - which will vary based on their health (from CombatStatsComponent). It is required that the entity also implements a CombatStatsComponent for this to work. When created, a single texture is added - this is the base texture and has a threshold of MAX_INT. For all additional textures the health threshold at which they display must be passed in the function.

Usage

1. Instantiate DamageTextureComponent

The DamageTextureComponent is created by first passing the path of the base texture - this path must be loaded by the ServiceLocator.getResourceService(). Alternatively, a Texture object can be passed directly to the constructor.

DamageTextureComponent dtc = new DamageTextureComponent("[BASE TEXTURE PATH].[EXT]");

or

Texture baseTexture = new Texture(...);
DamageTextureComponent dtc = new DamageTextureComponent(baseTexture);

2. Add additional Textures

Additional textures can be added to the component by calling addTexture(). An integer threshold must be passed and either a texture path or texture object - exactly the same as the constructor. The integer threshold will determine when the texture will be shown, i.e. if the threshold is 30 then the texture specified will be shown whenever the entity has health <= 30. Note that when multiple textures have been added then the texture will the lowest threshold will be shown. i.e. if there are two additional thresholds of 30 and 20 and the health is 15, then the texture with the threshold of 20 will be displayed.

dtc.addTexture(THRESHOLD, [TEXTURE PATH OR TEXTURE OBJECT]);

3. Easier Instantiation

The addTexture() method has been implemented using the builder design pattern and thus can be chained together with the constructor or multiple addTexture() calls. See the example below for an alternate way to initialise and add multiple textures to the DamageTextureComponent.

DamageTextureComponent dtc = new DamageTextureComponent("baseTexture.png")
                                  .addTexture(50, "HalfHealthTexture.png")
                                  .addTexture(0, "BrokenTexture.png");

4. Add to entity

This component then just needs to be added to any entity, as you would any other component.

Entity newEntity = new Entity().addComponent(dtc);

5. Integration Notes

This component has unspecified functionality in the following situations and thus should be avoided:

  • Another component is specifiying the texture or animation of the entity
  • Multiple calls to addTexture() are made with the same threshold - no guarantee of what texture will show
Clone this wiki locally