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

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

import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.core.asset.type.model.config.Model;
import com.hypixel.hytale.server.npc.sensorinfo.IPositionProvider;
import com.hypixel.hytale.server.core.modules.physics.util.PhysicsMath;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.npc.movement.Steering;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.npc.sensorinfo.InfoProvider;
import com.hypixel.hytale.server.npc.role.Role;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.npc.corecomponents.builders.BuilderHeadMotionBase;
import com.hypixel.hytale.server.npc.asset.builder.BuilderSupport;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.corecomponents.entity.builders.BuilderHeadMotionWatch;
import com.hypixel.hytale.server.core.modules.entity.component.ModelComponent;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.npc.corecomponents.HeadMotionBase;

public class HeadMotionWatch extends HeadMotionBase
{
    protected static final ComponentType<EntityStore, TransformComponent> TRANSFORM_COMPONENT_TYPE;
    protected static final ComponentType<EntityStore, ModelComponent> MODEL_COMPONENT_TYPE;
    protected final double relativeTurnSpeed;
    
    public HeadMotionWatch(@Nonnull final BuilderHeadMotionWatch builderHeadMotionWatch, @Nonnull final BuilderSupport support) {
        super(builderHeadMotionWatch);
        this.relativeTurnSpeed = builderHeadMotionWatch.getRelativeTurnSpeed(support);
    }
    
    @Override
    public boolean computeSteering(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, @Nullable final InfoProvider sensorInfo, final double dt, @Nonnull final Steering desiredSteering, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        if (sensorInfo == null || !sensorInfo.hasPosition()) {
            desiredSteering.clear();
            return true;
        }
        final IPositionProvider positionProvider = sensorInfo.getPositionProvider();
        final Ref<EntityStore> targetRef = positionProvider.getTarget();
        double x = positionProvider.getX();
        double y = positionProvider.getY();
        double z = positionProvider.getZ();
        if (targetRef != null) {
            final ModelComponent targetModelComponent = componentAccessor.getComponent(targetRef, HeadMotionWatch.MODEL_COMPONENT_TYPE);
            y += ((targetModelComponent != null) ? targetModelComponent.getModel().getEyeHeight() : 0.0);
        }
        final TransformComponent transformComponent = componentAccessor.getComponent(ref, HeadMotionWatch.TRANSFORM_COMPONENT_TYPE);
        assert transformComponent != null;
        final ModelComponent modelComponent = componentAccessor.getComponent(ref, HeadMotionWatch.MODEL_COMPONENT_TYPE);
        assert modelComponent != null;
        final Model model = modelComponent.getModel();
        final Vector3d position = transformComponent.getPosition();
        x -= position.getX();
        y -= position.getY() + model.getEyeHeight();
        z -= position.getZ();
        final float yaw = PhysicsMath.normalizeTurnAngle(PhysicsMath.headingFromDirection(x, z));
        final float pitch = PhysicsMath.pitchFromDirection(x, y, z);
        desiredSteering.clearTranslation();
        desiredSteering.setYaw(yaw);
        desiredSteering.setPitch(pitch);
        desiredSteering.setRelativeTurnSpeed(this.relativeTurnSpeed);
        return true;
    }
    
    static {
        TRANSFORM_COMPONENT_TYPE = TransformComponent.getComponentType();
        MODEL_COMPONENT_TYPE = ModelComponent.getComponentType();
    }
}
