Skip to content

Commit

Permalink
chore: add custom shooting sound support to ConfiguredProjectiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Elenterius committed Jan 2, 2024
1 parent 1cb2b2b commit d317313
Showing 1 changed file with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.elenterius.biomancy.item.weapon.Gun;
import com.github.elenterius.biomancy.util.function.FloatOperator;
import com.github.elenterius.biomancy.util.function.IntOperator;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.LivingEntity;
Expand All @@ -19,21 +20,27 @@ public final class ModProjectiles {
public static final ConfiguredProjectile<ToothProjectile> TOOTH = build("Sharp Tooth", 1.75f, 5f, 0, convertToInaccuracy(0.92f), ToothProjectile::new);
public static final ConfiguredProjectile<WitherProjectile> WITHER = build("Withershot", 0.8f, 8f, 0, convertToInaccuracy(0.9f), WitherProjectile::new);
public static final ConfiguredProjectile<CorrosiveAcidProjectile> CORROSIVE = build("Corrosive", 1.5f, 4, 0, convertToInaccuracy(0.9f), CorrosiveAcidProjectile::new);
public static final ConfiguredProjectile<AcidBlobProjectile> ACID_BLOB = build("Acid Blob", 1.2f, 2, 0, convertToInaccuracy(0.9f), AcidBlobProjectile::new);
public static final ConfiguredProjectile<AcidBlobProjectile> FALLING_ACID_BLOB = build("Falling Acid Blob", 0.1f, 2, 0, convertToInaccuracy(0.9f), AcidBlobProjectile::new);
public static final ConfiguredProjectile<AcidBlobProjectile> ACID_BLOB = build("Acid Blob", 1.2f, 2, 0, convertToInaccuracy(0.9f), SoundEvents.SLIME_JUMP_SMALL, AcidBlobProjectile::new);
public static final ConfiguredProjectile<AcidBlobProjectile> FALLING_ACID_BLOB = build("Falling Acid Blob", 0.1f, 2, 0, convertToInaccuracy(0.9f), SoundEvents.SLIME_SQUISH_SMALL, AcidBlobProjectile::new);
public static final ConfiguredProjectile<SapberryProjectile> SAPBERRY = build("Sapberry", 1.25f, 2, 0, convertToInaccuracy(0.9f), SapberryProjectile::new);

private static float convertToInaccuracy(float accuracy) {
return -Gun.MAX_INACCURACY * accuracy + Gun.MAX_INACCURACY;
}

private static <T extends BaseProjectile> ConfiguredProjectile<T> build(String name, float velocity, float damage, int knockback, float accuracy, ProjectileFactory<T> factory) {
ConfiguredProjectile<T> configuredProjectile = new ConfiguredProjectile<>(name, velocity, damage, knockback, convertToInaccuracy(accuracy), factory);
ConfiguredProjectile<T> configuredProjectile = new ConfiguredProjectile<>(name, velocity, damage, knockback, convertToInaccuracy(accuracy), SoundEvents.CROSSBOW_SHOOT, factory);
PRECONFIGURED_PROJECTILES.add(configuredProjectile);
return configuredProjectile;
}

public static <T extends BaseProjectile> boolean shootProjectile(Level level, LivingEntity shooter, float velocity, float damage, int knockback, float inaccuracy, ProjectileFactory<T> factory) {
private static <T extends BaseProjectile> ConfiguredProjectile<T> build(String name, float velocity, float damage, int knockback, float accuracy, SoundEvent shootSound, ProjectileFactory<T> factory) {
ConfiguredProjectile<T> configuredProjectile = new ConfiguredProjectile<>(name, velocity, damage, knockback, convertToInaccuracy(accuracy), shootSound, factory);
PRECONFIGURED_PROJECTILES.add(configuredProjectile);
return configuredProjectile;
}

public static <T extends BaseProjectile> boolean shootProjectile(Level level, LivingEntity shooter, float velocity, float damage, int knockback, float inaccuracy, SoundEvent shootSound, ProjectileFactory<T> factory) {
BaseProjectile projectile = factory.create(level, shooter.getX(), shooter.getEyeY() - 0.1f, shooter.getZ());
projectile.setOwner(shooter);

Expand All @@ -46,14 +53,14 @@ public static <T extends BaseProjectile> boolean shootProjectile(Level level, Li
projectile.shoot(direction.x(), direction.y(), direction.z(), velocity, inaccuracy);

if (level.addFreshEntity(projectile)) {
level.playSound(null, shooter.getX(), shooter.getY(), shooter.getZ(), SoundEvents.CROSSBOW_SHOOT, SoundSource.PLAYERS, 0.8f, 0.4f);
level.playSound(null, shooter.getX(), shooter.getY(), shooter.getZ(), shootSound, SoundSource.PLAYERS, 0.8f, 0.4f);
return true;
}

return false;
}

public static <T extends BaseProjectile> boolean shootProjectile(Level level, Vec3 origin, Vec3 target, float velocity, float damage, int knockback, float inaccuracy, ProjectileFactory<T> factory) {
public static <T extends BaseProjectile> boolean shootProjectile(Level level, Vec3 origin, Vec3 target, float velocity, float damage, int knockback, float inaccuracy, SoundEvent shootSound, ProjectileFactory<T> factory) {
BaseProjectile projectile = factory.create(level, origin.x, origin.y, origin.z);

projectile.setDamage(damage);
Expand All @@ -65,7 +72,7 @@ public static <T extends BaseProjectile> boolean shootProjectile(Level level, Ve
projectile.shoot(direction.x(), direction.y(), direction.z(), velocity, inaccuracy);

if (level.addFreshEntity(projectile)) {
level.playSound(null, origin.x, origin.y, origin.z, SoundEvents.CROSSBOW_SHOOT, SoundSource.PLAYERS, 0.8f, 0.4f);
level.playSound(null, origin.x, origin.y, origin.z, shootSound, SoundSource.PLAYERS, 0.8f, 0.4f);
return true;
}

Expand All @@ -76,22 +83,22 @@ public interface ProjectileFactory<T extends BaseProjectile> {
T create(Level level, double x, double v, double z);
}

public record ConfiguredProjectile<T extends BaseProjectile>(String name, float velocity, float damage, int knockback, float inaccuracy, ProjectileFactory<T> factory) {
public record ConfiguredProjectile<T extends BaseProjectile>(String name, float velocity, float damage, int knockback, float inaccuracy, SoundEvent shootSound, ProjectileFactory<T> factory) {

public boolean shoot(Level level, Vec3 origin, Vec3 target) {
return shootProjectile(level, origin, target, velocity, damage, knockback, inaccuracy, factory);
return shootProjectile(level, origin, target, velocity, damage, knockback, inaccuracy, shootSound, factory);
}

public boolean shoot(Level level, Vec3 origin, Vec3 target, FloatOperator velocityModifier, FloatOperator damageModifier, IntOperator knockbackModifier, FloatOperator inaccuracyModifier) {
return shootProjectile(level, origin, target, velocityModifier.apply(velocity), damageModifier.apply(damage), knockbackModifier.apply(knockback), inaccuracyModifier.apply(inaccuracy), factory);
return shootProjectile(level, origin, target, velocityModifier.apply(velocity), damageModifier.apply(damage), knockbackModifier.apply(knockback), inaccuracyModifier.apply(inaccuracy), shootSound, factory);
}

public boolean shoot(Level level, LivingEntity shooter) {
return shootProjectile(level, shooter, velocity, damage, knockback, inaccuracy, factory);
return shootProjectile(level, shooter, velocity, damage, knockback, inaccuracy, shootSound, factory);
}

public boolean shoot(Level level, LivingEntity shooter, FloatOperator velocityModifier, FloatOperator damageModifier, IntOperator knockbackModifier, FloatOperator inaccuracyModifier) {
return shootProjectile(level, shooter, velocityModifier.apply(velocity), damageModifier.apply(damage), knockbackModifier.apply(knockback), inaccuracyModifier.apply(inaccuracy), factory);
return shootProjectile(level, shooter, velocityModifier.apply(velocity), damageModifier.apply(damage), knockbackModifier.apply(knockback), inaccuracyModifier.apply(inaccuracy), shootSound, factory);
}

}
Expand Down

0 comments on commit d317313

Please sign in to comment.