Skip to content

Commit

Permalink
完成横扫之刃的设计
Browse files Browse the repository at this point in the history
  • Loading branch information
TartaricAcid committed Mar 25, 2024
1 parent 723496d commit e0a5859
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import net.minecraft.nbt.Tag;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.phys.AABB;

import java.util.Map;

Expand Down Expand Up @@ -41,6 +43,11 @@ public class FavorabilityManager {
private static final int LEVEL_2_ATTACK_DISTANCE = 5;
private static final int LEVEL_3_ATTACK_DISTANCE = 7;

private static final double LEVEL_0_SWEEP_RANGE = 1;
private static final double LEVEL_1_SWEEP_RANGE = 2;
private static final double LEVEL_2_SWEEP_RANGE = 3;
private static final double LEVEL_3_SWEEP_RANGE = 4;

private static final String TAG_NAME = "FavorabilityManagerCounter";

private final Map<String, Time> counter;
Expand Down Expand Up @@ -168,6 +175,23 @@ public int getAttackDistanceByPoint(int favorability) {
}
}

public AABB getSweepRange(Entity target, int favorability) {
AABB boundingBox = target.getBoundingBox();
if (favorability < LEVEL_1_POINT) {
return sweepRangeTransform(boundingBox, LEVEL_0_SWEEP_RANGE);
} else if (favorability < LEVEL_2_POINT) {
return sweepRangeTransform(boundingBox, LEVEL_1_SWEEP_RANGE);
} else if (favorability < LEVEL_3_POINT) {
return sweepRangeTransform(boundingBox, LEVEL_2_SWEEP_RANGE);
} else {
return sweepRangeTransform(boundingBox, LEVEL_3_SWEEP_RANGE);
}
}

private AABB sweepRangeTransform(AABB boundingBox, double range) {
return boundingBox.inflate(range, Math.max(range / 4, 0.25), range);
}

public int getPointByLevel(int level) {
switch (level) {
case LEVEL_1 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.ToolActions;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.common.util.ITeleporter;
Expand Down Expand Up @@ -589,16 +590,42 @@ public double getMeleeAttackRangeSqr(LivingEntity entity) {
}

@Override
public boolean doHurtTarget(Entity entityIn) {
boolean result = super.doHurtTarget(entityIn);
public boolean doHurtTarget(Entity target) {
boolean result = super.doHurtTarget(target);
if (result) {
doSweepHurt(target);
}
this.getMainHandItem().hurtAndBreak(1, this, (maid) -> maid.broadcastBreakEvent(InteractionHand.MAIN_HAND));
if (this.getTask() instanceof IAttackTask attackTask && attackTask.hasExtraAttack(this, entityIn)) {
boolean extraResult = attackTask.doExtraAttack(this, entityIn);
if (this.getTask() instanceof IAttackTask attackTask && attackTask.hasExtraAttack(this, target)) {
boolean extraResult = attackTask.doExtraAttack(this, target);
return result && extraResult;
}
return result;
}

private void doSweepHurt(Entity target) {
ItemStack mainhandItem = this.getItemInHand(InteractionHand.MAIN_HAND);
boolean canSweep = mainhandItem.canPerformAction(ToolActions.SWORD_SWEEP);
float sweepingDamageRatio = EnchantmentHelper.getSweepingDamageRatio(this);
if (canSweep && sweepingDamageRatio > 0) {
float baseDamage = (float) this.getAttributeValue(Attributes.ATTACK_DAMAGE);
float sweepDamage = 1.0f + sweepingDamageRatio * baseDamage;
AABB sweepRange = this.getFavorabilityManager().getSweepRange(target, this.getFavorability());
List<LivingEntity> hurtEntities = this.level.getEntitiesOfClass(LivingEntity.class, sweepRange);
for (LivingEntity entity : hurtEntities) {
if (entity != this && entity != target && !this.isAlliedTo(entity) && canAttack(entity) && canAttackType(entity.getType())) {
float posX = Mth.sin(this.getYRot() * ((float) Math.PI / 180F));
float posY = -Mth.cos(this.getYRot() * ((float) Math.PI / 180F));
entity.knockback(0.4, posX, posY);
entity.hurt(this.damageSources().mobAttack(this), sweepDamage);
}
}
this.level.playSound(null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_SWEEP, this.getSoundSource(), 1, 1);
this.spawnSweepAttackParticle();
}
}


@Override
public boolean hurt(DamageSource source, float amount) {
if (MinecraftForge.EVENT_BUS.post(new MaidAttackEvent(this, source, amount))) {
Expand Down Expand Up @@ -905,6 +932,16 @@ public void spawnRankUpParticle() {
}
}

private void spawnSweepAttackParticle() {
double xOffset = -Mth.sin(this.getYRot() * ((float) Math.PI / 180F));
double zOffset = Mth.cos(this.getYRot() * ((float) Math.PI / 180F));
if (this.level instanceof ServerLevel serverLevel) {
serverLevel.sendParticles(ParticleTypes.SWEEP_ATTACK,
this.getX() + xOffset, this.getY(0.5),
this.getZ() + zOffset, 0, xOffset, 0, zOffset, 0);
}
}

@Override
public void addAdditionalSaveData(CompoundTag compound) {
super.addAdditionalSaveData(compound);
Expand Down

0 comments on commit e0a5859

Please sign in to comment.