Aller au contenu

Entity Interactions

Interacciones específicas para entidades.

Interacciones con Entidades

UseEntityInteraction

Usar/interactuar con una entidad.

UseEntityInteraction use = new UseEntityInteraction();

// En contexto
Ref<EntityStore> target = ctx.getMetaObject(Interaction.TARGET_ENTITY);
if (target != null && target.isValid()) {
    Entity entity = commandBuffer.getEntity(target);
    // Interactuar con la entidad
}

Damage Interaction

Aplicar daño a una entidad.

// Definir daño en MetaKey
Damage damage = ctx.getMetaObject(Interaction.DAMAGE);
if (damage != null) {
    entity.applyDamage(damage);
}

Obtener Entidad Objetivo

// Obtener referencia a entidad
Ref<EntityStore> targetRef = ctx.getMetaObject(Interaction.TARGET_ENTITY);

if (targetRef != null && targetRef.isValid()) {
    // Obtener entidad del CommandBuffer
    CommandBuffer<EntityStore> buffer = ctx.getCommandBuffer();
    Entity entity = buffer.getEntity(targetRef);

    // Procesar entidad
    if (entity instanceof LivingEntity) {
        LivingEntity living = (LivingEntity) entity;
        // ...
    }
}

Datos de Impacto

// Ubicación del impacto
Vector4d hitLocation = ctx.getMetaObject(Interaction.HIT_LOCATION);
if (hitLocation != null) {
    double x = hitLocation.x;
    double y = hitLocation.y;
    double z = hitLocation.z;
    // w contiene información adicional
}

// Detalle del impacto (parte del modelo)
String hitDetail = ctx.getMetaObject(Interaction.HIT_DETAIL);
if (hitDetail != null) {
    System.out.println("Impactó en: " + hitDetail);
}

Ejemplo Completo

public class AttackEntityInteraction extends SimpleInstantInteraction {
    private float damageAmount = 10.0f;

    @Override
    protected void firstRun(InteractionType type,
                           InteractionContext ctx,
                           CooldownHandler cooldown) {
        // Obtener atacante
        Ref<EntityStore> attackerRef = ctx.getEntity();

        // Obtener objetivo
        Ref<EntityStore> targetRef =
            ctx.getMetaObject(Interaction.TARGET_ENTITY);

        if (targetRef == null || !targetRef.isValid()) {
            ctx.getState().state = InteractionState.Failed;
            return;
        }

        // Aplicar daño
        CommandBuffer<EntityStore> buffer = ctx.getCommandBuffer();
        LivingEntity target = (LivingEntity) buffer.getEntity(targetRef);

        Damage damage = new Damage();
        damage.amount = damageAmount;
        damage.source = attackerRef;

        target.applyDamage(damage);

        // Completar
        ctx.getState().state = InteractionState.Finished;
    }
}

Ver También