IndustrialWires/src/main/java/malte0811/industrialWires/converter/MechPartFlywheel.java

184 lines
5.4 KiB
Java
Raw Normal View History

2017-10-04 17:05:04 +02:00
/*
* This file is part of Industrial Wires.
2018-02-28 21:06:33 +01:00
* Copyright (C) 2016-2018 malte0811
2017-10-04 17:05:04 +02:00
* Industrial Wires is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Industrial Wires is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Industrial Wires. If not, see <http://www.gnu.org/licenses/>.
*/
package malte0811.industrialWires.converter;
import malte0811.industrialWires.IndustrialWires;
import malte0811.industrialWires.blocks.converter.MechanicalMBBlockType;
import malte0811.industrialWires.util.LocalSidedWorld;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.BakedQuadRetextured;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
2017-10-04 17:05:04 +02:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.AxisAlignedBB;
2017-10-04 17:05:04 +02:00
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.oredict.OreDictionary;
import java.util.List;
import java.util.stream.Collectors;
import static net.minecraft.util.math.BlockPos.ORIGIN;
2017-10-04 17:05:04 +02:00
public class MechPartFlywheel extends MechMBPart {
private static final double RADIUS = 1.25;
private static final double THICKNESS = 1;
private static final double VOLUME = Math.PI*RADIUS*RADIUS*THICKNESS;
2017-10-04 17:05:04 +02:00
private Material material;
public MechPartFlywheel() {}
public MechPartFlywheel(Material m) {
material = m;
}
2017-10-04 17:05:04 +02:00
//A flywheel simply adds mass (lots of mass!), it doesn't actively change speeds/energy
@Override
public void createMEnergy(MechEnergy e) {}
2017-10-04 17:05:04 +02:00
@Override
public double requestMEnergy(MechEnergy e) {
2017-10-04 17:05:04 +02:00
return 0;
}
@Override
public void insertMEnergy(double added) {}
2017-10-04 17:05:04 +02:00
@Override
public double getInertia() {
return .5*material.density*VOLUME*RADIUS*RADIUS;
2017-10-04 17:05:04 +02:00
}
@Override
2018-03-23 16:27:32 +01:00
public double getMaxSpeed() {
return Math.sqrt(material.tensileStrength /material.density)/RADIUS;
2017-10-04 17:05:04 +02:00
}
@Override
public void writeToNBT(NBTTagCompound out) {
out.setInteger("material", material.ordinal());
}
@Override
public void readFromNBT(NBTTagCompound in) {
material = Material.values()[in.getInteger("material")];
2017-10-04 17:05:04 +02:00
}
@Override
public ResourceLocation getRotatingBaseModel() {
return new ResourceLocation(IndustrialWires.MODID, "block/mech_mb/flywheel.obj");
}
@Override
@SideOnly(Side.CLIENT)
public List<BakedQuad> getRotatingQuads() {
List<BakedQuad> orig = super.getRotatingQuads();
TextureAtlasSprite newTex = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(material.blockTexture.toString());
return orig.stream().map((quad)->{
if (quad.getSprite().getIconName().contains("steel")) {
return new BakedQuadRetextured(quad, newTex);
} else {
return quad;
}
}).collect(Collectors.toList());
}
2017-10-04 17:05:04 +02:00
@Override
public boolean canForm(LocalSidedWorld w) {
BlockPos.PooledMutableBlockPos pos = BlockPos.PooledMutableBlockPos.retain(-1, 1, 0);
try {
material = null;
for (Material m:Material.values()) {
if (m.matchesBlock(w, pos, "block")) {
material = m;
break;
}
}
if (material==null) {
return false;
}
for (int x = -1;x<=1;x++) {
for (int y = -1;y<=1;y++) {
pos.setPos(x, y, 0);
if ((x!=0||y!=0)&&!material.matchesBlock(w, pos, "block")) {
return false;
}
}
}
pos.setPos(0, 0, 0);
return isValidDefaultCenter(w.getBlockState(pos));
2017-10-04 17:05:04 +02:00
} finally {
pos.release();
}
}
@Override
public IBlockState getOriginalBlock(BlockPos pos) {
if (pos.equals(ORIGIN)) {
return getDefaultShaft();
}
for (ItemStack block: OreDictionary.getOres("block"+material.oreName())) {
if (block.getItem() instanceof ItemBlock) {
ItemBlock ib = (ItemBlock) block.getItem();
return ib.getBlock().getStateFromMeta(block.getMetadata());
}
}
return Blocks.AIR.getDefaultState();
}
@Override
public void breakOnFailure(MechEnergy energy) {
world.setBlockState(ORIGIN, getDefaultShaft());
IBlockState state = Blocks.AIR.getDefaultState();
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
if (x != 0 || y != 0) {
world.setBlockState(new BlockPos(x, y, 0), state);
}
}
}
spawnBrokenParts(8, energy, material.blockTexture);
}
2017-10-04 17:05:04 +02:00
@Override
public short getFormPattern(int offset) {
2017-10-04 17:05:04 +02:00
return 0b111_111_111;
}
@Override
public MechanicalMBBlockType getType() {
return MechanicalMBBlockType.FLYWHEEL;
}
@Override
public AxisAlignedBB getBoundingBox(BlockPos offsetPart) {
if (ORIGIN.equals(offsetPart)) {
return Block.FULL_BLOCK_AABB;
}
final double small = .375;
double xMin = offsetPart.getX()<=0?0:small;
double xMax = offsetPart.getX()>=0?1:1-small;
double yMin = offsetPart.getY()>=0?0:small;
double yMax = offsetPart.getY()<=0?1:1-small;
return new AxisAlignedBB(xMin, yMin, .0625, xMax, yMax, .9375);
}
2017-10-04 17:05:04 +02:00
}