-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Astrinox/dev
- Loading branch information
Showing
6 changed files
with
244 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/astrinox/stellum/handlers/explosion/ExplosionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package astrinox.stellum.handlers.explosion; | ||
|
||
import java.util.List; | ||
import java.util.Random; | ||
|
||
import astrinox.stellum.util.PerlinNoiseHelper; | ||
import net.minecraft.block.Blocks; | ||
import net.minecraft.entity.LivingEntity; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.Box; | ||
import net.minecraft.util.math.BlockPos.Mutable; | ||
import net.minecraft.world.World; | ||
|
||
public class ExplosionHandler { | ||
private BlockPos pos = new BlockPos(0, 0, 0); | ||
private int size = 16; | ||
private double noiseScale = 0.3; | ||
private float noiseMultiplier = 100; | ||
private boolean breakBlocks = true; | ||
private boolean hurtEntities = true; | ||
private float damage = 10; | ||
|
||
public ExplosionHandler setPos(BlockPos pos) { | ||
this.pos = pos; | ||
return this; | ||
} | ||
|
||
public ExplosionHandler setSize(int size) { | ||
this.size = size; | ||
return this; | ||
} | ||
|
||
public ExplosionHandler setNoiseScale(double noiseScale) { | ||
this.noiseScale = noiseScale; | ||
return this; | ||
} | ||
|
||
public ExplosionHandler setNoiseMultiplier(float noiseMultiplier) { | ||
this.noiseMultiplier = noiseMultiplier; | ||
return this; | ||
} | ||
|
||
public ExplosionHandler setBreakBlocks(boolean breakBlocks) { | ||
this.breakBlocks = breakBlocks; | ||
return this; | ||
} | ||
|
||
public ExplosionHandler setHurtEntities(boolean hurtEntities) { | ||
this.hurtEntities = hurtEntities; | ||
return this; | ||
} | ||
|
||
public ExplosionHandler setDamage(float damage) { | ||
this.damage = damage; | ||
return this; | ||
} | ||
|
||
public void trigger(World world) { | ||
int sizeSquared = size * size; | ||
Mutable blockPos = new Mutable(); | ||
PerlinNoiseHelper mainNoise = new PerlinNoiseHelper(new Random().nextLong(), noiseScale); | ||
|
||
for (int x = pos.getX() - size; x <= pos.getX() + size; x++) { | ||
for (int y = pos.getY() - size; y <= pos.getY() + size; y++) { | ||
for (int z = pos.getZ() - size; z <= pos.getZ() + size; z++) { | ||
int dx = x - pos.getX(); | ||
int dy = y - pos.getY(); | ||
int dz = z - pos.getZ(); | ||
|
||
int distanceSquared = dx * dx + dy * dy + dz * dz; | ||
|
||
if (distanceSquared <= sizeSquared + mainNoise.noise(x, y, z) * noiseMultiplier) { | ||
blockPos.set(x, y, z); | ||
if (breakBlocks) { | ||
world.setBlockState(blockPos, Blocks.AIR.getDefaultState()); | ||
} | ||
if (hurtEntities) { | ||
Box box = new Box(x - 0.5, y - 0.5, z - 0.5, x + 0.5, y + 0.5, z + 0.5); | ||
List<LivingEntity> entities = world.getEntitiesByClass(LivingEntity.class, box, | ||
e -> e.isAlive()); | ||
for (LivingEntity entity : entities) { | ||
entity.damage(world.getDamageSources().generic(), damage); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// TODO: Add effects pass, for things like fire and scorching | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/astrinox/stellum/util/PerlinNoiseHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package astrinox.stellum.util; | ||
|
||
import java.util.Random; | ||
|
||
public class PerlinNoiseHelper { | ||
private int[] perm; | ||
private double scale; | ||
|
||
public PerlinNoiseHelper(long seed, double scale) { | ||
this.scale = scale; | ||
perm = new int[512]; | ||
Random random = new Random(seed); | ||
for (int i = 0; i < 256; i++) { | ||
perm[i] = i; | ||
} | ||
for (int i = 0; i < 256; i++) { | ||
int j = random.nextInt(256); | ||
int temp = perm[i]; | ||
perm[i] = perm[j]; | ||
perm[j] = temp; | ||
} | ||
System.arraycopy(perm, 0, perm, 256, 256); | ||
} | ||
|
||
private double fade(double t) { | ||
return t * t * t * (t * (t * 6 - 15) + 10); | ||
} | ||
|
||
private double lerp(double t, double a, double b) { | ||
return a + t * (b - a); | ||
} | ||
|
||
private double grad(int hash, double x, double y, double z) { | ||
int h = hash & 15; | ||
double u = h < 8 ? x : y; | ||
double v = h < 4 ? y : (h == 12 || h == 14 ? x : z); | ||
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); | ||
} | ||
|
||
public double noise(double x, double y, double z) { | ||
x *= scale; | ||
y *= scale; | ||
z *= scale; | ||
|
||
int X = (int) Math.floor(x) & 255; | ||
int Y = (int) Math.floor(y) & 255; | ||
int Z = (int) Math.floor(z) & 255; | ||
|
||
x -= Math.floor(x); | ||
y -= Math.floor(y); | ||
z -= Math.floor(z); | ||
|
||
double u = fade(x); | ||
double v = fade(y); | ||
double w = fade(z); | ||
|
||
int A = perm[X] + Y; | ||
int AA = perm[A] + Z; | ||
int AB = perm[A + 1] + Z; | ||
int B = perm[X + 1] + Y; | ||
int BA = perm[B] + Z; | ||
int BB = perm[B + 1] + Z; | ||
|
||
return lerp(w, lerp(v, lerp(u, grad(perm[AA], x, y, z), | ||
grad(perm[BA], x - 1, y, z)), | ||
lerp(u, grad(perm[AB], x, y - 1, z), | ||
grad(perm[BB], x - 1, y - 1, z))), | ||
lerp(v, lerp(u, grad(perm[AA + 1], x, y, z - 1), | ||
grad(perm[BA + 1], x - 1, y, z - 1)), | ||
lerp(u, grad(perm[AB + 1], x, y - 1, z - 1), | ||
grad(perm[BB + 1], x - 1, y - 1, z - 1)))); | ||
} | ||
|
||
} |