// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.npc.corecomponents.world;

import com.hypixel.hytale.server.npc.sensorinfo.InfoProvider;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.npc.role.Role;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.npc.corecomponents.builders.BuilderSensorBase;
import com.hypixel.hytale.server.npc.asset.builder.BuilderSupport;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.corecomponents.world.builders.BuilderSensorLeash;
import com.hypixel.hytale.server.npc.sensorinfo.PositionProvider;
import com.hypixel.hytale.server.npc.corecomponents.SensorBase;

public class SensorLeash extends SensorBase
{
    protected final double range;
    protected final double rangeSq;
    protected final PositionProvider positionProvider;
    
    public SensorLeash(@Nonnull final BuilderSensorLeash builderSensorLeash, @Nonnull final BuilderSupport builderSupport) {
        super(builderSensorLeash);
        this.positionProvider = new PositionProvider();
        this.range = builderSensorLeash.getRange(builderSupport);
        this.rangeSq = this.range * this.range;
    }
    
    @Override
    public boolean matches(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, final double dt, @Nonnull final Store<EntityStore> store) {
        if (!super.matches(ref, role, dt, store)) {
            return false;
        }
        final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
        assert transformComponent != null;
        final NPCEntity npcComponent = store.getComponent(ref, NPCEntity.getComponentType());
        final Vector3d leashPoint = npcComponent.getLeashPoint();
        if (transformComponent.getPosition().distanceSquaredTo(leashPoint) > this.rangeSq) {
            this.positionProvider.setTarget(leashPoint);
            return true;
        }
        this.positionProvider.clear();
        return false;
    }
    
    @Override
    public InfoProvider getSensorInfo() {
        return this.positionProvider;
    }
}
