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

package com.hypixel.hytale.server.npc.systems;

import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.dependency.Dependency;
import java.util.Set;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.modules.physics.component.Velocity;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ComponentType;

public class ComputeVelocitySystem extends SteppableTickingSystem
{
    @Nonnull
    private final ComponentType<EntityStore, NPCEntity> npcEntityComponentType;
    @Nonnull
    private final ComponentType<EntityStore, Velocity> velocityComponentType;
    @Nonnull
    private final ComponentType<EntityStore, TransformComponent> transformComponentType;
    @Nonnull
    private final Set<Dependency<EntityStore>> dependencies;
    @Nonnull
    private final Query<EntityStore> query;
    
    public ComputeVelocitySystem(@Nonnull final ComponentType<EntityStore, NPCEntity> npcEntityComponentType, @Nonnull final ComponentType<EntityStore, Velocity> velocityComponentType, @Nonnull final Set<Dependency<EntityStore>> dependencies) {
        this.transformComponentType = TransformComponent.getComponentType();
        this.npcEntityComponentType = npcEntityComponentType;
        this.velocityComponentType = velocityComponentType;
        this.dependencies = dependencies;
        this.query = (Query<EntityStore>)Query.and(npcEntityComponentType, this.transformComponentType, velocityComponentType);
    }
    
    @Nonnull
    @Override
    public Set<Dependency<EntityStore>> getDependencies() {
        return this.dependencies;
    }
    
    @Override
    public boolean isParallel(final int archetypeChunkSize, final int taskCount) {
        return EntityTickingSystem.maybeUseParallel(archetypeChunkSize, taskCount);
    }
    
    @Override
    public void steppedTick(final float dt, final int index, @Nonnull final ArchetypeChunk<EntityStore> archetypeChunk, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        final NPCEntity npcComponent = archetypeChunk.getComponent(index, this.npcEntityComponentType);
        assert npcComponent != null;
        final TransformComponent transformComponent = archetypeChunk.getComponent(index, this.transformComponentType);
        assert transformComponent != null;
        final Velocity velocityComponent = archetypeChunk.getComponent(index, this.velocityComponentType);
        assert velocityComponent != null;
        final Vector3d position = transformComponent.getPosition();
        final Vector3d oldPosition = npcComponent.getOldPosition();
        final double x = (position.getX() - oldPosition.getX()) / dt;
        final double y = (position.getY() - oldPosition.getY()) / dt;
        final double z = (position.getZ() - oldPosition.getZ()) / dt;
        velocityComponent.set(x, y, z);
    }
    
    @Nonnull
    @Override
    public Query<EntityStore> getQuery() {
        return this.query;
    }
}
