Skip to content

Commit

Permalink
Add missing Effect docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
nightm4re94 committed Oct 5, 2024
1 parent bfc0d9b commit fbe9a99
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,79 @@
import de.gurkenlabs.litiengine.entities.ICombatEntity;

/**
* An attribute effect applies an attribute modifier to the affected entity when applied and removes it when ceased.
* Represents an effect that modifies an attribute of a combat entity.
*
* @param <T>
* the generic type
* @param <T> the type of the attribute value
*/
public abstract class AttributeEffect<T extends Number> extends Effect {

/** The modifier. */
private final AttributeModifier<T> modifier;

/**
* Constructs an AttributeEffect with the specified targeting strategy, modification, and delta.
*
* @param targetingStrategy the strategy to determine the targets of the effect
* @param modification the type of modification to apply to the attribute
* @param delta the value to modify the attribute by
*/
protected AttributeEffect(final TargetingStrategy targetingStrategy,
final Modification modification,
final double delta) {
final Modification modification,
final double delta) {
this(targetingStrategy, null, modification, delta);
}

/**
* Constructs an AttributeEffect with the specified targeting strategy, executing entity, modification, and delta.
*
* @param targetingStrategy the strategy to determine the targets of the effect
* @param executingEntity the entity executing the effect
* @param modification the type of modification to apply to the attribute
* @param delta the value to modify the attribute by
*/
protected AttributeEffect(final TargetingStrategy targetingStrategy,
final ICombatEntity executingEntity,
final Modification modification,
final double delta) {
final ICombatEntity executingEntity,
final Modification modification,
final double delta) {
super(targetingStrategy, executingEntity);
this.modifier = new AttributeModifier<>(modification, delta);
}

/**
* Ceases the effect on the specified entity, removing the attribute modifier.
*
* @param affectedEntity the entity affected by the effect
*/
@Override
public void cease(final ICombatEntity affectedEntity) {
super.cease(affectedEntity);
this.getAttribute(affectedEntity).removeModifier(this.getModifier());
}

/**
* Gets the attribute modifier associated with this effect.
*
* @return the attribute modifier
*/
public AttributeModifier<T> getModifier() {
return this.modifier;
}

/**
* Applies the effect to the specified entity, adding the attribute modifier.
*
* @param affectedEntity the entity affected by the effect
*/
@Override
protected void apply(final ICombatEntity affectedEntity) {
super.apply(affectedEntity);
this.getAttribute(affectedEntity).addModifier(this.getModifier());
}

/**
* Gets the attribute to be modified for the specified entity.
*
* @param entity the entity whose attribute is to be modified
* @return the attribute to be modified
*/
protected abstract Attribute<T> getAttribute(final ICombatEntity entity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import de.gurkenlabs.litiengine.IUpdateable;
import de.gurkenlabs.litiengine.abilities.targeting.TargetingStrategy;
import de.gurkenlabs.litiengine.entities.ICombatEntity;

import java.awt.Shape;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -15,12 +14,11 @@
import java.util.concurrent.CopyOnWriteArrayList;

/**
* The `Effect` class represents an abstract base class for applying effects to combat entities.
* Effects are applied based on a `TargetingStrategy` and may affect multiple entities within
* a defined impact area. Effects are applied for a set duration and may have follow-up effects.
* The `Effect` class represents an abstract base class for applying effects to combat entities. Effects are applied based on a `TargetingStrategy`
* and may affect multiple entities within a defined impact area. Effects are applied for a set duration and may have follow-up effects.
* <p>
* This class handles effect application, event listeners for when effects are applied or ceased,
* and the management of active appliances (ongoing effect instances).
* This class handles effect application, event listeners for when effects are applied or ceased, and the management of active appliances (ongoing
* effect instances).
*/
public abstract class Effect implements IUpdateable {

Expand Down Expand Up @@ -194,24 +192,33 @@ public boolean isActive(final ICombatEntity entity) {
.anyMatch(a -> a.getAffectedEntities().stream().anyMatch(e -> e.equals(entity)));
}

/**
* Sets the delay before this effect is applied.
*
* @param delay the delay in ticks
*/
public void setDelay(final int delay) {
this.delay = delay;
}

/**
* Sets the duration this effect lasts once applied.
*
* @param duration the effect duration in ticks
*/
public void setDuration(final int duration) {
this.duration = duration;
}

/**
* Updates the effect, checking for appliances that have reached their duration and applying
* follow-up effects if needed. Removes appliances that have ended and detaches the effect
* from the game loop if no active appliances remain.
* Updates the effect, checking for appliances that have reached their duration and applying follow-up effects if needed. Removes appliances that
* have ended and detaches the effect from the game loop if no active appliances remain.
*/
@Override
public void update() {

for (final Iterator<EffectApplication> iterator = this.getActiveAppliances().iterator();
iterator.hasNext(); ) {
iterator.hasNext(); ) {
final EffectApplication appliance = iterator.next();
// if the effect duration is reached
if (this.hasEnded(appliance)) {
Expand All @@ -227,6 +234,12 @@ public void update() {
}
}

/**
* Applies the effect to the specified entity, adding this effect to the entity's list of applied effects and notifying all registered listeners
* that the effect has been applied.
*
* @param entity the entity to which the effect is applied
*/
protected void apply(final ICombatEntity entity) {
entity.getAppliedEffects().add(this);
final EffectEvent event = new EffectEvent(this, entity);
Expand Down Expand Up @@ -268,15 +281,31 @@ protected boolean hasEnded(final EffectApplication appliance) {
return effectDuration > this.getDuration();
}

/**
* Listener interface for receiving notifications when an effect is applied.
*/
@FunctionalInterface
public interface EffectAppliedListener extends EventListener {

/**
* Invoked when an effect is applied.
*
* @param event the event containing information about the applied effect
*/
void applied(EffectEvent event);
}

/**
* Listener interface for receiving notifications when an effect ceases.
*/
@FunctionalInterface
public interface EffectCeasedListener extends EventListener {

/**
* Invoked when an effect ceases.
*
* @param event the event containing information about the ceased effect
*/
void ceased(EffectEvent event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,49 @@
import java.awt.Shape;
import java.util.Collection;

/**
* Represents the application of an effect, including the entities affected and the area of impact.
*/
public class EffectApplication {
private final Collection<ICombatEntity> affectedEntities;
private final long applied;
private final Shape impactArea;

/**
* Constructs an EffectApplication with the specified affected entities and impact area.
*
* @param affectedEntities the entities affected by the effect
* @param impactArea the area where the effect is applied
*/
protected EffectApplication(final Collection<ICombatEntity> affectedEntities, final Shape impactArea) {
this.applied = Game.time().now();
this.affectedEntities = affectedEntities;
this.impactArea = impactArea;
}

/**
* Gets the entities affected by the effect.
*
* @return the collection of affected entities
*/
public Collection<ICombatEntity> getAffectedEntities() {
return this.affectedEntities;
}

/**
* Gets the time in ticks when the effect was applied.
*
* @return the applied time in ticks
*/
public long getAppliedTicks() {
return this.applied;
}

/**
* Gets the shape of the area where the effect is applied.
*
* @return the impact area
*/
public Shape getImpactArea() {
return this.impactArea;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import de.gurkenlabs.litiengine.entities.ICombatEntity;
import de.gurkenlabs.litiengine.resources.Resources;
import de.gurkenlabs.litiengine.sound.Sound;

import java.util.Arrays;

public class SoundEffect extends Effect {
Expand All @@ -22,18 +21,39 @@ public SoundEffect(ICombatEntity executingEntity, final Sound... sounds) {
this.sounds = sounds;
}

/**
* Initializes a new instance of the {@code SoundEffect} class with the specified sounds.
*
* @param sounds The sounds to choose from when applying the effect.
*/
public SoundEffect(final Sound... sounds) {
this(null, sounds);
}

/**
* Initializes a new instance of the {@code SoundEffect} class with the specified executing entity and sounds.
*
* @param executingEntity The entity executing the effect.
* @param sounds The names of the sounds to choose from when applying the effect.
*/
public SoundEffect(ICombatEntity executingEntity, final String... sounds) {
this(executingEntity, Arrays.stream(sounds).map(x -> Resources.sounds().get(x)).toArray(Sound[]::new));
}

/**
* Initializes a new instance of the {@code SoundEffect} class with the specified sounds.
*
* @param sounds The names of the sounds to choose from when applying the effect.
*/
public SoundEffect(final String... sounds) {
this(null, sounds);
}

/**
* Applies the sound effect to the specified entity, playing a random sound from the list of sounds.
*
* @param entity The entity to which the effect is applied.
*/
@Override
protected void apply(final ICombatEntity entity) {
super.apply(entity);
Expand Down

0 comments on commit fbe9a99

Please sign in to comment.