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

package com.hypixel.hytale.server.core.modules.entity.item;

import com.hypixel.hytale.server.core.modules.collision.BlockCollisionData;
import com.hypixel.hytale.math.shape.Box;
import com.hypixel.hytale.server.core.modules.collision.CollisionResult;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.component.RemoveReason;
import java.util.logging.Level;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.core.modules.collision.CollisionModule;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.modules.physics.component.Velocity;
import com.hypixel.hytale.server.core.modules.entity.component.BoundingBox;
import com.hypixel.hytale.component.ComponentType;
import javax.annotation.Nonnull;
import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;

public class ItemPhysicsSystem extends EntityTickingSystem<EntityStore>
{
    @Nonnull
    private static final HytaleLogger LOGGER;
    @Nonnull
    private final ComponentType<EntityStore, ItemPhysicsComponent> itemPhysicsComponentType;
    @Nonnull
    private final ComponentType<EntityStore, BoundingBox> boundingBoxComponentType;
    @Nonnull
    private final ComponentType<EntityStore, Velocity> velocityComponentType;
    @Nonnull
    private final ComponentType<EntityStore, TransformComponent> transformComponentType;
    @Nonnull
    private final Query<EntityStore> query;
    
    public ItemPhysicsSystem(@Nonnull final ComponentType<EntityStore, ItemPhysicsComponent> itemPhysicsComponentType, @Nonnull final ComponentType<EntityStore, Velocity> velocityComponentType, @Nonnull final ComponentType<EntityStore, BoundingBox> boundingBoxComponentType) {
        this.itemPhysicsComponentType = itemPhysicsComponentType;
        this.velocityComponentType = velocityComponentType;
        this.boundingBoxComponentType = boundingBoxComponentType;
        this.transformComponentType = TransformComponent.getComponentType();
        this.query = (Query<EntityStore>)Query.and(itemPhysicsComponentType, boundingBoxComponentType, velocityComponentType, this.transformComponentType);
    }
    
    @Nonnull
    @Override
    public Query<EntityStore> getQuery() {
        return this.query;
    }
    
    @Override
    public boolean isParallel(final int archetypeChunkSize, final int taskCount) {
        return false;
    }
    
    @Override
    public void tick(final float dt, final int index, @Nonnull final ArchetypeChunk<EntityStore> archetypeChunk, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        final World world = store.getExternalData().getWorld();
        final ItemPhysicsComponent itemPhysicsComponent = archetypeChunk.getComponent(index, this.itemPhysicsComponentType);
        assert itemPhysicsComponent != null;
        final Velocity velocityComponent = archetypeChunk.getComponent(index, this.velocityComponentType);
        assert velocityComponent != null;
        final TransformComponent transformComponent = archetypeChunk.getComponent(index, this.transformComponentType);
        assert transformComponent != null;
        final Vector3d position = transformComponent.getPosition();
        final Vector3d scaledVelocity = itemPhysicsComponent.scaledVelocity;
        final CollisionResult collisionResult = itemPhysicsComponent.collisionResult;
        velocityComponent.assignVelocityTo(scaledVelocity).scale(dt);
        final BoundingBox boundingBoxComponent = archetypeChunk.getComponent(index, this.boundingBoxComponentType);
        assert boundingBoxComponent != null;
        final Box boundingBox = boundingBoxComponent.getBoundingBox();
        if (CollisionModule.isBelowMovementThreshold(scaledVelocity)) {
            CollisionModule.findBlockCollisionsShortDistance(world, boundingBox, position, scaledVelocity, collisionResult);
        }
        else {
            CollisionModule.findBlockCollisionsIterative(world, boundingBox, position, scaledVelocity, true, collisionResult);
        }
        final BlockCollisionData blockCollisionData = collisionResult.getFirstBlockCollision();
        if (blockCollisionData != null) {
            if (blockCollisionData.collisionNormal.equals(Vector3d.UP)) {
                velocityComponent.setZero();
                position.assign(blockCollisionData.collisionPoint);
            }
            else {
                final Vector3d velocity = velocityComponent.getVelocity();
                final double dot = velocity.dot(blockCollisionData.collisionNormal);
                final Vector3d velocityToCancel = blockCollisionData.collisionNormal.clone().scale(dot);
                velocity.subtract(velocityToCancel);
            }
        }
        else {
            velocityComponent.assignVelocityTo(scaledVelocity).scale(dt);
            position.add(scaledVelocity);
        }
        collisionResult.reset();
        if (position.getY() < -32.0) {
            ItemPhysicsSystem.LOGGER.at(Level.WARNING).log("Item fell out of the world %s", archetypeChunk.getReferenceTo(index));
            commandBuffer.removeEntity(archetypeChunk.getReferenceTo(index), RemoveReason.REMOVE);
        }
    }
    
    static {
        LOGGER = HytaleLogger.forEnclosingClass();
    }
}
