Compare commits

..

2 Commits

Author SHA1 Message Date
5cc87d537c Lasers Render Correctly
All checks were successful
EnergonRelics/pipeline/head This commit looks good
2020-07-16 14:50:18 -04:00
4983948e3b Very Broken Lasers 2020-07-15 18:44:49 -04:00
14 changed files with 362 additions and 1 deletions

View File

@ -11,4 +11,8 @@ public class Config {
public static final int REACTOR_TIME = 2400;
public static final int REACTOR_ENERGY_OUTPUT = 250;
public static final int DEFENSIVE_LASER_RANGE = 18;
public static final int DEFENSIVE_LASER_IDLE_ENERGY_REQUIRED = 25;
public static final int DEFENSIVE_LASER_FIRE_ENERGY_REQUIRED = 64;
}

View File

@ -1,5 +1,6 @@
package com.thebrokenrail.energonrelics;
import com.thebrokenrail.energonrelics.block.DefensiveLaserBlock;
import com.thebrokenrail.energonrelics.block.battery.ActiveBatteryControllerBlock;
import com.thebrokenrail.energonrelics.block.reactor.ReactorControllerBlock;
import com.thebrokenrail.energonrelics.block.reactor.ReactorCoreBlock;
@ -58,6 +59,9 @@ public class EnergonRelics implements ModInitializer {
public static final Item CIRCUIT_BOARD_ITEM = new Item(new Item.Settings().group(ITEM_GROUP));
public static final Item DEFENSIVE_LASER_CORE_ITEM = new Item(new Item.Settings().group(ITEM_GROUP));;
public static final DefensiveLaserBlock DEFENSIVE_LASER_BLOCK = new DefensiveLaserBlock();
@Override
public void onInitialize() {
NETWORK_CHIP_ITEM = Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "network_chip"), new NetworkChipItem());
@ -82,5 +86,8 @@ public class EnergonRelics implements ModInitializer {
VERIDIUM_ORE_BLOCK.register("veridium_ore");
VERIDIUM_BLOCK_BLOCK.register("veridium_block");
Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, new Identifier(NAMESPACE, "veridium_ore"), VERIDIUM_ORE_FEATURE);
Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "defensive_laser_core"), DEFENSIVE_LASER_CORE_ITEM);
DEFENSIVE_LASER_BLOCK.register("defensive_laser");
}
}

View File

@ -0,0 +1,60 @@
package com.thebrokenrail.energonrelics.block;
import com.thebrokenrail.energonrelics.block.entity.DefensiveLaserBlockEntity;
import com.thebrokenrail.energonrelics.block.util.EnergyProviderBlock;
import com.thebrokenrail.energonrelics.client.render.DefensiveLaserBlockEntityRenderer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Material;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
import java.util.function.Function;
public class DefensiveLaserBlock extends EnergyProviderBlock {
public static final BooleanProperty POWERED = Properties.POWERED;
public DefensiveLaserBlock() {
super(FabricBlockSettings.of(Material.GLASS).sounds(BlockSoundGroup.GLASS).nonOpaque().strength(0.3f).allowsSpawning((state, world, pos, type) -> false).solidBlock((state, world, pos) -> false).suffocates((state, world, pos) -> false));
setDefaultState(getDefaultState().with(POWERED, false));
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(POWERED);
}
@Override
@SuppressWarnings("deprecation")
@Environment(EnvType.CLIENT)
public float getAmbientOcclusionLightLevel(BlockState state, BlockView world, BlockPos pos) {
return 1.0F;
}
@Override
public boolean isTranslucent(BlockState state, BlockView world, BlockPos pos) {
return true;
}
@Override
protected Function<BlockEntityType<BlockEntity>, BlockEntity> getFactory() {
return DefensiveLaserBlockEntity::new;
}
@Override
@Environment(EnvType.CLIENT)
protected Function<BlockEntityRenderDispatcher, BlockEntityRenderer<BlockEntity>> getRenderer() {
return DefensiveLaserBlockEntityRenderer::new;
}
}

View File

@ -0,0 +1,209 @@
package com.thebrokenrail.energonrelics.block.entity;
import com.thebrokenrail.energonrelics.Config;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.block.DefensiveLaserBlock;
import com.thebrokenrail.energonrelics.energy.core.EnergyReceiverBlockEntity;
import com.thebrokenrail.energonrelics.energy.core.util.Action;
import com.thebrokenrail.energonrelics.util.MissingCaseException;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RayTraceContext;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity {
private static final float MAX_PITCH = 0;
private static final float MIN_PITCH = -70;
private static final float ROTATION_INCREMENT = 2;
private static final int COUNTDOWN = 6;
public final Rotation rotation = new Rotation(0, 0, "Rotation");
private LivingEntity target;
private int countdown = 0;
private final Predicate<LivingEntity> predicate = entity -> {
if (entity.getBlockPos().isWithinDistance(getPos(), Config.DEFENSIVE_LASER_RANGE)) {
Vec3d start = getPosVec().add(rotation.getRayVector());
Vec3d end = entity.getPos();
HitResult result = entity.getEntityWorld().rayTrace(new RayTraceContext(start, end, RayTraceContext.ShapeType.COLLIDER, RayTraceContext.FluidHandling.ANY, entity));
return result.getType() == HitResult.Type.MISS;
} else {
return false;
}
};
private Vec3d getPosVec() {
return new Vec3d(getPos().getX() + 0.5d, getPos().getY() + 0.5d, getPos().getZ() + 0.5d);
}
@Override
public BlockState getCachedState() {
if (hasWorld()) {
return super.getCachedState();
} else {
return EnergonRelics.DEFENSIVE_LASER_BLOCK.getDefaultState();
}
}
public class Rotation {
private float yaw = 0;
private float pitch = 0;
private final String key;
private Rotation(float yaw, float pitch, String key) {
setRaw(yaw, pitch);
this.key = key;
}
private void fromTag(CompoundTag tag) {
CompoundTag rotation = tag.getCompound(key);
setRaw(rotation.getInt("Yaw"), rotation.getInt("Pitch"));
}
private void toTag(CompoundTag tag) {
CompoundTag rotation = new CompoundTag();
rotation.putFloat("Yaw", yaw);
rotation.putFloat("Pitch", pitch);
tag.put(key, rotation);
}
private void setRaw(float yaw, float pitch) {
setYaw(yaw);
setPitch(pitch);
}
private void setYaw(float yaw) {
this.yaw = yaw % 360;
}
private void setPitch(float pitch) {
this.pitch = pitch % 360;
}
public float getYaw() {
return yaw;
}
public float getPitch() {
return pitch;
}
private void change(float diffYaw, float diffPitch) {
setRaw(yaw + diffYaw, pitch + diffPitch);
markDirty();
}
private Vec3d getRayVector() {
float pitch = this.pitch * (float) DEG2RAD;
float yaw = this.yaw * (float) DEG2RAD;
float cosYaw = MathHelper.cos(yaw);
float sinYaw = MathHelper.sin(yaw);
float cosPitch = MathHelper.cos(pitch);
float sinPitch = MathHelper.sin(pitch);
return new Vec3d(sinYaw * cosPitch, -sinPitch, cosYaw * cosPitch);
}
private float getAngleChange(float from, float to) {
float diff = MathHelper.subtractAngles(from, to);
return MathHelper.clamp(diff, -ROTATION_INCREMENT, ROTATION_INCREMENT);
}
private void target(float targetYaw, float targetPitch) {
float yawChange = getAngleChange(yaw, targetYaw);
float pitchChange = getAngleChange(pitch, targetPitch);
change(yawChange, pitchChange);
}
}
public DefensiveLaserBlockEntity(BlockEntityType<?> type) {
super(type);
}
public static final double DEG2RAD = Math.PI / 180d;
private static final double RAD2DEG = 180d / Math.PI;
private float getTargetPitch(double x, double y, double z) {
Vec3d pos = getPosVec();
double diffX = x - pos.getX();
double diffY = y - pos.getY();
double diffZ = z - pos.getZ();
double g = MathHelper.sqrt(diffX * diffX + diffZ * diffZ);
return (float) -(MathHelper.atan2(diffY, g) * RAD2DEG);
}
private float getTargetYaw(double x, double z) {
Vec3d pos = getPosVec();
double diffX = x - pos.getX();
double diffZ = z - pos.getZ();
return (float) (MathHelper.atan2(diffZ, diffX) * RAD2DEG) - 90.0F;
}
@Override
protected void tickEnergy() {
assert getWorld() != null;
Vec3d vec = rotation.getRayVector().add(getPosVec());
((ServerWorld) getWorld()).spawnParticles(ParticleTypes.END_ROD, vec.getX(), vec.getY(), vec.getZ(), 1, 0, 0, 0, 0);
addAction(Action.createBlockStatePropertyAction(Config.DEFENSIVE_LASER_IDLE_ENERGY_REQUIRED, DefensiveLaserBlock.POWERED, true, false));
if (getCachedState().get(DefensiveLaserBlock.POWERED)) {
if (countdown > 0) {
countdown--;
}
if (countdown == 1) {
// Fire!
target = null;
} else if (countdown < 1) {
if (target == null) {
List<LivingEntity> entities = getWorld().getEntities(LivingEntity.class, new Box(getPos()).expand(Config.DEFENSIVE_LASER_RANGE), predicate);
if (entities.size() > 0) {
Collections.shuffle(entities);
target = entities.get(0);
}
} else {
if (predicate.test(target)) {
Optional<Vec3d> optional = target.getBoundingBox().rayTrace(getPosVec(), getPosVec().add(rotation.getRayVector().multiply(Config.DEFENSIVE_LASER_RANGE)));
if (optional.isPresent()) {
countdown = COUNTDOWN;
} else {
Vec3d targetPos = target.getPos();
float targetYaw = getTargetYaw(targetPos.getX(), targetPos.getZ());
float targetPitch = getTargetPitch(targetPos.getX(), targetPos.getY(), targetPos.getZ());
rotation.target(targetYaw, targetPitch);
}
} else {
target = null;
}
}
}
}
}
@Override
public CompoundTag toTag(CompoundTag tag) {
super.toTag(tag);
rotation.toTag(tag);
tag.putInt("Countdown", countdown);
return tag;
}
@Override
public void fromTag(BlockState state, CompoundTag tag) {
super.fromTag(state, tag);
rotation.fromTag(tag);
countdown = tag.getInt("Countdown");
}
}

View File

@ -1,14 +1,18 @@
package com.thebrokenrail.energonrelics.client;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.block.util.EnergyProviderBlock;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.minecraft.client.render.RenderLayer;
@Environment(EnvType.CLIENT)
public class EnergonRelicsClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
EnergyProviderBlock.initRenderer();
BlockRenderLayerMap.INSTANCE.putBlock(EnergonRelics.DEFENSIVE_LASER_BLOCK, RenderLayer.getCutout());
}
}

View File

@ -0,0 +1,46 @@
package com.thebrokenrail.energonrelics.client.render;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.block.entity.DefensiveLaserBlockEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
import net.minecraft.client.render.entity.feature.HeadFeatureRenderer;
import net.minecraft.client.render.entity.feature.ShulkerHeadFeatureRenderer;
import net.minecraft.client.render.model.json.ModelTransformation;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.item.ItemStack;
public class DefensiveLaserBlockEntityRenderer extends HighlightBlockEntityRenderer {
public DefensiveLaserBlockEntityRenderer(BlockEntityRenderDispatcher dispatcher) {
super(dispatcher);
}
@Override
public void render(BlockEntity entity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
super.render(entity, tickDelta, matrices, vertexConsumers, light, overlay);
matrices.push();
matrices.translate(0.5d, 0.5d, 0.5d);
matrices.scale(0.6f, 0.6f, 0.6f);
if (entity instanceof DefensiveLaserBlockEntity) {
DefensiveLaserBlockEntity laser = (DefensiveLaserBlockEntity) entity;
float yaw = (float) ((laser.rotation.getYaw()) * DefensiveLaserBlockEntity.DEG2RAD);
if (yaw != 0) {
matrices.multiply(Vector3f.POSITIVE_Y.getRadialQuaternion(yaw));
}
float pitch = (float) ((laser.rotation.getPitch() + 45) * DefensiveLaserBlockEntity.DEG2RAD);
if (pitch != 0) {
matrices.multiply(Vector3f.POSITIVE_X.getRadialQuaternion(pitch));
}
matrices.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion(-90));
MinecraftClient.getInstance().getItemRenderer().renderItem(new ItemStack(EnergonRelics.DEFENSIVE_LASER_CORE_ITEM), ModelTransformation.Mode.FIXED, light, overlay, matrices, vertexConsumers);
}
matrices.pop();
}
}

View File

@ -0,0 +1,7 @@
package com.thebrokenrail.energonrelics.util;
public class MissingCaseException extends RuntimeException {
public MissingCaseException(Enum<?> value) {
super(value.name());
}
}

View File

@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "energonrelics:block/defensive_laser"
}
}
}

View File

@ -18,5 +18,7 @@
"block.energonrelics.reactor_core": "Reactor Core",
"item.energonrelics.veridium_ingot": "Veridium Ingot",
"block.energonrelics.veridium_ore": "Veridium Ore",
"block.energonrelics.veridium_block": "Veridium Block"
"block.energonrelics.veridium_block": "Veridium Block",
"item.energonrelics.defensive_laser_core": "Defensive laser Core",
"block.energonrelics.defensive_laser": "Defensive Laser"
}

View File

@ -0,0 +1,6 @@
{
"parent": "minecraft:block/cube_all",
"textures": {
"all": "energonrelics:block/defensive_laser_base"
}
}

View File

@ -0,0 +1,3 @@
{
"parent": "energonrelics:block/defensive_laser"
}

View File

@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "energonrelics:item/defensive_laser_core"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 719 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 833 B