package org.treytomes.dwarfCraft.block.deployer;

import org.treytomes.dwarfCraft.block.BlockRegistry;

import net.minecraft.dispenser.IBlockSource;
import net.minecraft.dispenser.IPosition;
import net.minecraft.entity.Entity;
import net.minecraft.entity.IProjectile;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;

public abstract class BehaviorProjectileDeploy extends BehaviorDefaultDeployItem {
	
    /**
     * Dispense the specified stack, play the dispense sound and spawn particles.
     */
    public ItemStack deployStack(IBlockSource block, ItemStack stack) {
        World world = block.getWorld();
        IPosition position = BlockRegistry.getPositionFromBlock(block);
        EnumFacing facing = BlockRegistry.getFacing(block.getBlockMetadata());
        IProjectile projectile = getProjectileEntity(world, position);
        projectile.setThrowableHeading(
        		(double)facing.getFrontOffsetX(), (double)((float)facing.getFrontOffsetY() + 0.1f), (double)facing.getFrontOffsetZ(),
        		getThrowableHeadingMultiplier1(), getThrowableHeadingMultiplier2());
        world.spawnEntityInWorld((Entity)projectile);
        stack.splitStack(1);
        return stack;
    }

    /**
     * Play the dispense sound from the specified block.
     */
    protected void playDeploySound(IBlockSource block) {
        block.getWorld().playAuxSFX(1002, block.getXInt(), block.getYInt(), block.getZInt(), 0);
    }

    /**
     * Return the projectile entity spawned by this dispense behavior.
     */
    protected abstract IProjectile getProjectileEntity(World world, IPosition position);

    protected float getThrowableHeadingMultiplier2() {
        return 6.0f;
    }

    protected float getThrowableHeadingMultiplier1() {
        return 1.1f;
    }
}
