Skip to content

Commit

Permalink
Separated the explosion function into other, smaller functions in AC_…
Browse files Browse the repository at this point in the history
…EntityBomb
  • Loading branch information
Kiroto committed Oct 1, 2023
1 parent d7e9d75 commit af5c491
Showing 1 changed file with 155 additions and 135 deletions.
290 changes: 155 additions & 135 deletions src/main/java/dev/adventurecraft/awakening/common/AC_EntityBomb.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dev.adventurecraft.awakening.common;

import java.util.List;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity;
Expand All @@ -11,143 +9,165 @@
import net.minecraft.util.math.AxixAlignedBoundingBox;
import net.minecraft.world.World;

import java.util.List;
import java.util.Random;

public class AC_EntityBomb extends ItemEntity {
private static final double BOMB_DAMAGE = 20.0D;
private static final double BOMB_RANGE = 5.0D;
private static final double BOMB_DESTROY_RANGE = 3.0D;
private static final int BOMB_FUSE = 45;
private int fuse;
private Entity parentEntity;

public AC_EntityBomb(World var1) {
super(var1);
this.setSize(0.5F, 0.5F);
this.stack = new ItemStack(AC_Items.bomb);
this.fuse = BOMB_FUSE;
}

public AC_EntityBomb(World world, Entity entity) {
this(world);
this.parentEntity = entity;
this.setRotation(entity.yaw, entity.pitch);
this.xVelocity = 0.3D * -Math.sin(this.yaw / 180.0F * 3.141593F) * Math.cos(this.pitch / 180.0F * 3.141593F);
this.zVelocity = 0.3D * Math.cos(this.yaw / 180.0F * 3.141593F) * Math.cos(this.pitch / 180.0F * 3.141593F);
this.yVelocity = 0.3D * -Math.sin(this.pitch / 180.0F * 3.141593F) + (double)0.1F;
this.setPosition(entity.x, entity.y, entity.z);
this.prevX = this.x;
this.prevY = this.y;
this.prevZ = this.z;
}

public void tick() {
super.tick();
if(this.fuse == 45) {
this.world.playSound(this, "random.fuse", 1.0F, 1.0F);
}

--this.fuse;
double fuseRemaining = (double)this.fuse / 45.0D;
private static final double BOMB_DAMAGE = 20.0D;
private static final double BOMB_RANGE = 5.0D;
private static final double BOMB_DESTROY_RANGE = 3.0D;
private static final int BOMB_FUSE = 45;
private int fuse;
private Entity parentEntity;

public AC_EntityBomb(World var1) {
super(var1);
this.setSize(0.5F, 0.5F);
this.stack = new ItemStack(AC_Items.bomb);
this.fuse = BOMB_FUSE;
}

public AC_EntityBomb(World world, Entity entity) {
this(world);
this.parentEntity = entity;
this.setRotation(entity.yaw, entity.pitch);
this.xVelocity = 0.3D * -Math.sin(this.yaw / 180.0F * 3.141593F) * Math.cos(this.pitch / 180.0F * 3.141593F);
this.zVelocity = 0.3D * Math.cos(this.yaw / 180.0F * 3.141593F) * Math.cos(this.pitch / 180.0F * 3.141593F);
this.yVelocity = 0.3D * -Math.sin(this.pitch / 180.0F * 3.141593F) + (double) 0.1F;
this.setPosition(entity.x, entity.y, entity.z);
this.prevX = this.x;
this.prevY = this.y;
this.prevZ = this.z;
}

public void tick() {
super.tick();
if (this.fuse == 45) {
this.world.playSound(this, "random.fuse", 1.0F, 1.0F);
}

--this.fuse;
double fuseRemaining = (double) this.fuse / BOMB_FUSE;
// The particle effects animate as if the fuse was burning, drawing the fire and smoke lower!
double flameSourceMod = 0.2D * fuseRemaining;

if(this.fuse == 0) {
explode(this, this.parentEntity, this.world, this.x, this.y, this.z);
} else if(this.fuse % 2 == 0) {
this.world.addParticle("smoke", this.x, this.y + 0.675D + flameSourceMod, this.z, 0.0D, 0.0D, 0.0D);
} else {
this.world.addParticle("flame", this.x, this.y + 0.675D + flameSourceMod, this.z, 0.0D, 0.0D, 0.0D);
}

}

public static void explode(Entity exploder, Entity parent, World world, double x, double y, double z) {
exploder.remove();
world.playSound(x, y, z, "random.explode", 4.0F, 1.0F);
List explodees = world.getEntities(exploder, AxixAlignedBoundingBox.createAndAddToList(Math.floor(x - 5.0D), Math.floor(y - 5.0D), Math.floor(z - 5.0D), Math.ceil(x + 5.0D), Math.ceil(y + 5.0D), Math.ceil(z + 5.0D)));

int explodeeIndex;
for(explodeeIndex = 0; explodeeIndex < explodees.size(); ++explodeeIndex) {
Entity explodee = (Entity)explodees.get(explodeeIndex);
double distanceFromExplosion = explodee.distanceTo(x, y, z);
if(distanceFromExplosion < BOMB_RANGE) {
distanceFromExplosion = (BOMB_RANGE - distanceFromExplosion) / BOMB_RANGE; // Percentage of how close the character is
double xForce = explodee.x - x;
double yForce = explodee.y - y;
double zForce = explodee.z - z;
explodee.accelerate(distanceFromExplosion * xForce, distanceFromExplosion * yForce, distanceFromExplosion * zForce);
explodee.damage(parent, (int)Math.ceil(distanceFromExplosion * BOMB_DAMAGE));
}
}

// He reused this variable.
explodeeIndex = (int)x;
int yPosition = (int)y;
int zPosition = (int)z;

int blockOffsetA; // Used for X
int blockOffsetB; // User for Y
double flameSourceMod = 0.2D * fuseRemaining;

if (this.fuse == 0) {
explode(this, this.parentEntity, this.world, this.x, this.y, this.z);
} else if (this.fuse % 2 == 0) {
this.world.addParticle("smoke", this.x, this.y + 0.675D + flameSourceMod, this.z, 0.0D, 0.0D, 0.0D);
} else {
this.world.addParticle("flame", this.x, this.y + 0.675D + flameSourceMod, this.z, 0.0D, 0.0D, 0.0D);
}

}

private static void harmEntitiesAround(World world, Entity exploder, Entity explosionParent) {
double x = exploder.x;
double y = exploder.y;
double z = exploder.z;
List explodees = world.getEntities(
exploder,
AxixAlignedBoundingBox.createAndAddToList(
Math.floor(x - BOMB_RANGE),
Math.floor(y - BOMB_RANGE),
Math.floor(z - BOMB_RANGE),
Math.ceil(x + BOMB_RANGE),
Math.ceil(y + BOMB_RANGE),
Math.ceil(z + BOMB_RANGE)
)
);

int explodeeIndex;
for (explodeeIndex = 0; explodeeIndex < explodees.size(); ++explodeeIndex) {
Entity explodee = (Entity) explodees.get(explodeeIndex);
double distanceFromExplosion = explodee.distanceTo(x, y, z);
if (distanceFromExplosion < BOMB_RANGE) {
distanceFromExplosion = (BOMB_RANGE - distanceFromExplosion) / BOMB_RANGE; // Percentage of how close the character is
double xForce = explodee.x - x;
double yForce = explodee.y - y;
double zForce = explodee.z - z;
explodee.accelerate(distanceFromExplosion * xForce, distanceFromExplosion * yForce, distanceFromExplosion * zForce);
explodee.damage(explosionParent, (int) Math.ceil(distanceFromExplosion * BOMB_DAMAGE));
}
}
}

private static void destroyBombableBlocksAround(World world, int x, int y, int z) {

int bombDestroyRange = (int) BOMB_DESTROY_RANGE;
// Look for blocks in a 3x3x3 volume centered on the explosion's center block's origin corner, but not the outermost ones.
for(int xOffset = -bombDestroyRange; xOffset <= bombDestroyRange; ++xOffset) {
for(blockOffsetB = -bombDestroyRange; blockOffsetB <= 3; ++blockOffsetB) {
for(blockOffsetA = -bombDestroyRange; blockOffsetA <= bombDestroyRange; ++blockOffsetA) {
double distanceSquared = (double) xOffset * (double) xOffset + (double) (blockOffsetB * blockOffsetB) + (double) (blockOffsetA * blockOffsetA);
if(distanceSquared <= 9.0D) {
int blockAtOffset = world.getBlockId(explodeeIndex + xOffset, yPosition + blockOffsetB, zPosition + blockOffsetA);
for (int blockOffsetX = -bombDestroyRange; blockOffsetX <= bombDestroyRange; ++blockOffsetX) {
for (int blockOffsetY = -bombDestroyRange; blockOffsetY <= 3; ++blockOffsetY) {
for (int blockOffsetZ = -bombDestroyRange; blockOffsetZ <= bombDestroyRange; ++blockOffsetZ) {
double distanceSquared = (double) blockOffsetX * (double) blockOffsetX + (double) (blockOffsetY * blockOffsetY) + (double) (blockOffsetZ * blockOffsetZ);
if (distanceSquared <= 9.0D) {
int blockAtOffset = world.getBlockId(x + blockOffsetX, y + blockOffsetY, z + blockOffsetZ);
// Remove bombable tiles
if(Block.BY_ID[blockAtOffset] instanceof AC_BlockBombable) {
world.setBlock(explodeeIndex + xOffset, yPosition + blockOffsetB, zPosition + blockOffsetA, 0);
}
}
}
}
}

Random rng = new Random();
rng.setSeed(world.getWorldTime());

// He reused variables again smh
if (Block.BY_ID[blockAtOffset] instanceof AC_BlockBombable) {
world.setBlock(x + blockOffsetX, y + blockOffsetY, z + blockOffsetZ, 0);
}
}
}
}
}
}

private static void displayExplosionParticles(World world, double x, double y, double z) {
Random rng = new Random();
rng.setSeed(world.getWorldTime());

// This is for the smoke coming out of the explosion.
for(blockOffsetB = -3; blockOffsetB <= 3; ++blockOffsetB) { // Used for X
for(blockOffsetA = -3; blockOffsetA <= 3; ++blockOffsetA) { // Used for Y
for(int blockOffsetD = -3; blockOffsetD <= 3; ++blockOffsetD) {
double distanceSquared = (double) blockOffsetB * (double) blockOffsetB + (double) (blockOffsetA * blockOffsetA) + (double) (blockOffsetD * blockOffsetD);
if(rng.nextInt(3) == 0 && distanceSquared <= 9.0D) {
double xDirection = blockOffsetB;
double yDirection = blockOffsetA;
double zDirection = blockOffsetD;
double launchPower = Math.sqrt(distanceSquared) * (0.75D + 0.5D * rng.nextDouble()) * 1.5D / 3.0D;
xDirection = xDirection / launchPower;
yDirection = yDirection / launchPower;
zDirection = zDirection / launchPower;
world.addParticle("explode", x, y, z, xDirection, yDirection, zDirection);
world.addParticle("smoke", x, y, z, xDirection, yDirection, zDirection);
}
}
}
}

}

public boolean damage(Entity var1, int var2) {
if(!this.removed) {
this.setAttacked();
explode(this, this.parentEntity, this.world, this.x, this.y, this.z);
}

return false;
}

public void writeAdditional(CompoundTag var1) {
super.writeAdditional(var1);
var1.put("Fuse", (byte)this.fuse);
}

public void readAdditional(CompoundTag var1) {
super.readAdditional(var1);
this.fuse = var1.getByte("Fuse");
}

public void onPlayerCollision(PlayerEntity var1) {
}
for (int xDirectionForce = -3; xDirectionForce <= 3; ++xDirectionForce) { // Used for X
for (int yDirectionForce = -3; yDirectionForce <= 3; ++yDirectionForce) { // Used for Y
for (int zDirectionForce = -3; zDirectionForce <= 3; ++zDirectionForce) {
double distanceSquared = (double) xDirectionForce * (double) xDirectionForce + (double) (yDirectionForce * yDirectionForce) + (double) (zDirectionForce * zDirectionForce);
if (rng.nextInt(3) == 0 && distanceSquared <= 9.0D) {
double xDirection = xDirectionForce;
double yDirection = yDirectionForce;
double zDirection = zDirectionForce;
double launchPower = Math.sqrt(distanceSquared) * (0.75D + 0.5D * rng.nextDouble()) * 1.5D / 3.0D;
xDirection = xDirection / launchPower;
yDirection = yDirection / launchPower;
zDirection = zDirection / launchPower;
world.addParticle("explode", x, y, z, xDirection, yDirection, zDirection);
world.addParticle("smoke", x, y, z, xDirection, yDirection, zDirection);
}
}
}
}
}

public static void explode(Entity exploder, Entity parent, World world, double x, double y, double z) {
exploder.remove();
world.playSound(x, y, z, "random.explode", 4.0F, 1.0F);

harmEntitiesAround(world, exploder, parent);

destroyBombableBlocksAround(world, (int) x, (int) y, (int) z);

displayExplosionParticles(world, x, y, z);

}

public boolean damage(Entity var1, int var2) {
if (!this.removed) {
this.setAttacked();
explode(this, this.parentEntity, this.world, this.x, this.y, this.z);
}

return false;
}

public void writeAdditional(CompoundTag var1) {
super.writeAdditional(var1);
var1.put("Fuse", (byte) this.fuse);
}

public void readAdditional(CompoundTag var1) {
super.readAdditional(var1);
this.fuse = var1.getByte("Fuse");
}

public void onPlayerCollision(PlayerEntity var1) {
}
}

0 comments on commit af5c491

Please sign in to comment.